namespace Tiobon.Core.Services;

/// <summary>
/// 培训费用_宝连通 (服务)
/// </summary>
public class Ghre_TrainFeeApplyOrder_BoltoneServices : BaseServices<Ghre_TrainFeeApplyOrder_Boltone, Ghre_TrainFeeApplyOrder_BoltoneDto, InsertGhre_TrainFeeApplyOrder_BoltoneInput, EditGhre_TrainFeeApplyOrder_BoltoneInput>, IGhre_TrainFeeApplyOrder_BoltoneServices
{
    private readonly IBaseRepository<Ghre_TrainFeeApplyOrder_Boltone> _dal;
    public Ghre_TrainFeeApplyOrder_BoltoneServices(ICaching caching, IBaseRepository<Ghre_TrainFeeApplyOrder_Boltone> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

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

        var result = await base.QueryFilterPage(filter, "WorkState=1", IsEnable);

        var DT_TableDataT1 = result.result.DT_TableDataT1;

        var applicantDeptIds = DT_TableDataT1.Select(x => x.ApplicantDeptId).Distinct().ToList();
        var deptIds = DT_TableDataT1.Select(x => x.DeptId).Distinct().ToList();
        var teacherIds = DT_TableDataT1.Select(x => x.TeacherId).Distinct().ToList();
        deptIds.AddRange(applicantDeptIds);
        deptIds = deptIds.Distinct().ToList();



        var staffIds = DT_TableDataT1.Select(x => x.ApplicantId).Distinct().ToList();
        staffIds.AddRange(teacherIds);
        staffIds = staffIds.Distinct().ToList();

        var staffs = await Db.Queryable<Ghra_Staff>().Where(x => staffIds.Contains(x.StaffID)).ToListAsync();
        var depts = await Db.Queryable<Ghro_Dept>().Where(x => deptIds.Contains(x.DeptID)).ToListAsync();


        for (int i = 0; i < DT_TableDataT1.Count; i++)
        {
            DT_TableDataT1[i].InOrOut = await GetParaLabel("CourseInOrOut", DT_TableDataT1[i].InOrOut);
            DT_TableDataT1[i].TrainFeeCourseType = await GetParaLabel("TrainFeeCourseType-Boltone", DT_TableDataT1[i].TrainFeeCourseType);
            DT_TableDataT1[i].TrainTimeType = await GetParaLabel("TrainTimeType-Boltone", DT_TableDataT1[i].TrainTimeType);
            if (DT_TableDataT1[i].ApplicantId.IsNotEmptyOrNull())
                DT_TableDataT1[i].ApplicantName = staffs.FirstOrDefault(x => x.StaffID == DT_TableDataT1[i].ApplicantId)?.StaffName;

            if (DT_TableDataT1[i].ApplicantDeptId.IsNotEmptyOrNull())
                DT_TableDataT1[i].ApplicantDeptName = depts.FirstOrDefault(x => x.DeptID == DT_TableDataT1[i].ApplicantDeptId)?.DeptName;

            if (DT_TableDataT1[i].DeptId.IsNotEmptyOrNull())
                DT_TableDataT1[i].DeptName = depts.FirstOrDefault(x => x.DeptID == DT_TableDataT1[i].DeptId)?.DeptName;
            if (DT_TableDataT1[i].TeacherId.IsNotEmptyOrNull())
            {
                DT_TableDataT1[i].StaffNo = staffs.FirstOrDefault(x => x.StaffID == DT_TableDataT1[i].TeacherId)?.StaffNo;
                DT_TableDataT1[i].TeacherName = staffs.FirstOrDefault(x => x.StaffID == DT_TableDataT1[i].TeacherId)?.StaffName;

            }

            DT_TableDataT1[i].IsApplyFee1 = DT_TableDataT1[i].IsApplyFee == 1 ? "是" : "否";

            if (DT_TableDataT1[i].PlanId.IsNotEmptyOrNull())
            {

                var planIds = JsonHelper.JsonToObj<List<long>>(DT_TableDataT1[i].PlanId);
                var commonSql = await Db.Queryable<Ghrs_ListCommonSql>().Where(o => o.ListCommonSqlNo == "TrainingPlan").FirstAsync();

                if (commonSql != null)
                {
                    for (int j = 0; j < planIds.Count; j++)
                    {
                        string sql = commonSql.SqlIDToName.Replace("{@KeyWords}", planIds[j].ObjToString());
                        if (j == 0)
                            DT_TableDataT1[i].PlanName = await Db.Ado.GetStringAsync(sql);
                        else
                            DT_TableDataT1[i].PlanName += "、" + await Db.Ado.GetStringAsync(sql);
                    }
                }
            }

        }

        return result;
    }

}