package weaver.interfaces.htxc.job;


import cn.hutool.core.date.DateTime;
import com.engine.common.util.ServiceUtil;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.workflow.create.service.CustomizeworkflowService;
import com.engine.htxc.workflow.create.service.impl.CustomizeworkflowServiceImpl;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>如果合同是展缓签订，当前时间大于等于展缓时间，则触发签订流程</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName ConstractSignSynThread.java
 * @createTime 2022年07月19日 14:30:00
 */
public class ConstractSignSynThread extends BaseCronJob {


    private CustomizeworkflowService customizeworkflowService=
            ServiceUtil.getService(CustomizeworkflowServiceImpl.class);
    public  DbTools dbTools=new DbTools();
    @Override
    public void execute() {
        createContractSign();
    }
    public void createContractSign(){

        String tableunnumber=new PropBean().getPropName("constract.sign.unnumber");
        String tablename=dbTools.getUnnumberToTablename(tableunnumber);

        String  sql="select t.sczhrq,t.ngr,t.requestid,t.czxz,t.zhfqzt from  "+tablename+" t join workflow_requestbase wr on t.requestid=wr.requestid " +
                       "  where wr.currentnodetype=3 and  " +
                       "  t.czxz=1 and (t.zhfqzt!=1 or t.zhfqzt is null) and to_date(t.sczhrq,'yyyy-MM-dd')<=to_date('"+ DateTime.now().toString("yyyy-MM-dd") +"','yyyy-MM-dd') ";
        List<Map<String,Object>> list=dbTools.getSqlToList(sql,(Object) null);
        list.forEach((Map<String,Object> map)->{
            String requestid= Util.null2String(map.get("requestid"));
            String ngr= Util.null2String(map.get("ngr"));
            int ngrvalue=Util.getIntValue(ngr,-1);
            Map<String,Object> retmap=new HashMap<>(2);
            retmap.put("requestid",requestid);
            retmap.put("unkey","zbhtqd");
            int newrequestid= customizeworkflowService.createContractEnd(retmap,new User(ngrvalue));
            if(newrequestid>0){
                oldcallback(requestid,tablename);
            }
        });
    }
    public void oldcallback(String requestid,String tablename){
        if(StringUtils.isBlank(requestid) || StringUtils.isBlank(tablename)){
            return ;
        }
        Map<String,String> setmap=new HashMap<>(1);
        setmap.put("zhfqzt","1");
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("requestid",requestid);
        dbTools.updateSql(tablename,setmap,wheremap);
    }
}
