123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MySystem.Models;
- using Library;
- using LitJson;
- namespace MySystem
- {
- public class FluxService
- {
- public readonly static FluxService Instance = new FluxService();
- private FluxService()
- { }
- public void Start(JobMqMsg jobInfo)
- {
- string content = "";
- try
- {
- string Msg = CheckFlux();
- // jobInfo.Status = Msg == "success" ? 1: 0;
- // jobInfo.Msg = Msg == "success" ? "执行完成": Msg;
- // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "FluxBack");
- }
- catch (Exception ex)
- {
- if (!string.IsNullOrEmpty(content))
- {
- Dictionary<string, string> data = new Dictionary<string, string>();
- data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- data.Add("ErrMsg", ex.ToString());
- LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "fluxback_err");
- }
- else
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "fluxback_service");
- }
- }
- }
- //检查机具扣费情况
- private string CheckFlux()
- {
- DateTime StatDate = DateTime.Now.AddMonths(-1);
- DateTime StartDate = DateTime.Parse(StatDate.ToString("yyyy-MM") + "-01 00:00:00");
- DateTime EndDate = StartDate.AddMonths(1);
- string TradeMonth = StatDate.ToString("yyyyMM");
- WebCMSEntities db = new WebCMSEntities();
- using (var tran = db.Database.BeginTransaction())
- {
- try
- {
- List<FluxProfitSummary> list = db.FluxProfitSummary.Where(m => m.TradeMonth == TradeMonth && m.CheckStatus == 0).ToList();
- foreach (FluxProfitSummary sub in list)
- {
- int UserId = sub.UserId; //创客
- decimal FluxProfitAmt = sub.FluxProfitAmt; //交易分润
- decimal ActualProfit = 0;
- //核对
- bool check = db.FluxProfitDetail.Any(m => m.UserId == sub.UserId && m.TradeDate == sub.TradeMonth);
- if (check)
- {
- ActualProfit = db.FluxProfitDetail.Where(m => m.UserId == sub.UserId && m.TradeDate == sub.TradeMonth).Sum(m => m.FluxProfitAmt);
- }
- if (ActualProfit == FluxProfitAmt)
- {
- //核对成功
- FluxProfitSummary edit = db.FluxProfitSummary.FirstOrDefault(m => m.Id == sub.Id);
- if (edit != null)
- {
- edit.CheckStatus = 1;
- db.SaveChanges();
- }
- UserAccount account = db.UserAccount.FirstOrDefault(m => m.UserId == sub.UserId);
- if (account == null)
- {
- account = db.UserAccount.Add(new UserAccount()
- {
- Id = sub.UserId,
- CreateDate = DateTime.Now,
- UserId = sub.UserId,
- UserProperty = 0,
- UserType = 2,
- }).Entity;
- db.SaveChanges();
- }
- decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += FluxProfitAmt;
- account.TotalAmount += FluxProfitAmt;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- db.SaveChanges();
- UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = sub.UserId, //创客
- ChangeType = 60, //变动类型
- ProductType = sub.BrandId, //产品类型
- ChangeAmount = FluxProfitAmt, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- }).Entity;
- db.SaveChanges();
- RedisDbconn.Instance.HAddNumber("TmpFluxProfit", sub.UserId + "_" + sub.BrandId + "_" + StatDate.ToString("yyyy年MM月") + "_" + StatDate.ToString("yyyyMM"), FluxProfitAmt);
- }
- else
- {
- ErrorMsg msg = new ErrorMsg()
- {
- Time = DateTime.Now,
- ErrorContent = "同步流量卡分佣失败,数据不一致",
- Obj = Newtonsoft.Json.JsonConvert.SerializeObject(sub),
- };
- RedisDbconn.Instance.AddList("FluxServiceWarning", msg);
- }
- }
- //首页-快联盟产品-收益查看-流量卡分佣
- Dictionary<string, decimal> datalist = RedisDbconn.Instance.HGetAll<decimal>("TmpFluxProfit");
- foreach (string key in datalist.Keys)
- {
- string[] fields = key.Split('_');
- string UserId = fields[0];
- string BrandId = fields[1];
- string TradeM = fields[2];
- string TradeMValue = fields[3];
- RedisDbconn.Instance.AddList("FluxProfitSummary:" + UserId + ":" + BrandId, new TradeProfitItem()
- {
- TradeProfit = datalist[key],
- TradeMonth = TradeM,
- TradeMonthValue = TradeMValue,
- CreateDate = DateTime.Now,
- });
- }
- RedisDbconn.Instance.Clear("TmpFluxProfit");
-
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- ErrorMsg msg = new ErrorMsg()
- {
- Time = DateTime.Now,
- ErrorContent = ex.ToString(),
- };
- LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "ProfitServiceError");
- }
- }
- db.Dispose();
- return "success";
- }
- }
- }
|