package weaver.interfaces.htsc.cus;

import com.engine.htsc.payment.util.PayUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author K1810012
 */
public class UpdateBankInfoAction implements Action {

  private final Log log = LogFactory.getLog(UpdateBankInfoAction.class.getName());

  @Override
  public String execute(RequestInfo requestInfo) {
    RequestManager rm = requestInfo.getRequestManager();
    int rid = rm.getRequestid();
    String tb = rm.getBillTableName();
    log.info("拟稿提交节点后更新银行卡信息:@requestId=" + rid + "@");
    RecordSetTrans rst = new RecordSetTrans();
    rst.setAutoCommit(false);
    List<Map<String, String>> dt10List = new ArrayList<>();
    try {
      ResourceComInfo rc = new ResourceComInfo();
      String sql = "select t1.SS,t1.ID,t1.KHX,t1.BXR,t1.YXZH,t2.NGRGH,t2.REQUESTID from " + tb + "_DT10 t1\n" +
              "left join " + tb + " t2\n" +
              "on t1.MAINID=t2.ID\n" +
              "where t2.REQUESTID=" + rid;
      rst.executeQuery(sql);
      Map<String, String> singleInfo;
      while (rst.next()) {
        singleInfo = new HashMap<>();
        String YXZH = Util.null2String(rst.getString("YXZH"));
        String KHX = Util.null2String(rst.getString("KHX"));
//        String NGRGH = Util.null2String(rst.getString("NGRGH"));
        String BXRGH = rc.getWorkcode(Util.null2String(rst.getString("BXR")));
        String SS = Util.null2String(rst.getString("SS"));
        singleInfo.put("YXZH", YXZH);
        singleInfo.put("KHX", KHX);
        singleInfo.put("NGRGH", BXRGH);
        singleInfo.put("SS", SS);
        dt10List.add(singleInfo);
      }

      //多条记录处理
      for (Map<String, String> map : dt10List) {
        String YXZH = map.get("YXZH");
        String KHX = map.get("KHX");
        String NGRGH = map.get("NGRGH");
        String SS = map.get("SS");

        if (StringUtils.isEmpty(YXZH)) {
          rm.setMessageid("300");
          rm.setMessagecontent("表单异常,银行账户信息为空");
          return FAILURE_AND_CONTINUE;
        }
        rst.executeQuery("select ID from uf_lhclyxxx where KHR='" + NGRGH + "' ORDER BY ID DESC ");
        if (!rst.next()) {
          log.info("银行卡信息未维护到建模表单uf_lhclyxxx");
          log.info("需要维护的信息@开户人=" + NGRGH + ",银行账户=" + YXZH + ",开户行=" + KHX + "@");
          rst.executeQuery("select FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE " +
                  " from UF_LHCLYXXX where ROWNUM=1 and FORMMODEID is not null");
          rst.next();
          String formmodeid = rst.getString("FORMMODEID");
          String modedatacreater = rst.getString("MODEDATACREATER");
          String modedatacreatertype = rst.getString("MODEDATACREATERTYPE");
          DateTimeFormatter ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
          DateTimeFormatter hms = DateTimeFormatter.ofPattern("HH:mm:ss");
          String modeuuid = UUID.randomUUID().toString();
          LocalDateTime now = LocalDateTime.now();
          String ymdValue = now.format(ymd);
          String hmsValue = now.format(hms);
          List<Object> input = new ArrayList<>();
          input.add(formmodeid);
          input.add(modedatacreater);
          input.add(modedatacreatertype);
          input.add(ymdValue);
          input.add(hmsValue);
          input.add(modeuuid);
          input.add(NGRGH);
          input.add(KHX);
          input.add(YXZH);
          input.add(SS);
          rst.executeUpdate("INSERT INTO UF_LHCLYXXX " +
                  "(FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME,MODEUUID," +
                  "KHR,YXMC,YXKH,SS) \n" +
                  "VALUES (?,?,?,?,?,?,?,?,?,?)", input);
          rst.commit();
          log.info("更新成功");
          int dataId = PayUtil.getDataIdByUUID(modeuuid);
          ModeRightInfo modeRightInfo = new ModeRightInfo();
          modeRightInfo.editModeDataShare(1, Integer.parseInt(formmodeid), dataId);

        } else {
          //update
          String ID = Util.null2String(rst.getString("ID"));
          RecordSet rs = new RecordSet();
          rs.executeQuery("SELECT formmodeid,modeuuid,KHR,YXMC,YXKH,SS FROM uf_lhclyxxx WHERE ID=?", ID);
          String khr = "";
          String yxmc = "";
          String yxkh = "";
          String ss = "";
          String modeuuid = "";
          String formmodeid = "";
          if (rs.next()) {
            khr = Util.null2String(rs.getString("KHR"));
            yxmc = Util.null2String(rs.getString("YXMC"));
            yxkh = Util.null2String(rs.getString("YXKH"));
            ss = Util.null2String(rs.getString("SS"));
            modeuuid = Util.null2String(rs.getString("modeuuid"));
            formmodeid = Util.null2String(rs.getString("formmodeid"));
          }

          if (!khr.equals(NGRGH) || !yxmc.equals(KHX) || !yxkh.equals(YXZH) || !ss.equals(SS)) {
            rs.executeUpdate("UPDATE uf_lhclyxxx SET YXMC=?,YXKH=?,SS=? WHERE ID=?", KHX, YXZH, SS, ID);
            log.info(String.format("银行卡更新成功, YXMC=%s,YXKH=%s,SS=%s,ID=%s", yxmc, yxkh, ss, ID));
            int dataId = PayUtil.getDataIdByUUID(modeuuid);
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.editModeDataShare(1, Integer.parseInt(formmodeid), dataId);
          }

        }

      }
      return SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      rst.rollback();
      rm.setMessageid("500");
      rm.setMessagecontent("更新银行卡信息失败:" + e);
      return FAILURE_AND_CONTINUE;
    }

  }
}
