package weaver.interfaces.htxc.contract.action;

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

import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.engine.htxc.workflow.create.business.contract.util.ContractUpdateStatus;
import com.engine.integration.util.StringUtils;
import com.weaver.general.BaseBean;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import static com.engine.htxc.workflow.create.business.contract.init.ContractType.ThePerformanceToComplete;

/**
 * @author K1810043
 * @version 1.0.0
 * @ClassName ContractPerformanceTimedTaskAction.java
 * @createTime 2022年08月03日 10:13:00
 */
public class ContractPerformanceTimedTaskAction extends BaseBean implements Action {
    DbTools dbtools = new DbTools();
    PropBean pb = new PropBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        CustomizeLog.printLog("合同履约办结触发");
        String requestid = requestInfo.getRequestid();
        if (StringUtils.isBlank(requestid)) {
            return Action.FAILURE_AND_CONTINUE;
        }
        updatestatus(requestInfo);
        return Action.SUCCESS;
    }

    /**
     * <p>合同履约流程办结后，当前时间到达填写的履约结束日期时</p>
     * @title performanceEnd
     * @author K1810015
     * @updateTime 2022/8/30 19:20
     */
    public boolean performanceEnd(String requestid) {
        boolean flag;
        // 获取当前日期
        Date date1 = DateUtil.parse(DateUtil.date().toString("yyyy-MM-dd"));
        String tableName = dbtools.getTableName(requestid);
        String sql = "select lyjs from " + tableName + " where requestid = ? ";
        Map<String, Object> map = dbtools.getsqltomap(sql, requestid);
        if (map == null || map.size() <= 0) {
            CustomizeLog.printLog("合同履约办结后sqlerror:" + sql);
            return false;
        }
        String lyjs = Util.null2String(map.get("lyjs"));
        if (StringUtils.isBlank(lyjs)) {
            return false;
        }
        Date date2 = DateUtil.date();
        try {
            date2 = DateUtil.parse(lyjs);
        } catch (Exception e) {
            CustomizeLog.printLog("合同履约办结后时间转换异常:" + lyjs);
            return false;
        }
        // 当前时间到达、超过填写的履约结束日期
        Long datebet = DateUtil.between(date1, date2, DateUnit.DAY);
        if (date1.equals(date2) || date2.before(date1)) {
            flag = true;
        } else {
            flag = false;
        }
        CustomizeLog.printLog("合同履约办结后时间查询:" + "开始时间:" + date1 + "结束时间:" + date2 + "差值:" + datebet);
        return flag;
    }
    /**
     * <p>合同状态修改操作</p>
     * @title updatestatus
     * @author K1810015
     * @updateTime 2022/8/30 19:20
     */
    public void updatestatus(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        User user = requestInfo.getRequestManager().getUser();
        ContractUpdateStatus.createContractUpdateStatus(requestid, user).convertionend();
        if (performanceEnd(requestid)) {
            Map<String,String> tablesetmap=new HashMap<>(1);
            tablesetmap.put("lyjszt", "1");
            ContractUpdateStatus.createContractUpdateStatus(requestid, user)
                    .modeupdatezt(ThePerformanceToComplete.getValue()).tableUpdateType(tablesetmap);
        }
    }

}
