using FactorySystemCommon;
using FactorySystemModel.BusinessModel;
using FactorySystemModel.EnumModel;
using FactorySystemModel.SqlSugarModel;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FactorySystemBll
{
    public class HalfMaterialTeamworkBll    {
        /// 
        /// 公共列表接口
        /// 
        //public object GetTeamworkPageList(Dictionary inParam, int userId, out int totalCount)
        //{
        //    totalCount = 0;
        //    int pageIndex = 1, pageSize = 15;
        //    List paramName = new List() { "FDeleted!=1" };
        //    List paramVal = new List();
        //    if (inParam != null && inParam.Count > 0)
        //    {
        //        foreach (var item in inParam)
        //        {
        //            if (item.Key == "FPageIndex" || item.Key == "PageIndex")
        //            {
        //                int.TryParse(item.Value.ToString(), out pageIndex);
        //            }
        //            else if (item.Key == "FPageSize" || item.Key == "PageSize")
        //            {
        //                int.TryParse(item.Value.ToString(), out pageSize);
        //            }
        //            else if (item.Key == "FAddDate" || item.Key == "AddDate")
        //            {
        //                paramName.Add("convert(varchar(10),FAddDate,120)=@FAddDate");
        //                paramVal.Add(new SugarParameter("@FAddDate", item.Value.ToString()));
        //            }
        //            else if (item.Key == "FDataType" || item.Key == "DataType")
        //            {
        //                if (item.Value.ToString() == "1")//我发起的
        //                {
        //                    paramName.Add("FAddUser=@FAddUser");
        //                    paramVal.Add(new SugarParameter("@FAddUser", userId));
        //                }
        //                else if (item.Value.ToString() == "2")//我参与的
        //                {
        //                    paramName.Add(string.Format(@"FID in(select distinct FTeamID from TFS_FTeamProcess where ','+FChargeID+',' like '%,{0},%'
        //                    union select distinct FTeamID from TFS_Task where ',' + FUserID + ',' like '%,{0},%')", userId));
        //                }
        //            }
        //            else if (item.Key == "FProgress" || item.Key == "FProgress")
        //            {
        //                string pVal = item.Value.ToString();
        //                if (!string.IsNullOrEmpty(pVal))
        //                {
        //                    if (pVal == "100" || pVal == "100%")
        //                    {
        //                        paramName.Add("FProgress='100'");
        //                    }
        //                    else
        //                    {
        //                        paramName.Add("FProgress!='100'");
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                //检索,全转成字符串
        //                paramName.Add("','+cast(" + item.Key + " as varchar)+',' like '%'+@" + item.Key + "+'%'");
        //                paramVal.Add(new SugarParameter("@" + item.Key, item.Value.ToString()));
        //            }
        //        }
        //    }
        //    string searchKey = "a.FID,a.FSaleCode,a.FTestCode,a.FViewType,a.FMdmCode,a.FFormulaID,a.FWeightUnit,a.FBomState,a.FProgress," +
        //        "a.FDeleted,a.FAddUser,a.FAddDate,a.FEditDate,b.FName FAddUserName,a.FFormulaName,a.FFormulaTestCode,a.FMaterialHalfIDs";
        //    SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
        //    var temp = db.Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.FAddUser == b.FID)).Select(searchKey);
        //    List resultList = db.Queryable(temp).Where(string.Join(" and ", paramName), paramVal)
        //        .OrderBy("FID desc").ToPageList(pageIndex, pageSize, ref totalCount);
        //    if (resultList.Count() > 0)
        //    {
        //        List teamIds = resultList.Select(s => s.FID).ToList();
        //        List taskList = db.Queryable().Where(s => teamIds.Contains(s.FTeamID) && s.FState == 1)
        //            .Select("FTeamID,FType,FName").GroupBy("FTeamID,FType,FName").OrderBy("FType").ToList();
        //        if (taskList.Count() > 0)
        //        {
        //            foreach (TFS_FTeamwork item in resultList)
        //            {
        //                item.FTaskList = taskList.Where(s => s.FTeamID == item.FID).ToList();
        //            }
        //        }
        //    }
        //    return resultList;
        //}
        public object GetTeamworkPageList(Dictionary inParam, int userId, out int totalCount) 
        {
            totalCount = 0;
            int pageIndex = 1, pageSize = 15;
            List paramName = new List() { "FDeleted!=1" };
            List paramVal = new List();
            List FAddDate = null;
            if (inParam.ContainsKey("FAddDate"))
            {
                FAddDate = JsonConvert.DeserializeObject>(inParam["FAddDate"].ToString());
            }
            if (inParam != null && inParam.Count > 0)
            {
                foreach (var item in inParam)
                {
                    if (item.Key == "FPageIndex" || item.Key == "PageIndex")
                    {
                        int.TryParse(item.Value.ToString(), out pageIndex);
                    }
                    else if (item.Key == "FPageSize" || item.Key == "PageSize")
                    {
                        int.TryParse(item.Value.ToString(), out pageSize);
                    }
                    else if (item.Key == "FAddDate" || item.Key == "AddDate")
                    {
                        if (!string.IsNullOrWhiteSpace(FAddDate[0].ToString())) 
                        {
                            paramName.Add("convert(varchar(10),FAddDate,120)>='" + FAddDate[0].ToString() + "'");
                            paramName.Add("convert(varchar(10),FAddDate,120)<='" + FAddDate[1].ToString() + "'");
                        }
                    }
                    else if (item.Key == "FDataType" || item.Key == "DataType")
                    {
                        if (item.Value.ToString() == "1")//我发起的
                        {
                            paramName.Add("FAddUser=@FAddUser");
                            paramVal.Add(new SugarParameter("@FAddUser", userId));
                        }
                        else if (item.Value.ToString() == "2")//我参与的
                        {
                            paramName.Add(string.Format(@"FID in(select distinct FTeamID from TFS_FTeamProcess where ','+FChargeID+',' like '%,{0},%'
                            union select distinct FTeamID from TFS_Task where ',' + FUserID + ',' like '%,{0},%')", userId));
                        }
                    }
                    else if (item.Key == "FProgress" || item.Key == "FProgress")
                    {
                        string pVal = item.Value.ToString();
                        if (!string.IsNullOrEmpty(pVal))
                        {
                            if (pVal == "100" || pVal == "100%")
                            {
                                paramName.Add("FProgress='100'");
                            }
                            else
                            {
                                paramName.Add("FProgress!='100'");
                            }
                        }
                    }
                    else
                    {
                        //检索,全转成字符串
                        paramName.Add("','+cast(" + item.Key + " as varchar)+',' like '%'+@" + item.Key + "+'%'");
                        paramVal.Add(new SugarParameter("@" + item.Key, item.Value.ToString()));
                    }
                }
            }
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            string searchKey = string.Format(@"a.*,d.FName");
            var temp =db.Queryable((a,d) => new JoinQueryInfos(JoinType.Left, a.FAddUser == d.FID)).Select