
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 Tiobon.Core.Common.DB.Dapper.Extensions;

namespace Tiobon.Core.Services
{
    /// <summary>
    /// 考核周期类别 (服务)
    /// </summary>
    public class Ghrp_PerformancePeriodTypeServices : BaseServices<Ghrp_PerformancePeriodType, Ghrp_PerformancePeriodTypeDto, InsertGhrp_PerformancePeriodTypeInput, EditGhrp_PerformancePeriodTypeInput>, IGhrp_PerformancePeriodTypeServices
    {
        private readonly IBaseRepository<Ghrp_PerformancePeriodType> _dal;
        public Ghrp_PerformancePeriodTypeServices(ICaching caching, IBaseRepository<Ghrp_PerformancePeriodType> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
            base._caching = caching;
        }
        public override async Task<ServicePageResult<Ghrp_PerformancePeriodTypeDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
        {

            var result = await base.QueryFilterPage(filter, condition, IsEnable);

            result.result.DT_TableDataT1.ForEach(async x =>
            {
                x.Stage = await GetParaLabel("PerformanceStage", x.Stage);
                x.StageItems = await GetParaLabelListString("Performance", "PerformanceStageItem", x.StageItems);
                x.DataBelongName =  Db.Ado.GetString("select DataBelongName from Ghrs_DataBelong where DataBelongID=@DataBelongID"
                    , new { DataBelongID=x.DataBelongID });
            }); 
            return result;
        }

        public override async Task<long> Add(InsertGhrp_PerformancePeriodTypeInput entity)
        {
            if (entity.StageItems_List != null)
                entity.StageItems = JsonHelper.ObjToJson(entity.StageItems_List);
            var result = await base.Add(entity);

            return result;
        }

        public override async Task<bool> Update(long Id, EditGhrp_PerformancePeriodTypeInput editModel)
        {
            if (editModel.StageItems_List != null)
                editModel.StageItems = JsonHelper.ObjToJson(editModel.StageItems_List);
            var result = await base.Update(Id, editModel, null, ["Status"]);
            return result;
        }

        public override async Task<ServiceFormResult<Ghrp_PerformancePeriodTypeDto>> QueryForm(QueryForm body)
        {
            var result = await base.QueryForm(body);

            var StageItems = result.result.DT_TableDataT1[0].StageItems;
            if (!string.IsNullOrWhiteSpace(StageItems))
                result.result.DT_TableDataT1[0].StageItems_List = JsonConvert.DeserializeObject<List<string>>(StageItems);

            return result;
        }

        public async Task<ServiceResult<List<string>>> GetStageItems(string Stage)
        {
            List<string> StageItmes = [];
            if (Stage == "1Q1C") StageItmes = new List<string> { "C" };
            if (Stage == "1Q2C") StageItmes = new List<string> { "A", "C" };
            if (Stage == "1Q3C") StageItmes = new List<string> { "A", "B", "C" };

            return ServiceResult<List<string>>.OprateSuccess("查询成功！", StageItmes);
        }
    }
}