package com.engine.organization.util;

import com.engine.organization.entity.hrmresource.param.HrmStatusHistoryParam;
import com.engine.organization.entity.hrmresource.po.ResourceChartPO;
import weaver.conn.RecordSet;
import weaver.hrm.company.DepartmentComInfo;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liang.cheng
 * @Date 2024/5/29 2:50 PM
 * @Description: 人员工作记录
 * @Version 1.0
 */
public class HrmStatusHistoryUtil {

    /**
     * @param resourceChartPOList 调整前人员信息
     * @param departmentID 调整后部门
     */
    public static void personWorkRecord(List<ResourceChartPO> resourceChartPOList,Integer departmentID,String changeReason){

        RecordSet rs = new RecordSet();

        DepartmentComInfo comInfo = new DepartmentComInfo();
        String subcompanyid1 = comInfo.getSubcompanyid1(String.valueOf(departmentID));
        Integer newSub = "".equals(subcompanyid1) ? null : Integer.valueOf(subcompanyid1);

        List<HrmStatusHistoryParam> historyParamList = new ArrayList<>();
        resourceChartPOList.forEach(item -> historyParamList.add(HrmStatusHistoryParam.builder()
                .resourceId(item.getId())
                .changeDate(OrganizationDateUtil.getFormatLocalDate(LocalDate.now()))
                .changeReason(changeReason)
                .oldJobtitleId(item.getJobTitle())
                .newjobtitleId(item.getJobTitle())
                .typeN(4)
                .oldDepartmentId(item.getDepartmentId())
                .newDepartmentId(departmentID)
                .oldSubcompanyId(item.getSubcompanyid1())
                .newSubcompanyId(newSub)
        .build()));

        historyParamList.forEach(item -> {
            rs.executeUpdate("insert into hrmstatushistory(resourceid,changedate,changereason,oldjobtitleid,newjobtitleid,type_n,olddepartmentid,newdepartmentid,oldsubcompanyid,newsubcompanyid) " +
                    " values(?,?,?,?,?,?,?,?,?,?)",item.getResourceId(),item.getChangeDate(),item.getChangeReason(),item.getOldJobtitleId(),item.getNewjobtitleId(),item.getTypeN(),
                    item.getOldDepartmentId(),item.getNewDepartmentId(),item.getOldSubcompanyId(),item.getNewSubcompanyId());
        });
    }
}
