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

namespace WebAPIBase.NetCore.BusinessCore
{
    public class LicenceCheck : DbContext<SystemUser>
    {

        public void InitLicence(string connString, string VerName, string VerMsg, string LicenceType)
        {

            RightModel.InitRightModelList(VerName, VerMsg);
            string commandText = "IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[LicenceInfo]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\r\nCREATE TABLE [dbo].[LicenceInfo](\r\n  LicenceVerMsg varchar(2000)\r\n)\r\n";
            Db.Ado.ExecuteCommand(commandText);

            commandText = "select LicenceVerMsg from LicenceInfo";
            var dataTable = Db.Ado.GetDataTable(commandText);
            if (dataTable.Rows.Count != 0 && !(string.Concat(dataTable.Rows[0]["LicenceVerMsg"], "") != VerMsg))
            {
                return;
            }
            commandText = ((dataTable.Rows.Count != 0) ? string.Format("update LicenceInfo set LicenceVerMsg = '{0}'", VerMsg.Replace("'", "''")) : string.Format("insert into LicenceInfo (LicenceVerMsg) values ('{0}')", VerMsg.Replace("'", "''")));
            Db.Ado.ExecuteCommand(commandText);

            ArrayList arrayList = new ArrayList();
            ArrayList rootFunctionStructureByValid = RightModel.GetRootFunctionStructureByValid(isValid: true);
            foreach (string item in rootFunctionStructureByValid)
            {
                commandText = string.Format("update Functionstructure set IsAvailable = {0} where FunctionStructureCode like '{1}%' and isnull(IsAvailable,0) <> {0} and FunctionStructureCode not in ('{2}')", 0, item.Replace("'", "''"), RightModel.UnavailableFunctionStructureCodes.Replace(",", "','"));
                arrayList.Add(commandText);
            }
            rootFunctionStructureByValid = RightModel.GetRootFunctionStructureByValid(isValid: false);
            foreach (string item2 in rootFunctionStructureByValid)
            {
                commandText = string.Format("update Functionstructure set IsAvailable = {0} where FunctionStructureCode like '{1}%' and isnull(IsAvailable,0) <> {0} and FunctionStructureCode not in ('{2}')", 1, item2.Replace("'", "''"), RightModel.UnavailableFunctionStructureCodes.Replace(",", "','"));
                arrayList.Add(commandText);
            }
            if (VerName.ToLower() == "v35")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
            }
            if (VerName.ToLower() == "v50")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
            }
            if (VerName.ToLower() == "v55")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
                RightModelItem itemByKey3 = RightModel.GetItemByKey("UQ");
                if (itemByKey3 != null && itemByKey3.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('670204')";
                    arrayList.Add(commandText);
                }
            }
            if (VerName.ToLower() == "v56")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
                RightModelItem itemByKey3 = RightModel.GetItemByKey("UQ");
                if (itemByKey3 != null && itemByKey3.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('670204')";
                    arrayList.Add(commandText);
                }
            }
            if (VerName.ToLower() == "v561")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
                RightModelItem itemByKey3 = RightModel.GetItemByKey("UQ");
                if (itemByKey3 != null && itemByKey3.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('670204')";
                    arrayList.Add(commandText);
                }
            }
            if (VerName.ToLower() == "v60")
            {
                RightModelItem itemByKey = RightModel.GetItemByKey("UJ");
                RightModelItem itemByKey2 = RightModel.GetItemByKey("UO");
                if (itemByKey != null && itemByKey2 != null && !itemByKey.IsValid && itemByKey2.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('62','6200','620001')";
                    arrayList.Add(commandText);
                }
                RightModelItem itemByKey3 = RightModel.GetItemByKey("UQ");
                if (itemByKey3 != null && itemByKey3.IsValid)
                {
                    commandText = "update Functionstructure set IsAvailable = 0 where FunctionStructureCode in ('670204')";
                    arrayList.Add(commandText);
                }
            }
            commandText = "update Functionstructure set IsAvailable=1 where FunctionStructureCode in ('{0}')";
            commandText = string.Format(commandText, RightModel.UnavailableFunctionStructureCodes.Replace(",", "','"));
            arrayList.Add(commandText);
            string value = "delete roleoperation where operationcode not in (select functionstructurecode from functionstructure where isAvailable = 0)\r\ndelete stationfunctionarea where stationfunctioncode in (select stationfunctioncode from stationfunction where functionstructurecode not in (select functionstructurecode from functionstructure where isAvailable = 0))\r\ndelete stationfunction where functionstructurecode not in (select functionstructurecode from functionstructure where isAvailable = 0)";
            arrayList.Add(value);
            try
            {
                Db.BeginTran();
                foreach (var item in arrayList)
                {
                    Db.Ado.ExecuteCommand(item.ToString());
                }
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw;
            }

        }


        public static void DeleteInvalidFunctionStructure(string VerMsg)
        {
            ArrayList arrayList = new ArrayList();
            ArrayList rootFunctionStructureByValid = RightModel.GetRootFunctionStructureByValid(isValid: false);
            foreach (KeyValuePair<string, FdcFunction> function in FdcUnitStructure.Instance.FunctionList)
            {
                foreach (string item in rootFunctionStructureByValid)
                {
                    if (function.Key.StartsWith(item))
                    {
                        arrayList.Add(function.Key);
                        break;
                    }
                }
            }
            foreach (string item2 in arrayList)
            {
                if (VerMsg.IndexOf("UO") < 0 || (!(item2 == "62") && !(item2 == "6200") && !(item2 == "620001")))
                {
                    FdcUnitStructure.Instance.FunctionList.Remove(item2);
                }
            }
        }
    }
}
