package com.saye.hospitalgd.service.impl;

import com.saye.hospitalgd.entity.HisbillsHistory;
import com.saye.hospitalgd.mapper.HisbillsHistoryMapper;
import com.saye.hospitalgd.mapper.system.DicinfoMapper;
import com.saye.hospitalgd.model.Dicinfo;
import com.saye.hospitalgd.service.HisbillsHistoryService;
import lombok.extern.slf4j.Slf4j;
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/2
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
@Slf4j
public class HisbillsHistoryServiceImpl implements HisbillsHistoryService {

    @Resource
    private HisbillsHistoryMapper hisbillsHistoryMapper;

    @Resource
    private DicinfoMapper dicinfoMapper;

    @Override
    public void saveHisHistoryData(HashMap<Object, Object> hisbillsOriginal) {

        // 支付方式
        List<Dicinfo> payTypeList = dicinfoMapper.findDicinfoTreeNodeList("PAY_TYPE");
        HashMap<String, String> payTypeMap = new HashMap<>();
        for (int i1 = 0; i1 < payTypeList.size(); i1++) {
            Dicinfo dicinfo = payTypeList.get(i1);
            String dicname = dicinfo.getDicname();
            // 微信 支付宝 现金 特殊处理下
            if (dicname.contains("聚合")) {
                payTypeMap.put("聚合", dicinfo.getDicvalue());
                payTypeMap.put("聚合支付", dicinfo.getDicvalue());
            } else if (dicname.contains("银行卡")) {
                payTypeMap.put("银行卡", dicinfo.getDicvalue());
                payTypeMap.put("银行卡支付", dicinfo.getDicvalue());
            }
            payTypeMap.put(dicinfo.getDicname(), dicinfo.getDicvalue());
        }

        if (hisbillsOriginal.get("zfzt").equals("支付")) {// 更改支付状态为方便对比的数据
            hisbillsOriginal.put("zfzt", "1");
        } else {
            hisbillsOriginal.put("zfzt", "2");
        }

        String zffs = (String) hisbillsOriginal.get("zffs");

        if (zffs.contains("聚合")) {// 更改支付方式方便数据对比
            hisbillsOriginal.put("zffs", payTypeMap.get("聚合支付"));
        } else if (zffs.contains("银行卡")) {
            hisbillsOriginal.put("zffs", payTypeMap.get("银行卡支付"));
        } else {
            hisbillsOriginal.put("zffs", payTypeMap.get("其他"));
        }
        hisbillsHistoryMapper.saveHisHistoryData(hisbillsOriginal);
    }

    @Override
    public List<HashMap<Object, Object>> findHisBillsByDate(HashMap<Object, Object> searchMap) {

        return hisbillsHistoryMapper.findHisBillsByDate(searchMap);

    }

    @Override
    public List<HashMap<Object, Object>> findHisDetailByParam(HashMap<Object, Object> searchNotUniqueObjMap) throws Exception {
        return hisbillsHistoryMapper.findHisDetailByParam(searchNotUniqueObjMap);
    }


    @Override
    public void saveHisbillsOriginalData(List<HisbillsHistory> hisbillsHistories, String trade_date) {
        HashMap<Object, Object> delMap = new HashMap<>();
        delMap.put("trade_date", trade_date);

        ArrayList<HisbillsHistory> histories = new ArrayList<>();

        for (HisbillsHistory hisbillsHistory : hisbillsHistories) {

            hisbillsHistory.setTrade_date(trade_date);
            histories.add(hisbillsHistory);
        }

        hisbillsHistoryMapper.deleteOriginalDataByDate(delMap);// 先删除当天的旧的。再获取新的数据

        hisbillsHistoryMapper.insertHisBillsOriginalList(histories);// 原封不动的保存原始数据
    }

    @Override
    public void saveHisbillsHistoryData(List<HisbillsHistory> hisbillsHistories, String trade_date) {
        HashMap<Object, Object> delMap = new HashMap<>();

        delMap.put("trade_date", trade_date);

        hisbillsHistoryMapper.deleteHistoryDataByDate(delMap);// 先删除当天的旧的。再获取新的数据

        ArrayList<HisbillsHistory> histories = new ArrayList<>();

        for (HisbillsHistory hisbillsHistory : hisbillsHistories) {
            if (!hisbillsHistory.getAmount().equals("0")) {
                String tradeTime = hisbillsHistory.getTradeTime();
                String[] split = tradeTime.split(" ");
                hisbillsHistory.setTrade_date(split[0]);
                if (hisbillsHistory.getPayType().equals("0")) {
                    hisbillsHistory.setPayType("1");
                }
                if (hisbillsHistory.getPayType().equals("-1")) {
                    hisbillsHistory.setPayType("5");
                }
                histories.add(hisbillsHistory);
            }
        }
        hisbillsHistoryMapper.insertHisbillsHistoryList(histories);

    }

}
