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.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Entities.HM;
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;

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSRiskSubmitContentDeal")]
    public partial class BSRiskSubmitContentDealController : AuthorizeApiController<T_BS_RISK_SUBMIT_CONTENT_DEAL>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IPFCodeRuleService CodeRuleService { get; set; }
        IPFSysLogService SysLogService { get; set; }
        public BSRiskSubmitContentDealController(IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFCodeRuleService codeRuleService, IPFSysLogService sysLogService)
        {
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            CodeRuleService = codeRuleService;
            SysLogService = sysLogService;
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetAll")]
        public JsonActionResult<T_BS_RISK_SUBMIT_CONTENT_DEAL> GetAll([FromBody] KeywordFilter filter)
        {
            return SafeExecute<T_BS_RISK_SUBMIT_CONTENT_DEAL>(() =>
            {
                ICollection<string> Include = new List<string>();
                foreach (var item in filter.Include)
                {
                    if (item.Contains(".Nav_UserSignFiles"))
                    {
                        Include.Add(item.Split(".Nav_UserSignFiles")[0]);
                    }
                    else
                    {
                        Include.Add(item);
                    }
                }
                filter.Include = Include;
                var modelSubConDel = GetEntity<T_BS_RISK_SUBMIT_CONTENT_DEAL>(null, filter);
                if (modelSubConDel == null)
                {
                    throw new Exception("获取整改单信息失败！");
                }
                List<Guid> listUserID = new List<Guid>();
                if (modelSubConDel.DEAL_USER_ID.HasValue)
                    listUserID.Add(modelSubConDel.DEAL_USER_ID.Value);
                if (modelSubConDel.CHECK_USER_ID.HasValue)
                    listUserID.Add(modelSubConDel.CHECK_USER_ID.Value);
                if (modelSubConDel.Nav_ApproveCheckAudit != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails.Any())
                {
                    foreach (var item in modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails)
                    {
                        if (item.APPROVE_USER_ID.HasValue)
                            listUserID.Add(item.APPROVE_USER_ID.Value);
                    }
                }
                if (listUserID.Count > 0)
                {
                    listUserID = listUserID.Distinct().ToList();
                    var listUsersSign = GetEntities<T_FM_USER_SIGN_FILE>(e => listUserID.Contains(e.USER_ID), null, "Nav_ImgFile.Nav_File");
                    if (modelSubConDel.DEAL_USER_ID.HasValue)
                        modelSubConDel.Nav_UserDeal.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == modelSubConDel.DEAL_USER_ID.Value).ToList();
                    if (modelSubConDel.CHECK_USER_ID.HasValue)
                        modelSubConDel.Nav_UserCheck.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == modelSubConDel.CHECK_USER_ID.Value).ToList();
                    if (modelSubConDel.Nav_ApproveCheckAudit != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails != null && modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails.Any())
                    {
                        foreach (var item in modelSubConDel.Nav_ApproveCheckAudit.Nav_ApproveDetails)
                        {
                            if (item.APPROVE_USER_ID.HasValue)
                                item.Nav_ApproveUser.Nav_UserSignFiles = listUsersSign.Where(e => e.USER_ID == item.APPROVE_USER_ID).ToList();
                        }
                    }
                }

                if (modelSubConDel.RISK_SUBMIT_CONTENT_ID.HasValue)
                {
                    var applyDetail = GetEntity<T_BS_RISK_SUBMIT_DELAY_APPLY_DETAIL>(e => e.RISK_SUBMIT_CONTENT_ID == modelSubConDel.RISK_SUBMIT_CONTENT_ID.Value);
                    modelSubConDel.Nav_ApplyDetail = applyDetail;
                }
                return modelSubConDel;
            });
        }
    }
}
