package weaver.interfaces.htxc.seal.action;

import cn.hutool.core.date.DateTime;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *<p>用印登记单退回原因修改</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName SealRejectreason.java
 * @createTime 2022年07月28日 10:00:00
 */
public class SealRejectreason extends BaseBean implements Action {

    public String getDetailnum() {
        return detailnum;
    }

    public void setDetailnum(String detailnum) {
        this.detailnum = detailnum;
    }

    public String detailnum;

    @Override
    public String execute(RequestInfo requestInfo) {
        DbTools dbTools=new DbTools();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String userid = String.valueOf(requestInfo.getRequestManager().getUserId());
        String oldshwtxxid= Util.null2String(new PropBean().getPropName("contracrt.eseal.shwtxxid"));
        CustomizeLog.printLog("----SealRejectreason-------" + requestid);
        try{
            String mainid = "";
            String shwtxx = "";
            String qt = "";
            String yzgly = "";
            String sql = "select id,shwtxx,qt from "+tablename+" where requestid = '"+requestid+"'";
            CustomizeLog.printLog(sql);
            Map<String,Object> map=dbTools.getsqltomap(sql);
            if(map==null || map.size()<=0){
                return Action.SUCCESS;
            }
            mainid = Util.null2String(map.get("id"));
            shwtxx = Util.null2String(map.get("shwtxx"));
            qt = Util.null2String(map.get("qt"));

            if(!StringUtils.isBlank(shwtxx)){
                List<String> problemList = new ArrayList<>();
                List<String> shwtxxList = Arrays.asList(shwtxx.split(","));
                Collections.sort(shwtxxList);
                for(String shwtxxid:shwtxxList){
                    if(oldshwtxxid.equals(shwtxxid)){
                        problemList.add(qt);
                    }else{
                        problemList.add(getProblemById(shwtxxid));
                    }
                }
                String problem = String.join(";",problemList);

                String time = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
                String tablenamenew=tablename+"_dt"+detailnum;
                Map<String,String> setmap=new HashMap<>();
                setmap.put("mainid",mainid);
                setmap.put("sj",time);
                setmap.put("yygly",userid);
                setmap.put("yy",problem);
                dbTools.insert(tablenamenew,setmap);
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    private String getProblemById(String id){
        String problem ="";

        Map<String,String> problemMap = new HashMap<>();
        problemMap.put("0","用印件与流程不一致");
        problemMap.put("1","文档内容重大错误");
        problemMap.put("2","印章选择与文件落款不一致");
        problemMap.put("3","缺少必要的审核环节");
        problemMap.put("4","用印件格式错误");
        problemMap.put("5","流程审核意见未沟通");
        problemMap.put("6","用印件超过时效");
        problemMap.put("7","经办人要求退回修改");
        problemMap.put("8","仅走流程审核无需用印");
        problem = problemMap.get(id);

        return problem;
    }
}
