package nckd.jxccl.swc.hpdi.business.coordination.api;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.hr.hbp.business.coordination.api.ext.CoordBizSaveHandler;
import kd.hr.hbp.business.coordination.model.CoordBizContext;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SwcCoordBizSaveHandler implements CoordBizSaveHandler {

    private static final Log log = LogFactory.getLog(SwcCoordBizSaveHandler.class);
    private static HRBaseServiceHelper SALARYGROUP_HELPER = new HRBaseServiceHelper("nckd_persalarygroup");
    private static String SALARYGROUP_ENTITY = "nckd_persalarygroup";
    @Override
    public Map<String, Object> handle(Map<String, DynamicObject> map, Map<String, Map<String, Object>> map1, DynamicObject dynamicObject, CoordBizContext coordBizContext) {
        log.info(" -------- begin SwcCoordBizSaveHandler handle --------");
        // 返回的Map
        Map<String, Object> returnMap = new HashMap<String, Object>();
        DynamicObject salaryFile = map.get("hsas_salaryfile");
        if(!ObjectUtils.isEmpty(salaryFile)) {
            Date startDate = salaryFile.getDate("nckd_groupstartdate");
            Date endDate = salaryFile.getDate("nckd_groupenddate");
            DynamicObject salaryGroup = salaryFile.getDynamicObject("nckd_salarygroup");
            DynamicObject employee = salaryFile.getDynamicObject("employee");

            DynamicObject dyn = SALARYGROUP_HELPER.generateEmptyDynamicObject();
            dyn.set("startdate", startDate);
            dyn.set("enddate", endDate);
            dyn.set("nckd_salarygroup", salaryGroup);
            dyn.set("nckd_employee", employee);
            dyn.set("nckd_salaryfile", salaryFile.getLong("boid"));

            OperationResult result = SaveServiceHelper.saveOperate(SALARYGROUP_ENTITY, new DynamicObject[]{dyn}, OperateOption.create());
            log.info("-------- 薪酬分类保存结果：" + result.isSuccess());
            if(result.isSuccess()) {
                returnMap.put("success",true);
            }
            else {
                returnMap.put("success",false);
                log.info("-------- 薪酬分类错误信息：" + result.getMessage());
                throw new KDBizException("-------- 薪酬分类保存失败：" + result.getMessage());
            }
        }
        return returnMap;
    }
}
