using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.FO;
using APT.MS.Domain.Entities.SC;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace APT.SC.WebApi.Controllers.Api.SC
{
    [Route("api/SC/SCMTPalnSet")]
    public class SCMTPalnSetController : AuthorizeApiController<T_SC_MT_PLAN_SET>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_SC_MT_PLAN_SET entity)
        {
            return SafeExecute<bool>(() =>
            {
                var listDep = entity.Nav_ListSetDepartment;
                entity.Nav_ListSetDepartment = null;
                if (listDep != null && listDep.Any())
                {
                    listDep.ForEach((i) => { i.Nav_Department = null; });
                }
                string Msg = string.Empty;
                switch (entity.PLANCHECKFREQUENCY)
                {
                    case BSPLANCHECKFREQUENCYEnum.None:
                    //case BSPLANCHECKFREQUENCYEnum.OneTime:
                    case BSPLANCHECKFREQUENCYEnum.Year:
                    case BSPLANCHECKFREQUENCYEnum.HalfYear:
                    case BSPLANCHECKFREQUENCYEnum.Date:
                        break;
                    case BSPLANCHECKFREQUENCYEnum.Week:
                        if (!entity.WEEKDATA.HasValue)
                            Msg = "请选择触发星期";
                        break;
                    case BSPLANCHECKFREQUENCYEnum.Month:
                    case BSPLANCHECKFREQUENCYEnum.Quarter:
                        if (!entity.DATA.HasValue)
                            Msg = "请选择日";
                        break;
                    default:
                        break;
                }
                if (!string.IsNullOrEmpty(Msg))
                    throw new Exception(Msg);

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (listDep != null && listDep.Any())
                        this.BantchSaveEntityNoCommit(listDep);
                });
                return true;
            });
        }
    }
}