package com.saye.hospitalgd.scheduler.jobMethod;

import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.commons.uuid.UUIDGenerator;
import com.saye.hospitalgd.service.*;
import com.saye.hospitalgd.service.historyLog.ReconciliationLogService;
import com.saye.hospitalgd.service.historyLog.impl.ReconciliationLogServiceImpl;
import com.saye.hospitalgd.service.impl.*;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import com.saye.hospitalgd.service.system.impl.ServiceParamsServiceImpl;


import java.math.BigDecimal;
import java.util.*;

/**
 * @author thuang
 * @version 1.0
 * @description: 对账方法
 * @date 2021/9/30 13:45
 */
public class ReconciliationMethod {

    public HashMap<Object, Object> getDate(String id, String name, String trade_date, String userName) {
        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        if ("".equals(trade_date)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -1);
            Date time = calendar.getTime();
            trade_date = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, time);
        }


        //查询获取记录的情况，没有的生成一条失败的记录 原因XX未下载完成
//        HisBillLogService hisBillLogService = GetBeanUtil.getBean(HisBillLogServiceImpl.class);
        BankbillsHistoryService bankbillsHistoryService = GetBeanUtil.getBean(BankbillsHistoryServiceImpl.class);


        ReconciliationLogService reconciliationLogService = GetBeanUtil.getBean(ReconciliationLogServiceImpl.class);
        ServiceParamsService serviceParamsService = GetBeanUtil.getBean(ServiceParamsServiceImpl.class);


        HisbillsHistoryService hisbillsHistoryService = GetBeanUtil.getBean(HisbillsHistoryServiceImpl.class);


        //记录是第几次对账
        int managerNum = 0;
        try {
            HashMap<Object, Object> searchMap = new HashMap<>();
            searchMap.put("trade_date", trade_date);
            searchMap.put("is_ok", 1);

            //先判断是否已生成
            List<HashMap<Object, Object>> reconciliationLog = reconciliationLogService.findReconciliationLogByParam(searchMap);

            if (reconciliationLog.size() > 0) {
                managerNum = Integer.parseInt(StringDUtil.changeNullToEmpty(reconciliationLog.get(0).get("MANAGER_NUM")));
            }

            //查询his和三方记录
            List<HashMap<Object, Object>> hisbillsList = hisbillsHistoryService.findHisBillsByDate(searchMap);

            List<HashMap<Object, Object>> bankbillsList = bankbillsHistoryService.findBankBillsByDate(searchMap);
            //his 和三方都有记录且成功
            if (hisbillsList.size() > 0 && bankbillsList.size() > 0) {


                TransactionDetailService transactionDetailService = GetBeanUtil.getBean(TransactionDetailServiceImpl.class);
                UnilateralService unilateralService = GetBeanUtil.getBean(UnilateralServiceImpl.class);

                String cash_code = StringDUtil.changeNullToEmpty(serviceParamsService.findParamValByParamCode("cash_code"));

                //先创建关联表 用来后面查询交易明细
                searchMap.put("cash_code", cash_code);
                transactionDetailService.insertHisAndThirdJoinData(searchMap);


                //查询当天所有账单
                List<HashMap<Object, Object>> errJoinDataList = transactionDetailService.findHisAndThirdJoinDataByParam(searchMap);

                //删除当天单边账表的记录保留人工处理过的记录
                unilateralService.deleteUnilateralByNoManager(searchMap);

                //循环所有情况 比对单边账表  产生单边账表数据
                for (int i = 0; i < errJoinDataList.size(); i++) {
                    HashMap<Object, Object> hashMap = errJoinDataList.get(i);
                    String err_type = StringDUtil.changeNullToEmpty(hashMap.get("ERR_TYPE"));
                    String check_result = StringDUtil.changeNullToEmpty(hashMap.get("CHECK_RESULT"));


                    //银行表提取出的对账有用信息，his与第三方账单结合后整理出的有用数据
                    String jysj = StringDUtil.changeNullToEmpty(hashMap.get("JYSJ"));
                    String i_ddh = StringDUtil.changeNullToEmpty(hashMap.get("I_DDH"));
                    String i_jylx = StringDUtil.changeNullToEmpty(hashMap.get("I_JYLX"));
                    String i_jyje = StringDUtil.changeNullToEmpty(hashMap.get("I_JYJE"));
                    String i_jyqd = StringDUtil.changeNullToEmpty(hashMap.get("I_JYQD"));


                    //his表提取出的对账有用信息，his与第三方账单结合后整理出的有用数据
                    String tranid = StringDUtil.changeNullToEmpty(hashMap.get("TRANID"));
                    String h_jylx = StringDUtil.changeNullToEmpty(hashMap.get("H_JYLX"));
                    String payType = StringDUtil.changeNullToEmpty(hashMap.get("PAYTYPE"));
                    String biztype = StringDUtil.changeNullToEmpty(hashMap.get("BIZTYPE"));
                    String amount = StringDUtil.changeNullToEmpty(hashMap.get("AMOUNT"));
                    String hisopernum = StringDUtil.changeNullToEmpty(hashMap.get("HISOPERNUM")); //his系统获取的操作员号
                    String thistrade_date = StringDUtil.changeNullToEmpty(hashMap.get("TRADE_DATE"));

                    String tradeTime = StringDUtil.changeNullToEmpty(hashMap.get("TRADETIME"));


                    //由于假设第二天获取的记录为第一天的， 由于查询是根据消费日期的记录是第二天的。所以相关只有第二天的账
                    //而要对比第一天，只能重新执行第一天的对账。
                    if ("0".equals(check_result)) {
                        //查询历史是否有对应记录 前面全删了对齐的以前只可能是单边账 如果人工改了那就不动，如果没改那也已经被删了
                        continue;
                    }


                    //数据库中对比完是只有三种状态 his单边 三方单边 差异 分别判断3种账单实际情况 如单边是因为跨天
                    //err_type 1 his单边 2 三方单边 3 差异
                    if ("1".equals(check_result) && "1".equals(err_type)) {
                        //his单边账 查询历史单边账表是否是否已有记录 有不动 没有添加
                        List<HashMap<Object, Object>> unilateralList = null;
                        HashMap<Object, Object> noCashSearchMap = new HashMap<>();
                        noCashSearchMap.put("tranid", tranid);
                        noCashSearchMap.put("h_jylx", h_jylx);
                        noCashSearchMap.put("trade_date", thistrade_date);
                        unilateralList = unilateralService.findUnilateralByParam(noCashSearchMap);
//                        }

                        //如果没有对应单边账记录
                        if (!(unilateralList != null && unilateralList.size() > 0)) {
                            HashMap<Object, Object> addMap = new HashMap<>();

                            //生成记录
                            addMap.put("id", UUIDGenerator.getUUID());
                            addMap.put("payType", payType);
                            addMap.put("tranID", tranid);
                            addMap.put("h_jylx", h_jylx);
                            addMap.put("trade_date", thistrade_date);
                            addMap.put("hisOperNum", hisopernum);
                            addMap.put("amount", amount);
                            addMap.put("i_jyje", "");
                            addMap.put("i_jylx", "");
                            addMap.put("i_ddh", "");
                            addMap.put("i_jyqd", "");
                            addMap.put("trade_time", tradeTime);
                            addMap.put("is_manager", "0");
                            addMap.put("manager_time", "");
                            addMap.put("err_type", err_type);
                            addMap.put("remark", "");
                            addMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("join_id", "");

                            unilateralService.insertUnilateral(addMap);
                        } else {
                            //如果有记录 将查询处理方式 将记录对应的处理修改
                            String managerType = StringDUtil.changeNullToEmpty(unilateralList.get(0).get("MANAGERTYPE"));

                            //如果处理方式是其他 不用修改 如果处理方式是不计统计那样需要将此数据标志为无效
                            if ("2".equals(managerType)) {
                                HashMap<Object, Object> updateMap = new HashMap<>();
//                                updateMap.put("payMethod", payMethod);
                                updateMap.put("payType", payType);
                                updateMap.put("hisOperNum", hisopernum);
                                updateMap.put("trade_date", thistrade_date);
                                updateMap.put("tranID", tranid);
                                updateMap.put("h_jylx", h_jylx);
                                updateMap.put("i_jylx", i_jylx);
                                updateMap.put("is_active", "0");
                                transactionDetailService.updateJoinSetActiveByHis(updateMap);
                            }
                        }

                        //非现金账还可能出现跨天账
//                        if (!(cash_code.equals(payType))) {
                        //查询是否有这条记录对应的三方记录
                        //用his的工行订单号去查找与第三方订单号相同的对应的记录,这里不按时间来筛查，可以查到跨天相同订单号的跨天帐
                        HashMap<Object, Object> lastDaySearchMap = new HashMap<>();
                        lastDaySearchMap.put("i_ddh", tranid);
                        lastDaySearchMap.put("i_jylx", h_jylx);

                        List<HashMap<Object, Object>> list = new ArrayList<>();
                        if (!"".equals(tranid)) {
                            list = transactionDetailService.findHisAndThirdJoinDataByParam(lastDaySearchMap);
                        }
                        if (list != null && list.size() > 0) {
                            //说明有一条对应的记录  查看金额是否相同, 如果相同,核销 如果不同 状态改为跨天差异账
                            String thirdJe = StringDUtil.changeNullToEmpty(list.get(0).get("I_JYJE"));
                            HashMap<Object, Object> updateHisMap = new HashMap<>();
                            HashMap<Object, Object> updateThirdMap = new HashMap<>();
                            if (new BigDecimal(thirdJe).compareTo(new BigDecimal(amount)) == 0) {


                                //修改该记录状态为自动核销
                                updateHisMap.put("is_manager", "0");
                                updateHisMap.put("remark", "系统自动核销");
                                updateHisMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                                updateHisMap.put("tranID", tranid);
                                updateHisMap.put("h_jylx", h_jylx);
                                updateHisMap.put("check_result", "2");


                                updateThirdMap.put("is_manager", "0");
                                updateThirdMap.put("remark", "系统自动核销");
                                updateThirdMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                                updateThirdMap.put("i_ddh", tranid);
                                updateThirdMap.put("i_jylx", h_jylx);
                                updateThirdMap.put("check_result", "2");

//                                updateThirdMap.put("is_manager", "0");
//                                updateThirdMap.put("remark", "系统自动核销");
//                                updateThirdMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
//                                updateThirdMap.put("i_ghddh", h_ghddh);
//                                updateThirdMap.put("i_jylx", zfzt);
//                                updateThirdMap.put("check_result", "2");

                                //修改明细表
                                transactionDetailService.updateJoinDate(updateHisMap);
                                transactionDetailService.updateJoinDate(updateThirdMap);

                                //修改单边账记录表
                                unilateralService.updateUnilateral(updateHisMap);

                            }
                        }
                    } else if ("1".equals(check_result) && "2".equals(err_type)) {
                        //三方单边账 查询历史单边账表是否已有记录 有不动 没有添加

                        List<HashMap<Object, Object>> unilateralList = null;
                        HashMap<Object, Object> noCashSearchMap = new HashMap<>();
                        noCashSearchMap.put("i_ddh", i_ddh);
                        noCashSearchMap.put("i_jylx", i_jylx);
                        noCashSearchMap.put("trade_date", thistrade_date);
                        unilateralList = unilateralService.findUnilateralByParam(noCashSearchMap);
//                        }

                        if (!(unilateralList != null && unilateralList.size() > 0)) {
                            HashMap<Object, Object> addMap = new HashMap<>();


                            //由于是第三方单边账，所以his相关数据应该是空的，所以，添加数据时，his相关数据设置为空
                            addMap.put("id", UUIDGenerator.getUUID());
                            addMap.put("payType", "");
                            addMap.put("tranID", "");
                            addMap.put("h_jylx", "");
                            addMap.put("trade_date", thistrade_date);
                            addMap.put("hisOperNum", "");
                            addMap.put("amount", "");


                            addMap.put("i_jylx", i_jylx);
                            addMap.put("i_jyje", i_jyje);
                            addMap.put("i_ddh", i_ddh);
                            addMap.put("i_jyqd", i_jyqd);
                            addMap.put("tradeTime", tradeTime);


                            addMap.put("is_manager", "0");
                            addMap.put("manager_time", "");

                            addMap.put("err_type", err_type);
                            addMap.put("remark", "");


                            addMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("join_id", "");
                            unilateralService.insertUnilateral(addMap);
                        } else {
                            //如果有记录 将查询处理方式 将记录对应的处理修改
                            String managerType = StringDUtil.changeNullToEmpty(unilateralList.get(0).get("MANAGERTYPE"));

                            //如果处理方式是其他 不用修改 如果处理方式是不计统计那样需要将此数据标志为无效
                            if ("2".equals(managerType)) {
                                HashMap<Object, Object> updateMap = new HashMap<>();
                                // 三方没有门诊住院 牵扯使用这个的数据也没有 似乎只需使用ysddh
                                updateMap.put("hisOperNum", hisopernum);
                                updateMap.put("i_jyqd", i_jyqd);
                                updateMap.put("trade_date", thistrade_date);
                                updateMap.put("i_ddh", i_ddh);
                                updateMap.put("h_jylx", h_jylx);
                                updateMap.put("i_jylx", i_jylx);
//                                updateMap.put("cash_code", cash_code);

                                updateMap.put("is_active", "0");
                                transactionDetailService.updateJoinSetActiveByThird(updateMap);
                            }
                        }

                        //非现金账还可能出现跨天账
//                        if (!(cash_code.equals(c_zffs))) {
                        //查询是否有这条记录对应的HIS记录 结果肯定不是当天的
                        //用第三方账单的信息来查找his相关账单的信息，能查到，说明是对的上的，只是跨天了
                        HashMap<Object, Object> lastDaySearchMap = new HashMap<>();
                        lastDaySearchMap.put("tranid", i_ddh);
                        lastDaySearchMap.put("h_jylx", i_jylx);

                        List<HashMap<Object, Object>> list = new ArrayList<>();
                        if (!"".equals(i_ddh)) {
                            list = transactionDetailService.findHisAndThirdJoinDataByParam(lastDaySearchMap);
                        }
                        if (list != null && list.size() > 0) {
                            //说明有一条对应的记录  查看金额是否相同, 如果相同,核销 如果不同 状态改为跨天差异账
                            String hisJe = StringDUtil.changeNullToEmpty(list.get(0).get("AMOUNT"));
                            HashMap<Object, Object> updateHisMap = new HashMap<>();
                            HashMap<Object, Object> updateThirdMap = new HashMap<>();

                            //如果两边金额相同 设置核销 更新关联明细表为核销
                            if (new BigDecimal(hisJe).compareTo(new BigDecimal(i_jyje)) == 0) {
                                //修改该记录状态为自动核销
                                updateHisMap.put("is_manager", "0");
                                updateHisMap.put("remark", "系统自动核销");
                                updateHisMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                                updateHisMap.put("tranID", i_ddh);
                                updateHisMap.put("h_jylx", h_jylx);
                                updateHisMap.put("check_result", "2");

                                updateThirdMap.put("is_manager", "0");
                                updateThirdMap.put("remark", "系统自动核销");
                                updateThirdMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                                updateThirdMap.put("i_ddh", i_ddh);
                                updateThirdMap.put("i_jylx", i_jylx);
                                updateThirdMap.put("check_result", "2");


                                //修改明细表
                                transactionDetailService.updateJoinDate(updateHisMap);
                                transactionDetailService.updateJoinDate(updateThirdMap);

                                //修改单边账记录表
                                unilateralService.updateUnilateral(updateThirdMap);

                            }
                        }
                    } else if ("1".equals(check_result) && "3".equals(err_type)) {
                        //查询单边账表是否有这条记录 差异账 有就不动 没有就添加
                        List<HashMap<Object, Object>> unilateralList = null;
                        HashMap<Object, Object> noCashSearchMap = new HashMap<>();
                        noCashSearchMap.put("tranid", i_ddh);
                        noCashSearchMap.put("h_jylx", i_jylx);
                        noCashSearchMap.put("trade_date", thistrade_date);
                        unilateralList = unilateralService.findUnilateralByParam(noCashSearchMap);

                        if (!(unilateralList != null && unilateralList.size() > 0)) {
                            HashMap<Object, Object> addMap = new HashMap<>();

                            addMap.put("id", UUIDGenerator.getUUID());
                            addMap.put("tranID", tranid);
                            addMap.put("h_jylx", h_jylx);
                            addMap.put("hisOperNum", hisopernum);
                            addMap.put("i_ddh", i_ddh);
                            addMap.put("i_jylx", i_jylx);
                            addMap.put("trade_date", thistrade_date);
                            addMap.put("amount", amount);
                            addMap.put("i_jyje", i_jyje);
                            addMap.put("tradeTime", tradeTime);
                            addMap.put("is_manager", "0");
                            addMap.put("manager_time", "");
                            addMap.put("i_jyqd", i_jyqd);
                            addMap.put("err_type", err_type);
                            addMap.put("remark", "");
                            addMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                            addMap.put("join_id", "");


                            unilateralService.insertUnilateral(addMap);
                        } else {
                            //如果有记录 将查询处理方式 将记录对应的处理修改
                            String managerType = StringDUtil.changeNullToEmpty(unilateralList.get(0).get("MANAGERTYPE"));

                            //如果处理方式是其他 不用修改 如果处理方式是不计统计那样需要将此数据标志为无效
                            if ("2".equals(managerType)) {
                                HashMap<Object, Object> updateMap = new HashMap<>();
                                // 三方没有门诊住院 牵扯使用这个的数据也没有 似乎只需使用ysddh
                                updateMap.put("hisOperNum", hisopernum);
//                                updateMap.put("payType", payType);
                                updateMap.put("i_jyqd", i_jyqd);
                                updateMap.put("trade_date", thistrade_date);
                                updateMap.put("i_ddh", i_ddh);
                                updateMap.put("tranID", tranid);
                                updateMap.put("h_jylx", h_jylx);
                                updateMap.put("i_jylx", i_jylx);
//                                updateMap.put("cash_code", cash_code);

                                updateMap.put("is_active", "0");
                                transactionDetailService.updateJoinSetActiveByThird(updateMap);
                            }
                        }
                    }
                }

                /**
                 * 循环处理完成后 处理剩余情况 如有重复记录 进行处理 （由于上面的只处理了错误情况，所以能关联上的记录不会经过上面的处理除非两条单边帐，
                 * 两条单边账第一次会是什么都处理，第二次依据上面的情况为两条按同一种方式处理）
                 * 此处理基于银行端不会出现重复记录 如非此情况，数据将会有问题
                 * 可能情况有重复记录的情况
                 * 1 现金账重复， 现金账会相加在处理，结果不同，会产生差异账，无需处理
                 * 2 特殊要求的支票帐重复， 同现金帐
                 * 3 普通帐重复，关联将产生两条相同记录 单边账表和关联表新增一个id，用于区分重复记录
                 * 如果普通情况 查询后处理查询的第二条 对两条记录分别添加id，将一条记录去除一边数据标识为单边账。
                 * 将记录添加到单边账表，由于正常账不会找单边账记录，所以有重复即使执行也会到这里
                 * 4 退款两次 这两次都属于正常记录， 可能会有更多次
                 * 5 少记录 银行那边退了两笔，而his只记录了一笔
                 *
                 * 由于错误和多比对账的不确定性 单边账记录表删除重新生成
                 *
                 */
                HashMap<String, Integer> trueIdMap = new HashMap<>();
                List<HashMap<Object, Object>> trueList = new ArrayList<>();

                //先查询重复的记录，的id和状态
                List<HashMap<Object, Object>> notUniqueList = transactionDetailService.findHisAndThirdJoinDataByParamAndNotUnique(searchMap);

                //删除所有重复记录。
                for (int i = 0; i < notUniqueList.size(); i++) {
                    HashMap<Object, Object> notUniqueObj = notUniqueList.get(i);

                    String tranid = StringDUtil.changeNullToEmpty(notUniqueObj.get("TRANID"));
                    String h_jylx = StringDUtil.changeNullToEmpty(notUniqueObj.get("H_JYLX"));
                    HashMap<Object, Object> delMap = new HashMap<>();

                    delMap.put("trade_date", trade_date);
                    delMap.put("tranID", tranid);
                    delMap.put("h_jylx", h_jylx);
                    transactionDetailService.deleteHisAndThirdJoinDataByParamAndNotUnique(delMap);

                    //删除当天已经生成的该条对应的可能错帐记录
                    unilateralService.deleteUnilateralByIdandStatus(delMap);

                }

                //删除当天单边账里join_id不为空的记录
                unilateralService.deleteUnilateralByJoinIdNotNull(searchMap);


                //处理查询的记录
                for (int i = 0; i < notUniqueList.size(); i++) {
                    HashMap<Object, Object> notUniqueObj = notUniqueList.get(i);

                    String tranid = StringDUtil.changeNullToEmpty(notUniqueObj.get("TRANID"));
                    String h_jylx = StringDUtil.changeNullToEmpty(notUniqueObj.get("H_JYLX"));


                    //分别查询银行端和his端的记录

                    HashMap<Object, Object> searchNotUniqueObjMap = new HashMap<>();
                    searchNotUniqueObjMap.put("h_jylx", h_jylx);
                    searchNotUniqueObjMap.put("tranID", tranid);
                    searchNotUniqueObjMap.put("i_ddh", tranid);
                    searchNotUniqueObjMap.put("i_jylx", h_jylx);
                    searchNotUniqueObjMap.put("trade_date", trade_date);


                    searchNotUniqueObjMap.put("orderby_je", "true");

                    //todo
                    //his端重复记录
                    List<HashMap<Object, Object>> list = hisbillsHistoryService.findHisDetailByParam(searchNotUniqueObjMap);
//                    hisDetailService.findHisDetailByParam(searchNotUniqueObjMap);

                    //三方端重复记录
                    List<HashMap<Object, Object>> list2 = bankbillsHistoryService.findBankbillHistoryByParam(searchNotUniqueObjMap);


                    //循环两个list 如果his端有多的 放入his单边list 能对上的放入true关联集合 最后航端多的银行端单边
                    //最后处理的时候 his单边和银行单边都有的按一对一 变成差异，只有一边有的就是单边
                    List<HashMap<Object, Object>> hisUnilateralList = new ArrayList<>();
                    for (int j = 0; j < list.size(); j++) {
                        HashMap<Object, Object> hisDetailMap = list.get(j);
                        String amount = StringDUtil.changeNullToEmpty(hisDetailMap.get("AMOUNT"));
                        //可能字段不匹配，
                        String hJylx = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADINGSTATUS"));

                        String tradeTime = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADETIME"));

                        String payType = StringDUtil.changeNullToEmpty(hisDetailMap.get("PAYTYPE"));

                        String hisopernum = StringDUtil.changeNullToEmpty(hisDetailMap.get("HISOPERCODE"));

                        String bizType = StringDUtil.changeNullToEmpty(hisDetailMap.get("BIZTYPE"));
                        String patientid = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTID"));
                        String patientname = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTNAME"));

                        Iterator<HashMap<Object, Object>> iterator = list2.iterator();

                        boolean isDel = false;
                        while (iterator.hasNext()) {
                            HashMap<Object, Object> bankDetailMap = iterator.next();
                            String i_jyje = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYJE"));
                            String i_jyrq = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYRQ"));
                            String i_jysj = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYSJ"));
                            String i_jyqd = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYQD"));
                            String i_jylx = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYLX"));
                            String i_ddh = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_DDH"));

                            if (new BigDecimal(amount).compareTo(new BigDecimal(i_jyje)) == 0) {

                                HashMap<Object, Object> addMap = new HashMap<>();
                                addMap.put("jysj", i_jyrq + " " + i_jysj);
                                addMap.put("i_ddh", i_ddh);
                                addMap.put("i_jyje", i_jyje);
                                addMap.put("i_jylx", i_jylx);
                                addMap.put("i_jyqd", i_jyqd);
                                addMap.put("tradeTime", tradeTime);


                                addMap.put("tranID", tranid);
                                addMap.put("BizType", bizType);

                                addMap.put("h_jylx", hJylx);
                                addMap.put("amount", amount);
                                addMap.put("payType", payType);
                                addMap.put("hisOperNum", hisopernum);
                                addMap.put("patientid", patientid);
                                addMap.put("patientname", patientname);
                                addMap.put("sort_date", tradeTime);
                                addMap.put("trade_date", trade_date);
                                addMap.put("err_type", "");
                                addMap.put("check_result", "0");
                                addMap.put("is_active", "1");
                                addMap.put("join_id", "");
                                trueList.add(addMap);

                                isDel = true;
                                iterator.remove();
                                break;
                            }
                        }

                        // 如果没有，说明循环了一轮还是没有相同的，那就先添加到hisUnilateralList
                        if (!isDel) {
                            hisUnilateralList.add(hisDetailMap);
                        }
                    }

                    //处理两边多的。
                    Iterator<HashMap<Object, Object>> hisIterator = hisUnilateralList.iterator();
                    Iterator<HashMap<Object, Object>> bankIterator = list2.iterator();

                    int errNum = 0;

                    //如果两边都有 取出数据设置差异
                    if (hisIterator.hasNext() && bankIterator.hasNext()) {
                        HashMap<Object, Object> hisDetailMap = hisIterator.next();
                        HashMap<Object, Object> bankDetailMap = bankIterator.next();

                        String amount = StringDUtil.changeNullToEmpty(hisDetailMap.get("AMOUNT"));
//                        String payMethod = StringDUtil.changeNullToEmpty(hisDetailMap.get("PAYMETHOD"));
                        String tradeTime = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADETIME"));
                        String payType = StringDUtil.changeNullToEmpty(hisDetailMap.get("PAYTYPE"));
                        String hisopernum = StringDUtil.changeNullToEmpty(hisDetailMap.get("HISOPERCODE"));
                        String hJylx = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADINGSTATUS"));

                        String bizType = StringDUtil.changeNullToEmpty(hisDetailMap.get("BIZTYPE"));
                        String patientid = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTID"));
                        String patientname = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTNAME"));

                        String i_jyje = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYJE"));
                        String i_jyrq = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYRQ"));
                        String i_jysj = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYSJ"));
                        String i_jyqd = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYQD"));
                        String i_jylx = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYLX"));
                        String i_ddh = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_DDH"));

                        errNum++;
                        String join_id = tranid + "err" + errNum;

                        HashMap<Object, Object> addMap = new HashMap<>();
                        addMap.put("jysj", i_jyrq + " " + i_jysj);
                        addMap.put("i_ddh", tranid);
                        addMap.put("i_jyje", i_jyje);
                        addMap.put("i_jylx", h_jylx);
                        addMap.put("i_jyqd", i_jyqd);
                        addMap.put("tradeTime", tradeTime);


                        addMap.put("tranID", tranid);
                        addMap.put("BizType", bizType);
                        addMap.put("h_jylx", hJylx);
                        addMap.put("amount", amount);
                        addMap.put("payType", payType);
                        addMap.put("hisOperNum", hisopernum);
                        addMap.put("patientid", patientid);
                        addMap.put("patientname", patientname);
                        addMap.put("sort_date", tradeTime);
                        addMap.put("trade_date", trade_date);
                        addMap.put("err_type", "3");
                        addMap.put("check_result", "1");
                        addMap.put("is_active", "1");
                        addMap.put("join_id", join_id);
                        trueList.add(addMap);

                        HashMap<Object, Object> addUnilateralMap = new HashMap<>();
                        addUnilateralMap.put("id", UUIDGenerator.getUUID());
                        addUnilateralMap.put("payType", payType);
                        addUnilateralMap.put("tranID", tranid);
                        addUnilateralMap.put("h_jylx", h_jylx);
                        addUnilateralMap.put("hisOperNum", hisopernum);
                        addUnilateralMap.put("amount", amount);
                        addUnilateralMap.put("i_jyje", i_jyje);
                        addUnilateralMap.put("i_jylx", h_jylx);
                        addUnilateralMap.put("i_ddh", tranid);
                        addUnilateralMap.put("i_jyqd", i_jyqd);
                        addUnilateralMap.put("tradeTime", tradeTime);
                        addUnilateralMap.put("trade_date", trade_date);
                        addUnilateralMap.put("is_manager", "0");
                        addUnilateralMap.put("manager_time", "");
                        addUnilateralMap.put("err_type", "3");
                        addUnilateralMap.put("remark", "");
//                        addUnilateralMap.put("payMethod", payMethod);
                        addUnilateralMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("join_id", join_id);

                        //将记录添加到单边账表
                        unilateralService.insertUnilateral(addUnilateralMap);

                    } else if (hisIterator.hasNext()) {

                        HashMap<Object, Object> hisDetailMap = hisIterator.next();

                        String amount = StringDUtil.changeNullToEmpty(hisDetailMap.get("AMOUNT"));
//                        String payMethod = StringDUtil.changeNullToEmpty(hisDetailMap.get("PAYMETHOD"));
                        String tradeTime = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADETIME"));
                        String payType = StringDUtil.changeNullToEmpty(hisDetailMap.get("PAYTYPE"));
                        String hisopernum = StringDUtil.changeNullToEmpty(hisDetailMap.get("HISOPERCODE"));
                        String hJylx = StringDUtil.changeNullToEmpty(hisDetailMap.get("TRADINGSTATUS"));

                        String bizType = StringDUtil.changeNullToEmpty(hisDetailMap.get("BIZTYPE"));
                        String patientid = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTID"));
                        String patientname = StringDUtil.changeNullToEmpty(hisDetailMap.get("PATIENTNAME"));


                        errNum++;
                        String join_id = tranid + "err" + errNum;

                        HashMap<Object, Object> addMap = new HashMap<>();
                        addMap.put("jysj", "");
                        addMap.put("i_ddh", "");
                        addMap.put("i_jyje", "");
                        addMap.put("i_jylx", "");
                        addMap.put("i_jyqd", "");
                        addMap.put("tradeTime", tradeTime);


                        addMap.put("tranID", tranid);
                        addMap.put("BizType", bizType);
                        addMap.put("h_jylx", hJylx);
                        addMap.put("amount", amount);
                        addMap.put("payType", payType);
                        addMap.put("hisOperNum", hisopernum);
                        addMap.put("patientid", patientid);
                        addMap.put("patientname", patientname);
                        addMap.put("sort_date", tradeTime);
                        addMap.put("trade_date", trade_date);
                        addMap.put("err_type", "1");
                        addMap.put("check_result", "1");
                        addMap.put("is_active", "1");
                        addMap.put("join_id", join_id);
                        trueList.add(addMap);


                        HashMap<Object, Object> addUnilateralMap = new HashMap<>();
                        addUnilateralMap.put("id", UUIDGenerator.getUUID());
                        addUnilateralMap.put("payType", payType);
                        addUnilateralMap.put("tranID", tranid);
                        addUnilateralMap.put("h_jylx", h_jylx);
                        addUnilateralMap.put("hisOperNum", hisopernum);
                        addUnilateralMap.put("amount", amount);
                        addUnilateralMap.put("i_jyje", "");
                        addUnilateralMap.put("i_jylx", "");
                        addUnilateralMap.put("i_ddh", "");
                        addUnilateralMap.put("i_jyqd", "");
                        addUnilateralMap.put("tradeTime", tradeTime);
                        addUnilateralMap.put("trade_date", trade_date);
                        addUnilateralMap.put("is_manager", "0");
                        addUnilateralMap.put("manager_time", "");
                        addUnilateralMap.put("err_type", "1");
                        addUnilateralMap.put("remark", "");
                        addUnilateralMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("join_id", join_id);

                        //将记录添加到单边账表
                        unilateralService.insertUnilateral(addUnilateralMap);

                    } else if (bankIterator.hasNext()) {
                        HashMap<Object, Object> bankDetailMap = bankIterator.next();

                        String i_jyje = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYJE"));
                        String i_jyrq = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYRQ"));
                        String i_jysj = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYSJ"));
                        String i_jyqd = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYQD"));
                        String i_jylx = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_JYLX"));
                        String i_ddh = StringDUtil.changeNullToEmpty(bankDetailMap.get("I_DDH"));

                        errNum++;
                        String join_id = tranid + "err" + errNum;

                        HashMap<Object, Object> addMap = new HashMap<>();

                        addMap.put("jysj", i_jyrq + " " + i_jysj);
                        addMap.put("i_ddh", tranid);
                        addMap.put("i_jyje", i_jyje);
                        addMap.put("i_jylx", h_jylx);
                        addMap.put("i_jyqd", i_jyqd);
                        addMap.put("tradeTime", i_jysj);


                        addMap.put("tranID", "");
                        addMap.put("BizType", "");
                        addMap.put("h_jylx", "");
                        addMap.put("amount", "");
                        addMap.put("payType", "");
                        addMap.put("hisOperNum", "");
                        addMap.put("patientid", "");
                        addMap.put("patientname", "");
                        addMap.put("sort_date", "");
                        addMap.put("trade_date", trade_date);
                        addMap.put("err_type", "3");
                        addMap.put("check_result", "1");
                        addMap.put("is_active", "1");
                        addMap.put("join_id", join_id);
                        trueList.add(addMap);

                        HashMap<Object, Object> addUnilateralMap = new HashMap<>();
                        addUnilateralMap.put("id", UUIDGenerator.getUUID());
                        addUnilateralMap.put("payType", "");
                        addUnilateralMap.put("tranID", "");
                        addUnilateralMap.put("h_jylx", "");
                        addUnilateralMap.put("hisOperNum", "");
                        addUnilateralMap.put("amount", "");
                        addUnilateralMap.put("i_jyje", i_jyje);
                        addUnilateralMap.put("i_jylx", i_jylx);
                        addUnilateralMap.put("i_ddh", i_ddh);
                        addUnilateralMap.put("i_jyqd", i_jyqd);
                        addUnilateralMap.put("tradeTime", i_jysj);
                        addUnilateralMap.put("trade_date", trade_date);
                        addUnilateralMap.put("is_manager", "0");
                        addUnilateralMap.put("manager_time", "");
                        addUnilateralMap.put("err_type", "2");
                        addUnilateralMap.put("remark", "");
                        addUnilateralMap.put("create_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
                        addUnilateralMap.put("join_id", join_id);

                        //将记录添加到单边账表
                        unilateralService.insertUnilateral(addUnilateralMap);
                    }
                }

                //将记录添加到数据库
                if (trueList.size() > 0) {
                    transactionDetailService.addNotUniqueData(trueList);
                }

                //!!!!
                managerReconciliation(transactionDetailService, searchMap);
            } else {
                errCode = "999";
                errMsg = "生成对账信息失败，原因：his或三方有记录没有获取到。";
            }
        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "生成对账信息失败，原因：" + e.getMessage();
        }

        try {
            //对账记录表  记录对账是否成功的记录
            String modify_time = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd);
            HashMap<Object, Object> addMap = new HashMap<>();
            addMap.put("trade_date", trade_date);
            addMap.put("merchant_name", "");
            addMap.put("merchant_id", "");
            addMap.put("create_time", modify_time);
            addMap.put("modify_time", modify_time);
            addMap.put("quartz_id", id);
            addMap.put("quartz_name", name);

            managerNum++;
            addMap.put("manager_num", managerNum);

            if ("0".equals(errCode)) {
                addMap.put("status", "1");
            } else {
                addMap.put("status", "0");
            }

            if ("".equals(userName)) {
                addMap.put("user_name", "定时任务对账");
            } else {
                addMap.put("user_name", userName);
            }

            reconciliationLogService.insertReconciliationLog(addMap);
        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "生成对账记录失败，原因：" + e.getMessage();
        }

        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);
        return responseMap;
    }

    /**
     * @description: 处理对账结果记录
     * @author thuang
     * @date 2021/11/30 16:54
     * @version 1.0
     */
    public static void managerReconciliation(TransactionDetailService transactionDetailService, HashMap<Object, Object> searchMap) throws Exception {

        //生成对账结果记录表
        //查询当天的 三方结算 his结算 三方单边 his单边 处理的历史单边 当日单边账条数
        List<HashMap<Object, Object>> list = transactionDetailService.findHisAndThirdJoinDataByParam(searchMap);

        String trade_date = StringDUtil.changeNullToEmpty(searchMap.get("trade_date"));

        BigDecimal hisTotalMoney = new BigDecimal(0);
        BigDecimal thirdTotalMoney = new BigDecimal(0);
        BigDecimal thirdUnilateralMoney = new BigDecimal(0);
        BigDecimal hisUnilateralMoney = new BigDecimal(0);
        BigDecimal managerHistoryUnilateralMoney = new BigDecimal(0);
        int todayUnilateralNum = 0;

        for (int i = 0; i < list.size(); i++) {
            HashMap<Object, Object> hashMap = list.get(i);
            String i_jyje = StringDUtil.changeNullToEmpty(hashMap.get("I_JYJE"));
            String amount = StringDUtil.changeNullToEmpty(hashMap.get("AMOUNT"));
            String err_type = StringDUtil.changeNullToEmpty(hashMap.get("ERR_TYPE"));
            String check_result = StringDUtil.changeNullToEmpty(hashMap.get("CHECK_RESULT"));

            //如果金额为空 设置为0 方便计算
            if ("".equals(i_jyje)) {
                i_jyje = "0";
            }
            if ("".equals(amount)) {
                amount = "0";
            }

            thirdTotalMoney = thirdTotalMoney.add(new BigDecimal(i_jyje));
            hisTotalMoney = hisTotalMoney.add(new BigDecimal(amount));

            //统计单边账金额
            if ("1".equals(check_result)) {
                //非his单边账  就是三方单边账 和差错账单
                if (!"1".equals(err_type)) {
                    thirdUnilateralMoney = thirdUnilateralMoney.add(new BigDecimal(i_jyje));
                }
                if (!"2".equals(err_type)) {
                    hisUnilateralMoney = hisUnilateralMoney.add(new BigDecimal(amount));
                }
                todayUnilateralNum++;
            }

            //统计核销历史单边账金额
            if ("2".equals(check_result)) {
                thirdUnilateralMoney = thirdUnilateralMoney.add(new BigDecimal(i_jyje));
                hisUnilateralMoney = hisUnilateralMoney.add(new BigDecimal(amount));

                BigDecimal addMoney = new BigDecimal(i_jyje).add(new BigDecimal(amount));
                managerHistoryUnilateralMoney = managerHistoryUnilateralMoney.add(addMoney);
            }
        }

        //对账结果记录表
        ReconciliationResultService reconciliationResultService = GetBeanUtil.getBean(ReconciliationResultServiceImpl.class);

        //如果当天的单边账条数为0 通过。
        String status = "2";
        if (todayUnilateralNum == 0) {
            status = "1";
        }

        HashMap<Object, Object> addMap = new HashMap<>();
        addMap.put("trade_date", trade_date);
        addMap.put("third_money", thirdTotalMoney.toString());
        addMap.put("his_money", hisTotalMoney.toString());
        addMap.put("difference", ""); //分开两个差额不做计算
        addMap.put("third_unilateral", thirdUnilateralMoney.toString());
        addMap.put("his_unilateral", hisUnilateralMoney.toString());
        addMap.put("manager_history_unilateral", managerHistoryUnilateralMoney);
        addMap.put("today_unilateral", ""); //不知道怎么算 暂时没算
        addMap.put("unilateral_num", todayUnilateralNum);
        addMap.put("pending_unilateral", "0");
        addMap.put("pending_unilateral_num", "0");
        addMap.put("status", status);
        addMap.put("area", "");

        reconciliationResultService.insertReconciliationResult(addMap);
    }


}
