package com.saye.hospitalgd.service.impl;

import com.saye.hospitalgd.mapper.HisDetailMapper;
import com.saye.hospitalgd.service.HisDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author thuang
 * @version 1.0
 * @description: TODO
 * @date 2021/9/28 13:10
 */
@Service
@Transactional
public class HisDetailServiceImpl implements HisDetailService {

    @Autowired
    private HisDetailMapper hisDetailMapper;

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

    @Override
    public long findHisDetailCount(HashMap<Object, Object> map) throws Exception {
        return hisDetailMapper.findHisDetailCount(map);
    }

    /**
     * @description: 中联的原始记录表添加
     * @author thuang
     * @date 2021/11/9 13:47
     * @version 1.0
     */
    @Override
    public void insertHisBillOriginal(List<HashMap<Object, Object>> list, String trade_date, String his_wsdl_id) throws Exception {
        //删除以前的
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        hisDetailMapper.deleteHisBillOriginalByParam(deleteMap);

        int listNum=0;
        List<HashMap<Object, Object>> addList =new ArrayList<>();
        for (HashMap<Object, Object> hashMap : list) {
            listNum++;
            hashMap.put("his_wsdl_id",his_wsdl_id);
            addList.add(hashMap);
            if (listNum >= 1000) {
                this.hisDetailMapper.insertAllHisBillOriginal(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if(listNum>0){
            this.hisDetailMapper.insertAllHisBillOriginal(addList);
        }
    }

    /**
     * @description: 卫宁的原始记录表添加
     * @author thuang
     * @date 2021/11/9 13:47
     * @version 1.0
     */
    @Override
    public void insertHisBillOriginalWN(List<HashMap<Object, Object>> list, String trade_date, String his_wsdl_id) throws Exception {
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        hisDetailMapper.deleteHisBillOriginalWNByParam(deleteMap);

        int listNum=0;
        List<HashMap<Object, Object>> addList =new ArrayList<>();
        for (HashMap<Object, Object> hashMap : list) {
            listNum++;
            hashMap.put("his_wsdl_id",his_wsdl_id);
            addList.add(hashMap);
            if (listNum >= 1000) {
                this.hisDetailMapper.insertAllHisBillOriginalWN(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if(listNum>0){
            this.hisDetailMapper.insertAllHisBillOriginalWN(addList);
        }
    }

    @Override
    public void insertHisBillOriginalHY(List<HashMap<Object, Object>> list, String trade_date, String his_wsdl_id) throws Exception {
        //删除以前的
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        hisDetailMapper.deleteHisBillOriginalHYByParam(deleteMap);

        int listNum=0;
        List<HashMap<Object, Object>> addList =new ArrayList<>();
        for (HashMap<Object, Object> hashMap : list) {
            listNum++;
            hashMap.put("his_wsdl_id",his_wsdl_id);
            addList.add(hashMap);
            if (listNum >= 1000) {
                this.hisDetailMapper.insertAllHisBillOriginalHY(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if(listNum>0){
            this.hisDetailMapper.insertAllHisBillOriginalHY(addList);
        }
    }

    @Override
    public void insertHisBillList(List<HashMap<Object, Object>> list,String trade_date,String his_wsdl_id) throws Exception {

        //删除以前的
        HashMap<Object,Object> deleteMap=new HashMap<>();
        deleteMap.put("trade_date",trade_date);
        hisDetailMapper.deleteHisBillByParam(deleteMap);

        //将数据同样全部添加到历史表
        int listNum=0;
        List<HashMap<Object, Object>> addList =new ArrayList<>();
        for (HashMap<Object, Object> hashMap : list) {
            listNum++;
            hashMap.put("his_wsdl_id",his_wsdl_id);
            addList.add(hashMap);
            if (listNum >= 1000) {
                this.hisDetailMapper.insertAllHisBillHistory(addList);
                listNum = 0;
                addList = new ArrayList<>();
            }
        }
        if(listNum>0){
            this.hisDetailMapper.insertAllHisBillHistory(addList);
        }


    }

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

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

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

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

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

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

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


}
