xuhy
2025-10-15 1f1b5cc1856b5b46eadf8743184c02c1d6529e12
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TErpProcurementServiceImpl.java
@@ -8,8 +8,12 @@
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.AddProcurementDto;
import com.ruoyi.system.export.ProcurementReportClinicExport;
import com.ruoyi.system.export.ProcurementReportPlatformExport;
import com.ruoyi.system.export.ProcurementReportSupplierExport;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.ClinicProcurementReportQuery;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.query.TErpProcurementQuery;
import com.ruoyi.system.service.TErpGoodsService;
@@ -32,6 +36,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
@@ -44,9 +49,12 @@
import java.math.RoundingMode;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -67,10 +75,10 @@
    private TSysCommissionMapper sysCommissionMapper;
    @Resource
    private  TCrmClinicMapper crmClinicMapper;
    private TCrmClinicMapper crmClinicMapper;
    @Resource
    private  TCrmSupplierMapper crmSupplierMapper;
    private TCrmSupplierMapper crmSupplierMapper;
    @Resource
    private SysUserMapper sysUserMapper;
@@ -87,7 +95,20 @@
    @Autowired
    private TErpProcurementGoodsService erpProcurementGoodsService;
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
    @Resource
    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
    @Resource
    private TCrmWarehouseMapper crmWarehouseMapper;
    @Override
    public PageInfo<TErpProcurementVo> pageList(TErpProcurementQuery query, SysUser user) {
@@ -95,38 +116,38 @@
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if(roleType == 4){
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId =crmSupplier.getId();
            supplierClinicId = crmSupplier.getId();
        }
        if(roleType == 5){
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId =crmClinic.getId();
            supplierClinicId = crmClinic.getId();
        }
        String sTime=null;
        String eTime =null;
        if(query.getTime()!=null && !query.getTime().isEmpty()){
        String sTime = null;
        String eTime = null;
        if (query.getTime() != null && !query.getTime().isEmpty()) {
            String[] split = query.getTime().split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpProcurementVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpProcurementVo> list = this.baseMapper.pageList(query,pageInfo,user,sTime,eTime,supplierClinicId);
        if(list.isEmpty()){
        List<TErpProcurementVo> list = this.baseMapper.pageList(query, pageInfo, user, sTime, eTime, supplierClinicId);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> ids = list.stream().map(TErpProcurementVo::getId).collect(Collectors.toList());
        List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
                (new LambdaQueryWrapper<TErpProcurementGoods>().in(TErpProcurementGoods::getProcurementId, ids));
        List<TSysCommission> tSysCommissions =new ArrayList<>();
        if(!tErpProcurementGoods.isEmpty()){
        List<TSysCommission> tSysCommissions = new ArrayList<>();
        if (!tErpProcurementGoods.isEmpty()) {
            List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
            tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
        }
@@ -136,11 +157,11 @@
            tErpProcurementVo.setTypeNum(size);
            BigDecimal reduce = collect.stream().map(TErpProcurementGoods::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            tErpProcurementVo.setTotalPrice(reduce);
            if(!collect.isEmpty()){
            if (!collect.isEmpty()) {
                List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
                List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
                BigDecimal reduce1 = collect2.stream().filter(e->e.getType()==1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce2 = collect2.stream().filter(e->e.getType()==2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementVo.setPlatformMoney(reduce1);
                tErpProcurementVo.setSupplierMoney(reduce2);
            }
@@ -153,23 +174,84 @@
    }
    @Override
    public PageInfo<TErpProcurementVo> pageListWarehouse(TErpProcurementQuery query, SysUser user) {
        Integer roleType = user.getRoleType();
        List<String> supplierClinicId = null;
        if (roleType == 6) {
            TCrmWarehouse warehouse = crmWarehouseMapper.selectOne(Wrappers.lambdaQuery(TCrmWarehouse.class)
                    .eq(TCrmWarehouse::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            // 供应商
            supplierClinicId = crmSupplierMapper.selectList(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getWarehouseId, warehouse.getId())).stream().map(TCrmSupplier::getId).collect(Collectors.toList());
            String sTime = null;
            String eTime = null;
            if (query.getTime() != null && !query.getTime().isEmpty()) {
                String[] split = query.getTime().split(" - ");
                sTime = split[0] + " 00:00:00";
                eTime = split[1] + " 23:59:59";
            }
            PageInfo<TErpProcurementVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
            if(CollectionUtils.isEmpty(supplierClinicId)){
                return pageInfo;
            }
            List<TErpProcurementVo> list = this.baseMapper.pageListWarehouse(query, pageInfo, user, sTime, eTime, supplierClinicId);
//            if (list.isEmpty()) {
//                return pageInfo;
//            }
//            List<String> ids = list.stream().map(TErpProcurementVo::getId).collect(Collectors.toList());
//            List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
//                    (new LambdaQueryWrapper<TErpProcurementGoods>().in(TErpProcurementGoods::getProcurementId, ids));
//            List<TSysCommission> tSysCommissions = new ArrayList<>();
//            if (!tErpProcurementGoods.isEmpty()) {
//                List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
//                tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
//            }
//            for (TErpProcurementVo tErpProcurementVo : list) {
//                List<TErpProcurementGoods> collect = tErpProcurementGoods.stream().filter(e -> e.getProcurementId().equals(tErpProcurementVo.getId())).collect(Collectors.toList());
//                int size = collect.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toSet()).size();
//                tErpProcurementVo.setTypeNum(size);
//                BigDecimal reduce = collect.stream().map(TErpProcurementGoods::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
//                tErpProcurementVo.setTotalPrice(reduce);
//                if (!collect.isEmpty()) {
//                    List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
//                    List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
//                    BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                    BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                    tErpProcurementVo.setPlatformMoney(reduce1);
//                    tErpProcurementVo.setSupplierMoney(reduce2);
//                }
//
//            }
            pageInfo.setRecords(list);
            return pageInfo;
        }
        return null;
    }
    @Override
    public TErpProcurementDetailVo detail(String id, SysUser user, String goodsName) {
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        if(roleType == 4){
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId =crmSupplier.getId();
            supplierClinicId = crmSupplier.getId();
        }
        if(roleType == 5){
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            supplierClinicId =crmClinic.getId();
            supplierClinicId = crmClinic.getId();
        }
        TErpProcurementDetailVo tErpProcurementVo = new TErpProcurementDetailVo();
@@ -187,7 +269,7 @@
        tErpProcurementVo.setUserName(sysUser.getNickName());
        tErpProcurementVo.setCreateTime(tErpProcurement.getCreateTime());
        tErpProcurementVo.setUpdateTime(tErpProcurement.getUpdateTime());
        if(tErpProcurement.getWarehousingUserId()!=null){
        if (tErpProcurement.getWarehousingUserId() != null) {
            SysUser sysUser1 = sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getWarehousingUserId()));
            tErpProcurementVo.setOperName(sysUser1.getNickName());
        }
@@ -201,7 +283,7 @@
                (new LambdaQueryWrapper<TErpProcurementGoods>().eq(TErpProcurementGoods::getProcurementId, id)
                        .like(goodsName != null && !goodsName.isEmpty(), TErpProcurementGoods::getGoodsName, goodsName)
                        .eq(TErpProcurementGoods::getSupplierId, supplierClinicId));
        if(tErpProcurementGoods.isEmpty()){
        if (tErpProcurementGoods.isEmpty()) {
            return tErpProcurementVo;
        }
@@ -213,11 +295,11 @@
            tErpProcurementDetailNextVo.setNum(tErpProcurementGood.getPurchaseCount());
            tErpProcurementDetailNextVo.setTotalPrice(tErpProcurementGood.getTotalPrice());
            tErpProcurementDetailNextVo.setQuasiNumber(tErpProcurementGood.getQuasiNumber());
            tErpProcurementDetailNextVo.setPurchasePrice(tErpProcurementGood.getPurchasePrice());
            tErpProcurementDetailNextVo.setPurchasePrice(tErpProcurementGood.getSalesAmount());
            List<TSysCommission> tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().eq(TSysCommission::getTErpProcurementGoodsId, tErpProcurementGood.getId()));
            if(!tSysCommissions.isEmpty()){
            if (!tSysCommissions.isEmpty()) {
                BigDecimal reduce = tSysCommissions.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementDetailNextVo.setPlatformMoney(reduce);
                BigDecimal reduce1 = tSysCommissions.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -233,40 +315,40 @@
    @Override
    public PageInfo<TErpGoods> pageGoodsPageList(TErpGoodsQuery query, SysUser user) {
        PageInfo<TErpGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        PageInfo<TErpGoods> p =erpGoodsMapper.selectPage(pageInfo,new LambdaQueryWrapper<TErpGoods>().eq(TErpGoods::getGoodsSource,1).isNotNull(TErpGoods::getClinicPurchasePrice).orderByDesc(BaseModel::getCreateTime));
        PageInfo<TErpGoods> p = erpGoodsMapper.selectPage(pageInfo, new LambdaQueryWrapper<TErpGoods>().eq(TErpGoods::getGoodsSource, 1).isNotNull(TErpGoods::getClinicPurchasePrice).orderByDesc(BaseModel::getCreateTime));
        List<TErpGoods> list = p.getRecords();
        List<String> typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList());
        if(!typeIds.isEmpty()){
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoods tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        List<String> supplierClinicId = list.stream().filter(e->e.getGoodsSource()==1).map(TErpGoods::getSupplierClinicId).collect(Collectors.toList());
        if(!supplierClinicId.isEmpty()){
        List<String> supplierClinicId = list.stream().filter(e -> e.getGoodsSource() == 1).map(TErpGoods::getSupplierClinicId).collect(Collectors.toList());
        if (!supplierClinicId.isEmpty()) {
            List<TCrmSupplier> crmClinics = crmSupplierMapper.selectBatchIds(supplierClinicId);
            for (TErpGoods tErpGoodsVO : list) {
                crmClinics.stream().filter(t -> t.getId().equals(tErpGoodsVO.getSupplierClinicId())).findFirst().ifPresent(t -> tErpGoodsVO.setSupplierName(t.getSupplierName()));
            }
        }
        pageInfo.setRecords( list);
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public List<TErpGoodsVO> inventoryNotEnoughList(SysUser user) {
        List<TErpGoodsVO> list =  this.baseMapper.inventoryNotEnoughList(user);
        List<TErpGoodsVO> list = this.baseMapper.inventoryNotEnoughList(user);
        List<String> typeIds = list.stream().map(TErpGoodsVO::getTypeId).collect(Collectors.toList());
        if(!typeIds.isEmpty()){
        if (!typeIds.isEmpty()) {
            List<TErpGoodsType> typeList = erpGoodsTypeMapper.selectBatchIds(typeIds);
            for (TErpGoodsVO tErpGoodsVO : list) {
                typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName()));
            }
        }
        List<String> supplierClinicId = list.stream().filter(e->e.getGoodsSource()==1).map(TErpGoodsVO::getSupplierClinicId).collect(Collectors.toList());
        if(!supplierClinicId.isEmpty()){
        List<String> supplierClinicId = list.stream().filter(e -> e.getGoodsSource() == 1).map(TErpGoodsVO::getSupplierClinicId).collect(Collectors.toList());
        if (!supplierClinicId.isEmpty()) {
            List<TCrmSupplier> crmSuppliers = crmSupplierMapper.selectBatchIds(supplierClinicId);
            for (TErpGoodsVO tErpGoodsVO : list) {
                crmSuppliers.stream().filter(t -> t.getId().equals(tErpGoodsVO.getSupplierClinicId())).findFirst().ifPresent(t -> tErpGoodsVO.setSupplierName(t.getSupplierName()));
@@ -286,7 +368,7 @@
            dto.setPlatformCommissionPrice(tErpGoodsVO.getPlatformCommissionPrice());
            dto.setSupplierId(tErpGoodsVO.getSupplierClinicId());
            dto.setGoodsSalesAmount(tErpGoodsVO.getClinicPurchasePrice());
            if(dto.getSalesAmount()==null){
            if (dto.getSalesAmount() == null) {
                dto.setSalesAmount(tErpGoodsVO.getClinicPurchasePrice());
            }
        }
@@ -298,7 +380,7 @@
            List<AddProcurementDto> value = entry.getValue();
            TErpProcurement tErpProcurement = new TErpProcurement();
            tErpProcurement.setClinicId(clinicSupplierId);
            tErpProcurement.setProcurementCode("C"+DateUtils.dateTimeNow());
            tErpProcurement.setProcurementCode("C" + DateUtils.dateTimeNow());
            tErpProcurement.setSupplierId(supplierClinicId);
            tErpProcurement.setTermNo(supplier.getTermNo());
@@ -308,19 +390,22 @@
            BigDecimal all = BigDecimal.ZERO;
            for (AddProcurementDto addProcurementDto : value) {
                BigDecimal multiply = addProcurementDto.getSalesAmount().multiply(BigDecimal.valueOf(addProcurementDto.getPurchaseCount()));
                BigDecimal multiply = addProcurementDto.getGoodsSalesAmount().multiply(BigDecimal.valueOf(addProcurementDto.getPurchaseCount()));
                all = all.add(multiply);
            }
            tErpProcurement.setPayMoney(all);
            AddProcurementDto addProcurementDto = value.get(0);
            if(addProcurementDto.getState()!=null && addProcurementDto.getState()==1){
            if (addProcurementDto.getState() != null && addProcurementDto.getState() == 1) {
                tErpProcurement.setStatus(1);
            }else {
            } else {
                tErpProcurement.setStatus(2);
            }
            ArrayList<TErpProcurementGoods> tErpProcurementGoods1 = new ArrayList<>();
            BigDecimal add =BigDecimal.ZERO;
            BigDecimal add = BigDecimal.ZERO;
            BigDecimal supplierCommissionPrice = BigDecimal.ZERO;
            for (AddProcurementDto dto : value) {
                TErpProcurementGoods tErpProcurementGoods = new TErpProcurementGoods();
@@ -332,16 +417,19 @@
                tErpProcurementGoods.setQuasiNumber(goods.getQuasiNumber());
                TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId());
                tErpProcurementGoods.setUnitName(tErpGoodsUnit.getUnitName());
                if(tErpGoodsUnit!=null){
                    tErpProcurementGoods.setUnitName(tErpGoodsUnit.getUnitName());
                }
                tErpProcurementGoods.setPurchasePrice(dto.getGoodsSalesAmount());
                tErpProcurementGoods.setSalesAmount(dto.getSalesAmount());
                tErpProcurementGoods.setTotalPrice(dto.getSalesAmount().multiply(new BigDecimal(dto.getPurchaseCount())));
                tErpProcurementGoods.setTotalPrice(dto.getGoodsSalesAmount().multiply(new BigDecimal(dto.getPurchaseCount())));
                tErpProcurementGoods.setPurchaseCount(dto.getPurchaseCount());
                tErpProcurementGoods.setSupplierId(supplierClinicId);
                tErpProcurementGoods.setRecvMerchantNo(supplier.getRecvMerchantNo());
                tErpProcurementGoods.setSupplierMoney(dto.getSalesAmount().subtract(dto.getPlatformCommissionPrice()));
                if(tErpProcurementGoods.getSupplierMoney().doubleValue()<0){
                supplierCommissionPrice = supplierCommissionPrice.add(tErpProcurementGoods.getSupplierMoney().multiply(BigDecimal.valueOf(dto.getPurchaseCount())));
                if (tErpProcurementGoods.getSupplierMoney().doubleValue() < 0) {
                    throw new RuntimeException("平台抽成不能大于售卖价格");
                }
                add = add.add(dto.getPlatformCommissionPrice().multiply(BigDecimal.valueOf(dto.getPurchaseCount())));
@@ -350,11 +438,12 @@
            BigDecimal bigDecimal = tErpProcurement.getPayMoney().multiply(BigDecimal.valueOf(0.0038)).setScale(2, RoundingMode.HALF_UP);
            tErpProcurement.setMoney(add.subtract(bigDecimal));
            if(tErpProcurement.getMoney().doubleValue()<0){
            if (tErpProcurement.getMoney().doubleValue() < 0) {
                throw new RuntimeException("金额设置错误,请联系平台管理员");
            }
            tErpProcurement.setSupplierMoney(supplierCommissionPrice);
            this.save(tErpProcurement);
            tErpProcurementGoods1.forEach(e->e.setProcurementId(tErpProcurement.getId()));
            tErpProcurementGoods1.forEach(e -> e.setProcurementId(tErpProcurement.getId()));
            erpProcurementGoodsService.saveBatch(tErpProcurementGoods1);
        }
@@ -370,6 +459,10 @@
        TCrmSupplier supplier = crmSupplierMapper.selectById(tErpProcurement.getSupplierId());
        detailProcurementVO.setSupplierName(supplier.getSupplierName());
        detailProcurementVO.setUserName(sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getCreateId())).getUserName());
        TCrmClinic tCrmClinic = crmClinicMapper.selectById(tErpProcurement.getClinicId());
        detailProcurementVO.setClinicName(tCrmClinic.getClinicName());
        detailProcurementVO.setClinicAddress(tCrmClinic.getClinicAddress());
        detailProcurementVO.setLogisticsNumber(tErpProcurement.getLogisticsNumber());
        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(new QueryWrapper<TErpProcurementGoods>().eq("procurement_id", id));
        ArrayList<DetailProcurementNextVO> detailProcurementNextVOS = new ArrayList<>();
        for (TErpProcurementGoods tErpProcurementGoods : list) {
@@ -381,10 +474,250 @@
            detailProcurementNextVO.setNum(tErpProcurementGoods.getPurchaseCount());
            detailProcurementNextVO.setTotalPrice(tErpProcurement.getPayMoney());
            detailProcurementNextVO.setId(tErpProcurementGoods.getId());
            detailProcurementNextVO.setPurchasePrice(tErpProcurementGoods.getPurchasePrice());
            List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoods = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().eq(TErpSupplierOutboundGoods::getProcurementGoodsId, tErpProcurementGoods.getId()));
            if(!tErpSupplierOutboundGoods.isEmpty()){
                ArrayList<DetailProcurementNextThreeVO> detailProcurementNextThreeVOS = new ArrayList<>();
                List<String> collect = tErpSupplierOutboundGoods.stream().map(TErpSupplierOutboundGoods::getWarehousingBatchId).collect(Collectors.toList());
                List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectBatchIds(collect);
                for (TErpSupplierOutboundGoods tErpSupplierOutboundGood : tErpSupplierOutboundGoods) {
                    List<TErpSupplierWarehousingBatch> collect1 = tErpSupplierWarehousingBatches.stream().filter(e -> e.getId().equals(tErpSupplierOutboundGood.getWarehousingBatchId())).collect(Collectors.toList());
                    for (TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch : collect1) {
                        DetailProcurementNextThreeVO detailProcurementNextThreeVO = new DetailProcurementNextThreeVO();
                        detailProcurementNextThreeVO.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                        detailProcurementNextThreeVO.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate());
                        detailProcurementNextThreeVO.setExpirationDate(tErpSupplierWarehousingBatch.getExpiryDate());
                        detailProcurementNextThreeVO.setNum(tErpSupplierOutboundGood.getOutboundCount());
                        detailProcurementNextThreeVOS.add(detailProcurementNextThreeVO);
                    }
                }
                detailProcurementNextVO.setDetailProcurementNextThreeVOS(detailProcurementNextThreeVOS);
            }
            detailProcurementNextVOS.add(detailProcurementNextVO);
        }
        detailProcurementVO.setList(detailProcurementNextVOS);
        return detailProcurementVO;
    }
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void detailProcurementSend(List<String> clinicSupplierId, SysUser user, String id,String logisticsNumber) {
        // 查出这个采购的下级商品
        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(new QueryWrapper<TErpProcurementGoods>().eq("procurement_id", id));
        List<TErpSupplierWarehousing> tErpSupplierWarehousings = erpSupplierWarehousingMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousing>()
                .in(TErpSupplierWarehousing::getSupplierId, clinicSupplierId).in(TErpSupplierWarehousing::getGoodsId, list.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toList()))
                .orderByAsc(BaseModel::getCreateTime));
        if(tErpSupplierWarehousings.isEmpty()){
            throw new RuntimeException("商品供应商未入库,请先入库");
        }
        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().in(TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousings.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
        // 获取所有的出库记录
        List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoodsLists = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().in(TErpSupplierOutboundGoods::getWarehousingBatchId, tErpSupplierWarehousingBatches.stream().map(TErpSupplierWarehousingBatch::getId).collect(Collectors.toList())));
        outer:for (TErpProcurementGoods tErpProcurementGoods : list) {
            // 分别入库
            String goodsId = tErpProcurementGoods.getGoodsId();
            // 查询这个供应商的 该商品是否入过库 未入库提示  出库判断库存 先找先入库的
            List<TErpSupplierWarehousing> collect = tErpSupplierWarehousings.stream().filter(e -> e.getGoodsId().equals(goodsId)).collect(Collectors.toList());
            if(collect.isEmpty()){
                throw new RuntimeException("商品供应商未入库,请先入库");
            }
            // 需要出库的数量
            Integer purchaseCount = tErpProcurementGoods.getPurchaseCount();
            if(purchaseCount==0){
                continue;
            }
            for (TErpSupplierWarehousing tErpSupplierWarehousing : collect) {
                List<TErpSupplierWarehousingBatch> warehousingBatches = tErpSupplierWarehousingBatches.stream().filter(e -> e.getWarehousingId().equals(tErpSupplierWarehousing.getId())).collect(Collectors.toList());
                for (TErpSupplierWarehousingBatch warehousingBatch : warehousingBatches) {
                    // 已出库数量
                    int sum = tErpSupplierOutboundGoodsLists.stream().filter(e -> e.getWarehousingBatchId().equals(warehousingBatch.getId())).mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
                    // 该批次剩余的数量
                    int count = warehousingBatch.getWarehousingNumber() - sum;
                    // 该批次没有数量 找下个批次
                    if(count==0 ){
                        continue;
                    }
                    // 如果大于这个数量 则使用这个批次
                    if(count>=purchaseCount){
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(tErpSupplierWarehousing.getSupplierId());
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutboundGoods.setProcurementGoodsId(tErpProcurementGoods.getId());
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                        continue outer;
                    }
                    if(count<purchaseCount){
                        purchaseCount = purchaseCount - count;
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(tErpSupplierWarehousing.getSupplierId());
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutboundGoods.setProcurementGoodsId(tErpProcurementGoods.getId());
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                    }
                }
            }
            if(purchaseCount>0){
                throw new RuntimeException("商品供应商库存不足,请先入库");
            }
        }
        TErpProcurement byId = this.getById(id);
        byId.setStatus(4);
        byId.setSendTime(LocalDateTime.now());
        byId.setLogisticsNumber(logisticsNumber);
        this.updateById(byId);
    }
    @Override
    public List<SupplierThirtyProcurementStatisticsVO> supplierThirtyProcurementStatistics(String supplierAndClinicId, String startTime, String endTime,Integer roleType) {
        return this.baseMapper.supplierThirtyProcurementStatistics(supplierAndClinicId, startTime, endTime, roleType);
    }
    @Override
    public List<SupplierThirtyMoneyProcurementStatisticsVO> supplierThirtyMoneyProcurementStatistics(String supplierAndClinicId, String startTime, String endTime,Integer roleType) {
        return this.baseMapper.supplierThirtyMoneyProcurementStatistics(supplierAndClinicId, startTime, endTime, roleType);
    }
    @Override
    public Integer supplierSalesStatisticsTypeCount(String supplierId) {
        return this.baseMapper.supplierSalesStatisticsTypeCount(supplierId);
    }
    @Override
    public Integer supplierSalesStatisticsCount(String supplierId) {
        return this.baseMapper.supplierSalesStatisticsCount(supplierId);
    }
    @Override
    public BigDecimal supplierSalesStatisticsMoney(String supplierId, String startTime, String endTime) {
        return this.baseMapper.supplierSalesStatisticsMoney(supplierId,startTime, endTime);
    }
    @Override
    public List<SupplierSalesStatisticsGoodsTypeVO> supplierSalesStatisticsGoodsType(String supplierId, String startTime, String endTime) {
        return this.baseMapper.supplierSalesStatisticsGoodsType(supplierId,startTime, endTime);
    }
    @Override
    public List<SupplierSalesStatisticsGoodsCountVO> supplierSalesStatisticsGoodsCount(String supplierId, String startTime, String endTime, String goodsName) {
        return this.baseMapper.supplierSalesStatisticsGoodsCount(supplierId,startTime, endTime,goodsName);
    }
    @Override
    public List<SupplierSalesStatisticsGoodsCountVO> supplierSalesStatisticsGoodsCountMonth(String supplierId, String startTime, String endTime, String goodsName) {
        return this.baseMapper.supplierSalesStatisticsGoodsCountMonth(supplierId,startTime, endTime,goodsName);
    }
    @Override
    public PageInfo<ClinicProcurementReportVO> clinicProcurementReport(ClinicProcurementReportQuery query) {
        PageInfo<ClinicProcurementReportVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<ClinicProcurementReportVO> list = this.baseMapper.clinicProcurementReport(query, pageInfo);
        if(CollectionUtils.isEmpty(list)){
            return pageInfo;
        }
        List<String> procurementIds = list.stream().map(TErpProcurement::getId).collect(Collectors.toList());
        List<TErpProcurementGoods> erpProcurementGoods = erpProcurementGoodsMapper.selectList(new QueryWrapper<TErpProcurementGoods>().lambda().in(TErpProcurementGoods::getProcurementId, procurementIds));
        for (ClinicProcurementReportVO clinicProcurementReportVO : list) {
            List<TErpProcurementGoods> procurementGoods = erpProcurementGoods.stream().filter(tErpProcurementGoods -> tErpProcurementGoods.getProcurementId().equals(clinicProcurementReportVO.getId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(procurementGoods)){
                int sum = procurementGoods.stream().mapToInt(TErpProcurementGoods::getPurchaseCount).sum();
                clinicProcurementReportVO.setProcurementGoodsNum(sum);
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public List<ProcurementReportClinicExport> exportProcurementReportClinic(ClinicProcurementReportQuery query) {
        List<ProcurementReportClinicExport> list = this.baseMapper.exportProcurementReportClinic(query);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<String> procurementIds = list.stream().map(ProcurementReportClinicExport::getId).collect(Collectors.toList());
        List<TErpProcurementGoods> erpProcurementGoods = erpProcurementGoodsMapper.selectList(new QueryWrapper<TErpProcurementGoods>().lambda().in(TErpProcurementGoods::getProcurementId, procurementIds));
        for (ProcurementReportClinicExport procurementReportClinicExport : list) {
            List<TErpProcurementGoods> procurementGoods = erpProcurementGoods.stream().filter(tErpProcurementGoods -> tErpProcurementGoods.getProcurementId().equals(procurementReportClinicExport.getId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(procurementGoods)){
                int sum = procurementGoods.stream().mapToInt(TErpProcurementGoods::getPurchaseCount).sum();
                procurementReportClinicExport.setProcurementGoodsNum(sum);
            }
            if(Objects.nonNull(procurementReportClinicExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportClinicExport.getPayTime());
                procurementReportClinicExport.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
    @Override
    public List<ProcurementReportSupplierExport> exportProcurementReportSupplier(ClinicProcurementReportQuery query) {
        List<ProcurementReportSupplierExport> list = this.baseMapper.exportProcurementReportSupplier(query);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        for (ProcurementReportSupplierExport procurementReportSupplierExport : list) {
            if(Objects.nonNull(procurementReportSupplierExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportSupplierExport.getPayTime());
                procurementReportSupplierExport.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
    @Override
    public List<ProcurementReportPlatformExport> exportProcurementReportPlatform(ClinicProcurementReportQuery query) {
        List<ProcurementReportPlatformExport> list = this.baseMapper.exportProcurementReportPlatform(query);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        for (ProcurementReportPlatformExport procurementReportPlatformExport : list) {
            if(Objects.nonNull(procurementReportPlatformExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportPlatformExport.getPayTime());
                procurementReportPlatformExport.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
}