123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading;
- using MySystem.PxcModels;
- using Library;
- namespace MySystem
- {
- /// <summary>
- /// 运营中心定时程序
- /// </summary>
- public class OperateService
- {
- public readonly static OperateService Instance = new OperateService();
- private OperateService()
- { }
- #region 每天统计一次数据
- public void Start()
- {
- Thread th = new Thread(doSomething);
- th.IsBackground = true;
- th.Start();
- }
- public void doSomething()
- {
- while (true)
- {
- if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 4)
- {
- try
- {
- string Month = DateTime.Now.ToString("yyyyMM");
- string Date = DateTime.Now.ToString("yyyyMMdd");
- string check = function.ReadInstance("/Operate/" + Month + ".txt");
- if (string.IsNullOrEmpty(check))
- {
- function.WritePage("/Operate/", "" + Month + ".txt", DateTime.Now.ToString("HH:mm:ss"));
- WebCMSEntities db = new WebCMSEntities();
- OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
- //统计发货量
- Dictionary<int, int> OpData = new Dictionary<int, int>();
- if(DateTime.Now.Day == 1)
- {
- var list = opdb.SendMonthSummary.Select(m => new { m.TradeMonth, m.OpId, m.SendCount }).Where(m => m.TradeMonth == Month).ToList();
- foreach(var sub in list)
- {
- if(OpData.ContainsKey(sub.OpId))
- {
- OpData[sub.OpId] += sub.SendCount;
- }
- else
- {
- OpData.Add(sub.OpId, sub.SendCount);
- }
- }
- foreach(int OpId in OpData.Keys)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == OpId);
- if(user != null)
- {
- user.ThisMonthSend = OpData[OpId];
- }
- }
- db.SaveChanges();
- }
- OpData.Clear();
- var datelist = opdb.SendDaySummary.Select(m => new { m.TradeDate, m.OpId, m.SendCount }).Where(m => m.TradeDate == Date).ToList();
- foreach(var sub in datelist)
- {
- if(OpData.ContainsKey(sub.OpId))
- {
- OpData[sub.OpId] += sub.SendCount;
- }
- else
- {
- OpData.Add(sub.OpId, sub.SendCount);
- }
- }
- foreach(int OpId in OpData.Keys)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == OpId);
- if(user != null)
- {
- user.ThisMonthSend += OpData[OpId];
- }
- }
- db.SaveChanges();
- //统计库存
- Dictionary<int, int> StoreList = db.StoreHouse.Select(m => new { m.UserId, m.LaveNum }).Where(m => m.LaveNum > 0).GroupBy(m => m.UserId).Select(m => new { UserId = m.Key, Count = m.Count() }).ToDictionary(m => m.UserId, m => m.Count);
- foreach(int UserId in StoreList.Keys)
- {
- Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
- if(user != null)
- {
- user.StoreStock += StoreList[UserId];
- }
- }
- db.SaveChanges();
- opdb.Dispose();
- db.Dispose();
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "运营中心重置本月发货量异常");
- }
- }
- Thread.Sleep(800000);
- }
- }
- #endregion
- }
- }
|