using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.SpModels;
using Library;
using LitJson;
using System.Threading;

namespace MySystem
{
    public class SycnSpBindZkbService
    {
        public readonly static SycnSpBindZkbService Instance = new SycnSpBindZkbService();
        private SycnSpBindZkbService()
        { }

        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    List<string> BrandIds = new List<string>();
                    BrandIds.Add("17");
                    WebCMSEntities spdb = new WebCMSEntities();
                    PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
                    DateTime start = DateTime.Now.AddDays(-30);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
                    var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && BrandIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).ToList();
                    foreach (var Bind in Binds)
                    {
                        var tran = db.Database.BeginTransaction();
                        try
                        {
                            PxcModels.UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == Bind.Field3) ?? new PxcModels.UserForMakerCode();
                            PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new PxcModels.Users();

                            PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Bind.MerSnNo);
                            if (pos == null)
                            {
                                pos = db.PosMachinesTwo.Add(new PxcModels.PosMachinesTwo()
                                {
                                    CreateDate = DateTime.Now,
                                    PosSn = Bind.MerSnNo, //SN编号
                                    BrandId = 17, //产品类型
                                    BatchNo = Bind.Field1,
                                    DeviceName = "吱客宝",
                                    BuyUserId = user.Id,
                                    UserId = user.Id,
                                    BindingState = 1,
                                    BindingTime = Bind.CreateTime,
                                    ActivationState = 1,
                                    ActivationTime = Bind.CreateTime,
                                }).Entity;
                                db.SaveChanges();
                            }

                            PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqSnNo == Bind.MerSnNo);
                            if (merchant == null)
                            {
                                merchant = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
                                {
                                    CreateDate = DateTime.Now,
                                    MerStandardDate = DateTime.Now,
                                    KqSnNo = Bind.MerSnNo, //SN编号
                                    KqMerNo = Bind.MerNo, //商户号
                                    MerchantNo = Bind.MerNo,
                                    MerchantName = Bind.MerName,
                                    MerRealName = Bind.MerName,
                                    MerStatus = 1,
                                    ActiveStatus = 1,
                                    BrandId = 17, //产品类型
                                    UserId = user.Id,
                                    MerIdcardNo = Bind.SeoKeyword,
                                    MerchantMobile = Bind.MerNewSnNo,
                                }).Entity;
                                db.SaveChanges();
                            }
                            pos.BindMerchantId = merchant.Id;

                            PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo);
                            if (posFor == null)
                            {
                                posFor = db.MachineForSnNo.Add(new PxcModels.MachineForSnNo()
                                {
                                    SnNo = Bind.MerSnNo,
                                    SnId = pos.Id,
                                }).Entity;
                            }
                            else
                            {
                                posFor.SnId = pos.Id;
                            }
                            PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
                            if (merFor == null)
                            {
                                merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
                                {
                                    MerNo = Bind.MerNo,
                                    SnId = pos.Id,
                                }).Entity;
                            }
                            else
                            {
                                merFor.SnId = pos.Id;
                            }
                            db.SaveChanges();

                            BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                                spdb.SaveChanges();
                            }
                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP吱客宝绑定数据到MAIN异常");
                        }
                        tran.Dispose();
                    }
                    spdb.SaveChanges();
                    spdb.Dispose();
                    db.SaveChanges();
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
                }
                Thread.Sleep(1000);
            }
        }
    
    }
}