package weaver.interfaces.htsc.seal;


import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

public class SealProblemAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String userid = String.valueOf(requestInfo.getRequestManager().getUserId());
        writeLog("----SealProblemAction-------" + requestid);
        try{
            String mainid = "";
            String shwtxx = "";
            String qt = "";
            String yzgly = "";
            String sql = "select id,shwtxx,qt,yzgly from "+tablename+" where requestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                shwtxx = Util.null2String(rs.getString("shwtxx"));
                qt = Util.null2String(rs.getString("qt"));
                yzgly = Util.null2String(rs.getString("yzgly"));
            }

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

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                String insertSql = "insert into "+tablename+"_dt12(mainid,sj,yygly,yy) values('"+mainid+"','"+time+"','"+userid+"','"+problem+"')";
                writeLog(insertSql);
                rs.executeUpdate(insertSql);
            }

            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;
    }
}
