Beispiel #1
0
        public void Create(FormCollection collection)
        {
            var msg = new Msg();
            var Db  = new DbContext().Db;

            try
            {
                var rate = new Rates().GetLast();
                // 验证费率信息
                if (rate == null)
                {
                    throw new Exception("请先到'系统设置-费率及基础配额设置'设置费率信息");
                }
                // 验证基础配额信息
                var quota = new Quotas().GetLast();
                if (quota == null)
                {
                    throw new Exception("请先到'系统设置-费率及基础配额设置'设置基础配额信息");
                }

                ///1.获取数据

                var cold_water_value = Convert.ToSingle(collection["cold_water_value"]);
                var hot_water_value  = Convert.ToSingle(collection["hot_water_value"]);
                var electric_value   = Convert.ToSingle(collection["electric_value"]);
                ///1.1判断输入数值
                if (cold_water_value < 0 || hot_water_value < 0 || electric_value < 0 || cold_water_value > 9999999 || hot_water_value > 9999999 || electric_value > 9999999)
                {
                    throw new Exception("数值输入有误,读表数值应在0~999999之间");
                }
                var Pid  = Convert.ToInt32(collection["pid"]); // 宿舍ID
                var Note = collection["note"];
                if (Pid < 0 || Pid > 99999999)
                {
                    throw new Exception("宿舍ID输入有误,应在1~99999999之间");
                }
                ///1.2获取宿舍信息
                var room = Db.Queryable <T_Room>().Single(x => x.Room_id == Pid && x.Room_model_state);
                if (room == null)
                {
                    throw new Exception("该宿舍不存在");
                }
                if (room.Number < 1)
                {
                    throw new Exception("该宿舍无人居住,无需登记");
                }
                ///1.3判断该宿舍是否已登记, 避免重复操作
                if (new DbHelper.Useds().IsRecord(Pid))
                {
                    throw new Exception("该宿舍本月已经登记过了,无需再次登记");
                }

                ///2.获取上次读数
                var this_cold_water_value = cold_water_value;
                var this_hot_water_value  = hot_water_value;
                var this_electric_value   = electric_value;
                ///3.计算本次用量
                var usedinfo = Db.Queryable <T_Used_total>().Single(x => x.Ut_room_id == Pid && x.Ut_model_state);
                if (usedinfo != null)
                {
                    ///3.1判断本次数值是否大于等于上次数值
                    if (this_cold_water_value < usedinfo.Ut_cold_water_value || this_hot_water_value < usedinfo.Ut_hot_water_value || this_electric_value < usedinfo.Ut_electric_value)
                    {
                        throw new Exception("数值输入有误,本期水表电表数值应大于等于上期读表数值");
                    }
                    // 本次数值=本次读数-上次读数
                    this_cold_water_value -= usedinfo.Ut_cold_water_value;
                    this_hot_water_value  -= usedinfo.Ut_hot_water_value;
                    this_electric_value   -= usedinfo.Ut_electric_value;
                }
                else
                {
                    usedinfo = new T_Used_total();
                }
                usedinfo.Ut_dorm_id          = room.Room_dorm_id;
                usedinfo.Ut_building_id      = room.Room_building_id;
                usedinfo.Ut_room_id          = Pid;
                usedinfo.Ut_hot_water_value  = hot_water_value;
                usedinfo.Ut_cold_water_value = cold_water_value;
                usedinfo.Ut_electric_value   = electric_value;

                ///3.1生成用量单
                var used = new T_Used()
                {
                    Used_electric_value   = this_electric_value,
                    Used_hot_water_value  = this_hot_water_value,
                    Used_cold_water_value = this_cold_water_value,
                    Used_building_id      = room.Room_building_id,
                    Used_dorm_id          = room.Room_dorm_id,
                    Used_note             = Note,
                    Used_room_id          = Pid,
                    Used_post_user_id     = (int)Session["id"],
                };
                ///3.2扣除基础配额数据,最终使用量 =(本次读表数值-上次读表数值)-(基础配额*人数)
                if (quota != null && quota.Quota_is_active)
                {
                    this_cold_water_value -= quota.Quota_cold_water_value * room.Number;
                    this_hot_water_value  -= quota.Quota_hot_water_value * room.Number;
                    this_electric_value   -= quota.Quota_electric_value * room.Number;
                }
                Db.Ado.BeginTran(); // 开始事务
                ///3.3保存用量单
                var uid = Db.Insertable(used).ExecuteReturnEntity();

                if (uid.Used_id < 1)// 插入并更新自增ID
                {
                    throw new Exception("保存登记信息时发生错误!");
                }

                ///5.计算本次费用并生成账单,费用 = 最终使用量*费率,(无阶梯计费)
                var bill = new T_Bill();
                bill.Bill_used_id     = uid.Used_id;
                bill.Bill_room_id     = room.Room_id;
                bill.Bill_building_id = room.Room_building_id;
                bill.Bill_dorm_id     = room.Room_dorm_id;
                ///6.超过基础配额的才计费
                if (this_cold_water_value > 0)
                { // 冷水费
                    bill.Bill_cold_water_cost = (decimal)this_cold_water_value * (decimal)rate.Rate_cold_water_value;
                }
                if (this_hot_water_value > 0)
                { // 热水费
                    bill.Bill_hot_water_cost = (decimal)this_hot_water_value * (decimal)rate.Rate_hot_water_value;
                }
                if (this_electric_value > 0)
                { // 电费
                    bill.Bill_electric_cost = (decimal)this_electric_value * (decimal)rate.Rate_electric_value;
                }

                bill.Bill_rates_id = rate.Rate_id;
                bill.Bill_quota_id = quota.Quota_id;
                ///7.保存所有数据

                ///7.1保存读表信息
                if (usedinfo.Ut_id < 1)
                {
                    if (Db.Insertable(usedinfo).ExecuteCommand() < 1)
                    {
                        throw new Exception("保存读表信息时发生错误!");
                    }
                }
                else if (Db.Updateable(usedinfo).ExecuteCommand() < 1)
                {
                    throw new Exception("更新读表信息时发生错误!");
                }
                ///7.2保存账单
                if (Db.Insertable(bill).ExecuteCommand() < 1)
                {
                    throw new Exception("添加账单信息时发生错误!");
                }

                Db.Ado.CommitTran();// 提交事务
                msg.Message = "登记成功!";
            }
            catch (Exception ex)
            {
                //发生错误,回滚事务
                Db.Ado.RollbackTran();
                msg.Code    = -1;
                msg.Message = ex.Message;
            }
            Response.Write(msg.ToJson());
            Response.End();
        }
