package weaver.interfaces.organization.cronjob;

import com.engine.organization.service.impl.ChartServiceImpl;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.OrganizationDateUtil;
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/8/1 3:12 PM
 * @Description: 组织架构版本定时删除
 * @Version 1.0
 */
public class OrgVersionDeleteCron extends BaseCronJob {



    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public void execute() {

        RecordSet rs = new RecordSet();

        List<Integer> idList = new ArrayList<>();
        String createTime = "";
        rs.executeQuery("select id,createtime from jcl_org_chartversion order by createtime asc");
        if (rs.next()){
            idList.add(Util.getIntValue(rs.getString("id")));
            createTime = Util.null2String(rs.getString("createtime"));
        }

        OrganizationAssert.notEmpty(idList,"未查询到组织架构图历史版本");

        //1.type=1 删除最早版本记录日期
        //2.type=2 删除最早版本日期--往后一个月的数据
        if("2".equals(type)){
            String lastMonthDate = OrganizationDateUtil.getLastMonthDate(createTime);
            rs.executeQuery("select id from jcl_org_chartversion where createtime >= ? and createtime <= ?",createTime,lastMonthDate);
            while (rs.next()) {
                idList.add(Util.getIntValue(rs.getString("id")));
            }
        }
        //3.type=3 删除最早版本日期--往后一年的数据
        if("3".equals(type)){
            String lastYearDate = OrganizationDateUtil.getLastYearDate(createTime);
            rs.executeQuery("select id from jcl_org_chartversion where createtime >= ? and createtime <= ?",createTime,lastYearDate);
            while (rs.next()) {
                idList.add(Util.getIntValue(rs.getString("id")));
            }
        }

        //删除数据
        ChartServiceImpl chartService = new ChartServiceImpl();
        idList.forEach(chartService::versionDelete);
    }
}
