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

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

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

        public void dosomething()
        {
            // bool op = true;
            // while (op)
            // {
                try
                {
                    WebCMSEntities db = new WebCMSEntities();
                    DataTable dt = dbconn.dtable("select * from TmpStoreHouse where Status='正常' order by Id");
                    foreach (DataRow dr in dt.Rows)
                    {
                        string Id = dr["Id"].ToString();
                        string StoreCode = dr["StoreCode"].ToString();
                        string StoreName = dr["StoreName"].ToString();
                        string StoreAddress = dr["StoreAddress"].ToString();
                        string ManageMakerCode = dr["ManageMakerCode"].ToString();
                        string Manager = dr["Manager"].ToString();
                        string ManageMobile = dr["ManageMobile"].ToString();
                        string ManageEmail = dr["ManageEmail"].ToString();
                        string MakerCode = dr["MakerCode"].ToString();
                        string LimitMakerCode = dr["LimitMakerCode"].ToString();
                        string StoreKind = dr["StoreKind"].ToString();
                        string Status = dr["Status"].ToString();
                        string CreateMan = dr["CreateMan"].ToString();
                        string CreateDate = dr["CreateDate"].ToString();
                        UserForMakerCode manageUser = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == ManageMakerCode) ?? new UserForMakerCode();
                        UserForMakerCode userFor = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
                        UserForMakerCode limitFor = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == LimitMakerCode) ?? new UserForMakerCode();
                        StoreHouse store = db.StoreHouse.Add(new StoreHouse()
                        {
                            StoreNo = StoreCode,
                            StoreName = StoreName,
                            Address = StoreAddress,
                            ManageMobile = ManageMobile,
                            ManagerEmail = ManageEmail,
                            StoreKind = StoreKind == "灵动总部" ? 1 : 0,
                            Status = 1,
                            CreateMan = CreateMan,
                            CreateDate = DateTime.Parse(CreateDate),
                            ManageUserId = manageUser.UserId,
                            UserId = userFor.UserId,
                            LimitTopUserId = limitFor.UserId.ToString(),
                        }).Entity;
                        db.SaveChanges();
                        StoreForCode storeFor = db.StoreForCode.FirstOrDefault(m => m.Code == StoreCode);
                        if (storeFor == null)
                        {
                            storeFor = db.StoreForCode.Add(new StoreForCode()
                            {
                                Code = StoreCode,
                            }).Entity;
                            db.SaveChanges();
                        }
                        storeFor.StoreId = store.Id;
                        StoreForName storeForName = db.StoreForName.FirstOrDefault(m => m.Name == StoreName);
                        if (storeForName == null)
                        {
                            storeForName = db.StoreForName.Add(new StoreForName()
                            {
                               Name = StoreName,
                            }).Entity;
                            db.SaveChanges();
                        }
                        storeForName.StoreId = store.Id;
                        db.SaveChanges();
                        function.WritePage("/stat/", "StoreIdFlag.txt", Id);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "导仓库数据异常");
                }
                Thread.Sleep(200);
            // }
        }
    }
}