
using Tiobon.Core.IServices;
using Tiobon.Core.Model.Models;
using Tiobon.Core.Services.BASE;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.Common.Caches;
using NPOI.SS.Formula.Functions;
using NPOI.Util.Collections;

namespace Tiobon.Core.Services
{
    /// <summary>
    /// 考核指标 (服务)
    /// </summary>
    public class Ghrp_PerformanceItemServices : BaseServices<Ghrp_PerformanceItem, Ghrp_PerformanceItemDto, InsertGhrp_PerformanceItemInput, EditGhrp_PerformanceItemInput>, IGhrp_PerformanceItemServices
    {
        private readonly IBaseRepository<Ghrp_PerformanceItem> _dal;
        public Ghrp_PerformanceItemServices(ICaching caching, IBaseRepository<Ghrp_PerformanceItem> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
            base._caching = caching;
        }

        public override async Task<ServicePageResult<Ghrp_PerformanceItemDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
        {

            string? ItemStatus = null;
            if (filter.jsonParam != null)
                foreach (JProperty jProperty in filter.jsonParam.Properties())
                {
                    var name = jProperty.Name;
                    var value = jProperty.Value.ToString();
                    if (name == "ItemStatus")
                    { 
                        var jsonParam = JsonHelper.JsonToObj<JsonParam>(value);
                        ItemStatus = jsonParam.columnValue.ToString(); 
                    }
                }
            if (!string.IsNullOrEmpty(ItemStatus))
            {
                condition = $@" (case when BeginDate > convert(date,getdate()) then '2' 
                                         when convert(date,getdate()) between ISNULL(BeginDate,'1900-01-01') and ISNULL(EndDate,'2099-01-01') then '1' 
                                        else '0' end  ) = '{ItemStatus}' ";
            }
            var result = await base.QueryFilterPage(filter, condition, IsEnable);

            result.result.DT_TableDataT1.ForEach(async x =>
            {
                x.ItemType1 = await GetParaLabel("PerformanceItemType1", x.ItemType1);
                x.ItemType2 = await GetParaLabel("PerformanceItemType2", x.ItemType2);
                x.ItemType3 = await GetParaLabel("PerformanceItemType3", x.ItemType3);
                x.ItemKind = await GetParaLabel("PerformanceItemKind", x.ItemKind);
                x.DeptType = await GetParaLabel("PerformanceDeptType", x.DeptType);
                x.Tag1 = await GetParaLabel("PerformanceTag1", x.Tag1);
                x.Tag2 = await GetParaLabel("PerformanceTag2", x.Tag2);
                x.Tag3 = await GetParaLabel("PerformanceTag3", x.Tag3);
            });

            return result;
        }
    }
}