package nckd.jimin.jyyy.hr.hspm.business.repository.ext.service;

import kd.bos.algo.DataSet;
import kd.bos.db.DB;
import kd.bos.db.DBRoute;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.FilterItemInfo;
import kd.bos.exception.ErrorCode;
import kd.bos.exception.KDBizException;
import kd.bos.mservice.extreport.dataset.datasource.generic.runtimemodel.JoinType;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.common.util.HRDateTimeUtils;
import kd.sdk.hr.hspm.business.repository.ext.service.IEmpReportExtService;
import kd.sdk.hr.hspm.common.ext.file.EmpReportExtCalculateDTO;
import kd.sdk.hr.hspm.common.ext.file.EmpReportExtQueryFieldsDTO;
import kd.sdk.hr.hspm.common.ext.file.EmpReportExtReletionFilterDTO;
import kd.wtc.wtbs.common.util.third.collect.Lists;

import java.text.ParseException;
import java.util.Date;
import java.util.Set;

public class EmpReportPluginEx implements IEmpReportExtService {

    @Override
    public void addExtReletionFilter(EmpReportExtReletionFilterDTO empReportExtReletionFilterDTO) {
        // 查询日期过滤
        Date queryDate = getQueryDate(empReportExtReletionFilterDTO.getFilterInfo());
        // 开始日期 <= 查询日期 && 结束日期 >= 查询日期
    }

    /**
     * 获取查询日期
     *
     * @param filterInfo filterInfo
     * @return 查询日期
     */
    public static Date getQueryDate(FilterInfo filterInfo) {
        // 查询日期
        FilterItemInfo itemInfo = filterInfo.getFilterItem("date");
        Date queryDate = (Date) itemInfo.getValue();
        if(queryDate == null) {
            // 默认查询当前日期
            queryDate = new Date();
        }
        // 日期去除时分秒
        try {
            queryDate = HRDateTimeUtils.parseDate(HRDateTimeUtils.format(queryDate), "yyyy-MM-dd");
        } catch (ParseException e) {
            throw new KDBizException(e, new ErrorCode("", "BaselocationExtService queryDate format error."));
        }
        return queryDate;
    }

    @Override
    public void addExtQueryFields(EmpReportExtQueryFieldsDTO empReportExtQueryFieldsDTO) {
        // 添加用于计算时存在花名册查询配置实体上的查询字段
        Set<String> queryFieldSet = empReportExtQueryFieldsDTO.getQueryFieldSet();
        //queryFieldSet.add("TOTO_COLUMN");
    }

    @Override
    public DataSet addExtCalculate(EmpReportExtCalculateDTO empReportExtCalculateDTO) {
        // 获取dataset
        DataSet dataSet = empReportExtCalculateDTO.getDataSet();
        // 获取查询字段
        Set<String> queryFieldSet = empReportExtCalculateDTO.getQueryFieldSet();
        queryFieldSet.remove("hrpi_pernontsprop.description");
        String[] array = queryFieldSet.toArray(new String[queryFieldSet.size()]);
        // 对dataset结果集添加计算函数

        String sql = "select forgid,substring(ffullname,4,length(ffullname)) as \"hrpi_pernontsprop.description\" \n" +
                "from t_org_structure where fviewid = '1'";
        DataSet longNamedataSet = DB.queryDataSet(this.getClass().getName(), DBRoute.of("sys"), sql);

        DataSet ResultDataSet = dataSet.leftJoin(longNamedataSet).on("hrpi_empposorgrel.adminorg.boid","forgid")
                .select(array, new String[]{"hrpi_pernontsprop.description"}).finish();

        // 返回dataset,并将计算后的dataset设置到参数中，方便后续的二开插件使用
        empReportExtCalculateDTO.setDataSet(ResultDataSet);
        return ResultDataSet;
    }

}
