package weaver.interfaces.htsc.hthy.action;

import com.alibaba.fastjson.JSON;
import com.engine.htsc.payment.log.PaymentLogger;
import com.engine.htsc.payment.log.PaymentLoggerFactory;
import com.engine.htsc.payment.util.AssertUtil;
import com.engine.htsc.payment.util.CommonAPI;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RefreshDetailRows extends AbstractModeExpandJavaCodeNew {

  private static final PaymentLogger logger =
      PaymentLoggerFactory.getLogger(RefreshDetailRows.class);

  private static String billid = "";

  @Override
  public Map<String, String> doModeExpand(Map<String, Object> map) {
    //返回值,可空
    Map<String, String> rt = new HashMap<>();
    logger.info("刷新明细行数据处理");
    billid = (String) map.get("billid");
    String sql = "SELECT lastdeldt FROM UF_HYWH WHERE ID = ?";
    String lastdeldt = CommonAPI.querySingleFieldWithPlaceholder(sql, "lastdeldt", billid);
    List<String> sjcList = Arrays.stream(lastdeldt.split(","))
        .distinct()
        .filter(AssertUtil::isNotEmpty)
        .collect(Collectors.toList());
    if (AssertUtil.isEmpty(sjcList)) {
      logger.info("主表 lastdeldt 为空, 执行结束");
      return rt;
    }
    //刷新
    refresh(sjcList);
    return rt;
  }

  /**
   * 刷新 dt5,6
   */
  private void refresh(List<String> sjcList) {
    RecordSetTrans rst = new RecordSetTrans();
    rst.setAutoCommit(false);
    logger.info("主表 lastdeldt " + JSON.toJSONString(sjcList));
    try {
      List<Map<String, String>> dt5 = CommonAPI
          .queryWithPlaceholder("UF_HYWH_dt5", "id,mainid,sjc", "mainid", billid);
      String dt5_del = dt5.stream().filter(item -> sjcList.contains(item.get("sjc")))
          .map(item -> item.get("id"))
          .collect(Collectors.joining("','"));
      if (AssertUtil.isNotEmpty(dt5_del)) {
        String sql = String.format("delete from uf_hywh_dt5 where id  in ('%s')", dt5_del);
        logger.info("删除dt5 id: " + dt5_del);
        if (rst.executeUpdate(sql)) {
          rst.commitOnly();
        }
      }

      List<Map<String, String>> dt6 = CommonAPI
          .queryWithPlaceholder("UF_HYWH_dt6", "id,mainid,sjc", "mainid", billid);
      String dt6_del = dt6.stream().filter(item -> sjcList.contains(item.get("sjc")))
          .map(item -> item.get("id"))
          .collect(Collectors.joining("','"));
      if (AssertUtil.isNotEmpty(dt6_del)) {
        logger.info("删除dt6 id: " + dt6_del);
        String sql = String.format("delete from uf_hywh_dt6 where id  in ('%s')", dt6_del);
        if (rst.executeUpdate(sql)) {
          rst.commitOnly();
        }
      }

      //刷新完成,清空lastdeldt字段
      String sql = "UPDATE UF_HYWH SET lastdeldt = NULL WHERE id= ?";
      rst.executeUpdate(sql, billid);
      rst.commitOnly();

    } catch (Exception e) {
      logger.error("refresh 明细出错");
      rst.rollback();
    }

  }
}
