package nckd.jimin.jyyy.hr.task.util;

import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hrmp.hrpi.business.infrastructure.utils.QFilterUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static nckd.jimin.jyyy.hr.task.util.SyncOAHelper.PERFORMANCE_ENTITY;

public class SyncOAUtil {

    private static final Log log = LogFactory.getLog(SyncOAUtil.class);
    private static Map monthMap = new HashMap();
    private static Map seasonMap = new HashMap();
    static {
        monthMap.put("1月","-01-01");
        monthMap.put("2月","-02-01");
        monthMap.put("3月","-03-01");
        monthMap.put("4月","-04-01");
        monthMap.put("5月","-05-01");
        monthMap.put("6月","-06-01");
        monthMap.put("7月","-07-01");
        monthMap.put("8月","-08-01");
        monthMap.put("9月","-09-01");
        monthMap.put("10月","-10-01");
        monthMap.put("11月","-11-01");
        monthMap.put("12月","-12-01");
        seasonMap.put("第一季度","-03-01");
        seasonMap.put("第二季度","-06-01");
        seasonMap.put("第三季度","-09-01");
        seasonMap.put("第四季度","-12-01");
    }

    public static QFilter getPersonFilter(String personNumber) {
        QFilter filter = QFilterUtil.getCurrentQf();
        filter.and("number", QCP.equals, personNumber);
        return filter;
    }

    /**
     * 处理日期，OA返回参数：
     * 季度情况下：返回值为第一季度，第二季度，转换成所属季度的最后一个月的第一天
     * 月度情况下：返回值为1月，2月，转换成月份第一天
     * @param year
     * @param type
     * @param value
     * @return
     */
    public static Date handleDate(String year, String type, String value) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //月度
            if (type.equals("2")) {
                String dateStr = year + monthMap.get(value);
                return sdf.parse(dateStr);
            }
            //季度
            else if (type.equals("1")) {
                String dateStr = year + seasonMap.get(value);
                return sdf.parse(dateStr);
            }
        } catch (ParseException e) {
            log.info("转换日期出错-year : {}, type : {}, value : {}", year, type, value);
            throw new KDBizException("转换日期出错：" + e.getMessage());
        }
        return new Date();
    }

    public static boolean checkIsExist (String oaId) {
        HRBaseServiceHelper helper = HRBaseServiceHelper.create(PERFORMANCE_ENTITY);
        QFilter filter = new QFilter("nckd_oaid", QCP.equals, oaId);
        return helper.isExists(filter);
    }

}
