using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Sugar.Enties;

namespace WebAPIBase.NetCore.BusinessCore
{
    public class FdcUnit : DbContext<SystemUser>
    {
        public string UnitCode;

        public string FullCode;

        private string[] fullCodeList;

        public string UnitType;

        public string ParentCode;

        public string UnitName;

        public string RelaCode;

        public int RelSystem;

        private string fullName;

        public string SortId;

        private string fullSortId;

        internal List<FdcUnit> Children = new List<FdcUnit>();

        private FdcUnitStructure fus;

        public List<FdcStation> StationList = new List<FdcStation>();

        protected List<int> allUser = null;

        public string FullName
        {
            get
            {
                if (string.IsNullOrEmpty(fullName))
                {
                    for (int i = 0; i < fullCodeList.Length - 1; i++)
                    {
                        fullName = fullName + fus.GetUnitByCode(fullCodeList[i]).UnitName + "-";
                    }
                    fullName += UnitName;
                }
                return fullName;
            }
        }

        public string FullSortId
        {
            get
            {
                if (string.IsNullOrEmpty(fullSortId))
                {
                    for (int i = 0; i < fullCodeList.Length - 1; i++)
                    {
                        fullSortId = fullSortId + GetFillSortId(fus.GetUnitByCode(fullCodeList[i]).SortId) + ".";
                    }
                    fullSortId += GetFillSortId(SortId);
                }
                return fullSortId;
            }
        }

        private string GetFillSortId(string sortId)
        {
            string text = sortId;
            if (string.IsNullOrEmpty(text))
            {
                return text.PadLeft(25, 'ÿ');
            }
            return text.PadLeft(25, ' ');
        }

        internal FdcUnit(FdcUnitStructure _fus, string unitcode, string fullcode, string unittype, string parentcode, string unitname, string relacode, int systemcode)
        {
            fus = _fus;
            UnitCode = unitcode;
            FullCode = fullcode;
            UnitType = unittype;
            fullCodeList = fullcode.Split('-');
            ParentCode = parentcode;
            UnitName = unitname;
            RelaCode = relacode;
            RelSystem = systemcode;
            Children = new List<FdcUnit>();
        }

        public bool isMyParent(FdcUnit parentUnit)
        {
            if (FullCode.IndexOf(parentUnit.UnitCode) >= 0)
            {
                return true;
            }
            return false;
        }

        public bool isMyParent(string parentUnitCode)
        {
            if (FullCode.IndexOf(parentUnitCode) >= 0)
            {
                return true;
            }
            return false;
        }

        public bool isMyChild(FdcUnit unit)
        {
            if (unit.isMyParent(this))
            {
                return true;
            }
            return false;
        }

        public bool isMyChild(string unitCode)
        {
            FdcUnit unitByCode = fus.GetUnitByCode(unitCode);
            if (unitByCode != null)
            {
                return isMyChild(unitByCode);
            }
            return false;
        }

        internal void GetParent2List(List<FdcUnit> parent, string unittype)
        {
            for (int i = 0; i < fullCodeList.Length - 1; i++)
            {
                FdcUnit unitByCode = fus.GetUnitByCode(fullCodeList[i]);
                if (unitByCode != null && unitByCode.UnitType == unittype && !parent.Contains(unitByCode))
                {
                    parent.Add(unitByCode);
                }
            }
        }

        internal void GetChild2List(List<FdcUnit> children, string unittype)
        {
            bool flag = true;
            if (unittype == null || unittype.Length == 0)
            {
                throw new ApplicationException("参数错误 type不能为空");
            }
            if (children.Contains(this))
            {
                flag = false;
            }
            if (UnitType == unittype && flag)
            {
                children.Add(this);
            }
            foreach (FdcUnit child in Children)
            {
                child.GetChild2List(children, unittype);
            }
        }

        internal void GetChild2List(List<FdcUnit> children)
        {
            if (children.Contains(this))
            {
                return;
            }
            children.Add(this);
            foreach (FdcUnit child in Children)
            {
                child.GetChild2List(children);
            }
        }

        public void GetProject2List(List<FdcUnit> projectList)
        {
            GetChild2List(projectList, "项目");
            GetParent2List(projectList, "项目");
            GetChild2List(projectList, "物管处");
            GetParent2List(projectList, "物管处");
        }

        public void GetCompany2List(List<FdcUnit> companyList)
        {
            GetChild2List(companyList, "公司");
            GetParent2List(companyList, "公司");
            GetChild2List(companyList, "集团");
            GetParent2List(companyList, "集团");
        }

        public List<int> GetUnitAllUserList()
        {
            if (allUser == null)
            {
                allUser = new List<int>();

                string commandText = "select distinct su.usercode from unit u join station s on u.unitcode=s.unitcode join stationuser su on s.stationcode=su.stationcode where u.fullcode like '%' + @FullCode + '%'";
                var parameters = new List<SugarParameter>()
                {
                    new SugarParameter("@FullCode", FullCode)
                };

                var dataTable = Db.Ado.GetDataTable(commandText, parameters);
                foreach (DataRow row in dataTable.Rows)
                {
                    allUser.Add(int.Parse(row["usercode"].ToString()));
                }
            }
            return allUser;
        }

        public List<int> GetUnitUserList()
        {
            List<int> list = new List<int>();
            foreach (FdcStation station in StationList)
            {
                foreach (int user in station.UserList)
                {
                    if (!list.Contains(user))
                    {
                        list.Add(user);
                    }
                }
            }
            return list;
        }
    }
}
