namespace APT.PF.WebApi.Controllers.Api
{
    using APT.Infrastructure.Core;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    using APT.Utility;
    using APT.Infrastructure.Api;
    using APT.BaseData.Domain.ApiModel.PF;
    using APT.BaseData.Domain.Entities;
    using System.Linq;
    using System;
    using APT.MS.Domain.Entities.PF;

    [Route("api/PF/PFApproveTemp")]
    public partial class ApproveTempController : AuthorizeApiController<T_PF_APPROVE_TEMP>
    {

        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PF_APPROVE_TEMP entity)
        {
            return SafeExecute<bool>(() =>
            {
                var detail = entity.Nav_ApproveTempDetails;
                entity.Nav_ApproveTempDetails = null;
                if (detail != null && detail.Any())
                { 
                    var first = detail.OrderByDescending(t=>t.NUM).FirstOrDefault();
                    if (first != null)
                    {
                        var roleName = this.GetEntity<T_PF_APPROVAL_ROLE>(t => t.ID == first.APPROVE_ROLE_ID);
                        entity.APPROVE_ROLE_NAME = roleName?.NAME;
                    }
                }
                UnifiedCommit(() =>
                {
                    this.UpdateEntityNoCommit(entity);
                    if (detail != null)
                        this.BantchSaveEntityNoCommit(detail);
                });
                return true;
            });
        }

        /// <summary>
        /// 审批模板下拉带出明细数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetDetail")]
        public JsonActionResult<T_PF_APPROVE> GetDetail([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                T_PF_APPROVE main = new T_PF_APPROVE();
                List<T_PF_APPROVE_DETAIL> detailList = new List<T_PF_APPROVE_DETAIL>();
                //传入的识别表CODE不为空
                if (!string.IsNullOrEmpty(filter.Keyword))
                {
                    var approveTemp = GetEntity<T_PF_APPROVE_TEMP>(t => t.ID.ToString() == filter.Keyword, false, "Nav_ApproveTempDetails", "Nav_ApproveTempDetails.Nav_DefaultApproveUser");
                    if (approveTemp != null && approveTemp.Nav_ApproveTempDetails != null && approveTemp.Nav_ApproveTempDetails.Any())
                    {
                        //一般任务
                        approveTemp.Nav_ApproveTempDetails.ForEach(t =>
                        {
                            T_PF_APPROVE_DETAIL detail = new T_PF_APPROVE_DETAIL();
                            detail.NUM = t.NUM;
                            detail.NAME = t.NAME;
                            detail.APPROVE_USER_ID = (Guid)t.DEFAULT_APPROVE_USER_ID;
                            detail.Nav_ApproveUser = t.Nav_DefaultApproveUser;
                            detail.IS_ALLOW_UPDATE = t.IS_ALLOW_UPDATE;
                            detailList.Add(detail);
                        });
                    }
                }
                main.Nav_ApproveDetails = detailList;
                return main;
            });
        }

    }
}
