using System; using System.Collections.Generic; using Library; using LitJson; using System.Linq; using System.Data; using System.Threading; using MySystem.OpModels; namespace MySystem { public class OperateAmountService { public readonly static OperateAmountService Instance = new OperateAmountService(); private OperateAmountService() { } public void Start() { Thread th = new Thread(dosomething); th.IsBackground = true; th.Start(); } public void dosomething() { while (true) { string data = RedisDbconn.Instance.RPop("OperateAmountQueue"); if (!string.IsNullOrEmpty(data)) { try { WebCMSEntities db = new WebCMSEntities(); function.WriteLog(DateTime.Now.ToString() + "\r\n" + data + "\r\n\r\n", "运营中心返额度日志"); JsonData jsonObj = JsonMapper.ToObject(data); int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString())); //运营中心所属人创客Id int DataId = int.Parse(function.CheckInt(jsonObj["DataId"].ToString())); //机具Id或机具券Id int Kind = int.Parse(function.CheckInt(jsonObj["Kind"].ToString())); //1-机具,2-机具券 decimal Amount = decimal.Parse(function.CheckNum(jsonObj["Amount"].ToString())); // UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId); // if (account == null) // { // account = db.UserAccount.Add(new UserAccount() // { // Id = UserId, // UserId = UserId, // }).Entity; // db.SaveChanges(); // } // decimal BeforeAmount = account.ValidAmount; //变更前总金额 // account.ValidAmount += Amount; // decimal AfterAmount = account.ValidAmount; //变更后总金额 // AmountRecord add = db.AmountRecord.Add(new AmountRecord() // { // CreateDate = DateTime.Now, // UpdateDate = DateTime.Now, // OperateType = 1, // AfterAmount = AfterAmount, // AfterValidForGetAmount = account.ValidForGetAmount, // AfterTotalAmt = account.TotalAmt, // AfterValidAmount = account.ValidAmount, // BeforeAmount = BeforeAmount, // UseAmount = Amount, // UserId = UserId, // ApplyId = DataId, // SeoDescription = Kind == 1 ? "机具激活返还" : "机具券兑换返还", // Version = 3, // }).Entity; // db.SaveChanges(); // AmountChangeRecord amountChangeRecord = db.AmountChangeRecord.Add(new AmountChangeRecord() // { // AmountType = 2,//1 未使用额度 2 可提现额度 3 关联分仓额度 // CreateDate = DateTime.Now, // Title = Kind == 1 ? "机具激活返还" : "机具券兑换返还", // UserId = UserId, //运营中心Id // BeforeAmount = BeforeAmount,//使用前剩余额度 // AfterAmount = AfterAmount,//使用后剩余额度 // ChangeAmount = Amount,//操作金额 // OperateType = 1,//操作类别 // }).Entity; // db.Dispose(); OpAmountItem item = new OpAmountItem() { UserId = UserId, OperateType = 1, ChangeType = Kind == 1 ? 9 : 23, Remark = Kind == 1 ? "机具激活返还" : "机具券兑换返还", UseAmount = Amount, UseValidAmount = Amount, DataType = Kind == 1 ? 2 : 6, DataId = DataId, }; RedisDbconn.Instance.AddList("OperateAmountRecordServiceQueue", item); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心返额度异常"); } } else { Thread.Sleep(5000); } } } } }