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

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

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

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    WebCMSEntities spdb = new WebCMSEntities();
                    PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
                    DateTime start = DateTime.Now.AddDays(-1);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantRecordId.txt")));
                    var Mers = spdb.MerchantRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.Status == 1 && m.ProductType == "12").OrderByDescending(m => m.Id).Take(10).ToList();
                    foreach (var Mer in Mers)
                    {
                        var tran = db.Database.BeginTransaction();
                        try
                        {
                            PxcModels.MachineForMerNo machineForMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
                            PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForMerNo.SnId) ?? new PxcModels.PosMachinesTwo();
                            
                            PxcModels.PosMerchantInfo merinfo = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                            if(merinfo != null)
                            {
                                merinfo.MerIdcardNo = Mer.LegalIdCard;
                                merinfo.MerchantName = Mer.LegalName;
                            }
                            int IsFirst = 1;
                            if(!string.IsNullOrEmpty(Mer.LegalIdCard))
                            {
                                string startNo = Mer.LegalIdCard.Substring(0, 6);
                                string endNo = Mer.LegalIdCard.Substring(Mer.LegalIdCard.Length - 4, 4).ToUpper();
                                string Name = Mer.MerName;
                                if (Mer.ProductType == "2")
                                {
                                    if (Name.Contains("-"))
                                    {
                                        Name = Name.Split('-')[1];
                                    }
                                    else if (Name.Contains("_"))
                                    {
                                        Name = Name.Split('_')[1];
                                    }
                                }
                                else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
                                {
                                    Name = Mer.SeoTitle;
                                }
                                else if (Mer.ProductType == "10")
                                {
                                    Name = Name.Replace("*", "");
                                }
                                Name = Name.Replace("个体户", "");
                                Name = Name.Replace("个体商户", "");
                                Name = Name.Replace("企业户", "");
                                Name = Name.Replace("企业商户", "");
                                function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
                                PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name) && m.Id != pos.BindMerchantId);
                                if(check != null)
                                {
                                    function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
                                    IsFirst = 0;
                                }
                            }
                            pos.IsFirst = IsFirst;

                            
                            MerchantRecord edit = spdb.MerchantRecord.FirstOrDefault(m => m.Id == Mer.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" + Mer.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(10000);
            }
        }
    }
}