package weaver.interfaces.htxc.contract.action;

import com.engine.common.util.ServiceUtil;
import com.engine.htxc.contract.entity.CghtMainEntity;
import com.engine.htxc.contract.service.impl.ContractServiceImpl;
import com.engine.integration.util.StringUtils;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
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;


/**
 * @author K1810039
 * 从合同起草预设流程转入数据创建合同草稿
 * @version 1.0.0
 * @ClassName HtqcAction.java
 */
public class HtqcAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog("---HtqcAction---requestid:"+requestid);
        User user =requestInfo.getRequestManager().getUser();
        if(StringUtils.isBlank(requestid) || user==null ){
            return Action.FAILURE_AND_CONTINUE;
        }
        try {
            //合同类型
            String htlx = "";
            //合同名称
            String htmc = "";
            //采购项目编号
            String cgxmbh = "";
            //拟稿人所在部门
            String szbm = "";
            //拟稿人
            String ngr = "";
            String sql = "select HTLX,HTMC,CGXMBH,SZBM,NGR from "+tablename+" where REQUESTID = " + requestid;
            writeLog(sql);
            RecordSet rs = new RecordSet();
            rs.execute(sql);
            if (rs.next()){
                htlx = Util.null2String(rs.getString("HTLX"));
                htmc = Util.null2String(rs.getString("HTMC"));
                cgxmbh = Util.null2String(rs.getString("CGXMBH"));
                szbm = Util.null2String(rs.getString("SZBM"));
                ngr = Util.null2String(rs.getString("NGR"));
            }
            Map<String , String > map = new HashMap<>();
            map.put("htlx",htlx);
            map.put("htmc",htmc);
            map.put("cgxmbh",cgxmbh);
            map.put("szbm",szbm);
            map.put("ngr",ngr);
            writeLog("参数打印paramMap:"+map);

            //构造数据实体
            final CghtMainEntity cghtMainEntity = new CghtMainEntity();
            cghtMainEntity.setHtmc(htmc);
            cghtMainEntity.setXgcgx(cgxmbh);
            cghtMainEntity.setHtlx(htlx);
            cghtMainEntity.setQcr(Integer.parseInt(ngr));
            cghtMainEntity.setQcbm(Integer.parseInt(szbm));
            final CghtMainEntity result = ServiceUtil.getService(ContractServiceImpl.class).createCghtqc(cghtMainEntity);
            writeLog("result:"+result);
            if(result == null||result.getId() == 0){
                writeLog("-1 创建合同草稿失败");
            }else{
                writeLog("200 创建合同草稿成功:"+result);
            }
            return Action.SUCCESS;
        }catch (Exception e){
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
