package weaver.interfaces.htxc.contract.action;

import com.engine.common.util.ServiceUtil;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.engine.htxc.workflow.create.business.contract.util.ContracrtBaseUtil;
import com.engine.htxc.workflow.create.business.contract.util.ContractUpdateStatus;
import com.engine.htxc.workflow.create.service.CustomizeworkflowService;
import com.engine.htxc.workflow.create.service.impl.CustomizeworkflowServiceImpl;
import com.engine.integration.util.StringUtils;
import com.weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

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

/**

 * <p>合同签订办结触发归档、履约</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName WrokFlowEnd.java
 * @createTime 2022年07月18日 09:47:00
 * @Remark
 *
 */
public class SignWorkFlowEnd extends BaseBean implements Action{

    private CustomizeworkflowService customizeworkflowService=
            ServiceUtil.getService(CustomizeworkflowServiceImpl.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid=requestInfo.getRequestid();
        User user =requestInfo.getRequestManager().getUser();
        Map<String,Object> mainmap=new DbTools().getRequestidToMap(requestid,null);
        CustomizeLog.printLog("requestid:"+requestid+"user:"+user
        +"map:"+mainmap.toString());
        if(StringUtils.isBlank(requestid) || mainmap==null || mainmap.size()<=0){
            return Action.FAILURE_AND_CONTINUE;
        }
        String czxz= Util.null2String(mainmap.get("czxz"));
        if(StringUtils.isBlank(czxz) || "0,1,2".indexOf(czxz)<0){
            return Action.FAILURE_AND_CONTINUE;
        }
        Map<String,Object> map=new HashMap<>(2);
        map.put("requestid",requestid);
        map.put("creatorid",user.getUID());
        if("1".equals(czxz)){
            ContractUpdateStatus.createContractUpdateStatus(requestid,user).signend("");
            return Action.SUCCESS;
        }
        if("0".equals(czxz)){
            map.put("unkey","zbhtgd");
            //调用归档流程
        }else if("2".equals(czxz)){
            map.put("unkey","zbhtfq");
            //调用废止流程  合同状态 废弃中
        }
       int newdocid= customizeworkflowService.createContractEnd(map,user);
        if(newdocid>0){
            ContractUpdateStatus.createContractUpdateStatus(requestid,user).signend(Util.null2String(newdocid));
            return Action.SUCCESS;
        }else{
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public void ContractwaitSign(String requestid){
        DbTools dbtools=new DbTools();
        Map<String,Object> mainmap=dbtools.getRequestidToMap(requestid,null);
        String xzhtlb=Util.null2String(mainmap.get("xzhtlb"));
        String htbb=Util.null2String(mainmap.get("htbb"));
        Map<String,Object> wheremap=new HashMap<>();
        wheremap.put("htbh",xzhtlb);
        wheremap.put("htbb",htbb);
        Map<String,Object> requestnamemap= ContracrtBaseUtil.getufhtqcmap(wheremap);

        String contractmodeid=Util.null2String(requestnamemap.get("id"));
        String tablename="uf_htqc";
        Map<String,String> wheremapmodel=new HashMap<>();
        wheremapmodel.put("id",contractmodeid);
        Map<String,String> setmap=new HashMap<>();
        setmap.put("zt", Util.null2String(ContractwaitSign.getValue()));
        dbtools.updateSql(tablename,setmap,wheremapmodel);
    }
}
