Example #1
0
        /// <summary>
        /// 进行计费汇算,本次会算值作为是否校准的依据,如需要校准,则返回true
        /// </summary>
        /// <param name="meter"></param>
        /// <param name="dataItem"></param>
        /// <returns></returns>
        public bool Calculate(Meter meter, SubmitData dataItem, SubmitResult returnResult)
        {
            #region

            /*
             * //计算当前阶段用气量
             * decimal JieDuanYongQiLiang = dataItem.LJGas - meter.LastTotal;// dataItem.LastLJGas;
             * decimal[] prices = new decimal[5];
             * prices[0] = meter.Price1;
             * prices[1] = meter.Price2;
             * prices[2] = meter.Price3;
             * prices[3] = meter.Price4;
             * prices[4] = meter.Price5;
             * decimal[] gas = new decimal[4];
             * gas[0] = meter.Gas1;
             * gas[1] = meter.Gas2;
             * gas[2] = meter.Gas3;
             * gas[3] = meter.Gas4;
             * decimal currentPirce = meter.Price1;
             * decimal currentGas = meter.Gas1;
             * decimal amount = meter.LastSettlementAmount;
             *
             * decimal[] fees = new decimal[5];
             * decimal[] usedGas = new decimal[5];
             * if (meter.IsUsedLadder)
             * {
             *  //启用了阶梯价
             *  int iLadder = 1;
             *  while (iLadder < meter.Ladder)
             *  {
             *      currentPirce = prices[iLadder - 1];
             *      currentGas = gas[iLadder - 1];
             *      if (JieDuanYongQiLiang > currentGas)
             *      {
             *          //当前阶段总用气量大于当前阶梯用气量
             *          fees[iLadder - 1] = currentGas * currentPirce;
             *          usedGas[iLadder - 1] = currentGas;
             *
             *          amount -= fees[iLadder - 1];
             *          JieDuanYongQiLiang -= currentGas;
             *          iLadder++;
             *      }
             *      else if (JieDuanYongQiLiang > 0)
             *      {
             *          fees[iLadder - 1] = JieDuanYongQiLiang * currentPirce;
             *          usedGas[iLadder - 1] = JieDuanYongQiLiang;
             *
             *          amount -= fees[iLadder - 1];
             *          JieDuanYongQiLiang = 0;
             *          iLadder++;
             *          break;
             *      }
             *      else
             *      {
             *          iLadder++;
             *          break;
             *      }
             *  }
             *  if (JieDuanYongQiLiang > 0)
             *  {
             *      //计算最后一个阶梯
             *      currentPirce = prices[iLadder - 1];
             *
             *      fees[iLadder - 1] = JieDuanYongQiLiang * currentPirce;
             *      usedGas[iLadder - 1] = JieDuanYongQiLiang;
             *      amount -= fees[iLadder - 1];
             *  }
             * }
             * else
             * {
             *  //未使用阶梯价
             *  amount -= JieDuanYongQiLiang * currentPirce;
             *  fees[0] = JieDuanYongQiLiang * currentPirce;
             *  usedGas[0] = JieDuanYongQiLiang;
             * }
             */
            #endregion
            if (meter.BillID == null)
            {
                meter.CreateBillID();
            }
            decimal amount = CalculateGasFee(meter, dataItem);

            //判断结算日是否到达
            //判断是否到结算日,该处的功能依赖表必须传输结算日气量数据,且上报的数据必须按时间顺序传输。
            if (meter.Jiange(meter.GetSettlementTimePoint()) >= 0)
            {
                decimal[] gas = new decimal[5];
                gas[0] = meter.Gas1;
                gas[1] = meter.Gas2;
                gas[2] = meter.Gas3;
                gas[3] = meter.Gas4;
                gas[4] = -1;

                //记录结算记录
                BillRecord record = new BillRecord()
                {
                    UserID = meter.UserID, MeterNo = meter.Mac
                };
                record.RecordDate     = dataItem.ReadDate.ToString("yyyy-MM-dd HH:mm:ss");
                record.BillRecordType = BillRecordType.结算点记录;
                record.BillID         = meter.BillID;
                record.Ladder         = meter.CurrentLadder;

                if (meter.NextSettlementPointGas != -1 && dataItem.LJGas < meter.NextSettlementPointGas)
                {
                    record.EndPoint = dataItem.LJGas;
                    if (meter.IsPricing)
                    {
                        record.BeginPoint = meter.TiaoJiaPointGas;
                    }
                    else
                    {
                        record.BeginPoint = meter.NextSettlementPointGas - gas[meter.CurrentLadder - 1];
                    }
                    record.Gas     = record.EndPoint - record.BeginPoint;
                    record.Price   = meter.CurrentPrice;
                    record.Amount  = record.Gas * record.Price;
                    record.Balance = meter.CurrentBalance;
                }
                else if (meter.NextSettlementPointGas != -1 && dataItem.LJGas == meter.NextSettlementPointGas)
                {
                    record.EndPoint = meter.NextSettlementPointGas;
                    if (meter.IsPricing)
                    {
                        record.BeginPoint = meter.TiaoJiaPointGas;
                    }
                    else
                    {
                        record.BeginPoint = meter.NextSettlementPointGas - gas[meter.CurrentLadder - 1];
                    }
                    record.Gas     = record.EndPoint - record.BeginPoint;
                    record.Price   = meter.CurrentPrice;
                    record.Amount  = record.Gas * record.Price;
                    record.Balance = meter.CurrentBalance;
                }
                else
                {
                    //最后一个阶梯
                    record.EndPoint = dataItem.LJGas;
                    if (meter.IsPricing)
                    {
                        record.BeginPoint = meter.TiaoJiaPointGas;
                    }
                    else
                    {
                        record.BeginPoint = meter.LastTotal;
                        for (int i = 0; i < meter.Ladder; i++)
                        {
                            record.BeginPoint += gas[i];
                        }
                    }
                    record.Gas     = record.EndPoint - record.BeginPoint;
                    record.Price   = meter.CurrentPrice;
                    record.Amount  = record.Gas * record.Price;
                    record.Balance = meter.CurrentBalance;
                }
                new M_BillRecordService().AddBillRecord(record);
                meter.IsPricing = false;
                meter.CreateBillID();
                //创建新的账单
                Bill bill = new Bill()
                {
                    BillID = meter.BillID, UserID = meter.UserID, BeginDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                new M_BillRecordService().AddBill(bill);

                returnResult.IsReLoadMeter = true;
                //更新结算数据
                if (meter.NextSettlementPointGas != 0 && !String.IsNullOrEmpty(meter.SettlementDateTime))
                {
                    meter.LastTotal = dataItem.LJGas;
                }
                meter.SetNextSettlementDateTime();
                meter.TotalAmount          = dataItem.LJGas;
                meter.LastSettlementAmount = amount;
                meter.CurrentLadder        = 1;
                meter.CurrentPrice         = meter.Price1;

                if (meter.IsUsedLadder && meter.CurrentLadder < meter.Ladder)
                {
                    //设置第1个节点的结算点气量
                    meter.NextSettlementPointGas = meter.LastTotal + gas[meter.CurrentLadder - 1];
                }
                else
                {
                    //下一次结算点气量为无穷大
                    meter.NextSettlementPointGas = -1;
                }
                new TaskManageDA().UpdateMeter(meter);
                //TODO:在此记录阶段用气账单
            }
            //处理调价。
            TiaoJiaDowith(meter, dataItem);

            decimal syMoney = Convert.ToDecimal(dataItem.SYMoney.ToString("0.00"));
            //在此可以定义系统参数,修正精度,默认差值大于1角做修正处理
            if (meter.IsDianHuo && Math.Abs(syMoney - amount) > 0.1m)
            {
                //修正时间、修正原因、 表号、表上传的当前余额、 上次结算日累计气量(4字节),上次结算日剩余金额(4字节),累计购入金额(4字节),当前结算日
                Console.WriteLine("需要修正表计量数据,表余额:{0} ,后台系统余额:{1}", dataItem.SYMoney, amount);
                CorrectRecord correct = new CorrectRecord();
                correct.MeterBalance = syMoney;
                correct.MeterNo      = meter.Mac;
                correct.MeterLastSettleMentDayLJGas = dataItem.LastLJGas;
                correct.MeterLJGas    = dataItem.LJGas;
                correct.MeterLJMoney  = dataItem.LJMoney;
                correct.MeterReadDate = dataItem.ReadDate.ToString("yyyy-MM-dd HH:mm:ss");

                correct.SettlementDay          = (byte)meter.SettlementDay;
                correct.CorrectDate            = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                correct.CorrectReason          = string.Format("表计量数据和后台不一致,表余额误差超出允许范围,表余额:{0} ,后台余额:{1} 差额:{2}", dataItem.SYMoney, amount, Math.Abs(syMoney - amount));
                correct.TotalTopUp             = meter.TotalTopUp;
                correct.SettlementBalance      = amount;
                correct.LastSettlementDayLJGas = meter.LastTotal;
                dataItem.SYMoney = amount;
                Task   _task;
                string result = new CorrectDA().AddCorrentTask(correct, out _task);//记录修正数据和添加修正任务
                if (result == "")
                {
                    returnResult.IsCalibration = true;
                    returnResult.Calibrations.Add(_task);
                }
            }
            return(returnResult.IsCalibration);
        }
Example #2
0
        /// <summary>
        /// 添加表对象到mongodb库中(用于新表安装)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public string InsertMeter(IoT_Meter info)
        {
            Meter _meter = QueryMeter(info.MeterNo.Trim());

            if (_meter != null)
            {
                MongoDBHelper <Meter> mongo = new MongoDBHelper <Meter>();
                var iDelete = new QueryDocument();
                iDelete.Add("Mac", info.MeterNo.Trim());
                mongo.Delete(CollectionNameDefine.MeterCollectionName, iDelete);
            }

            Meter meter = new Meter();

            meter.MeterID         = info.ID;
            meter.UserID          = info.CompanyID.Trim() + info.UserID.Trim();
            meter.Key             = info.MKey;
            meter.Mac             = info.MeterNo.Trim();
            meter.MeterState      = info.MeterState.ToString();
            meter.MeterType       = info.MeterType;
            meter.MKeyVer         = (byte)(info.MKeyVer & 0xff);
            meter.PriceCheck      = info.PriceCheck.ToString();
            meter.SettlementDay   = (int)info.SettlementDay;
            meter.SettlementType  = info.SettlementType;
            meter.TotalAmount     = (decimal)info.TotalAmount;
            meter.TotalTopUp      = (decimal)info.TotalTopUp;
            meter.ValveState      = info.ValveState.ToString();
            meter.LastTopUpSer    = 0;
            meter.IsUsedLadder    = (bool)info.IsUsed;
            meter.Ladder          = (int)info.Ladder;
            meter.MeterType       = info.MeterType;
            meter.Price1          = (decimal)info.Price1;
            meter.Price2          = (decimal)info.Price2;
            meter.Price3          = (decimal)info.Price3;
            meter.Price4          = (decimal)info.Price4;
            meter.Price5          = (decimal)info.Price5;
            meter.Gas1            = (decimal)info.Gas1;
            meter.Gas2            = (decimal)info.Gas2;
            meter.Gas3            = (decimal)info.Gas3;
            meter.Gas4            = (decimal)info.Gas4;
            meter.SettlementMonth = (int)info.SettlementMonth;
            meter.SettlementDay   = (int)info.SettlementDay;
            meter.SettlementType  = info.SettlementType;
            meter.CurrentLadder   = 1;
            meter.CurrentPrice    = meter.Price1;
            if (meter.IsUsedLadder && meter.CurrentLadder < meter.Ladder)
            {
                meter.NextSettlementPointGas = meter.TotalAmount + meter.Gas1;
                meter.SetNextSettlementDateTime();
            }
            meter.LastTotal            = meter.TotalAmount;
            meter.LastSettlementAmount = (decimal)info.TotalTopUp;
            meter.LastGasPoint         = meter.TotalAmount;
            meter.CurrentBalance       = (decimal)info.TotalTopUp;
            meter.LJMoney   = 0;
            meter.IsDianHuo = false;//点火标记 false 未点火
            meter.IsPricing = false;
            meter.CreateBillID();

            return(this.InsertMeter(meter));//同时插入数据到mongoDB中
        }