package com.dxhy.erp.service.impl;

import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.erp.dao.VoucherRecordDao;
import com.dxhy.erp.dao.VoucherRecordItemDao;
import com.dxhy.erp.entity.VoucherRecord;
import com.dxhy.erp.entity.VoucherRecordItem;
import com.dxhy.erp.service.VoucherRecordItemService;
import com.dxhy.erp.service.VoucherRecordService;
import com.dxhy.erp.utils.DistributedKeyMaker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wangzhikun
 * @Date 2023/3/23 2023/3/23
 */
@Service
public class VoucherRecordServiceImpl extends MpBaseServiceImpl<VoucherRecordDao, VoucherRecord> implements VoucherRecordService {
    @Resource
    private VoucherRecordDao voucherRecordDao;

    @Resource
    private VoucherRecordItemDao voucherRecordItemDao;
    @Resource
    private VoucherRecordItemService voucherRecordItemService;
    @Override
    /**
     * 接口推送的凭证接收数据逻辑
     * 1.先根据推送数据的公司代码+会计年度+期间分组  然后查询出符合条件的数据取出主键
     * 然后比对推送的数据是否有相同的，如果有相同的删除重新新增
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addVoucherRecords(List<VoucherRecord> voucherRecords) {
        boolean b = false;
        if(!CollectionUtils.isEmpty(voucherRecords)){
            //新推送的记录。直接入库
            List<VoucherRecord> newRecordList = new ArrayList<>();
            //重复的记录，删除之后重新入库
            List<VoucherRecord> repeatRecordList = new ArrayList<>();
            Map<String, List<VoucherRecord>> collect = voucherRecords.stream().collect(Collectors.groupingBy(v -> v.getBukrs() + v.getGjahr() + v.getMonat()));
            Set<String> key = collect.keySet();
            for(String s:key){
                //根据公司代码+年份+月份查出主键
                List<String> repeatList =voucherRecordDao.selectRepeat(s);
                List<VoucherRecord> voucherRecords1 = collect.get(s);
                Map<String, List<VoucherRecord>> collect1 = voucherRecords1.stream().collect(Collectors.groupingBy(v -> v.getBukrs() + v.getBelnr() + v.getGjahr()));
                Set<String> belnrs = collect1.keySet();
                for(String belnr:belnrs){
                    if(repeatList.contains(belnr)){
                        repeatRecordList.addAll(collect1.get(belnr));
                    }else {
                        newRecordList.addAll(collect1.get(belnr));
                    }
                }
            }
            List<VoucherRecordItem> addItems = new ArrayList<>();
            for(VoucherRecord voucherRecordNew:newRecordList){
                String s = DistributedKeyMaker.generateShotKey();
                voucherRecordNew.setId(s);
                for(VoucherRecordItem voucherRecordItem:voucherRecordNew.getZITEM()){
                    voucherRecordItem.setRelationId(s);
                }
                addItems.addAll(voucherRecordNew.getZITEM());
            }
            List<VoucherRecord> deleteRecord = new ArrayList<>();
            for(VoucherRecord voucherRecordRepeat:repeatRecordList){
                VoucherRecord deleteVoucher = new VoucherRecord();
                deleteVoucher.setBukrs(voucherRecordRepeat.getBukrs());
                deleteVoucher.setBelnr(voucherRecordRepeat.getBelnr());
                deleteVoucher.setGjahr(voucherRecordRepeat.getGjahr());
                String s = DistributedKeyMaker.generateShotKey();
                voucherRecordRepeat.setId(s);
                for(VoucherRecordItem item:voucherRecordRepeat.getZITEM()){
                    item.setRelationId(s);
                }
                addItems.addAll(voucherRecordRepeat.getZITEM());
                deleteRecord.add(deleteVoucher);
            }
            if(!CollectionUtils.isEmpty(deleteRecord)){
                //根据主键查出对应明细数据的关联id,然后根据id删除明细
                List<String> list = new ArrayList<>();
                for(VoucherRecord voucherRecord:deleteRecord){
                    String str = voucherRecord.getBukrs()+voucherRecord.getBelnr()+voucherRecord.getGjahr();
                    list.add(str);
                }
                List<String> ids=voucherRecordDao.selectRepeatByBelnr(list);
                if(!CollectionUtils.isEmpty(ids)){
                    voucherRecordItemDao.deleteByRelationid(ids);
                }
                voucherRecordDao.deleteBatch(deleteRecord);
            }
            this.saveBatch(newRecordList);
            this.saveBatch(repeatRecordList);
             b = voucherRecordItemService.inserBatch(addItems);
        }
        return b;
    }


}
