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

/// <summary>
/// 分仓临时额度提现结果导入
/// </summary>
namespace MySystem
{
    public class StoreHouseWithdrawalResultsService
    {
        public readonly static StoreHouseWithdrawalResultsService Instance = new StoreHouseWithdrawalResultsService();
        private StoreHouseWithdrawalResultsService()
        {
        }

        public void Start()//启动
        {
            Thread thread = new Thread(ImportPostDo);
            thread.IsBackground = true;
            thread.Start();
        }

        public void ImportPostDo()
        {
            while (true)
            {
                try
                {
                    string data = RedisDbconn.Instance.RPop<string>("StoreHouseWithdrawalResultsQueue");
                    if (!string.IsNullOrEmpty(data))
                    {
                        string[] dataList = data.Split("#cut#");
                        string _ExcelPath = dataList[0];
                        string _Kind = dataList[1];
                        string checkKey = dataList[2];
                        string Operator = dataList[3]; // 操作人
                        int SuccessCount = 0;
                        int DoCount = 0;
                        string FullExcelPath = function.getPath(_ExcelPath);
                        FullExcelPath = FullExcelPath.Replace("//", "/");
                        DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
                        int TotalCount = list.Rows.Count;
                        while (DoCount < list.Rows.Count)
                        {
                            WebCMSEntities db = new WebCMSEntities();
                            //导入结果
                            if (_Kind == "1")
                            {
                                var tran = db.Database.BeginTransaction();
                                try
                                {
                                    int Size = 100;
                                    if (list.Rows.Count - DoCount < 100)
                                    {
                                        Size = list.Rows.Count - DoCount;
                                    }
                                    Dictionary<string, int> storeData = new Dictionary<string, int>();
                                    for (int i = DoCount; i < DoCount + Size; i++)
                                    {
                                        DataRow dr = list.Rows[i];
                                        string Id = dr[0].ToString();
                                        string IsOk = dr[11].ToString();
                                        var id = int.Parse(Id);
                                        var Info = db.StoreHouseAmountRecord.FirstOrDefault(m => m.Id == id) ?? new StoreHouseAmountRecord();
                                        if (Info.Id > 0)
                                        {
                                            if (IsOk == "是") Info.Status = 1;
                                            if (IsOk == "否")
                                            {
                                                Info.Status = -1;
                                                var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == Info.UserId) ?? new UserAccount();
                                                if (userAccount.Id > 0)
                                                {
                                                    userAccount.TempAmount += Info.UseAmount;//退还卡充值临额
                                                    userAccount.ValidAmount += Info.UseAmount;//退还可用额度
                                                }
                                            }
                                            SuccessCount += 1;
                                        }
                                        else
                                        {
                                            RedisDbconn.Instance.AddList("ErrList" + checkKey, "未找到记录为Id" + Id + "相关信息");
                                        }
                                    }
                                    DoCount += Size;
                                    db.SaveChanges();
                                    tran.Commit();
                                    if (DoCount >= list.Rows.Count)
                                    {
                                        RedisDbconn.Instance.Set("StoreHouseWithdrawalResultsCheckImport:" + checkKey, "success|" + SuccessCount);
                                        RedisDbconn.Instance.SetExpire("StoreHouseWithdrawalResultsCheckImport:" + checkKey, 60000);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    DoCount = list.Rows.Count;
                                    function.WriteLog(ex.ToString(), "分仓临时额度提现结果导入");
                                    tran.Rollback();
                                    ErrorMsg msg = new ErrorMsg()
                                    {
                                        Time = DateTime.Now,
                                        ErrorContent = ex.ToString(),
                                    };
                                    function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "分仓临时额度提现结果导入Excel文件异常");
                                }
                                tran.Dispose();
                            }

                        }
                    }
                    else
                    {
                        Thread.Sleep(50000);
                    }
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
                }
            }
        }
    }
}