package com.saye.hospitalgd.service.impl;

import com.saye.hospitalgd.entity.BankbillHistory;
import com.saye.hospitalgd.mapper.BankbillHistoryMapper;
import com.saye.hospitalgd.mapper.system.DicinfoMapper;
import com.saye.hospitalgd.model.Dicinfo;
import com.saye.hospitalgd.service.BankbillHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * (BankbillHistory)表服务实现类
 *
 * @author makejava
 * @since 2021-09-09 15:35:54
 */
@Service
@Transactional
public class BankbillHistoryServiceImpl implements BankbillHistoryService {
    @Resource
    private BankbillHistoryMapper bankbillHistoryMapper;

    @Autowired
    private DicinfoMapper dicinfoMapper;

    @Override
    public List<HashMap<Object,Object>> findBankbillHistoryById(HashMap<Object,Object> map) {
        return this.bankbillHistoryMapper.findBankbillHistoryById(map);
    }

    @Override
    public  List<HashMap<Object, Object>> findBankbillHistoryList(HashMap<Object,Object> map) {
        return this.bankbillHistoryMapper.findBankbillHistoryList(map);
    }

    @Override
    public void insertBankBillOriginal(List<BankbillHistory> list, String trade_date, String bill_table_name) throws Exception {
        //删除之前的
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        deleteMap.put("bill_table_name",bill_table_name);
        this.bankbillHistoryMapper.deleteBankbillOriginalByParam(deleteMap);

        int listNum=0;
        List<BankbillHistory> addList =new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BankbillHistory bankbillHistory = list.get(i);

            listNum++;
            addList.add(bankbillHistory);
            if(listNum>=1000){
                this.bankbillHistoryMapper.insertBankbillOriginal(addList);
                listNum=0;
                addList=new ArrayList<>();
            }
        }
        if(listNum>0){
            this.bankbillHistoryMapper.insertBankbillOriginal(addList);
        }
    }

    @Override
    public void insertAllBankHistory(List<BankbillHistory> list,String trade_date,String bill_table_name)  throws Exception{

        //删除之前的
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        deleteMap.put("bill_table_name",bill_table_name);
        this.bankbillHistoryMapper.deleteBankbillHistoryByParam(deleteMap);

        //支付方式
        List<Dicinfo> payMethodList = dicinfoMapper.findDicinfoTreeNodeList("THIRD_PAY");
        HashMap<String,String> payMethodMap=new HashMap<>();
        for (int i1 = 0; i1 < payMethodList.size(); i1++) {
            Dicinfo dicinfo = payMethodList.get(i1);
            String dicname = dicinfo.getDicname();
            //微信 支付宝 现金 特殊处理下
            if (dicname.contains("微信")){
                payMethodMap.put("微信",dicinfo.getDicvalue());
                payMethodMap.put("微信支付",dicinfo.getDicvalue());
            }else if(dicname.contains("支付宝")){
                payMethodMap.put("支付宝",dicinfo.getDicvalue());
                payMethodMap.put("支付宝支付",dicinfo.getDicvalue());
            }else if (dicname.contains("云闪付")){
                payMethodMap.put("云闪付",dicinfo.getDicvalue());
                payMethodMap.put("云闪付支付",dicinfo.getDicvalue());
            }

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

        int listNum=0;
        List<BankbillHistory> addList =new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BankbillHistory bankbillHistory = list.get(i);

            String billTableName = bankbillHistory.getBillTableName();
            String cZffs = bankbillHistory.getCZffs();
            String zffs = payMethodMap.get(cZffs);
            //修改支付方式变为字典表中对应的值
            if ("招商商户银联卡对账单".equals(billTableName)){
                zffs = payMethodMap.get("银行卡支付");
                bankbillHistory.setCZffs(zffs);
            }else if ("浦发商户银联卡对账单".equals(billTableName)) {
                zffs = payMethodMap.get("银行卡支付");
                bankbillHistory.setCZffs(zffs);
            }
            else if ("招商商户对账单".equals(billTableName)){
                zffs = payMethodMap.get("掌医支付");
                bankbillHistory.setCZffs(zffs);
            }else if ("浦发商户对账单".equals(billTableName)){
                zffs = payMethodMap.get("掌医支付");
                bankbillHistory.setCZffs(zffs);
            }
            else{
                if (zffs==null){
                    zffs = payMethodMap.get("其他支付");
                    bankbillHistory.setCZffs(zffs);
                }else {
                    bankbillHistory.setCZffs(zffs);
                }
            }

            //修改交易类型 消费 消费撤消
            String cJyje = bankbillHistory.getCJyje();
            /*String cJylx = bankbillHistory.getCJylx();
            if("消费".equals(cJylx)){
                bankbillHistory.setCJylx("1");
            }else if ("消费撤消".equals(cJylx) ||"联机退货".equals(cJylx)){
                bankbillHistory.setCJylx("2");
            }*/

            BigDecimal bigDecimal = new BigDecimal(cJyje);
            if (bigDecimal.compareTo(new BigDecimal(0))>0){
                bankbillHistory.setCJylx("1");
            }else {
                bankbillHistory.setCJylx("2");
            }

            listNum++;
            addList.add(bankbillHistory);
            if(listNum>=1000){
                this.bankbillHistoryMapper.insertAllBankHistory(addList);
                listNum=0;
                addList=new ArrayList<>();
            }
        }
        if(listNum>0){
            this.bankbillHistoryMapper.insertAllBankHistory(addList);
        }
    }

    @Override
    public List<HashMap<Object, Object>> findCzffs() throws Exception {
        return this.bankbillHistoryMapper.findCzffs();
    }

    @Override
    public void deleteTempTable() throws Exception {
        bankbillHistoryMapper.deleteTempTable();
    }

    @Override
    public void findBankbillHistoryToTemp(HashMap<Object, Object> map) throws Exception {
        bankbillHistoryMapper.findBankbillHistoryToTemp(map);
    }

    @Override
    public List<HashMap<Object, Object>> findBankbillHistoryByParam(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findBankbillHistoryByParam(map);
    }

    @Override
    public List<HashMap<Object, Object>> findBankbillHistoryByTimeAndPlatformTransId(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findBankbillHistoryByTimeAndPlatformTransId(map);
    }

    @Override
    public List<HashMap<Object, Object>> findBankBillNumByTime(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findBankBillNumByTime(map);
    }

    @Override
    public List<HashMap<Object, Object>> findBankBillMoneyByTime(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findBankBillMoneyByTime(map);
    }

    @Override
    public List<HashMap<Object, Object>> findbankbillHistoryCount(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findbankbillHistoryCount(map);
    }

    @Override
    public List<HashMap<Object, Object>> findbankbillHistorySum(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findbankbillHistorySum(map);
    }

    @Override
    public List<HashMap<Object, Object>> findBankbillCountData(HashMap<Object, Object> map) throws Exception {
        return bankbillHistoryMapper.findBankbillCountData(map);
    }


}
