using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using MySystem.Models;
using Library;

namespace MySystem
{
    /// <summary>
    /// 设置预发机超过30天预发押金自动扣减
    /// </summary>
    public class SetDespositHelper
    {
        public readonly static SetDespositHelper Instance = new SetDespositHelper();
        private SetDespositHelper()
        { }

        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 < 9)
                {
                    try
                    {
                        string check = function.ReadInstance("/SetDesposit/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                        if (string.IsNullOrEmpty(check))
                        {
                            function.WritePage("/SetDesposit/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                            WebCMSEntities db = new WebCMSEntities();
                            var date = DateTime.Now.AddDays(-30);
                            bool op = true;
                            while (op)
                            {
                                var smallPos = db.PreSendStockDetail.Where(m => m.Status == 1 && m.Sort == 0 && m.CreateDate <= date && m.ApplyFlag == 0 && m.AuthFlag == 1).ToList();
                                foreach (var item in smallPos)
                                {
                                    var tuserAccount = db.UserAccount.FirstOrDefault(m => m.Id == item.ToUserId) ?? new UserAccount();
                                    var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == item.BrandId);
                                    var amount = 0;
                                    if (brandInfo.Name.Contains("电签"))
                                    {
                                        amount = 200;
                                    }
                                    if (brandInfo.Name.Contains("大POS"))
                                    {
                                        amount = 300;
                                    }
                                    if (tuserAccount.Id > 0)
                                    {
                                        tuserAccount.SmallStoreDeposit += amount;//添加小分仓押金
                                        tuserAccount.ValidPreAmount += amount;//添加小分仓可用额度
                                        var add = db.UserAccountRecord.Add(new UserAccountRecord()
                                        {
                                            CreateDate = DateTime.Now,
                                            Remark = "小分仓押金暂扣",
                                            ChangeType = 63,
                                            BeforeBalanceAmount = tuserAccount.SmallStoreDeposit - amount, //变更前余额
                                            AfterBalanceAmount = tuserAccount.SmallStoreDeposit, //变更后余额
                                            ChangeAmount = amount, //变更金额
                                            UserId = item.ToUserId,

                                        }).Entity;
                                        db.SaveChanges();
                                    }
                                    item.Sort = 1;
                                    db.SaveChanges();
                                }
                            }
                            db.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "设置预发机超过30天预发押金自动扣减异常");
                    }
                }
                Thread.Sleep(5000);
            }
        }
    }
}