package weaver.interfaces.organization.cronjob;

import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.mapper.job.JobMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liang.cheng
 * @Date 2024/10/18 11:25 AM
 * @Description: 自动清除已失效的岗位 （执行前请注意编制的引用，会出现名称为空）
 * @Version 1.0
 */
public class DeleteIsCancelJobCrob extends BaseCronJob {

    @Override
    public void execute() {

        RecordSet rs = new RecordSet();

        List<JobPO> jobList = new ArrayList<>();
        rs.executeQuery("select id,ec_jobtitle,ec_department from jcl_org_job where delete_type = 0");
        while (rs.next()) {
            jobList.add(JobPO.builder()
                    .id((long)Util.getIntValue(rs.getString("id")))
                    .ecJobTitle(Util.getIntValue(rs.getString("ec_jobtitle")))
                    .ecDepartment(Util.getIntValue(rs.getString("ec_department")))
                    .build());
        }



        List<Long> deleteIds = new ArrayList<>();

        jobList.forEach(job -> {

            //1.岗位是否封存或删除
            rs.executeQuery("select id from hrmjobtitles where id = ? and (canceled is null or canceled != 1)",job.getEcJobTitle());
            if (!rs.next()) {
                deleteIds.add(job.getId());
                return;
            }

            //2.部门是否封存或删除
            rs.executeQuery("select id from hrmdepartment where id = ? and (canceled is null or canceled != 1)",job.getEcDepartment());
            if (!rs.next()) {
                deleteIds.add(job.getId());
            }

        });

        if (CollectionUtils.isNotEmpty(deleteIds)) {
            MapperProxyFactory.getProxy(JobMapper.class).deleteByIds(deleteIds);
        }



    }
}
