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

public class LogoutUserHelper
{
    public readonly static LogoutUserHelper Instance = new LogoutUserHelper();
    private LogoutUserHelper()
    {
    }

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

    // 判断已注销的创客在每月1号，分离创客表之后，伞下自动归到注销创客的上级
    public void DoWorks()
    {
        Utils.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "每月归档注销创客日志");
        WebCMSEntities db = new WebCMSEntities();
        OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
        try
        {
            string Month = DateTime.Now.ToString("yyyyMM");
            string check = function.ReadInstance("/LogoutUser/" + Month + ".txt");
            if(string.IsNullOrEmpty(check))
            {
                function.WritePage("/LogoutUser/", Month + ".txt", DateTime.Now.ToString());
                var users = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav, m.Status }).Where(m => m.Status == -1).ToList();
                foreach(var user in users)
                {
                    Utils.WriteLog("创客Id：" + user.Id + "，上级：" + user.ParentNav, "每月归档注销创客日志");
                    int ParentUserId = user.ParentUserId;
                    bool op = true;
                    while(op)
                    {
                        Utils.WriteLog("上级创客Id：" + user.ParentUserId, "每月归档注销创客日志");
                        Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId);
                        if(puser != null)
                        {
                            if(puser.Status > -1)
                            {
                                op = false;
                                RelationForUserSub(db, user.Id, puser);
                                db.SaveChanges();
                            }
                            else
                            {
                                ParentUserId = puser.ParentUserId;
                            }
                        }
                        else
                        {
                            op = false;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "每月归档注销创客异常");
        }
        db.Dispose();
        Utils.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "每月归档注销创客日志");
    }

    private void RelationForUserSub(WebCMSEntities db, int ParentUserId, Users puser)
    {
        var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav }).Where(m => m.ParentUserId == ParentUserId).ToList();
        foreach (var subuser in subusers)
        {
            Users user = db.Users.FirstOrDefault(m => m.Id == subuser.Id);
            if (user != null)
            {
                user.ParentUserId = puser.Id;
                user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
                RelationForUserSub(db, user.Id, user);
            }
        }
    }
}