package com.engine.htsc.ydbx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.payment.util.LuhnUtil;
import com.engine.htsc.ydbx.service.PreExaminationService;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Weaver.SongYuchen
 * @Date 2022-05-27
 * @Description
 */
public class PreExaminationServiceImpl extends Service implements PreExaminationService {
    /**
     * 发票重复性校验
     *
     * @param fps  发票信息
     * @param lcbh 流程编号
     * @return
     */
    @Override
    public Map<String, Object> getInvoiceRepeat(String fps, String lcbh) {
        BaseBean bb = new BaseBean();
        Map<String, Object> resMap = new HashMap<>();
        RecordSet recordSet = new RecordSet();
        bb.writeLog("发票信息：" + fps + ", 流程编号" + lcbh);
        try {
            List<String> fpsList = changeStringToList(fps);
            //未提供发票，默认校验通过
            if (CollectionUtils.isEmpty(fpsList)) {
                resMap.put("status", "true");
                resMap.put("des", "发票重复性校验通过");
                return resMap;
            }

            List<String> dTips = new ArrayList<>();
            String sqlUf = "SELECT t2.REQUESTID, t2.REQUESTMARK, t1.FPDM, t1.FPHM FROM UF_PJXXGL t1\n" +
                    " LEFT JOIN WORKFLOW_REQUESTBASE t2\n" +
                    " ON t1.REQUESTID = t2.REQUESTID\n" +
                    " WHERE t1.FPDM = ? AND t1.FPHM = ?";
            bb.writeLog("sqlUf: " + sqlUf);
            fpsList.stream().distinct().forEach(item -> {
                if (!"@".equals(item)) {
                    if (!item.startsWith("@") && !item.endsWith("@")) {
                        recordSet.executeQuery(sqlUf, item.split("@")[0], item.split("@")[1]);
                        while (recordSet.next()) {
                            if (!recordSet.getString("REQUESTMARK").equals(lcbh)) {
                                dTips.add("发票" + recordSet.getString("FPHM")
                                        + "(" + recordSet.getString("FPDM")
                                        + ")已被流程" + recordSet.getString("REQUESTMARK") + "占用，请核实发票信息");
                            }
                        }
                    }
                }
            });
            bb.writeLog("dTips: " + dTips);
            if (dTips.isEmpty()) {
                resMap.put("status", "true");
                resMap.put("des", "发票重复性校验通过");
            } else {
                resMap.put("status", "false");
                resMap.put("des", dTips);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("status", "false");
            resMap.put("des", e.toString());
        }
        return resMap;
    }

    /**
     * 通用报销-银行账户信息校验
     *
     * @param khx    开户行
     * @param yxzfje 银行支付金额
     * @param yxzh   银行账号
     * @return
     */
    @Override
    public Map<String, Object> getBankAccountInfo(String khx, String yxzfje, String yxzh) {
        Map<String, Object> resMap = new HashMap<>();
        boolean desFlag;
        String checkBank = "^[a-zA-Z0-9]{5,35}$";
        try {
            //防止有多个开户行
            boolean khxFlag = true;
            if (khx.contains(",")) {
                khxFlag = false;
            } else {
                if (StringUtils.isBlank(khx) || LuhnUtil.checkString(khx.replaceAll(" ", ""))) {
                    khxFlag = false;
                }
            }
            if (!yxzh.matches(checkBank) || StringUtils.isBlank(yxzh) || !khxFlag) {
                desFlag = false;
            } else if ("0".equals(yxzfje) || StringUtils.isBlank(yxzfje)) {
                desFlag = false;
            } else {
                desFlag = true;
            }

            if (!desFlag) {
                resMap.put("status", "false");
                resMap.put("des", "请核实收款方银行账号信息");
            } else {
                resMap.put("status", "true");
                resMap.put("des", "银行账户信息校验通过");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resMap.put("status", "false");
            resMap.put("des", e.toString());
        }
        return resMap;
    }

    /**
     * String转List
     *
     * @param str String类型入参
     * @return
     */
    private List<String> changeStringToList(String str) {
        BaseBean bb = new BaseBean();
        List<String> stringList = new ArrayList<>();
        JSONArray array = JSONObject.parseArray(str);
        for (int i = 0; i < array.size(); i++) {
            stringList.add(Util.null2String(array.get(i)));
        }
        bb.writeLog("StringToList解析的结果是：" + JSONObject.toJSONString(stringList));
        return stringList;
    }
}