using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using System;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System.Linq;
using APT.MS.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using System.Linq.Expressions;
using Newtonsoft.Json;
using APT.BaseData.Domain.IServices.BS;
using APT.MS.Domain.Entities.BS;


namespace APT.BaseData.Services.Services.BS
{
    /// <summary>
    /// 安全检查
    /// </summary>
    public class BSSafeCheckService : CommonService, IBSSafeCheckService
    {
        public BSSafeCheckService(IRepository repository)
            : base(repository)
        {
        }

        /// <summary>
        /// 危险源辨识 添加安全检查
        /// </summary>
        /// <param name="RISK_AREA_ID">区域ID</param>
        /// <param name="dicProjctContents">辨识对象 list<管控措施></param>
        /// <param name="DateID">用户ID</param>
        /// <param name="DEPARTMENTID">部门ID</param>
        /// <param name="OrgId"></param>
        /// <param name="DateID"></param>
        public void HazardsIni(Guid RISK_AREA_ID, Dictionary<string, List<string>> dicProjctContents, Guid UserID, Guid DEPARTMENTID, Guid OrgId, Guid DateID, ref T_BS_SAFE_CHECK modelCheck, ref List<T_BS_CHECK_PROJECT> listProject, ref List<T_BS_SAFE_CHECK_DETAIL> listDetail)
        {
            modelCheck = new T_BS_SAFE_CHECK();
            modelCheck.ID = DateID;
            modelCheck.ORG_ID = OrgId;
            DateTime dtNow = DateTime.Now;

            modelCheck.NAME = "危险源辨识触发安全检查";
            modelCheck.CODE = dtNow.ToString("yyyyMMddHHmmss");
            //变成可空
            //modelCheck.CHECK_TYPE_ID =           ;
            //modelCheck.CHECKOBJECT =           ;
            //modelCheck.CHECK_TYPE_LEVEL_ID =           ;

            modelCheck.ENABLE_STATUS = 0;// FMEnableStatusEnum.启用;
            modelCheck.CREACTTYPE = CREACTTYPEEnum.HazardsAdd;
            modelCheck.IS_DELETED = false;
            modelCheck.CREATE_TIME = dtNow;
            modelCheck.CREATER_ID = UserID;
            modelCheck.STATUCHECK = 0;// HMAuditStatusEnmu.草稿;
            modelCheck.STATUSPLAN = 0;// HMAuditStatusEnmu.草稿;
            modelCheck.DEPARTMENTID = DEPARTMENTID;
            modelCheck.USERID_DOPLAN = UserID;
            modelCheck.RISKSTATE = 0;

            //modelCheck.PLAN_SET_ID =           ;
            //modelCheck.CHECKTIME =           ;
            //modelCheck.ENTITY_ORG_TPYE =           ;
            //modelCheck.FORM_ID =           ;
            //modelCheck.FLOW_STATUS =           ;
            //modelCheck.FLOW_SEND_STATUS =           ;
            //modelCheck.FLOW_ID =           ;
            //modelCheck.MODIFY_TIME =           ;
            //modelCheck.MODIFIER_ID =           ;
            //modelCheck.APPROVE_ID =           ;
            //modelCheck.APPROVE_CHECKAUDIT_ID =           ;
            //modelCheck.APPROVE_TEMP_ID =           ;
            //modelCheck.CheckUsers =           ;


            listDetail = new List<T_BS_SAFE_CHECK_DETAIL>();
            listProject = new List<T_BS_CHECK_PROJECT>();
            int rowNO = 0;
            T_BS_CHECK_PROJECT operateProject = null;
            foreach (var item in dicProjctContents)
            {
                operateProject = GetEntity<T_BS_CHECK_PROJECT>(e => e.NAME == item.Key);
                if (operateProject == null)
                {
                    operateProject = new T_BS_CHECK_PROJECT();
                    operateProject.NAME = item.Key;
                    operateProject.ID = Guid.NewGuid();
                    operateProject.CREATER_ID = UserID;
                    operateProject.CREATE_TIME = dtNow;
                    operateProject.ENABLE_STATUS = 0;
                    operateProject.IS_DELETED = false;
                    operateProject.ORG_ID = OrgId;
                    listProject.Add(operateProject);
                }
                for (int i = 0; i < item.Value.Count; i++)
                {
                    rowNO++;
                    T_BS_SAFE_CHECK_DETAIL modelDetail = new T_BS_SAFE_CHECK_DETAIL();
                    modelDetail.ID = Guid.NewGuid();
                    modelDetail.SAFE_CHECK_ID = DateID;
                    modelDetail.CHECKTYPE = 0;
                    modelDetail.IS_DELETED = false;
                    modelDetail.ORG_ID = OrgId;
                    modelDetail.CREATE_TIME = dtNow;
                    modelDetail.CREATER_ID = UserID;
                    modelDetail.ROW_NO = rowNO;
                    modelDetail.CHECKCONTENT = item.Value[i];
                    modelDetail.CHECK_PROJECT_ID = operateProject.ID;
                    modelDetail.RISK_AREA_ID = RISK_AREA_ID;

                    //modelDetail.CHECK_MAIN_ID =         ;
                    //modelDetail.REMARK_RESULT =         ;
                    //modelDetail.QUESTION_LEVEL =         ;
                    //modelDetail.ENTITY_ORG_TPYE =         ;
                    //modelDetail.FORM_ID =         ;
                    //modelDetail.FLOW_STATUS =         ;
                    //modelDetail.FLOW_SEND_STATUS =         ;
                    //modelDetail.FLOW_ID =         ;
                    //modelDetail.MODIFY_TIME =         ;
                    //modelDetail.MODIFIER_ID =         ;
                    //modelDetail.CHECKRESULT =         ;
                    //modelDetail.CHECKPROOF =         ;
                    //modelDetail.CHECK_PROJECT_PROJECT_CATEGORY_ID =         ;
                    //modelDetail.CHECK_QUESTION_ID =         ;
                    //modelDetail.SERIOUS_RISK =         ;

                    listDetail.Add(modelDetail);
                }
            }

            //UnifiedCommit(() =>
            //{
            //    if (modelCheck != null)//添加安全检查
            //        AddEntity(modelCheck);
            //    if (listProject.Any())//添加检查项目
            //        BantchSaveEntityNoCommit(listProject);
            //    if (listDetail.Any())//添加审批详情
            //        BantchSaveEntityNoCommit(listDetail);
            //});
        }


