using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Linq;
using APT.BaseData.Domain.ApiModel;
using Ubiety.Dns.Core;
using System.Security.AccessControl;
using APT.MS.Domain.Entities.FO;
using APT.BaseData.Domain.Entities;
using APT.MS.Domain.Enums;
using APT.MS.Domain.Entities.BI;
using static APT.MS.Domain.Enums.BI.BIEnums;
using APT.Utility;

namespace APT.SC.WebApi.Controllers.Api.BI
{
    /// <summary>
    ///  年度监测汇总
    /// </summary>
    [Route("api/BI/BISafe")]
    public class BISafeController : AuthorizeApiController<T_BI_BSSAFECHECK>
    {
        //IBIBSSafeCheckService BIBSSafeCheckService { get; set; }
        //public BISafeController(IBIBSSafeCheckService bIBSSafeCheckService)
        //{
        //    BIBSSafeCheckService = bIBSSafeCheckService;
        //}

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetShow")]
        public JsonActionResult<BISafe> GetShow([FromBody] KeywordFilter filter)
        {
            return SafeExecute<BISafe>(() =>
            {
                BISafe result = new BISafe();
                string Parameter1 = string.Empty;
                string Parameter2 = string.Empty;
                if (filter.FilterGroup.Rules.Count == 2)
                {
                    foreach (var item in filter.FilterGroup.Rules)
                    {
                        if (item.Field == "Parameter1")
                        {
                            Parameter1 = item.Value.ToString();
                        }
                        else if (item.Field == "Parameter2")
                        {
                            if (item.Value == null)
                            {
                                throw new Exception("参数2为空！");
                            }
                            else
                            {
                                Parameter2 = item.Value.ToString();
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(Parameter1) || !string.IsNullOrEmpty(Parameter2))
                {
                    string Code = "BI002_BSSAFECHECK"; //编码
                    FilterTimeType? timeType = null;
                    BSMineTypeEditEnum? mineType = null;
                    try
                    {
                        timeType = (FilterTimeType)(int.Parse(Parameter1));
                    }
                    catch { }
                    try
                    {
                        mineType = (BSMineTypeEditEnum)(int.Parse(Parameter2));
                    }
                    catch { }

                    if (timeType != null && mineType != null)
                    {
                        var listSafeCheck = GetEntities<T_BI_BSSAFECHECK>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value, null).OrderBy(e => e.TEAMNAME).ToList();
                        var listCheckType = GetEntities<T_BI_BSCHECKTYPE>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value, null).OrderBy(e => e.name).ToList();
                        var summary = GetEntity<T_BI_SUMMARY>(e => e.ORG_ID == filter.OrgId.Value && e.TimeType == timeType.Value && e.MineType == mineType.Value && e.CODE == Code);
                        result.listSafeCheck = listSafeCheck;
                        result.listCheckType = listCheckType;
                        result.summary = summary;

                    }
                }
                return result;
            });
        }
    }


    public class BISafe
    {
        public List<T_BI_BSSAFECHECK> listSafeCheck { get; set; }
        public List<T_BI_BSCHECKTYPE> listCheckType { get; set; }
        public T_BI_SUMMARY summary { get; set; }

    }
}