package com.saye.hospitalgd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.saye.hospitalgd.entity.BankbillsHistory;
import com.saye.hospitalgd.mapper.BankbillsHistoryMapper;
import com.saye.hospitalgd.mapper.system.DicinfoMapper;
import com.saye.hospitalgd.model.Dicinfo;
import com.saye.hospitalgd.service.BankbillsHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2022/12/6
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
public class BankbillsHistoryServiceImpl implements BankbillsHistoryService {
    @Resource
    BankbillsHistoryMapper bankbillsHistoryMapper;

    @Resource
    DicinfoMapper dicinfoMapper;

    @Override
    public List<HashMap<Object, Object>> findBankbillHistoryByParam(HashMap<Object, Object> searchNotUniqueObjMap) {
        return bankbillsHistoryMapper.findBankbillHistoryByParam(searchNotUniqueObjMap);
    }

    @Override
    public List<HashMap<Object, Object>> findBankBillsByDate(HashMap<Object, Object> searchMap) {
        return bankbillsHistoryMapper.findBankBillsByDate(searchMap);
    }

    @Override
    public void insertOriginal(List<BankbillsHistory> bankbillsHistories, String trade_date, String bill_table_name) {
        // 删除之前的
        HashMap<Object, Object> deleteMap = new HashMap<>();
        deleteMap.put("trade_date", trade_date);
//        deleteMap.put("bill_table_name", bill_table_name);
        bankbillsHistoryMapper.deleteOriginalByParam(deleteMap);

        int listNum = 0;
        List<BankbillsHistory> addList = new ArrayList<>();
        for (int i = 0; i < bankbillsHistories.size(); i++) {
            BankbillsHistory bankbillsHistory = bankbillsHistories.get(i);
            listNum++;
            addList.add(bankbillsHistory);
            if (listNum >= 1000) {
                bankbillsHistoryMapper.insertOriginal(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if (listNum > 0) {
            bankbillsHistoryMapper.insertOriginal(addList);
        }
    }

    @Override
    public void insertHistory(List<BankbillsHistory> bankbillsHistories, String trade_date, String bill_table_name) {
        // 删除之前的
        HashMap<Object, Object> deleteMap = new HashMap<>();
        deleteMap.put("trade_date", trade_date);
//        deleteMap.put("bill_table_name", bill_table_name);
        bankbillsHistoryMapper.deleteHistoryByParam(deleteMap);

        // 交易渠道
        List<Dicinfo> tradeChannelList = dicinfoMapper.findDicinfoTreeNodeList("THIRD_PAY");
        HashMap<String, String> tradeChannelMap = new HashMap<>();
        for (int i1 = 0; i1 < tradeChannelList.size(); i1++) {
            Dicinfo dicinfo = tradeChannelList.get(i1);
            String dicname = dicinfo.getDicname();
            // 微信 支付宝 现金 特殊处理下
            if (dicname.contains("微信")) {
                tradeChannelMap.put("微信", dicinfo.getDicvalue());
                tradeChannelMap.put("微信支付", dicinfo.getDicvalue());
            } else if (dicname.contains("支付宝")) {
                tradeChannelMap.put("支付宝", dicinfo.getDicvalue());
                tradeChannelMap.put("支付宝支付", dicinfo.getDicvalue());
            } else if (dicname.contains("刷卡")) {
                tradeChannelMap.put("刷卡付", dicinfo.getDicvalue());
                tradeChannelMap.put("刷卡支付", dicinfo.getDicvalue());
            }

            tradeChannelMap.put(dicinfo.getDicname(), dicinfo.getDicvalue());
        }

        int listNum = 0;

        List<BankbillsHistory> addList = new ArrayList<>();
        for (int i = 0; i < bankbillsHistories.size(); i++) {
            BankbillsHistory bankbillsHistory = bankbillsHistories.get(i);


//            String billTableName = bankbillHistory.getBillTableName();
//            交易方式
//            String iJyfs = bankbillsHistory.getIJyfs();
//            交易渠道
            String iJyqd = bankbillsHistory.getIJyqd();
//            交易类型
            String iJylx = bankbillsHistory.getIJylx();


            if (!iJyqd.equals("")) {
                bankbillsHistory.setIJyqd(tradeChannelMap.get(iJyqd));
            } else {

                bankbillsHistory.setIJyqd("其他交易渠道");
            }

            if (!iJylx.equals("")) {
                if (iJylx.equals("退费")) {
                    bankbillsHistory.setIJylx("2");
                } else {
                    bankbillsHistory.setIJylx("1");
                }
            }

            listNum++;
            addList.add(bankbillsHistory);
            if (listNum >= 1000) {
                this.bankbillsHistoryMapper.insertHistory(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if (listNum > 0) {
            this.bankbillsHistoryMapper.insertHistory(addList);
        }
    }


    @Override
    public int saveOriginalData(List<HashMap<Object, Object>> savList, String trade_date) {
        // 删除之前的
        HashMap<Object, Object> deleteMap = new HashMap<>();
        deleteMap.put("trade_date", trade_date);
//        deleteMap.put("bill_table_name", bill_table_name);
        bankbillsHistoryMapper.deleteOriginalByParam(deleteMap);

        return bankbillsHistoryMapper.saveOriginalData(savList);
    }


    @Override
    public int saveHistoryData(List<HashMap<Object, Object>> savList, String trade_date) {
        // 删除之前的
        HashMap<Object, Object> deleteMap = new HashMap<>();
        deleteMap.put("trade_date", trade_date);
//        deleteMap.put("bill_table_name", bill_table_name);
        bankbillsHistoryMapper.deleteHistoryByParam(deleteMap);
        // 交易渠道
        List<Dicinfo> tradeChannelList = dicinfoMapper.findDicinfoTreeNodeList("THIRD_PAY");
        HashMap<String, String> tradeChannelMap = new HashMap<>();
        for (int i1 = 0; i1 < tradeChannelList.size(); i1++) {
            Dicinfo dicinfo = tradeChannelList.get(i1);
            String dicname = dicinfo.getDicname();
            // 微信 支付宝 现金 特殊处理下
            if (dicname.contains("微信")) {
                tradeChannelMap.put("微信", dicinfo.getDicvalue());
                tradeChannelMap.put("微信支付", dicinfo.getDicvalue());
            } else if (dicname.contains("支付宝")) {
                tradeChannelMap.put("支付宝", dicinfo.getDicvalue());
                tradeChannelMap.put("支付宝支付", dicinfo.getDicvalue());
            } else if (dicname.contains("刷卡")) {
                tradeChannelMap.put("刷卡付", dicinfo.getDicvalue());
                tradeChannelMap.put("刷卡支付", dicinfo.getDicvalue());
            }

            tradeChannelMap.put(dicinfo.getDicname(), dicinfo.getDicvalue());
        }
        // 遍历数据，将数据保存为易于对比的数据,主要是 支付状态，支付方式的修改
        ArrayList<HashMap<Object, Object>> maps = new ArrayList<>();

        for (HashMap<Object, Object> map : savList) {
            if (Double.parseDouble(map.get("i_jyje").toString()) > 0) {
                map.put("i_jylx", "1");
            } else {
                map.put("i_jylx", "2");
            }
            if (!map.get("i_fxk").toString().equals("")) {
                if (StrUtil.isNotEmpty(tradeChannelMap.get(map.get("i_fxk").toString()))) {

                    map.put("i_fxk", tradeChannelMap.get(map.get("i_fxk").toString()));
                } else {
                    map.put("i_fxk", "1_4");
                }

            }

            maps.add(map);
        }
        return bankbillsHistoryMapper.saveHistoryData(maps);
    }
}
