package com.engine.htsc.movereimburse.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

public class MoveReimbursedCMD extends AbstractCommonCommand<Map<String, Object>> {
    private Log log = LogFactory.getLog(this.getClass());
    private String invInfo;
    private String requestId;

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public MoveReimbursedCMD(String invInfo, String requestId) {
        this.invInfo = invInfo;
        this.requestId = requestId;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        log.info("获取的参数是：" + invInfo + ", " + requestId);
        List<String> wfInvoiceInfo = invString2List(invInfo);
        // 查询流程内的发票是否与发票关联关系表中存在连号发票
        Map<String, Object> checDbResult = checkwithdb(wfInvoiceInfo);
        log.info("检查关联关系表的结果是:" + JSONObject.toJSONString(checDbResult));
        return checDbResult;
    }

    private List<String> invString2List(String invStringInfo) {
        log.info("接收的发票数据为：" + invStringInfo);
        List<String> wfInvoiceInfo = new ArrayList<>();
        JSONArray array = JSONObject.parseArray(invStringInfo);
        for (int i = 0; i < array.size(); i++) {
            wfInvoiceInfo.add(Util.null2String(array.get(i)));
        }
        log.info("解析的结果是：" + JSONObject.toJSONString(wfInvoiceInfo));
        return wfInvoiceInfo;
    }

    /**
     * 查看本流程中的发票是否与关联关系表中的发票存在连号关系
     *
     * @param wfInvoiceInfo 流程发票信息
     *                      连号规则：发票代码相同，发票号码+1
     * @return Map<String, Object>
     */
    public Map<String, Object> checkwithdb(List<String> wfInvoiceInfo) {
        RecordSet rs = new RecordSet();
        String sql;
        Map<String, Object> result = new HashMap<>();
        StringBuilder coliedMsg = new StringBuilder();
        try {
            for (String invoiceInfo : wfInvoiceInfo) {
                log.info("invoiceInfo: " + invoiceInfo);
                String fpdm = invoiceInfo.split("@")[0];
                String fphm = invoiceInfo.split("@")[1];
                log.info("fpdm: " + fpdm + ", fphm: " + fphm);
                if (StringUtils.isNotEmpty(fpdm) && StringUtils.isNotEmpty(fphm)) {
                    String status1 = 0 == fphm.indexOf("0") ? "0" + Util.null2String(Long.parseLong(fphm) - 1) : Util.null2String(Long.parseLong(fphm) - 1);
                    String status2 = 0 == fphm.indexOf("0") ? "0" + Util.null2String(Long.parseLong(fphm)) : Util.null2String(Long.parseLong(fphm));
                    String status3 = 0 == fphm.indexOf("0") ? "0" + Util.null2String(Long.parseLong(fphm) + 1) : Util.null2String(Long.parseLong(fphm) + 1);
                    String[] fphms = {status1, status2, status3};
                    sql = "select COUNT(*) as counts from uf_pjxxgl where fpdm = ? and fphm = ? and requestid != ?";
                    for (String theFphm : fphms) {
                        log.info("theFphm: " + theFphm);
                        rs.executeQuery(sql, fpdm, theFphm, requestId);
                        if (rs.next()) {
                            boolean coliedOne = "0".equals(Util.null2String(rs.getString("counts")));
                            if (!coliedOne) {
                                coliedMsg.append(fpdm).append("_").append(fphm).append("存在连号发票;");
                                break;
                            }
                        }
                    }
                }
            }
            result.put("status", StringUtils.isEmpty(coliedMsg.toString()) ? "true" : "false");
            result.put("msg", coliedMsg.toString());
        } catch (Exception e) {
            log.info("检查流程与发票关联关系表连号关系中发生错误");
            log.info(e.getMessage());
            result.put("status", "error");
            result.put("msg", "连号判断时出现错误，请检查");
        }
        return result;
    }
}