using APT.BaseData.Domain.ApiModel.PF;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Api;
using APT.Infrastructure.Core;
using APT.Migrations;
using APT.MS.Domain.ApiModel.BS;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.SC.SC;
using APT.MS.Domain.Enums;
using APT.Utility;
using APT.WebApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSOperateLog")]
    public partial class BSOperateLogController : AuthorizeApiController<T_BS_OPERATE_LOG>
    {
        /// <summary>
        /// 排序查询所有数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>

        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<List<T_BS_OPERATE_LOG>>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<List<T_BS_OPERATE_LOG>>>(() =>
            {
                if (string.IsNullOrEmpty(filter.Keyword))
                {
                    return new List<List<T_BS_OPERATE_LOG>>();
                }
                else
                {
                    Guid Keyword = new Guid(filter.Keyword);
                    Guid? CheckID = null;
                    Guid? SubmitID = null;
                    Guid? SubmitContentID = null;

                    var check = GetEntity<T_BS_SAFE_CHECK>(Keyword);
                    T_BS_RISK_SUBMIT_CONTENT riskContent = null;
                    T_BS_RISK_SUBMIT risk = null;
                    if (check != null)
                    {
                        CheckID = check.ID;
                        risk = GetEntity<T_BS_RISK_SUBMIT>(e => e.CHECK_ID.HasValue && e.CHECK_ID.Value == CheckID);
                        if (risk != null)
                        {
                            SubmitID = risk.ID;
                        }
                    }
                    else
                    {
                        risk = GetEntity<T_BS_RISK_SUBMIT>(Keyword);
                        if (risk != null)
                        {
                            SubmitID = risk.ID;
                            if (risk.CHECK_ID.HasValue)
                            {
                                CheckID = risk.CHECK_ID.Value;
                            }
                        }
                        else
                        {
                            riskContent = GetEntity<T_BS_RISK_SUBMIT_CONTENT>(Keyword);
                            if (riskContent != null)
                            {
                                SubmitContentID = riskContent.ID;
                                SubmitID = riskContent.RISK_SUBMIT_ID;
                                risk = GetEntity<T_BS_RISK_SUBMIT>(SubmitID.Value);
                                if (risk != null && risk.CHECK_ID.HasValue)
                                {
                                    CheckID = risk.CHECK_ID.Value;
                                }
                            }
                        }
                    }

                    List<List<T_BS_OPERATE_LOG>> result = new List<List<T_BS_OPERATE_LOG>>();
                    if (CheckID == null && SubmitID == null && SubmitContentID == null)
                    {
                        return result;
                    }

                    List<T_BS_OPERATE_LOG> resultCheck1 = new List<T_BS_OPERATE_LOG>();//检查任务
                    List<T_BS_OPERATE_LOG> resultCheck2 = new List<T_BS_OPERATE_LOG>();//检查记录
                    List<T_BS_OPERATE_LOG> resultRisk1 = new List<T_BS_OPERATE_LOG>();//隐患上报
                    List<T_BS_OPERATE_LOG> resultRiskDeal = new List<T_BS_OPERATE_LOG>();//隐患整改
                    List<T_BS_OPERATE_LOG> resultRiskEnd = new List<T_BS_OPERATE_LOG>();//隐患上报

                    if (CheckID != null)
                    {
                        var resultT = GetEntities<T_BS_OPERATE_LOG>(e => e.DATA_ID == CheckID.Value, null, null).OrderBy(e => e.OPERATEPOINT).ToList();
                        resultCheck1 = resultT.Where(e => e.OPERATEPOINT == OPERATEPOINT_Enums.GotCheck || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckForm || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckAudit || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckerCheck).OrderBy(e => e.OPERATEPOINT).ToList();

                        resultCheck2 = resultT.Where(e => e.OPERATEPOINT == OPERATEPOINT_Enums.CheckRegisterRun || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckRegister || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckAgree || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckResultAudit || e.OPERATEPOINT == OPERATEPOINT_Enums.CheckEnd).OrderBy(e => e.OPERATEPOINT).ToList();
                    }
                    result.Add(resultCheck1);
                    result.Add(resultCheck2);
                    int RiskContentCount = 0;//隐患数
                    if (SubmitID != null)
                    {
                        Expression<Func<T_BS_OPERATE_LOG, bool>> expression = e => e.DATA_ID == SubmitID.Value;
                        if (SubmitContentID != null)
                        {
                            expression = e => e.DATA_ID == SubmitID.Value && (!e.DATA_ID_SUB.HasValue || e.DATA_ID_SUB.Value == SubmitContentID);
                        }

                        var resultT = GetEntities(expression, null, null).OrderBy(e => e.DATA_ID_SUB).ThenBy(e => e.OPERATEPOINT).ToList();
                        resultRisk1 = resultT.Where(e => (!e.DATA_ID_SUB.HasValue && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskEnd) || (e.OPERATEPOINT == OPERATEPOINT_Enums.RiskNoticeSD || e.OPERATEPOINT == OPERATEPOINT_Enums.RiskUpSD || e.OPERATEPOINT == OPERATEPOINT_Enums.RiskInfoCompleteSD)).OrderBy(e => e.OPERATEPOINT).ToList();
                        result.Add(resultRisk1);

                        //多个上报 多组信息
                        var listSubID = resultT.Where(e => e.DATA_ID_SUB.HasValue && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskUpSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskInfoCompleteSD).Select(e => e.DATA_ID_SUB).Distinct().ToList();
                        if (listSubID != null && listSubID.Count > 0)
                        {
                            foreach (var item in listSubID)
                            {
                                RiskContentCount++;
                                if (RiskContentCount == 1)
                                {
                                    resultRiskDeal = resultT.Where(e => e.DATA_ID_SUB.HasValue && e.DATA_ID_SUB.Value == item && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskNoticeSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskUpSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskInfoCompleteSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskGet).OrderBy(e => e.CREATE_TIME).ThenBy(e => e.OPERATEPOINT).ThenBy(e => e.ORDERNO).ToList();
                                    result.Add(resultRiskDeal);
                                }
                                else
                                {
                                    result.Add(resultT.Where(e => e.DATA_ID_SUB.HasValue && e.DATA_ID_SUB.Value == item && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskNoticeSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskUpSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskInfoCompleteSD && e.OPERATEPOINT != OPERATEPOINT_Enums.RiskGet).OrderBy(e => e.CREATE_TIME).ThenBy(e => e.OPERATEPOINT).ThenBy(e => e.ORDERNO).ToList());
                                }
                            }
                        }
                        else
                        {
                            result.Add(resultRiskDeal);
                        }

                        var resultRiskEndT = resultT.FirstOrDefault(e => !e.DATA_ID_SUB.HasValue && e.OPERATEPOINT == OPERATEPOINT_Enums.RiskEnd);
                        if (resultRiskEndT != null)
                        {
                            resultRiskEnd.Add(resultRiskEndT);
                            result.Add(resultRiskEnd);
                        }
                    }

                    #region    给他们 补齐 OPERATEPOINT

                    List<EnumsResult> listEnums = DataHelper.GetEnum("OPERATEPOINT_Enums");

                    //检查任务
                    //检查记录
                    //隐患上报
                    /* 
                     * 
                     * 隐患整改
                     * 
                     */
                    //结束

                    T_BS_OPERATE_LOG ModelLog = null;
                    if (CheckID != null)
                    {
                        //安全检查
                        if (resultCheck1.Count > 0)
                        {
                            ModelLog = resultCheck1[resultCheck1.Count - 1];
                            if (ModelLog.OPERATEPOINT != OPERATEPOINT_Enums.CheckerCheck)
                            {
                                resultCheck1.AddRange(GetListLog((int)ModelLog.OPERATEPOINT, (int)OPERATEPOINT_Enums.CheckerCheck, listEnums));
                                result[1] = resultCheck1;
                            }
                        }
                        else
                        {
                            resultCheck1.AddRange(GetListLog((int)OPERATEPOINT_Enums.GotCheck, (int)OPERATEPOINT_Enums.CheckerCheck, listEnums));
                            result[1] = resultCheck1;
                        }

                        if (resultCheck2.Count > 0)
                        {
                            ModelLog = resultCheck2[resultCheck2.Count - 1];
                            if (ModelLog.OPERATEPOINT != OPERATEPOINT_Enums.CheckEnd)
                            {
                                resultCheck2.AddRange(GetListLog((int)ModelLog.OPERATEPOINT, (int)OPERATEPOINT_Enums.CheckEnd, listEnums));
                                result[1] = resultCheck2;
                            }
                        }
                        else
                        {
                            resultCheck2.AddRange(GetListLog((int)OPERATEPOINT_Enums.CheckRegisterRun, (int)OPERATEPOINT_Enums.CheckEnd, listEnums));
                            result[1] = resultCheck2;
                        }
                    }

                    bool isAddApplyDely = false;

                    if (SubmitID.HasValue)
                    {
                        if (resultRisk1 != null && resultRisk1.Count > 0)
                        {
                            if (resultRisk1[0].OPERATEPOINT != OPERATEPOINT_Enums.RiskGet) //检查上报 如果有 一定有值 不需要补
                            {
                                ModelLog = resultRisk1[resultRisk1.Count - 1];
                                if (ModelLog.OPERATEPOINT != OPERATEPOINT_Enums.RiskInfoCompleteSD)
                                {
                                    resultRisk1.AddRange(GetListLog((int)OPERATEPOINT_Enums.RiskUpSD, (int)OPERATEPOINT_Enums.RiskInfoCompleteSD, listEnums, isAddApplyDely, true));
                                    result[2] = resultRisk1;
                                }
                            }
                        }
                        else if (risk != null)
                        {
                            if (risk.SUBMIT_TYPE == 20)
                            {
                                result[2] = GetListLog((int)OPERATEPOINT_Enums.RiskNoticeSD, (int)OPERATEPOINT_Enums.RiskInfoCompleteSD, listEnums, isAddApplyDely, true);
                            }
                            else if (risk.SUBMIT_TYPE == 10)
                            {
                                result[2] = new List<T_BS_OPERATE_LOG>() { new T_BS_OPERATE_LOG() {
                                OPERATEPOINT = OPERATEPOINT_Enums.RiskGet
                                } };
                            }
                        }

                        //隐患整改
                        if (RiskContentCount == 0)
                        {
                            resultRiskDeal = GetListLog((int)OPERATEPOINT_Enums.RiskSendAudit, (int)OPERATEPOINT_Enums.RiskDealAudit, listEnums);//, false, false
                            if (resultRiskDeal != null && resultRiskDeal.Count > 0)
                                result[3].AddRange(resultRiskDeal);
                        }
                        else
                        {
                            List<T_BS_OPERATE_LOG> listLogT = null;
                            for (int i = 0; i < RiskContentCount; i++)
                            {
                                listLogT = result[i + 3];
                                if (listLogT.Count > 0)
                                {
                                    //第4个开始  下标 3
                                    ModelLog = listLogT[listLogT.Count - 1];
                                    if (ModelLog.OPERATEPOINT != OPERATEPOINT_Enums.RiskDealAudit)
                                    {
                                        var appCheck = listLogT.FirstOrDefault(e => e.OPERATEPOINT == OPERATEPOINT_Enums.RiskDelayApply);
                                        if (appCheck != null && appCheck.ID != Guid.Empty)
                                            isAddApplyDely = true;
                                        result[i + 3].AddRange(GetListLog((int)ModelLog.OPERATEPOINT, (int)OPERATEPOINT_Enums.RiskDealAudit, listEnums, isAddApplyDely));
                                    }
                                }
                                else
                                {
                                    result[i + 3] = GetListLog((int)OPERATEPOINT_Enums.RiskSendAudit, (int)OPERATEPOINT_Enums.RiskDealAudit, listEnums);
                                }
                            }
                        }

                        //结束
                        if (resultRiskEnd.Count == 0)
                        {
                            resultRiskEnd.Add(new T_BS_OPERATE_LOG()
                            {
                                OPERATEPOINT = OPERATEPOINT_Enums.RiskEnd
                            });
                            result.Add(resultRiskEnd);
                        }
                    }

                    #endregion

                    return result;
                }
            });
        }

        /// <summary>
        /// 添加固定项 Start 不添加 end 添加
        /// </summary>
        /// <param name="Start"></param>
        /// <param name="End"></param>
        /// <param name="listEnums"></param>
        /// <param name="isAddApplyDely"></param>
        /// <param name="isRiskSD"></param>
        /// <returns></returns>
        public List<T_BS_OPERATE_LOG> GetListLog(int Start, int End, List<EnumsResult> listEnums, bool isAddApplyDely = false, bool isRiskSD = false)
        {
            List<T_BS_OPERATE_LOG> listResult = new List<T_BS_OPERATE_LOG>();
            if (Start >= End)
                return listResult;

            foreach (var item in listEnums)
            {
                if (item.ID == 195 || item.ID == 205)//驳回操作 不默认添加
                    continue;

                if (Start < item.ID && item.ID <= End)
                {
                    if (!isAddApplyDely && (item.ID == 165 || item.ID == 170 || item.ID == 175))
                        continue;
                    if (isRiskSD)
                    {
                        if (item.ID == 120)
                            continue;//手动隐患上报  非手动隐患上报的信息 返回
                    }
                    else
                    {
                        if (item.ID == 90 || item.ID == 100 || item.ID == 110)
                            continue;//非手动隐患上报  手动隐患上报的信息 返回
                    }

                    listResult.Add(new T_BS_OPERATE_LOG()
                    {
                        OPERATEPOINT = (OPERATEPOINT_Enums)item.ID
                    });
                }
            }
            return listResult;
        }
    }
}