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/BSRiskSubmitContent")]
    public partial class BSRiskSubmitContentController : AuthorizeApiController<T_BS_RISK_SUBMIT_CONTENT>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IPFCodeRuleService CodeRuleService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        public BSRiskSubmitContentController(IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFCodeRuleService codeRuleService, IPFApproveCallBackService approveCallBackService)
        {
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            CodeRuleService = codeRuleService;
            ApproveCallBackService = approveCallBackService;
        }

        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetDevlp")]
        public JsonActionResult<T_BS_RISK_SUBMIT_CONTENT> GetDevlp([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var model = GetEntity<T_BS_RISK_SUBMIT_CONTENT>(null, filter, null);
                if (model.CREATER_ID.HasValue)
                {
                    var userUp = GetEntity<T_FM_USER>(model.CREATER_ID.Value);
                    model.CREATE_NAME = userUp.NAME;
                }
                return model;
            });
        }

        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedDevlp")]
        public PagedActionResult<T_BS_RISK_SUBMIT_CONTENT> OrderPagedDevlp([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_RISK_SUBMIT_CONTENT> result)
            {
                PagedActionResult<T_BS_RISK_SUBMIT_CONTENT> orderPageEntities = GetOrderPageEntities<T_BS_RISK_SUBMIT_CONTENT>(e => e.Nav_Submit.SUBMIT_TYPE != 10, pageFilter, null);
                if (orderPageEntities.TotalCount > 0)
                {
                    var listUserID = orderPageEntities.Data.Where(e => e.CREATER_ID.HasValue).Select(e => e.CREATER_ID.Value).Distinct();
                    var listUsers = GetEntities<T_FM_USER>(e => listUserID.Contains(e.ID), null, null);
                    foreach (var item in orderPageEntities.Data)
                    {
                        if (item.CREATER_ID.HasValue)
                            item.CREATE_NAME = listUsers.FirstOrDefault(e => e.ID == item.CREATER_ID.Value)?.NAME;
                        else
                            item.CREATE_NAME = "";
                    }
                }
                result.Data = orderPageEntities.Data;
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }
    }
}
