using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;


namespace WebAPIBase.NetCore.BusinessCore
{
    public class FdcUser : DbContext<SystemUser>
    {
        private string userCode;

        private string userName;

        private string userId;

        private string shortUserName;

        private int status;

        private string sortID;

        private string fZPassWord;

        private string workNo;

        private List<FdcStation> stationList = new List<FdcStation>();

        public string UserCode => userCode;

        public string UserName => userName;

        public string UserId => userId;

        public string ShortUserName => shortUserName;

        public int Status => status;

        public string SortID => sortID;

        public string FZPassWord => fZPassWord;

        public string WorkNo => workNo;

        public List<FdcStation> StationList => stationList;

        public bool HasRight(string functionCode)
        {
            if (FdcUnitStructure.Instance.FunctionList.ContainsKey(functionCode))
            {
                if (userCode == "100000")
                {
                    return true;
                }
                foreach (FdcStation station in stationList)
                {
                    if (station.HasRight(functionCode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool HasUnitRight(string functionCode, string unitCode)
        {
            if (FdcUnitStructure.Instance.FunctionList.ContainsKey(functionCode))
            {
                if (userCode == "100000")
                {
                    return true;
                }
                foreach (FdcStation station in stationList)
                {
                    if (station.HasUnitRight(functionCode, unitCode))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool HasSubUnitRight(string functionCode, string unitCode)
        {
            if (userCode == "100000")
            {
                return true;
            }
            foreach (FdcStation station in stationList)
            {
                if (station.HasSubUnitRight(functionCode, unitCode))
                {
                    return true;
                }
            }
            return false;
        }

        public bool HasGroupRight(string functionCode, string groupCode)
        {
            if (userCode == "100000")
            {
                return true;
            }
            foreach (FdcStation station in stationList)
            {
                if (station.HasGroupRight(functionCode, groupCode))
                {
                    return true;
                }
            }
            return false;
        }

        public bool HasRight(string functionCode, string unitCode, string groupCode)
        {
            if (userCode == "100000")
            {
                return true;
            }
            foreach (FdcStation station in stationList)
            {
                if (station.HasRight(functionCode, unitCode, groupCode))
                {
                    return true;
                }
            }
            return false;
        }

        public List<FdcFunction> GetRight()
        {
            List<FdcFunction> list = new List<FdcFunction>();
            List<string> list2 = new List<string>();
            foreach (FdcStation station in stationList)
            {
                foreach (KeyValuePair<string, innerFunction> function in station.functionList)
                {
                    innerFunction value = function.Value;
                    if (!list2.Contains(value.FunctionCode))
                    {
                        FdcFunction functionByCode = FdcUnitStructure.Instance.GetFunctionByCode(value.FunctionCode);
                        if (functionByCode != null)
                        {
                            list.Add(functionByCode);
                            list2.Add(functionByCode.FunctionStructureCode);
                        }
                    }
                }
            }
            return list;
        }

        public List<FdcFunctionUnit> GetRightByUnit(string unitCode)
        {
            List<FdcFunctionUnit> list = new List<FdcFunctionUnit>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                foreach (KeyValuePair<string, innerFunction> item in functionList)
                {
                    innerFunction value = item.Value;
                    foreach (FdcUnitRight unit in value.UnitList)
                    {
                        if (unit.unit.isMyChild(unitCode))
                        {
                            FdcFunctionUnit fdcFunctionUnit = new FdcFunctionUnit();
                            fdcFunctionUnit.FunctionCode = value.FunctionCode;
                            fdcFunctionUnit.unit = unit.unit;
                            fdcFunctionUnit.IsPerson = unit.IsPerson;
                            list.Add(fdcFunctionUnit);
                        }
                    }
                }
            }
            return list;
        }

        public List<FdcFunctionUnit> GetRightByGroup(string groupCode)
        {
            List<FdcFunctionUnit> list = new List<FdcFunctionUnit>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                foreach (KeyValuePair<string, innerFunction> item in functionList)
                {
                    innerFunction value = item.Value;
                    foreach (FdcGroupUnitRight groupUnit in value.GroupUnitList)
                    {
                        if (groupUnit.group.isMyChild(groupCode))
                        {
                            FdcFunctionUnit fdcFunctionUnit = new FdcFunctionUnit();
                            fdcFunctionUnit.FunctionCode = value.FunctionCode;
                            fdcFunctionUnit.unit = groupUnit.unit;
                            fdcFunctionUnit.IsPerson = groupUnit.IsPerson;
                            list.Add(fdcFunctionUnit);
                        }
                    }
                }
            }
            return list;
        }

        public List<FdcFunctionUnit> GetRightByUnitGroup(string unitCode, string groupCode)
        {
            List<FdcFunctionUnit> list = new List<FdcFunctionUnit>();
            if (unitCode == null || unitCode == "")
            {
                throw new ArgumentException("unit code不能为空");
            }
            if (groupCode == null || groupCode == "")
            {
                throw new ArgumentException("group code不能为空");
            }
            foreach (FdcStation station in stationList)
            {
                foreach (KeyValuePair<string, innerFunction> function in station.functionList)
                {
                    innerFunction value = function.Value;
                    foreach (FdcGroupUnitRight groupUnit in value.GroupUnitList)
                    {
                        if (groupUnit.unit.isMyChild(unitCode) && groupUnit.group.isMyChild(groupCode))
                        {
                            FdcFunctionUnit fdcFunctionUnit = new FdcFunctionUnit();
                            fdcFunctionUnit.FunctionCode = value.FunctionCode;
                            fdcFunctionUnit.unit = groupUnit.unit;
                            fdcFunctionUnit.IsPerson = groupUnit.IsPerson;
                            list.Add(fdcFunctionUnit);
                        }
                    }
                }
            }
            return list;
        }

        public List<FdcUnit> GetProjectsCanVisit()
        {
            List<FdcUnit> list = new List<FdcUnit>();
            foreach (FdcStation station in stationList)
            {
                foreach (FdcUnitRight unit in station.unitList)
                {
                    FdcUnit unitByCode = FdcUnitStructure.Instance.GetUnitByCode(unit.unit.UnitCode);
                    if (unitByCode != null && !list.Contains(unitByCode))
                    {
                        list.Add(unitByCode);
                    }
                }
            }
            List<FdcUnit> list2 = new List<FdcUnit>();
            foreach (FdcUnit item in list)
            {
                item.GetProject2List(list2);
            }
            return list2;
        }

        public List<FdcUnit> GetCompanysCanVisit()
        {
            List<FdcUnit> list = new List<FdcUnit>();
            foreach (FdcStation station in stationList)
            {
                foreach (FdcUnitRight unit in station.unitList)
                {
                    FdcUnit unitByCode = FdcUnitStructure.Instance.GetUnitByCode(unit.unit.UnitCode);
                    if (unitByCode != null && !list.Contains(unitByCode))
                    {
                        list.Add(unitByCode);
                    }
                }
            }
            List<FdcUnit> list2 = new List<FdcUnit>();
            foreach (FdcUnit item in list)
            {
                item.GetCompany2List(list2);
            }
            return list2;
        }

        public List<FdcUnit> GetAllUnitsCanVisit()
        {
            List<FdcUnit> list = new List<FdcUnit>();
            foreach (FdcStation station in stationList)
            {
                foreach (FdcUnitRight unit in station.unitList)
                {
                    FdcUnit unitByCode = FdcUnitStructure.Instance.GetUnitByCode(unit.unit.UnitCode);
                    if (unitByCode != null && !list.Contains(unitByCode))
                    {
                        list.Add(unitByCode);
                    }
                }
            }
            List<FdcUnit> list2 = new List<FdcUnit>();
            foreach (FdcUnit item in list)
            {
                item.GetChild2List(list2);
            }
            return list2;
        }

        public List<FdcSystemGroup> GetDefinedSystemGroup(string functionCode)
        {
            List<FdcSystemGroup> list = new List<FdcSystemGroup>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                if (!functionList.ContainsKey(functionCode))
                {
                    continue;
                }
                innerFunction innerFunction = functionList[functionCode];
                foreach (FdcGroupUnitRight groupUnit in innerFunction.GroupUnitList)
                {
                    FdcSystemGroup group = groupUnit.group;
                    if (!list.Contains(group))
                    {
                        list.Add(group);
                    }
                }
            }
            return list;
        }

        public List<FdcUnitRight> GetUnitRight(string functioncode)
        {
            List<FdcUnitRight> list = new List<FdcUnitRight>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                if (!functionList.ContainsKey(functioncode))
                {
                    continue;
                }
                innerFunction innerFunction = functionList[functioncode];
                foreach (FdcUnitRight unit in innerFunction.UnitList)
                {
                    bool flag = false;
                    foreach (FdcUnitRight item in list)
                    {
                        if (item.unit == unit.unit && item.IsPerson == unit.IsPerson)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        list.Add(unit);
                    }
                }
            }
            return list;
        }

        public List<FdcGroupUnitRight> GetGroupUnitRight(string functioncode)
        {
            List<FdcGroupUnitRight> list = new List<FdcGroupUnitRight>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                if (!functionList.ContainsKey(functioncode))
                {
                    continue;
                }
                innerFunction innerFunction = functionList[functioncode];
                foreach (FdcGroupUnitRight groupUnit in innerFunction.GroupUnitList)
                {
                    bool flag = false;
                    foreach (FdcGroupUnitRight item in list)
                    {
                        if (item.group == groupUnit.group && item.unit == groupUnit.unit && item.IsPerson == groupUnit.IsPerson)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        list.Add(groupUnit);
                    }
                }
            }
            return list;
        }

        public List<FdcSystemGroup> GetAllSystemGroup()
        {
            List<FdcSystemGroup> list = new List<FdcSystemGroup>();
            foreach (FdcStation station in stationList)
            {
                foreach (KeyValuePair<string, innerFunction> function in station.functionList)
                {
                    innerFunction value = function.Value;
                    foreach (FdcGroupUnitRight groupUnit in value.GroupUnitList)
                    {
                        groupUnit.group.GetChild2List(list);
                    }
                }
            }
            return list;
        }

        public List<FdcSystemGroup> GetAllSystemGroup(string functionCode)
        {
            List<FdcSystemGroup> list = new List<FdcSystemGroup>();
            foreach (FdcStation station in stationList)
            {
                Dictionary<string, innerFunction> functionList = station.functionList;
                if (!functionList.ContainsKey(functionCode))
                {
                    continue;
                }
                innerFunction innerFunction = functionList[functionCode];
                foreach (FdcGroupUnitRight groupUnit in innerFunction.GroupUnitList)
                {
                    FdcSystemGroup group = groupUnit.group;
                    group.GetChild2List(list);
                }
            }
            return list;
        }

        public List<FdcUnit> GetUserUnit()
        {
            List<FdcUnit> list = new List<FdcUnit>();
            foreach (FdcStation station in StationList)
            {
                FdcUnit unitByCode = FdcUnitStructure.Instance.GetUnitByCode(station.UnitCode);
                if (!list.Contains(unitByCode))
                {
                    list.Add(unitByCode);
                }
            }
            return list;
        }

        public bool IsUserUnderUnit(FdcUnit unit)
        {
            foreach (FdcStation station in StationList)
            {
                FdcUnit unitByCode = FdcUnitStructure.Instance.GetUnitByCode(station.UnitCode);
                if (unitByCode.isMyParent(unit))
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsUserUnderUnit(string unitCode)
        {
            return IsUserUnderUnit(FdcUnitStructure.Instance.GetUnitByCode(unitCode));
        }

        public RightScope GetUnitRightScope(string unitCode)
        {
            RightScope result = RightScope.无;
            foreach (FdcStation station in stationList)
            {
                RightScope unitRightScope = station.GetUnitRightScope(unitCode);
                if (unitRightScope == RightScope.部门)
                {
                    return RightScope.部门;
                }
                result = unitRightScope;
            }
            return result;
        }

        public FdcUser(string usercode)
        {
            userCode = usercode;
            string commandText = "select username,userid,shortusername,status,sortid,fzpassword,workno from systemuser where usercode=@usercode";
            var parameters = new List<SugarParameter>() {
               new SugarParameter( "@usercode", usercode)
            };
            using (var sqlDataReader = Db.Ado.GetDataReader(commandText, parameters))
            {
                if (!sqlDataReader.Read())
                {
                    throw new ApplicationException("用户不存在:" + usercode);
                }
                userName = sqlDataReader["username"].ToString();
                userId = sqlDataReader["userid"].ToString();
                shortUserName = sqlDataReader["shortusername"].ToString();
                status = (int)sqlDataReader["status"];
                if (sqlDataReader["sortid"] != DBNull.Value)
                {
                    sortID = sqlDataReader["sortid"].ToString();
                }
                else
                {
                    sortID = "";
                }
                if (sqlDataReader["fzpassword"] != DBNull.Value)
                {
                    fZPassWord = sqlDataReader["fzpassword"].ToString();
                }
                else
                {
                    fZPassWord = "";
                }
                if (sqlDataReader["workno"] != DBNull.Value)
                {
                    workNo = sqlDataReader["workno"].ToString();
                }
                else
                {
                    workNo = "";
                }
            }
            string commandText2 = "select stationcode from stationuser where usercode=@usercode";
            using (var sqlDataReader = Db.Ado.GetDataReader(commandText2, parameters))
            {
                while (sqlDataReader.Read())
                {
                    try
                    {
                        FdcStation stationByCode = FdcUnitStructure.Instance.GetStationByCode(sqlDataReader["stationcode"].ToString());
                        if (stationByCode != null)
                        {
                            stationList.Add(stationByCode);
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}
