using System;
using System.Collections.Generic;
using System.Linq;
using FactorySystemCommon;
using FactorySystemModel.EnumModel;
using FactorySystemModel.RequestModel;
using FactorySystemModel.ResponseModel;
using FactorySystemModel.SqlSugarModel;
using Newtonsoft.Json;
using SqlSugar;

namespace FactorySystemBll
{
    public class HalfMaterialTaskBll
    {
        /// <summary>
        /// 获取任务列表
        /// </summary>
        public List<object> GetList(TaskQuery tq, out int totalNumber)
        {
            totalNumber = 0;
            var db = AppSettingsHelper.GetSqlSugar();
            return db.Queryable<TFS_HalfMaterialTask, TFS_HalfMaterialFTeamwork, TBasicCode, TUser>((a, b, c, d) => new JoinQueryInfos(JoinType.Inner, a.FTeamID == b.FID,
                    JoinType.Inner, c.FType == 46 && a.FType == int.Parse(c.FValue), JoinType.Left, a.FEditUser == d.FID))
                // 事项状态
                .WhereIF(tq.FState > 0 && tq.FState != 99, (a, b) => a.FState == tq.FState)
                // 事项状态
                .WhereIF(tq.FState == 99, (a, b) => a.FState <= 1)
                .WhereIF(tq.FTestCode != null,(a,b)=>b.FNewTestCode.Contains(tq.FTestCode))
                // 销售号
                .WhereIF(tq.FSaleCode != null, (a, b) => b.FSaleCode.Contains(tq.FSaleCode))
                // 当前流程
                .WhereIF(tq.FType > 0, (a, b) => a.FType == tq.FType)
                // 发起时间1
                .WhereIF(tq.FDateRange != null && tq.FDateRange[0] != "", (a, b) => a.FAddDate >= DateTime.Parse(tq.FDateRange[0]))
                .WhereIF(tq.FDateRange != null && tq.FDateRange[1] != "", (a, b) => a.FAddDate <= DateTime.Parse(tq.FDateRange[1]))
                // 责任人
                .WhereIF(tq.FUserID != null, a => (',' + a.FUserID + ',').Contains(',' + tq.FUserID + ','))
                // 协同
                .WhereIF(tq.FTeamID > 0, a => a.FTeamID == tq.FTeamID).OrderBy((a, b, c) => a.FID, OrderByType.Desc)
                .Select<object>("a.*,b.FNewTestCode,b.FVersionCode,a.FType as 'FormulaType',b.FSaleCode,b.FFormulaTestCode as FTestCode,b.FMdmCode,b.FFormulaName,b.FMaterialHalfIDs,cast(substring(c.FRemark,4,2)as int)as FViewType,d.FName AS 'FUserName1',(case when b.FBomJson IS NULL then 0 when cast(b.FBomJson as nvarchar) = '' then 0 else 1 end) as FHasBomJson,b.FBomJson")
                .ToPageList(tq.FPageIndex, tq.FPageSize, ref totalNumber);
        }



        public int CloseHalfMaterialTask(int teamId, int userId, int type)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            List<TFS_HalfMaterialTask> taskList = db.Queryable<TFS_HalfMaterialTask>().Where(s => s.FType == type && s.FTeamID == teamId).ToList();
            int result = 0;

            if (taskList != null && taskList.Count > 0)
            {
                TFS_HalfMaterialTask fmt = taskList[0];

                fmt.FCanEdit = 2;
                fmt.FState = 2;
                fmt.FEditUser = userId;
                fmt.FEditDate = DateTime.Now;

                result = db.Updateable(fmt).IgnoreColumns(true).WhereColumns("FID").ExecuteCommand();
            }
            return result;
        }

        /// <summary>
        /// 关闭任务当是可以编辑
        /// </summary>
        /// <param name="teamId"></param>
        /// <param name="userId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public int CloseHalfMaterialTask2(int teamId, int userId, int type)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            List<TFS_HalfMaterialTask> taskList = db.Queryable<TFS_HalfMaterialTask>().Where(s => s.FType == type && s.FTeamID == teamId && s.FState == 1).ToList();
            int result = 0;

            if (taskList != null && taskList.Count > 0)
            {
                TFS_HalfMaterialTask fmt = taskList[0];

                fmt.FState = 2;
                fmt.FEditUser = userId;
                fmt.FEditDate = DateTime.Now;

                result = db.Updateable(fmt).IgnoreColumns(true).WhereColumns("FID").ExecuteCommand();
            }
            return result;
        }
    }
}
