using APT.BaseData.Domain.Enums.PF;
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.SC.PR;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Collections.Generic;
using APT.BaseData.Domain.Entities.FM;
using System;
using APT.BaseData.Domain.Entities;

namespace APT.SC.WebApi.Controllers.Api.PRController
{
    /// <summary>
    ///  测量记录表
    /// </summary>
    [Route("api/PR/PRSurveyRecord")]
    public partial class SurveyRecordController : AuthorizeApiController<T_PR_SURVEY_RECORD>
    {
        IPFCodeRuleService CodeRuleService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        /// <summary>
        /// 测量记录表
        /// </summary>
        /// <param name="notificationTaskService"></param>
        public SurveyRecordController(IPFCodeRuleService codeRuleService, IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
        {
            CodeRuleService = codeRuleService;
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
        }
        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PR_SURVEY_RECORD entity)
        {
            return SafeExecute(() =>
            {
                var files = entity.Nav_Files;
                var users = entity.Nav_Users;
                var details = entity.Nav_Details;
                if (files != null && files.Any())
                {
                    files.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.SURVEY_RECORD_ID = entity.ID;
                    });
                }
                if (users != null && users.Any())
                {
                    users.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.SURVEY_RECORD_ID = entity.ID;
                        t.Nav_User = null;
                    });
                }
                if (details != null && details.Any())
                {
                    details.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.SURVEY_RECORD_ID = entity.ID;
                    });
                }
                entity.STATUS = PFStandardStatus.Draft;
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.TaskID != Guid.Empty)
                {
                    var currTask = GetEntity<T_FM_NOTIFICATION_TASK>(t => t.ID == entity.TaskID);
                    if (currTask != null)
                    {
                        loginUserId = currTask.USER_ID;
                    }
                }
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    entity.STATUS = PFStandardStatus.Approving;
                    entity.Nav_Files = null;
                    entity.Nav_Users = null;
                    entity.Nav_Details = null;
                    var user = this.GetEntity<T_FM_USER>(t => t.ID == loginUserId && t.ENABLE_STATUS == 0, "Nav_Department");
                    var param = Enum.GetName(typeof(FMDepartmentType), user?.Nav_Department.DEPARTMENT_TYPE);
                    //取审批流水码
                    var sysFilter = new SystemCodeFilter();
                    sysFilter.CodeType = (int)PFCodeRuleType.审批流编码;
                    sysFilter.Count = 1;
                    sysFilter.OrgId = entity.ORG_ID;
                    var codes = CodeRuleService.NewGenSerial(sysFilter);
                    var serialCode = codes.Split(new char[] { ',' });
                    MFlowPermitService.InsertApprove(serialCode[0], "PR015", param, entity.ID, "PR015_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        if (entity != null)
                            this.UpdateEntityNoCommit(entity);
                        if (files != null && files.Any())
                            this.BantchSaveEntityNoCommit(files);
                        if (users != null && users.Any())
                            this.BantchSaveEntityNoCommit(users);
                        if (details != null && details.Any())
                            this.BantchSaveEntityNoCommit(details);
                    }, null, null, null, null, null, "PR015_SHOWPRINT", null);
                    return true;
                }
                entity.Nav_Files = null;
                entity.Nav_Users = null;
                entity.Nav_Details = null;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                    if (users != null && users.Any())
                        this.BantchSaveEntityNoCommit(users);
                    if (details != null && details.Any())
                        this.BantchSaveEntityNoCommit(details);
                });
                return true;
            });
        }
        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("BackUpdate")]
        public JsonActionResult<bool> BackUpdate(string id)
        {
            return SafeExecute(() =>
            {
                return ApproveCallBackService.CallBack("PR/PRSurveyRecord/BackUpdate", id);
            });
        }
        /// <summary>
        /// 获取测量基础配置
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetRecordDetail")]
        public JsonActionResult<T_PR_SURVEY_RECORD> GetRecordDetail([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                T_PR_SURVEY_RECORD main = new T_PR_SURVEY_RECORD();
                List<T_PR_SURVEY_RECORD_DETAIL> detailList = new List<T_PR_SURVEY_RECORD_DETAIL>();
                var detailSets = GetEntities<T_PR_SURVEY_RECORD_SET>(t => true, new BaseFilter(filter.OrgId));
                if (detailSets != null && detailSets.Any())
                {
                    detailSets.ForEach(t =>
                    {
                        T_PR_SURVEY_RECORD_DETAIL detail = new T_PR_SURVEY_RECORD_DETAIL();
                        detail.ORG_ID = t.ORG_ID;
                        detail.TYPE = t.TYPE;
                        detail.PARAMETER = t.PARAMETER;
                        detail.VALUE = t.VALUE;
                        detailList.Add(detail);
                    });
                }
                main.Nav_Details = detailList;
                return main;
            });
        }
    }
}