        /// <summary>
        /// 获取对应截止时间
        /// </summary>
        /// <param name="Quency"></param>
        /// <param name="dtNow"></param>
        /// <returns></returns>
        public DateTime GetEndTime(BSPLANCHECKFREQUENCYEnum Quency, DateTime? dtNow = null)
        {
            DateTime dtResult = DateTime.Now;
            if (!dtNow.HasValue)
                dtNow = DateTime.Now;
            switch (Quency)
            {
                case BSPLANCHECKFREQUENCYEnum.None://每年
                //case BSPLANCHECKFREQUENCYEnum.OneTime://
                case BSPLANCHECKFREQUENCYEnum.Year://每年
                    dtResult = Convert.ToDateTime(dtNow.Value.ToString("yyyy-12-31 23:59:59"));
                    break;
                case BSPLANCHECKFREQUENCYEnum.HalfYear:
                    if (dtNow.Value.Month <= 6)
                    {
                        dtResult = Convert.ToDateTime(dtNow.Value.ToString("yyyy-06-30 23:59:59"));
                    }
                    else
                    {
                        dtResult = Convert.ToDateTime(dtNow.Value.ToString("yyyy-12-31 23:59:59"));
                    }
                    break;
                case BSPLANCHECKFREQUENCYEnum.Date://每天
                    dtResult = dtNow.Value.Date.AddDays(1).AddSeconds(-1);
                    break;
                case BSPLANCHECKFREQUENCYEnum.Week://每周
                    int iweek = (int)dtNow.Value.DayOfWeek;
                    dtResult = dtNow.Value.Date.AddDays(-1 * iweek).AddDays(8).AddSeconds(-1);
                    break;
                case BSPLANCHECKFREQUENCYEnum.Month://每月
                    dtResult = Convert.ToDateTime(dtNow.Value.ToString("yyyy-MM-01 00:00:00")).AddMonths(1).AddSeconds(-1);
                    break;
                case BSPLANCHECKFREQUENCYEnum.Quarter://每季度
                    switch (dtNow.Value.Month)
                    {
                        case 1:
                        case 2:
                        case 3:
                            dtResult = Convert.ToDateTime(dtNow.Value.Year + "-03-31 23:59:59");
                            break;
                        case 4:
                        case 5:
                        case 6:
                            dtResult = Convert.ToDateTime(dtNow.Value.Year + "-06-30 23:59:59");
                            break;
                        case 7:
                        case 8:
                        case 9:
                            dtResult = Convert.ToDateTime(dtNow.Value.Year + "-09-30 23:59:59");
                            break;
                        case 10:
                        case 11:
                        case 12:
                        default:
                            dtResult = Convert.ToDateTime(dtNow.Value.Year + "-12-31 23:59:59");
                            break;
                    }
                    break;
                default:
                    break;
            }
            return dtResult;
        }
    }
}
