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.Enums;
using APT.Utility;
using APT.WebApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSCheckProject")]
    public partial class BSCheckProjectController : AuthorizeApiController<T_BS_CHECK_PROJECT>
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_BS_CHECK_PROJECT entity)//T_HM_EVALUATION_PLAN
        {
            return SafeExecute<bool>(() =>
            {
                List<T_BS_CHECK_PROJECT_PROJECT_CATEGORY> listCheckProjectProjectCategoryAll = entity.Nav_ListCheckProjectProjectCategory.ToList();
                List<T_BS_CHECK_PROJECT_PROJECT_CATEGORY> listCheckProjectProjectCategory = listCheckProjectProjectCategoryAll.FindAll(e => !e.IS_DELETED);
                if (listCheckProjectProjectCategory == null || listCheckProjectProjectCategory.Count < 1)
                {
                    throw new Exception("请新增检查项目分类!");
                }
                if (listCheckProjectProjectCategory.Count > 1)
                {
                    for (int i = 0; i < listCheckProjectProjectCategory.Count; i++)
                    {
                        for (int j = i + 1; j < listCheckProjectProjectCategory.Count; j++)
                        {
                            if (listCheckProjectProjectCategory[i].CHECK_PROJECT_CATEGORY_ID == listCheckProjectProjectCategory[j].CHECK_PROJECT_CATEGORY_ID)
                            {
                                throw new Exception("检查项目分类第【" + (i + 1) + "】项与第【" + (j + 1) + "】项不能重复!");
                            }
                        }
                    }
                }

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


        /// <summary>
        /// BS032 制定检查表时 字表选择 范围限定在属性值以内
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedSub")]
        public PagedActionResult<T_BS_CHECK_PROJECT> OrderPagedSub([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_CHECK_PROJECT> result)
            {
                List<Guid> listProjectID = new List<Guid>();
                if (pageFilter.FilterGroup.Rules != null && pageFilter.FilterGroup.Rules.Count > 0)
                {
                    //BS032 列表
                    List<FilterRule> listRules = pageFilter.FilterGroup.Rules.ToList();
                    for (int i = listRules.Count - 1; i > -1; i--)
                    {
                        if (listRules[i].Field == "Nav_ListCheckProject")
                        {
                            List<BS032SubSelectProject> listAreaSearch = JsonHelper.FromJson<List<BS032SubSelectProject>>(listRules[i].Value.ToString());
                            try
                            {
                                listProjectID = listAreaSearch.FindAll(e => !e.IS_DELETED.HasValue || !e.IS_DELETED.Value).Select(e => e.CHECK_PROJECT_ID).Distinct().ToList();
                                listRules.Remove(listRules[i]);
                            }
                            catch
                            {

                            }
                        }
                    }
                    pageFilter.FilterGroup.Rules = listRules;
                }
                if (listProjectID.Count < 1)
                {
                    result.Data = new List<T_BS_CHECK_PROJECT>();
                    result.TotalCount = 0;
                }
                else
                {
                    Expression<Func<T_BS_CHECK_PROJECT, bool>> expression = e => !e.IS_DELETED && listProjectID.Contains(e.ID);
                    PagedActionResult<T_BS_CHECK_PROJECT> orderPageEntities = GetOrderPageEntities<T_BS_CHECK_PROJECT>(expression, pageFilter, null);
                    result.Data = orderPageEntities.Data;
                    result.TotalCount = orderPageEntities.TotalCount;
                }
            });
        }



        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedSTATUS")]
        public PagedActionResult<T_BS_CHECK_PROJECT> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_CHECK_PROJECT> result)
            {
                if (pageFilter.FilterGroup.Rules.Count() == 0 && pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any() && pageFilter.FilterGroup.Groups.Count() == 1)
                {
                    pageFilter.FilterGroup.Rules = pageFilter.FilterGroup.Groups.ToList()[0].Rules;
                }

                PagedActionResult<T_BS_CHECK_PROJECT> orderPageEntities = GetOrderPageEntities<T_BS_CHECK_PROJECT>(null, pageFilter, null);
                result.Data = orderPageEntities.Data;
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }

    }

    public class BS032SubSelectProject
    {
        public Guid CHECK_PROJECT_ID { get; set; }
        public Guid ID { get; set; }
        public Guid SAFE_CHECK_ID { get; set; }
        public Guid ORG_ID { get; set; }
        public bool? IS_DELETED { get; set; }

    }
}
