package nckd.jimin.jyyy.hr.wtc.wtabm.opplugin.web.vaapply;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hr.hbp.business.application.impl.newhismodel.HisModelController;
import kd.hr.hbp.business.domain.model.newhismodel.HisResponse;
import kd.hr.hbp.business.domain.model.newhismodel.HisVersionParamBo;
import kd.hr.hbp.business.domain.model.newhismodel.VersionChangeRespData;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.constants.newhismodel.EnumHisOperateType;
import kd.hr.hbp.common.util.HRDynamicObjectUtils;
import kd.hrmp.hrpi.business.infrastructure.utils.QFilterUtil;
import kd.imc.rim.common.utils.StringUtils;

import java.util.List;

public class VaApplyBillUtil {

    private static String laborStatus_entity = "hbss_laborrelstatus";
    private static String perNonTsProp_entity = "hrpi_pernontsprop";
    private static String empEntRel_entity = "hdm_empentrel";
    private static String depEmp_entity = "hrpi_depemp";
    private static String ermanFile_entity = "hspm_ermanfile";
    private static final Log log = LogFactory.getLog(VaApplyBillUtil.class);
    /**
     * 更新员工基本信息上员工状态字段，保存历史版本
     * @param typeNumber 用工关系状态编码
     * @param personId  人员
     */
    public static void updatePersonType (String typeNumber, Long personId) {
        HRBaseServiceHelper helper = HRBaseServiceHelper.create(perNonTsProp_entity);
        QFilter filter = new QFilter("person", QCP.equals, personId);
        filter.and("datastatus", QCP.equals, "1");
        filter.and("iscurrentversion",QCP.equals, "1");
        String selectFields = "iscurrentversion,person,datastatus,nckd_isgbqs,boid";
        DynamicObject curVersionDyn = helper.queryOne(selectFields, new QFilter[]{filter});
        curVersionDyn = helper.loadSingle(curVersionDyn.getPkValue());
        DynamicObject newVersionDyn = helper.generateEmptyDynamicObject();
        HRDynamicObjectUtils.copy(curVersionDyn, newVersionDyn,true);
        DynamicObject ygzt = getLaborRelStatusDyn(typeNumber);
        newVersionDyn.set("nckd_ygzt", ygzt);
        HisVersionParamBo hisVersionParamBo = new HisVersionParamBo();
        hisVersionParamBo.setOperateType(EnumHisOperateType.NO_TIME_SAVE_VERSION.getType());
        hisVersionParamBo.setEntityNumber(perNonTsProp_entity);
        hisVersionParamBo.setHisDyns(new DynamicObject[]{newVersionDyn});
        HisModelController hisModelController = HisModelController.getInstance();
        HisResponse<VersionChangeRespData> hisResponse = hisModelController.noLineTimeHisVersionChange(hisVersionParamBo);
        if(StringUtils.isNoneBlank(hisResponse.getErrorMessage())) {
            log.error("保存历史模型失败：" + hisResponse.getErrorMessage());
        }
    }

    /**
     * 获取用工关系状态Dyn
     * @param typeNumber 编码
     * @return
     */
    public static DynamicObject getLaborRelStatusDyn (String typeNumber) {
        QFilter filter = new QFilter("number", QCP.equals, typeNumber);
        return BusinessDataServiceHelper.loadSingle(laborStatus_entity, new QFilter[]{filter});
    }

    /**
     * 获取员工当前用工关系状态编码
     * @param personId
     * @return
     */
    public static String getLaborStatusNumber (Long personId) {
        QFilter filter = QFilterUtil.getCurrentQf();
        filter.and("businessstatus", QCP.equals, "1");
        filter.and("person.id", QCP.equals, personId);
        DynamicObjectCollection cols = QueryServiceHelper.query(empEntRel_entity, "laborrelstatus.number", new QFilter[]{filter});
        if(cols.size() > 0) {
            return cols.get(0).getString("laborrelstatus.number");
        }
        else {
            return null;
        }
    }

    /**
     * 获取人员主职职位Dyn
     * @param personId
     */
    public static DynamicObject getPersonPrimaryPosition (Long personId) {
        QFilter filter = QFilterUtil.getCurrentQf();
        filter.and("businessstatus", QCP.equals, "1");
        filter.and("person.id", QCP.equals, personId);
        filter.and("isprimary", QCP.equals, "1");
        HRBaseServiceHelper helper = HRBaseServiceHelper.create(depEmp_entity);
        DynamicObject position = helper.queryOne("position.id", new QFilter[]{filter});
        return position;
    }

    /**
     * 获取职位上的兼职档案
     * @param positionIds
     * @return
     */
    public static DynamicObject[] getErmanFilesByPosition (List<Long> positionIds) {
        QFilter filter = new QFilter("depemp.iscurrentversion", QCP.equals, "1");
        filter.and("depemp.businessstatus", QCP.equals, "1");
        filter.and("depemp.postype.number",QCP.equals, "1020_S");
        filter.and("depemp.position", QCP.in, positionIds);
        filter.and("iscurrentversion", QCP.equals, "1");
        filter.and("businessstatus", QCP.equals, "1");
        HRBaseServiceHelper helper = HRBaseServiceHelper.create(ermanFile_entity);
        DynamicObject[] ermanFiles = helper.query(new QFilter[]{filter});
        return ermanFiles;
    }

    /**
     * 根据HR人员工号获取平台人员Dyn
     * @param personNo
     * @return
     */
    public static DynamicObject getUserByPersonNo (String personNo) {
        QFilter filter = new QFilter("number", QCP.equals, personNo);
        return QueryServiceHelper.queryOne("bos_user", "id", new QFilter[]{filter});
    }


}