Beispiel #2
0
        public void Create(FormCollection collection)
        {
            var msg = new Msg();

            SqlSugar.SqlSugarClient Db = null;
            try
            {
                var rate = new Rates().GetLast();
                // 验证费率信息
                var quota = new Quotas().GetLast();
                if (rate == null)
                {
                    throw new Exception("请先设置费率信息");
                }
                ///1.获取数据
                var cold_water_value = Convert.ToSingle(collection["cold_water_value"]);
                var hot_water_value  = Convert.ToSingle(collection["hot_water_value"]);
                var electric_value   = Convert.ToSingle(collection["electric_value"]);
                ///1.1判断输入数值
                if (cold_water_value < 0 || hot_water_value < 0 || electric_value < 0)
                {
                    throw new Exception("数值输入有误,费用不能小于0");
                }
                var Dorm_id     = Convert.ToInt32(collection["dorm_id"]);     // 园区ID
                var Building_id = Convert.ToInt32(collection["building_id"]); // 宿舍楼ID
                var Pid         = Convert.ToInt32(collection["pid"]);         // 宿舍ID
                var Note        = collection["note"];
                if (Dorm_id < 0 || Building_id < 0 || Pid < 0)
                {
                    throw new Exception("宿舍ID或宿舍楼ID或园区ID输入有误");
                }
                if (Note.Trim().Length < 3)
                {
                    throw new Exception("手动添加账单时请输入至少3个字符作为说明");
                }
                /// 生成一个虚拟用量登记
                var used = new T_Used()
                {
                    Used_room_id      = Pid,
                    Used_note         = "手动添加账单后自动生成的登记信息,并不会更新读表数据",
                    Used_building_id  = Building_id,
                    Used_dorm_id      = Dorm_id,
                    Used_post_user_id = (int)Session["id"],
                };
                /// 开始事务
                Db = new DbContext().Db;
                Db.Ado.BeginTran();
                var u = Db.Insertable(used).ExecuteReturnIdentity();
                /// 生成账单
                var bill = new T_Bill()
                {
                    Bill_note            = Note,
                    Bill_used_id         = u,
                    Bill_room_id         = Pid,
                    Bill_building_id     = Building_id,
                    Bill_dorm_id         = Dorm_id,
                    Bill_cold_water_cost = (decimal)cold_water_value,
                    Bill_electric_cost   = (decimal)hot_water_value,
                    Bill_hot_water_cost  = (decimal)electric_value,
                    Bill_rates_id        = rate.Rate_id,
                    Bill_quota_id        = quota.Quota_id,
                };
                /// 保存账单
                if (Db.Insertable(bill).ExecuteCommand() < 1)
                {
                    throw new Exception("添加账单信息时发生错误!");
                }

                Db.Ado.CommitTran();// 提交事务
                msg.Message = "添加成功!";
            }
            catch (Exception ex)
            {
                //发生错误,回滚事务
                if (Db != null)
                {
                    Db.Ado.RollbackTran();
                }
                msg.Code    = -1;
                msg.Message = ex.Message;
            }
            Response.Write(msg.ToJson());
            Response.End();
        }