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

namespace WebAPIBase.NetCore.BusinessCore
{
    public class UnitStrategyBuilder : StandardQueryStringBuilder
    {
        private DbContext<SystemUser> context = new DbContext<SystemUser>();
        public string QueryChildCountString = "";

        public string QueryChildCountOnlyHasUserString = "";

        public string QueryOBSString = "";

        public string QueryOBSOnlyHasUserString = "";

        public string QueryFullNameString = "";

        public string QueryOnlyFullNameString = "";

        public string QueryOnlyFullCodeString = "";

        public UnitStrategyBuilder()
        {
            QueryMainString = SqlManager.GetSqlStruct("Unit", "SelectAll").SqlString;
            QueryChildCountString = SqlManager.GetSqlStruct("Unit", "SelectChildCount").SqlString;
            QueryOBSString = SqlManager.GetSqlStruct("Unit", "SelectOBS").SqlString;
            QueryChildCountOnlyHasUserString = SqlManager.GetSqlStruct("Unit", "SelectChildCountOnlyHasUser").SqlString;
            QueryOBSOnlyHasUserString = SqlManager.GetSqlStruct("Unit", "SelectOBSOnlyHasUser").SqlString;
            QueryFullNameString = SqlManager.GetSqlStruct("Unit", "SelectFullName").SqlString;
            QueryOnlyFullNameString = SqlManager.GetSqlStruct("Unit", "SelectOnlyFullName").SqlString;
            QueryOnlyFullCodeString = SqlManager.GetSqlStruct("Unit", "SelectOnlyFullCode").SqlString;
            IsNeedWhere = true;
        }

        public override string BuildSingleStrategyString(Strategy strategy)
        {
            UnitStrategyName unitStrategyName = (UnitStrategyName)strategy.Name;
            string result = "";
            if (strategy.Type == StrategyType.Other)
            {
                switch (unitStrategyName)
                {
                    case UnitStrategyName.RelaCode:
                        {
                            string text = "项目";
                            if (strategy.GetParameterCount() > 1)
                            {
                                text = strategy.GetParameter(1);
                            }
                            result = string.Format("  exists (select * from UnitRelation where UnitCode = Unit.UnitCode and RelationCode = '{0}' and RelationType = '{1}') ", strategy.GetParameter(0).Replace("'", "''"), text.Replace("'", "''"));
                            break;
                        }
                    case UnitStrategyName.UnderUnitCode:
                        {
                            string parameter = strategy.GetParameter(0);
                            string unitFullCode = GetUnitFullCode(parameter);
                            result = string.Format("  FullCode like '{0}%' ", strategy.GetParameter(0).Replace("'", "''"));
                            break;
                        }
                    case UnitStrategyName.OnlyHasUser:
                        result = " dbo.GetUnitUserCount(UnitCode) > 0";
                        break;
                    case UnitStrategyName.ProjectCode:
                        result = string.Format(" FullCode LIKE dbo.GetUnitFullCodeByProjectCode('{0}') + '%'", strategy.GetParameter(0).Replace("'", "''"));
                        break;
                }
            }
            else
            {
                result = StandardStrategyStringBuilder.BuildStrategyString(strategy);
            }
            return result;
        }

        public string GetUnitFullCode(string unitCode)
        {
            try
            {
                string result = "";
                EntityData unitByCode = GetUnitByCode(unitCode);
                if (unitByCode.HasRecord())
                {
                    result = unitByCode.GetString("FullCode");
                }
                unitByCode.Dispose();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public EntityData GetUnitByCode(string code)
        {
            try
            {

                return SelectbyPrimaryKey(code, "Unit");

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public EntityData SelectbyPrimaryKey(object keyvalues, string entityName)
        {
            try
            {
                EntityData entitydata = null;

                SqlStruct sqlStruct = SqlManager.GetSqlStruct(entityName, "Select");
                if (sqlStruct.SqlString.Length != 0)
                {
                    if (1 != sqlStruct.ParamsList.Length)
                        throw new ApplicationException("参数列表不匹配");
                    entitydata = (EntityData)context.Db.Ado.GetDataSetAll(sqlStruct.SqlString, sqlStruct.ParamsList);


                }
                return entitydata;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public override void AddStrategy(Strategy strategy)
        {
            switch ((UnitStrategyName)strategy.Name)
            {
                case UnitStrategyName.UnitCode:
                    strategy.RelationFieldName = "UnitCode";
                    strategy.Type = StrategyType.StringEqual;
                    break;
                case UnitStrategyName.UnitCodeIn:
                    strategy.RelationFieldName = "UnitCode";
                    strategy.Type = StrategyType.StringIn;
                    break;
                case UnitStrategyName.UnitName:
                    strategy.RelationFieldName = "UnitName";
                    strategy.Type = StrategyType.StringLike;
                    break;
                case UnitStrategyName.SubjectSetCode:
                    strategy.RelationFieldName = "SubjectSetCode";
                    strategy.Type = StrategyType.StringEqual;
                    break;
                case UnitStrategyName.ParentUnitCode:
                    strategy.RelationFieldName = "ParentUnitCode";
                    strategy.Type = StrategyType.StringEqual;
                    break;
                case UnitStrategyName.UnitType:
                    strategy.RelationFieldName = "UnitType";
                    strategy.Type = StrategyType.StringRange;
                    break;
                case UnitStrategyName.SelfAccount:
                    strategy.RelationFieldName = "SelfAccount";
                    strategy.Type = StrategyType.IntegerEqual;
                    break;
                case UnitStrategyName.SortID:
                    strategy.RelationFieldName = "SortID";
                    strategy.Type = StrategyType.StringEqual;
                    break;
                case UnitStrategyName.ExpandParentUnitCode:
                    strategy.RelationFieldName = "ExpandParentUnitCode";
                    strategy.Type = StrategyType.StringEqual;
                    break;
                default:
                    strategy.Type = StrategyType.Other;
                    break;
            }
            if (SqlStrategy.CheckStrategy(strategy))
            {
                base.AddStrategy(strategy);
            }
        }

        public string BuildQueryChildCountString()
        {
            return QueryChildCountString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryOBSString()
        {
            return QueryOBSString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryChildCountOnlyHasUserString()
        {
            AddStrategy(new Strategy(UnitStrategyName.OnlyHasUser));
            return QueryChildCountOnlyHasUserString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryOBSOnlyHasUserString()
        {
            AddStrategy(new Strategy(UnitStrategyName.OnlyHasUser));
            return QueryOBSOnlyHasUserString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryFullNameString()
        {
            return QueryFullNameString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryOnlyFullNameString()
        {
            return QueryOnlyFullNameString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string BuildQueryOnlyFullCodeString()
        {
            return QueryOnlyFullCodeString + base.BuildStrategysString() + base.BuildOrderString();
        }

        public string GetDefaultOrder()
        {
            return " order by case isnull(sortid, '') when '' then '做' else sortid end, UnitName";
        }
    }

    public class StandardQueryStringBuilder : AbstractQueryStringBuilder
    {

    }
}
