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

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSCheckContents")]
    public partial class BSCheckContentsController : AuthorizeApiController<T_BS_CHECK_CONTENTS>
    {

        IFMUserService UserService { get; set; }

        public BSCheckContentsController(IFMUserService userService)
        {
            UserService = userService;
        }
        /// <summary>
        /// 配合检查类型  检查层级
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedObject")]
        public PagedActionResult<T_BS_CHECK_CONTENTS> OrderPagedLevel([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_CHECK_CONTENTS> result)
            {
                PagedActionResult<T_BS_CHECK_CONTENTS> orderPageEntities = null;
                List<int> listMineType = UserService.GetMineType();

                if (listMineType == null || listMineType.Count < 1)
                {
                    orderPageEntities = GetOrderPageEntities<T_BS_CHECK_CONTENTS>(null, pageFilter, null);
                }
                else
                {
                    List<BSMineTypeEnum> listMineEnums = new List<BSMineTypeEnum>();
                    try
                    {
                        listMineEnums.Add(BSMineTypeEnum.All);
                        listMineType.ForEach(m => listMineEnums.Add((BSMineTypeEnum)m));
                    }
                    catch { }

                    orderPageEntities = GetOrderPageEntities<T_BS_CHECK_CONTENTS>(e => listMineEnums.Contains(e.CHECKOBJECT), pageFilter, null);
                }

                //BSMineTypeEnum? CHECKOBJECT = null;
                //if (APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID.HasValue)
                //{
                //    var dep = GetEntity<T_FM_DEPARTMENT>(e => e.ID == APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID);
                //    if (dep != null)
                //    {
                //        if (dep.DEPARTMENT_STATUS == 2)
                //        {
                //            CHECKOBJECT = BSMineTypeEnum.All; //安环部门 可以选择所有
                //        }
                //        else
                //        {
                //            var depObj = GetEntity<T_BS_DEPARTMENT_AREAOBJECT>(e => e.DEPARTMENTID == dep.ID);
                //            if (depObj != null)
                //            {
                //                //对应范围
                //                CHECKOBJECT = depObj.CHECKOBJECT;
                //            }
                //            else
                //            {
                //                //未设置 所有
                //                CHECKOBJECT = BSMineTypeEnum.All;
                //            }
                //        }
                //    }
                //}

                //if (CHECKOBJECT.Value != BSMineTypeEnum.All)
                //{
                //    orderPageEntities = GetOrderPageEntities<T_BS_CHECK_CONTENTS>(e => e.CHECKOBJECT == CHECKOBJECT.Value, pageFilter, null);
                //}
                //else
                //{
                //    orderPageEntities = GetOrderPageEntities<T_BS_CHECK_CONTENTS>(null, pageFilter, null);
                //}

                result.Data = orderPageEntities.Data;
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }


        /// <summary>
        /// 获取责任对象
        /// </summary>
        /// <param name="isClass"></param>
        /// <param name="strObj"></param>
        /// <returns></returns>
        public BSPLANCHECKOBJECTEnum GetBSPLANCHECKOBJECTEnum(string strObj, bool? isClass)
        {
            BSPLANCHECKOBJECTEnum result = BSPLANCHECKOBJECTEnum.Head;
            if (string.IsNullOrEmpty(strObj))
            {
                if (isClass.HasValue && isClass.Value)
                {
                    result = BSPLANCHECKOBJECTEnum.ClassMonitor;
                }
            }
            else
            {
                var enumsObjV = Enum.GetValues<BSPLANCHECKOBJECTEnum>();
                foreach (var item in enumsObjV)
                {
                    if (item.GetDescription() == strObj)
                    {
                        result = item;
                    }
                }
            }
            return result;
        }



        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_BS_CHECK_CONTENT entity)
        {
            return SafeExecute(() =>
            {
                var listQuestions = entity.Nav_ListContentQuestions;
                var listAreas = entity.Nav_ListContentAreas;
                var listCheckTypes = entity.Nav_ListContentCheckTypes;
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (listQuestions != null && listQuestions.Any())
                        BantchSaveEntityNoCommit(listQuestions);
                    if (listAreas != null && listAreas.Any())
                        BantchSaveEntityNoCommit(listAreas);
                    if (listCheckTypes != null && listCheckTypes.Any())
                        BantchSaveEntityNoCommit(listCheckTypes);
                });
                return true;

            });

        }



        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdateS")]
        public JsonActionResult<bool> FullUpdateS([FromBody] T_BS_CHECK_CONTENTS entity)
        {
            return SafeExecute(() =>
            {
                var listQuestions = entity.Nav_ListCheckQuestion;
                entity.Nav_ListCheckQuestion = null;
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (listQuestions != null && listQuestions.Any())
                        BantchSaveEntityNoCommit(listQuestions);
                });
                return true;
            });
        }
        /// <summary>
        /// 排序分页查询数据 BS032
        /// 如果选的是 检查区域：全公司  CHECKOBJECT 不过滤
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedSuitAll")]
        public PagedActionResult<T_BS_CHECK_CONTENTS> OrderPagedSuitAll([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_CHECK_CONTENTS> result)
            {
                if (pageFilter.FilterGroup != null && pageFilter.FilterGroup.Rules != null && pageFilter.FilterGroup.Rules.Any())
                {
                    foreach (var item in pageFilter.FilterGroup.Rules)
                    {
                        if (item.Field == "CHECKOBJECT" && item.Value != null && item.Value.ToString() == "0")
                        {
                            pageFilter.FilterGroup.Rules.Remove(item);
                            break;
                        }
                    }
                }
                PagedActionResult<T_BS_CHECK_CONTENTS> orderPageEntities = GetOrderPageEntities<T_BS_CHECK_CONTENTS>(null, pageFilter, null);
                result.Data = orderPageEntities.Data;
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }
    }
}