using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.PxcModels;
using MySystem.JavaUserModels;
using Library;
using LitJson;
using System.Threading;

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

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

        public void DoWorks()
        {
            while (true)
            {
                try
                {
                    DoQueue();
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步创客信息异常");
                }
                Thread.Sleep(1800000);
            }
        }

        public void DoQueue()
        {           
            JavaUserModels.WebCMSEntities javaUser = new JavaUserModels.WebCMSEntities();
            PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();

            DateTime start = DateTime.Now.AddMonths(-3);
            DateTime end = DateTime.Now.AddHours(-1);
            IQueryable<JavaUserModels.KxsUser> kxsUser = javaUser.KxsUser.Where(m => m.CreateTime >= start && m.CreateTime < end);
            int minId = kxsUser.Min(m => m.Id);
            int maxId = kxsUser.Max(m => m.Id);
            List<int> checkUserIds = db.Users.Select(m => m.Id).Where(m => m >= minId && m <= maxId).ToList();
            var users = javaUser.KxsUser.Where(m => m.Id >= minId && m.Id <= maxId && !checkUserIds.Contains(m.Id)).ToList();
            Utils.WriteLog("\n\n\n同步数量:" + users.Count(), "同步创客基本信息");
            foreach(var user in users)
            {
                Utils.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(user), "同步创客基本信息");
                int id = user.Id;
                int pid = user.Pid;
                string pidPath = user.PidPath;
                DateTime CreateDate = user.CreateTime.Value;
                string MakerCode = user.UserCode;
                string Mobile = user.Phone;
                string RealName = user.Username;
                string HeadPhoto = user.Avatar;

                if(Mobile.Length == 11)
                {
                    IQueryable<Users> dels = db.Users.Where(m => m.Mobile.StartsWith(Mobile));
                    foreach(Users del in dels)
                    {
                        db.Users.Remove(del);
                    }
                    db.SaveChanges();

                    Users add = db.Users.FirstOrDefault(m => m.Id == id);
                    if(add == null)
                    {
                        add = db.Users.Add(new Users()
                        {
                            Id = id,
                            ParentUserId = pid,
                            ParentNav = pidPath,
                            CreateDate = CreateDate,
                            MakerCode = MakerCode,
                            Mobile = Mobile,
                            RealName = RealName,
                            HeadPhoto = HeadPhoto,
                        }).Entity;
                    }
                    else
                    {
                        add.ParentUserId = pid;
                        add.ParentNav = pidPath;
                        add.CreateDate = CreateDate;
                        add.MakerCode = MakerCode;
                        add.Mobile = Mobile;
                        add.RealName = RealName;
                        add.HeadPhoto = HeadPhoto;
                    }
                    db.SaveChanges();

                    UserForMobile userformobile = db.UserForMobile.FirstOrDefault(m => m.Mobile == Mobile);
                    if(userformobile == null)
                    {
                        db.UserForMobile.Add(new UserForMobile()
                        {
                            Mobile = Mobile,
                            UserId = add.Id
                        });
                    }
                    else
                    {
                        userformobile.UserId = add.Id;
                    }

                    UserForMakerCode userformakercode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
                    if(userformakercode == null)
                    {
                        db.UserForMakerCode.Add(new UserForMakerCode()
                        {
                            MakerCode = MakerCode,
                            UserId = add.Id
                        });
                    }
                    else
                    {
                        userformakercode.UserId = add.Id;
                    }
                    db.SaveChanges();
                }
            }
            javaUser.Dispose();
            db.Dispose();
        }
    }
}