123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Linq;
- using System.Data;
- using MySystem;
- using MySystem.PxcModels;
- using Library;
- using LitJson;
- public class StoreApplyHelper
- {
- public readonly static StoreApplyHelper Instance = new StoreApplyHelper();
- private StoreApplyHelper()
- {
- }
- public void Start()
- {
- Thread th = new Thread(DoWorks);
- th.IsBackground = true;
- th.Start();
- }
- // 每月1号重置仓库额度
- private void DoWorks()
- {
- while (true)
- {
- WebCMSEntities db = new WebCMSEntities();
- OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
- try
- {
- if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
- {
- string check = function.ReadInstance("/StoreApply/" + DateTime.Now.ToString("yyyyMM") + ".txt");
- if(string.IsNullOrEmpty(check))
- {
- function.WritePage("/StoreApply/", DateTime.Now.ToString("yyyyMM") + ".txt", DateTime.Now.ToString());
- Dictionary<int, decimal> dataDic = new Dictionary<int, decimal>();
- string pre = DateTime.Now.AddMonths(-2).ToString("yyyy-MM") + "-01 00:00:00";
- string start = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00";
- string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
- DataTable dts = OtherMySqlConn.dtable("select UserId from StoreHouse where CreateDate>='" + pre + "' and CreateDate<'" + end + "' and UserId in (select UserId from (select UserId,count(Id) from StoreHouse group by UserId HAVING count(Id)=1) tb)");
- foreach(DataRow dr in dts.Rows)
- {
- int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
- if(!dataDic.ContainsKey(UserId))
- {
- dataDic.Add(UserId, 20000);
- }
- else
- {
- dataDic[UserId] += 20000;
- }
- }
- DataTable dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (1,2,4,6,7,8) and TransType in (10,11,2) and StoreId>0 group by StoreId");
- foreach(DataRow dr in dt.Rows)
- {
- int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
- int Count = int.Parse(function.CheckInt(dr[1].ToString()));
- decimal AmountMore = Count * 200;
- StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
- if(!dataDic.ContainsKey(store.UserId))
- {
- dataDic.Add(store.UserId, AmountMore);
- }
- else
- {
- dataDic[store.UserId] += AmountMore;
- }
- }
- dt = OtherMySqlConn.dtable("select StoreId,count(Id) from StoreStockChange where CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId in (3,5,9) and TransType in (10,11,2) and StoreId>0 group by StoreId");
- foreach(DataRow dr in dt.Rows)
- {
- int StoreId = int.Parse(function.CheckInt(dr["StoreId"].ToString()));
- int Count = int.Parse(function.CheckInt(dr[1].ToString()));
- decimal AmountMore = Count * 300;
- StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
- if(!dataDic.ContainsKey(store.UserId))
- {
- dataDic.Add(store.UserId, AmountMore);
- }
- else
- {
- dataDic[store.UserId] += AmountMore;
- }
- }
- foreach(int UserId in dataDic.Keys)
- {
- decimal Amount = dataDic[UserId];
- decimal AmountMore = 0;
- // DataTable dtmore = OtherMySqlConn.dtable("select count(Id)*200 from PosMachinesTwo pos where StoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BuyUserId=0 and BrandId in (1,2,4,6,7,8) and `Status`>-1");
- // if(dtmore.Rows.Count > 0)
- // {
- // AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
- // }
- // dtmore = OtherMySqlConn.dtable("select count(Id)*300 from PosMachinesTwo pos where StoreId in (select Id from StoreHouse where UserId=" + UserId + ") and BuyUserId=0 and BrandId in (3,5,9) and `Status`>-1");
- // if(dtmore.Rows.Count > 0)
- // {
- // AmountMore += decimal.Parse(function.CheckNum(dtmore.Rows[0][0].ToString()));
- // }
- if(Amount < 10000 && SpecialUsers10000().Contains(UserId))
- {
- Amount = 10000;
- }
- if(Amount < 20000 && SpecialUsers20000().Contains(UserId))
- {
- Amount = 20000;
- }
- 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();
- }
- account.FixedAmount = Amount;
- account.ValidAmount = Amount + account.TempAmount - AmountMore;
- function.WriteLog("UserId:" + UserId + ";FixedAmount:" + account.FixedAmount + ";AmountMore:" + AmountMore + ";ValidAmount:" + account.ValidAmount, "计算分仓申请机具额度日志");
- }
- db.SaveChanges();
- }
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算分仓申请机具额度异常");
- }
- db.Dispose();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算分仓申请机具额度日志");
- Thread.Sleep(60000);
- }
- }
- public void StartEverTime()
- {
- Thread th = new Thread(StartEverTimeDo);
- th.IsBackground = true;
- th.Start();
- }
- private void StartEverTimeDo()
- {
- while (true)
- {
- WebCMSEntities db = new WebCMSEntities();
- try
- {
- string data = RedisDbconn.Instance.RPop<string>("StoreApplyQueue");
- if(!string.IsNullOrEmpty(data))
- {
- function.WriteLog("data:" + data, "分仓向总仓申请机具日志");
- JsonData jsonObj = JsonMapper.ToObject(data);
- if(jsonObj["Kind"].ToString() == "1") // 购买临时额度
- {
- int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
- Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
- if(order != null)
- {
- decimal TotalPrice = order.TotalPrice * 2;
- AddAmount2(db, order.UserId, TotalPrice, 1, order.Id);
- }
- }
- else if(jsonObj["Kind"].ToString() == "2") // 增减分仓临时额度
- {
- int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
- decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
- int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
- AddAmount(db, UserId, Amount, OperateType);
- }
- else if(jsonObj["Kind"].ToString() == "3") // 调低额度返回余额
- {
- int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
- decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
- AddAmount2(db, UserId, Amount, 0);
- decimal BalanceAmount = Amount / 2;
- 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 BeforeTotalAmount = account.TotalAmount; //变更前总金额
- decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
- decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
- account.BalanceAmount += BalanceAmount;
- decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
- decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
- decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
- UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- UserId = UserId, //创客
- ChangeType = 119, //变动类型
- ChangeAmount = BalanceAmount, //变更金额
- BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
- AfterTotalAmount = AfterTotalAmount, //变更后总金额
- BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
- AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
- BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
- AfterBalanceAmount = AfterBalanceAmount, //变更后余额
- }).Entity;
- }
- else if(jsonObj["Kind"].ToString() == "4") // 仓库发货,预发机申请
- {
- int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
- string SnIds = jsonObj["Data"]["SnIds"].ToString();
- StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
- if(store != null)
- {
- decimal Amount = 0;
- string[] SnIdList = SnIds.Split(',');
- foreach(string SnIdString in SnIdList)
- {
- int SnId = int.Parse(SnIdString);
- PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnId) ?? new PosMachinesTwo();
- if(pos.BrandId == 1 || pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8)
- {
- Amount += 200;
- }
- else if(pos.BrandId == 3 || pos.BrandId == 5 || pos.BrandId == 9)
- {
- Amount += 300;
- }
- }
- if(Amount > 0)
- {
- AddAmount(db, store.UserId, Amount, 1);
- }
- }
- }
- else if(jsonObj["Kind"].ToString() == "5") // 后台仓库调拨
- {
- int StoreId = int.Parse(jsonObj["Data"]["StoreId"].ToString());
- int BrandId = int.Parse(jsonObj["Data"]["BrandId"].ToString());
- string OpStorrString = jsonObj["Data"]["OpStoreNum"].ToString();
- int OpType = OpStorrString.StartsWith("-") ? 0 : 1;
- int OpStoreNum = int.Parse(OpStorrString.Replace("-", ""));
- StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
- if(store != null)
- {
- decimal Amount = 0;
- if(BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6 || BrandId == 7 || BrandId == 8)
- {
- Amount += 200 * OpStoreNum;
- }
- else if(BrandId == 3 || BrandId == 5 || BrandId == 9)
- {
- Amount += 300 * OpStoreNum;
- }
- if(Amount > 0)
- {
- AddAmount(db, store.UserId, Amount, OpType);
- }
- }
- }
- db.SaveChanges();
- }
- else
- {
- Thread.Sleep(5000);
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "分仓向总仓申请机具线程异常");
- }
- db.Dispose();
- }
- }
- public void AddAmount(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
- {
- 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 BeforeTotalAmount = account.ValidAmount; //变更前总金额
- if(OperateType == 1)
- {
- account.ValidAmount += Amount;
- }
- else
- {
- account.ValidAmount -= Amount;
- }
- decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
- StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- OperateType = OperateType,
- AmountType = 1,
- AfterAmount = AfterTotalAmount,
- BeforeAmount = BeforeTotalAmount,
- UseAmount = Amount,
- UserId = UserId,
- QueryCount = OrderId,
- }).Entity;
- db.SaveChanges();
- }
- public void AddAmount2(WebCMSEntities db, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
- {
- 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 BeforeTotalAmount = account.ValidAmount; //变更前总金额
- if(OperateType == 1)
- {
- account.TempAmount += Amount;
- account.ValidAmount += Amount;
- }
- else
- {
- account.TempAmount -= Amount;
- account.ValidAmount -= Amount;
- }
- decimal AfterTotalAmount = account.ValidAmount; //变更后总金额
- StoreHouseAmountRecord record = db.StoreHouseAmountRecord.Add(new StoreHouseAmountRecord()
- {
- CreateDate = DateTime.Now,
- UpdateDate = DateTime.Now,
- OperateType = OperateType,
- AmountType = 1,
- AfterAmount = AfterTotalAmount,
- BeforeAmount = BeforeTotalAmount,
- UseAmount = Amount,
- UserId = UserId,
- QueryCount = OrderId,
- }).Entity;
- db.SaveChanges();
- }
- private List<int> SpecialUsers10000()
- {
- List<int> ids = new List<int>();
- ids.Add(514);
- ids.Add(548);
- ids.Add(730);
- ids.Add(3596);
- ids.Add(11211);
- ids.Add(13185);
- ids.Add(24302);
- ids.Add(32630);
- ids.Add(33002);
- ids.Add(37887);
- ids.Add(40950);
- ids.Add(46284);
- ids.Add(59190);
- ids.Add(72099);
- ids.Add(120875);
- ids.Add(127884);
- return ids;
- }
- private List<int> SpecialUsers20000()
- {
- List<int> ids = new List<int>();
- ids.Add(8);
- ids.Add(415);
- ids.Add(527);
- ids.Add(532);
- ids.Add(553);
- ids.Add(558);
- ids.Add(1193);
- ids.Add(1195);
- ids.Add(1703);
- ids.Add(1797);
- ids.Add(1893);
- ids.Add(2275);
- ids.Add(2825);
- ids.Add(3015);
- ids.Add(3211);
- ids.Add(3492);
- ids.Add(4216);
- ids.Add(4328);
- ids.Add(4507);
- ids.Add(5774);
- ids.Add(6446);
- ids.Add(6538);
- ids.Add(6619);
- ids.Add(6751);
- ids.Add(10677);
- ids.Add(13650);
- ids.Add(15506);
- ids.Add(15600);
- ids.Add(15714);
- ids.Add(17767);
- ids.Add(18833);
- ids.Add(20667);
- ids.Add(21051);
- ids.Add(23099);
- ids.Add(23564);
- ids.Add(23883);
- ids.Add(23944);
- ids.Add(24528);
- ids.Add(26577);
- ids.Add(26875);
- ids.Add(27632);
- ids.Add(30869);
- ids.Add(32427);
- ids.Add(32458);
- ids.Add(32534);
- ids.Add(35808);
- ids.Add(37242);
- ids.Add(37556);
- ids.Add(40282);
- ids.Add(40886);
- ids.Add(41420);
- ids.Add(43735);
- ids.Add(44417);
- ids.Add(44697);
- ids.Add(46548);
- ids.Add(49927);
- ids.Add(50886);
- ids.Add(52449);
- ids.Add(52814);
- ids.Add(54784);
- ids.Add(58960);
- ids.Add(60371);
- ids.Add(60846);
- ids.Add(63010);
- ids.Add(68483);
- ids.Add(68705);
- ids.Add(70879);
- ids.Add(71019);
- ids.Add(71648);
- ids.Add(72029);
- ids.Add(74591);
- ids.Add(77107);
- ids.Add(77489);
- ids.Add(78313);
- ids.Add(80516);
- ids.Add(84627);
- ids.Add(88225);
- ids.Add(88579);
- ids.Add(89269);
- ids.Add(93049);
- ids.Add(93825);
- ids.Add(97771);
- ids.Add(99622);
- ids.Add(105286);
- ids.Add(106581);
- ids.Add(120563);
- ids.Add(120814);
- ids.Add(120844);
- ids.Add(120997);
- ids.Add(121082);
- ids.Add(121218);
- ids.Add(121465);
- ids.Add(121710);
- ids.Add(123354);
- ids.Add(123362);
- ids.Add(124423);
- ids.Add(124462);
- ids.Add(125184);
- ids.Add(125377);
- ids.Add(125578);
- ids.Add(125848);
- ids.Add(126613);
- ids.Add(126620);
- ids.Add(127561);
- ids.Add(128758);
- ids.Add(129380);
- ids.Add(129796);
- ids.Add(131010);
- ids.Add(132488);
- ids.Add(133216);
- ids.Add(133590);
- ids.Add(135440);
- return ids;
- }
- }
|