using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Extensions.Hosting;
using MySystem;
using MySystem.PxcModels;
using LitJson;
using Library;

public class ChangePosTimer
{
    public readonly static ChangePosTimer Instance = new ChangePosTimer();
    private ChangePosTimer()
    {
    }
    
    public void Start()
    {
        Thread th = new Thread(DoWorks);
        th.IsBackground = true;
        th.Start();
    }

    private void DoWorks()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("ChangePosTimerQueue");
            if (!string.IsNullOrEmpty(content))
            {
                JsonData jsonObj = JsonMapper.ToObject(content);
                string OldSnNum = jsonObj["OldSn"].ToString();
                string NewSnNum = jsonObj["NewSn"].ToString();
                string MerNo = jsonObj["MerNo"].ToString();
                int ChangeId = int.Parse(jsonObj["ChangeId"].ToString());
                WebCMSEntities db = new WebCMSEntities();
                MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
                MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
                PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId);
                if (oldpos == null)
                {
                    return;
                }
                if (oldpos.BindingState != 1)
                {
                    return;
                }
                PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId);
                if (oldpos == null)
                {
                    return;
                }
                KqProducts oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
                KqProducts newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
                PosMerchantInfo merchant = new PosMerchantInfo();
                if (!string.IsNullOrEmpty(MerNo))
                {
                    merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PosMerchantInfo();
                }
                else
                {
                    merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == oldpos.BindMerchantId) ?? new PosMerchantInfo();
                }
                newpos.BindMerchantId = merchant.Id;
                newpos.BuyUserId = oldpos.BuyUserId;
                newpos.UserId = oldpos.UserId;
                newpos.RecycEndDate = oldpos.RecycEndDate;
                newpos.ScanQrTrade = oldpos.ScanQrTrade;
                newpos.DebitCardTrade = oldpos.DebitCardTrade;
                newpos.CreditTrade = oldpos.CreditTrade;
                newpos.PosSnType = oldpos.PosSnType;
                newpos.TransferTime = oldpos.TransferTime;
                newpos.IsPurchase = oldpos.IsPurchase;
                newpos.BindingState = oldpos.BindingState;
                newpos.ActivationState = oldpos.ActivationState;
                newpos.BindingTime = oldpos.BindingTime;
                newpos.ActivationTime = oldpos.ActivationTime;
                bool checkActReward = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo);
                if (!checkActReward)
                {
                    newpos.SeoKeyword = oldpos.SeoKeyword;
                }
                oldpos.UserId = 0;
                MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo);
                if (forMerNo != null)
                {
                    forMerNo.SnId = newpos.Id;
                }
                merchant.KqSnNo = NewSnNum;
                // 金控电签售后换新仓  ID：762   仓库编号：S0000762
                // 开店宝电签售后换新仓  ID：763   仓库编号：S0000763
                // 金控大POS售后换新仓  ID：764   仓库编号：S0000764
                // 就机具归回售后仓库
                if (oldpos.BrandId == 1)
                {
                    oldpos.StoreId = 762;
                }
                else if (oldpos.BrandId == 2)
                {
                    oldpos.StoreId = 763;
                }
                else if (oldpos.BrandId == 3)
                {
                    oldpos.StoreId = 764;
                }
                MachineChange change = db.MachineChange.FirstOrDefault(m => m.Id == ChangeId) ?? new MachineChange();
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == oldpos.StoreId) ?? new StoreHouse();
                StoreStockChange query = db.StoreStockChange.Add(new StoreStockChange()
                {
                    CreateDate = DateTime.Now,
                    StoreId = oldpos.StoreId, //仓库
                    BrandId = oldpos.BrandId, //产品类型
                    ProductName = RelationClass.GetKqProductBrandInfo(oldpos.BrandId), //产品名称
                    ChangeNo = change.ChangeNo, //变更单号
                    TransType = 11, //交易类型
                    SnNo = oldpos.PosSn, //SN编号
                    SnType = oldpos.PosSnType, //SN机具类型
                    StockOpDirect = 1, //库存操作方向
                    DeviceType = oldpos.DeviceType, //设备类型
                    FromUserId = store.UserId, //出货人
                    FromDate = DateTime.Now, //出库时间
                    ToUserId = change.UserId, //收货人
                    ToStoreId = oldpos.StoreId, //退货收货仓库
                }).Entity;
                StoreChangeHistory history = db.StoreChangeHistory.Add(new StoreChangeHistory()
                {
                    CreateDate = DateTime.Now,
                    UserId = store.UserId, //创客
                    BrandId = oldpos.BrandId, //产品类型
                    ChangeRecordNo = change.ChangeNo, //变更记录单号
                    TransType = 2, //交易类型
                    SnNo = oldpos.PosSn, //SN编号
                    SnType = oldpos.PosSnType, //SN机具类型
                    StockOpDirect = 1, //库存操作方向
                    DeviceVendor = oldpos.DeviceName, //设备厂商
                    DeviceModel = oldpos.DeviceKind, //设备型号
                    DeviceType = oldpos.DeviceType, //设备类型
                    FromUserId = store.UserId, //出货创客
                    FromDate = DateTime.Now, //出库时间
                    SourceStoreId = oldpos.SourceStoreId, //源仓库
                    StoreId = oldpos.StoreId, //仓库
                }).Entity;
                if (store != null)
                {
                    store.LaveNum += 1;
                }
                db.SaveChanges();
                db.Dispose();
            }
            else
            {
                Thread.Sleep(5000);
            }
        }
    }
}