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.DomainServices
{
    /// <summary>
    /// 安全检查
    /// </summary>
    public class BSOperateLogService : CommonService, IBSOperateLogService
    {
        public BSOperateLogService(IRepository repository) : base(repository)
        {
        }

        /// <summary>  
        /// 直接添加多个操作
        /// 或者 一个操作 引起多个后续操作人
        /// </summary>
        /// <param name="OPERATEPOINT">操作的节点</param>
        /// <param name="DATA_ID">SafeCheckID RiskSubmitID</param>
        /// <param name="UserIDLogin">登入人ID</param>
        /// <param name="ListDATA_ID">可为null与 DATA_ID 只有一个</param>
        /// <param name="ListUserID">可为null</param>
        /// <param name="ListUserName">可为null</param>
        /// <param name="ORG_ID"></param>
        /// <param name="DATA_ID_SUB"></param>
        /// <param name="ListUserIDDone">默认操作通过的</param>
        /// <param name="isEnd"></param>
        /// <returns></returns>
        public List<T_BS_OPERATE_LOG> GetListOperateLog(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserIDLogin, List<Guid> ListDATA_ID, List<Guid> ListUserID, List<string> ListUserName, Guid? ORG_ID, Guid? DATA_ID_SUB = null, List<Guid> ListUserIDDone = null, bool isEnd = true)
        {
            List<T_BS_OPERATE_LOG> listResult = new List<T_BS_OPERATE_LOG>();
            T_BS_OPERATE_LOG modelLogUpdate = null;
            switch (OPERATEPOINT)
            {
                case OPERATEPOINT_Enums.GotCheck:
                    //系统触发 只是新增
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, ListDATA_ID[i], ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.CheckForm:
                    //BS032保存并发送 到审批流
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT_Enums.GotCheck, DATA_ID, UserIDLogin);
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.CheckAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin);//处理自己的待办日志
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++)//最后一个审批人 添加 通知
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckerCheck, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.CheckerCheck:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    break;
                case OPERATEPOINT_Enums.CheckRegister:
                    //检查登记
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++) //发起检查结果确认
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckAgree, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.CheckAgree:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++) //检查结果确认完 直接到审批
                            listResult.Add(AddModel(OPERATEPOINT_Enums.CheckResultAudit, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.CheckResultAudit:
                    modelLogUpdate = GetUpdateDeal(OPERATEPOINT, DATA_ID, UserIDLogin);//处理自己的
                    if (modelLogUpdate != null)
                        listResult.Add(modelLogUpdate);
                    if (isEnd) //最后一个审批就归档
                    {
                        listResult.Add(AddModel(OPERATEPOINT_Enums.CheckEnd, DATA_ID, UserIDLogin, APT.Infrastructure.Api.AppContext.CurrentSession.UserName, ORG_ID));
                    }
                    break;
                case OPERATEPOINT_Enums.CheckEnd:
                    //此处没操作
                    break;
                case OPERATEPOINT_Enums.RiskNoticeSD:
                    break;
                case OPERATEPOINT_Enums.RiskUpSD:
                    break;
                case OPERATEPOINT_Enums.RiskInfoCompleteSD:
                    break;
                case OPERATEPOINT_Enums.RiskGet:
                    //审批结束后 如果有隐患 
                    if (ListUserID != null && ListUserName != null)
                        for (int i = 0; i < ListUserID.Count; i++)
                            listResult.Add(AddModel(OPERATEPOINT, DATA_ID, ListUserID[i], ListUserName[i], ORG_ID, i));
                    break;
                case OPERATEPOINT_Enums.RiskSend:
                    break;
                default:
                    break;
            }
            if (ListUserIDDone != null && ListUserIDDone.Count > 0)
            {
                //除了登陆者 需要默认操作的
                listResult.ForEach(e =>
                {
                    if (ListUserIDDone.Contains(e.USER_ID))
                        e.OPERTETIME = DateTime.Now;
                });
            }
            return listResult;
        }



        /// <summary>
        /// 获取新记录
        /// </summary>
        /// <param name="OPERATEPOINT"></param>
        /// <param name="DATA_ID"></param>
        /// <param name="UserID"></param>
        /// <param name="UserName"></param>
        /// <param name="ORG_ID"></param>
        /// <returns></returns>
        private T_BS_OPERATE_LOG AddModel(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserID, string UserName, Guid? ORG_ID, int ORDERNO = 0)
        {
            T_BS_OPERATE_LOG ModelAdd = new T_BS_OPERATE_LOG()
            {
                ID = Guid.NewGuid(),
                ORG_ID = ORG_ID,
                LOGTYPE = ((int)OPERATEPOINT) > 80 ? LOGTYPE_Enums.RiskSubmit : LOGTYPE_Enums.SafeCheck,
                DATA_ID = DATA_ID,
                //DATA_ID_SUB = null,
                OPERATEPOINT = OPERATEPOINT,
                USER_ID = UserID,
                USRTNAME = UserName,
                ORDERNO = ORDERNO,
                //OPERTETIME = null,
                ISINTTIME = false,
                CREATE_TIME = DateTime.Now,
                CREATER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID
            };
            if (APT.Infrastructure.Api.AppContext.CurrentSession.UserID.HasValue && APT.Infrastructure.Api.AppContext.CurrentSession.UserID.Value == UserID)
            {
                ModelAdd.OPERTETIME = DateTime.Now;//默认通过
            }
            return ModelAdd;
        }

        /// <summary>
        /// 获取需要修改的Model
        /// </summary>
        /// <param name="OPERATEPOINT">操作节点</param>
        /// <param name="DATA_ID"></param>
        /// <param name="UserID"></param>
        /// <param name="DATA_ID_SUB"></param>
        /// <returns></returns>
        private T_BS_OPERATE_LOG GetUpdateDeal(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserID, Guid? DATA_ID_SUB = null)
        {
            Expression<Func<T_BS_OPERATE_LOG, bool>> expression = e => e.DATA_ID == DATA_ID && e.OPERATEPOINT == OPERATEPOINT && e.USER_ID == UserID;
            if (DATA_ID_SUB.HasValue)
                expression = expression.And(e => e.DATA_ID_SUB.HasValue && e.DATA_ID_SUB == DATA_ID_SUB);
            var model = GetEntity(expression);
            if (model != null)
                model.OPERTETIME = DateTime.Now;
            return model;
        }


        ///// <summary>
        ///// 关闭上一个操作，添加新的操作
        ///// </summary>
        ///// <param name="OPERATEPOINT">当前操作节点</param>
        ///// <param name="DATA_ID"></param>
        ///// <param name="UserID"></param>
        ///// <param name="UserName"></param>
        ///// <returns></returns>
        //public List<T_BS_OPERATE_LOG> GetOperateLog(OPERATEPOINT_Enums OPERATEPOINT, Guid DATA_ID, Guid UserID, string UserName, Guid? ORG_ID, Guid? DATA_ID_SUB, bool isNext = true)
        //{
        //    T_BS_OPERATE_LOG modelLogUpdate = null;
        //    T_BS_OPERATE_LOG modelLogAdd = null;
        //    switch (OPERATEPOINT)
        //    {
        //        case OPERATEPOINT_Enums.GotCheck:
        //            //系统触发 只是新增
        //            modelLogAdd = AddModel(OPERATEPOINT, DATA_ID, UserID, UserName, ORG_ID);
        //            break;
        //        case OPERATEPOINT_Enums.CheckForm:
        //            modelLogUpdate = GetUpdate(OPERATEPOINT_Enums.GotCheck, DATA_ID, UserID, DATA_ID_SUB);
        //            modelLogAdd = AddModel(OPERATEPOINT, DATA_ID, UserID, UserName, ORG_ID);

        //            break;
        //        case OPERATEPOINT_Enums.CheckAudit:
        //            break;
        //        case OPERATEPOINT_Enums.CheckerCheck:
        //            break;
        //        case OPERATEPOINT_Enums.CheckRegister:
        //            break;
        //        case OPERATEPOINT_Enums.CheckAgree:
        //            break;
        //        case OPERATEPOINT_Enums.CheckResultAudit:
        //            break;
        //        case OPERATEPOINT_Enums.CheckEnd:
        //            break;
        //        case OPERATEPOINT_Enums.RiskNotice:
        //            break;
        //        case OPERATEPOINT_Enums.RiskUp:
        //            break;
        //        case OPERATEPOINT_Enums.RiskInfoComplete:
        //            break;
        //        case OPERATEPOINT_Enums.RiskGet:
        //            break;
        //        case OPERATEPOINT_Enums.RiskSend:
        //            break;
        //        default:
        //            break;
        //    }

        //    return new List<T_BS_OPERATE_LOG>() { modelLogAdd ?? modelLogAdd, modelLogUpdate ?? modelLogUpdate };
        //}

    }
}