using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Domain.IServices;
using APT.MS.Domain.Entities.SC.PR;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums.PF;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Enums;
using System.Collections.Generic;
using System.Linq;
using System;
using APT.BaseData.Domain.Enums;

namespace APT.SC.WebApi.Controllers.Api.PRController
{
    /// <summary>
    ///  滑坡滚石监控处理记录表
    /// </summary>
    [Route("api/PR/PRLandslideRollstoneMonitor")]
    public partial class LandslideRollstoneMonitorController : AuthorizeApiController<T_PR_LANDSLIDE_ROLLSTONE_MONITOR>
    {
        IPFCodeRuleService CodeRuleService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        /// <summary>
        /// 滑坡滚石监控处理记录表
        /// </summary>
        /// <param name="notificationTaskService"></param>
        public LandslideRollstoneMonitorController(IPFCodeRuleService codeRuleService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
        {
            CodeRuleService = codeRuleService;
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
        }
        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PR_LANDSLIDE_ROLLSTONE_MONITOR entity)
        {
            return SafeExecute(() =>
            {
                if (entity.MONITOR_STATUS == 0)
                    throw new Exception("请选择监测情况");
                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;
                    }
                }
                entity.USER_ID = loginUserId;
                //附件
                var files = entity.Nav_Files;
                //附件保存
                if (files != null && files.Any())
                {
                    files.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.LANDSLIDE_ROLLSTONE_MONITOR_ID = entity.ID;
                    });
                }
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    entity.STATUS = PFStandardStatus.Approving;
                    //取审批流水码
                    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[] { ',' });
                    var param = entity.MONITOR_STATUS.GetDescription();
                    MFlowPermitService.InsertApprove(serialCode[0], "PR007", param, entity.ID, "PR007_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        if (entity != null)
                            this.UpdateEntityNoCommit(entity);
                        if (files != null && files.Any())
                            this.BantchSaveEntityNoCommit(files);
                    }, null, null, null, null, null, "PR007_SHOWPRINT", null);
                    return true;
                }
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                });
                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/PRLandslideRollstoneMonitor/BackUpdate", id);
            });
        }
    }
}
