xuhy
2 天以前 72aaf45389cd09c206ee1b90f0c7f5bf59782930
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TErpClinicWarehousingServiceImpl.java
@@ -2,20 +2,25 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.ClinicOutboundGoodsDto;
import com.ruoyi.system.dto.OutboundGoodsNextDto;
import com.ruoyi.system.dto.WarehouseGoodsDto;
import com.ruoyi.system.dto.clinicWarehouseGoodsDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.query.TErpInventoryQuery;
import com.ruoyi.system.query.ValidityPeriodWarningQuery;
import com.ruoyi.system.query.*;
import com.ruoyi.system.service.TCrmSupplierService;
import com.ruoyi.system.service.TErpClinicWarehousingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.InventoryDetailVo;
import com.ruoyi.system.vo.PageInventoryListVo;
import com.ruoyi.system.vo.TErpGoodsVO;
import com.ruoyi.system.vo.ValidityPeriodWarningVo;
import com.ruoyi.system.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
@@ -23,6 +28,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -66,36 +72,40 @@
    @Resource
    private TErpGoodsTypeMapper erpGoodsTypeMapper;
    @Resource
    private TErpMaintenanceReminderMapper erpMaintenanceReminderMapper;
    @Resource
    private TCrmSupplierMapper crmSupplierMapper;
    @Override
    public PageInfo<PageInventoryListVo> pageInventoryList(TErpInventoryQuery query, SysUser user) {
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        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<PageInventoryListVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<PageInventoryListVo> list = this.baseMapper.pageInventoryList(pageInfo,query,user,sTime,eTime,supplierClinicId);
        if(list.isEmpty()){
        List<PageInventoryListVo> list = this.baseMapper.pageInventoryList(pageInfo, query, user, sTime, eTime, supplierClinicId);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> collect = list.stream().map(PageInventoryListVo::getId).collect(Collectors.toList());
        List<TErpClinicInventoryGoods> tErpSupplierInventoryGoods = erpClinicInventoryGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicInventoryGoods>().in(TErpClinicInventoryGoods::getInventoryId,collect));
        List<TErpClinicInventoryGoods> tErpSupplierInventoryGoods = erpClinicInventoryGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicInventoryGoods>().in(TErpClinicInventoryGoods::getInventoryId, collect));
        for (PageInventoryListVo pageInventoryListVo : list) {
            // 盘盈
            BigDecimal add = BigDecimal.ZERO;
@@ -106,28 +116,28 @@
            for (TErpClinicInventoryGoods tErpSupplierInventoryGoods1 : tErpSupplierInventoryGoodsList) {
                if(tErpSupplierInventoryGoods1!=null){
                if (tErpSupplierInventoryGoods1 != null) {
                    // 盘盈 查入库
                    if(tErpSupplierInventoryGoods1.getInventoryType()==1){
                        TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierInventoryGoods1.getWarehousingId());
                    if (tErpSupplierInventoryGoods1.getInventoryType() == 1) {
                        TErpClinicWarehousingBatch tErpSupplierWarehousing = erpClinicWarehousingBatchMapper.selectById(tErpSupplierInventoryGoods1.getWarehousingBatchId());
//                        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().eq(TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousing.getId()));
//                        int sum = tErpSupplierWarehousingBatches.stream().mapToInt(TErpSupplierWarehousingBatch::getWarehousingNumber).sum();
                        String goodsId = tErpSupplierWarehousing.getGoodsId();
                        TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                        String goodsId = tErpSupplierInventoryGoods1.getGoodsId();
//                        TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                        int count = tErpSupplierInventoryGoods1.getInventoryCount() - tErpSupplierInventoryGoods1.getDamagedCount();
                        BigDecimal multiply = goods.getSalesAmount().multiply(new BigDecimal(count));
                        BigDecimal multiply = tErpSupplierWarehousing.getSalesAmount().multiply(new BigDecimal(count));
                        add = add.add(multiply);
                    }else {
                    } else {
                        // 盘亏 查出库
                        TErpClinicOutbound tErpSupplierOutbound = erpClinicOutboundMapper.selectById(tErpSupplierInventoryGoods1.getWarehousingId());
                        List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().eq(TErpClinicOutboundGoods::getOutboundId, tErpSupplierOutbound.getId()));
                        for (TErpClinicOutboundGoods tErpSupplierOutboundGood : tErpSupplierOutboundGoods) {
                            TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierOutboundGood.getWarehousingId());
                            String goodsId = tErpSupplierWarehousing.getGoodsId();
                            TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                            TErpClinicWarehousingBatch tErpSupplierWarehousing = erpClinicWarehousingBatchMapper.selectById(tErpSupplierOutboundGood.getWarehousingBatchId());
//                            String goodsId = tErpSupplierInventoryGoods1.getGoodsId();
//                            TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                            int count = tErpSupplierInventoryGoods1.getInventoryCount() - tErpSupplierInventoryGoods1.getDamagedCount();
                            BigDecimal multiply = goods.getSalesAmount().multiply(new BigDecimal(count));
                            BigDecimal multiply = tErpSupplierWarehousing.getSalesAmount().multiply(new BigDecimal(count));
                            add = add.add(multiply);
                        }
//                        int sum = tErpSupplierOutboundGoods.stream().mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
@@ -153,13 +163,23 @@
        ArrayList<InventoryDetailVo> inventoryDetailVos = new ArrayList<>();
        List<TErpClinicInventoryGoods> tErpSupplierInventoryGoods = erpClinicInventoryGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicInventoryGoods>().eq(TErpClinicInventoryGoods::getInventoryId, id));
        if(tErpSupplierInventoryGoods!=null && !tErpSupplierInventoryGoods.isEmpty()){
        if (tErpSupplierInventoryGoods != null && !tErpSupplierInventoryGoods.isEmpty()) {
            for (TErpClinicInventoryGoods tErpSupplierInventoryGood : tErpSupplierInventoryGoods) {
                InventoryDetailVo inventoryDetailVo = new InventoryDetailVo();
                if(tErpSupplierInventoryGood.getInventoryType()==1){
                if (tErpSupplierInventoryGood.getInventoryType() == 1) {
                    TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(tErpSupplierInventoryGood.getWarehousingId());
                    String goodsId = tErpSupplierWarehousing.getGoodsId();
                    String goodsId = tErpSupplierInventoryGood.getGoodsId();
                    TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                    if (goods.getGoodsSource() == 1) {
                        TCrmSupplier supplier = crmSupplierMapper.selectById(goods.getSupplierClinicId());
                        if (supplier != null) {
                            inventoryDetailVo.setSupplierName(supplier.getSupplierName());
                        }
                    } else {
                        inventoryDetailVo.setSupplierName(goods.getSupplierName());
                    }
                    inventoryDetailVo.setGoodsName(goods.getGoodsName());
                    TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId());
                    inventoryDetailVo.setUnitName(tErpGoodsUnit.getUnitName());
@@ -170,10 +190,10 @@
                    List<TErpClinicWarehousingBatch> tErpSupplierWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpClinicWarehousingBatch>().eq(TErpClinicWarehousingBatch::getBatchNumber, tErpSupplierWarehousingBatch.getBatchNumber()));
                    int sum = tErpSupplierWarehousingBatches.stream().mapToInt(TErpClinicWarehousingBatch::getWarehousingNumber).sum();
                    List<String> collect = tErpSupplierWarehousingBatches.stream().map(TErpClinicWarehousingBatch::getId).collect(Collectors.toList());
                    if(!collect.isEmpty()){
                    if (!collect.isEmpty()) {
                        List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().eq(TErpClinicOutboundGoods::getWarehousingBatchId, collect));
                        int sum1 = tErpSupplierOutboundGoods.stream().mapToInt(TErpClinicOutboundGoods::getOutboundCount).sum();
                        sum = sum-sum1;
                        sum = sum - sum1;
                    }
                    // sum = 剩余库存
@@ -181,6 +201,51 @@
                    inventoryDetailVo.setInventoryCount(tErpSupplierInventoryGood.getInventoryCount());
                    inventoryDetailVo.setInventoryType(tErpSupplierInventoryGood.getInventoryType());
                    inventoryDetailVo.setDamagedCount(tErpSupplierInventoryGood.getDamagedCount());
                } else {
                    TErpClinicOutbound tErpSupplierOutbound = erpClinicOutboundMapper.selectById(tErpSupplierInventoryGood.getWarehousingId());
                    List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().eq(TErpClinicOutboundGoods::getOutboundId, tErpSupplierOutbound.getId()));
                    for (TErpClinicOutboundGoods tErpSupplierOutboundGood : tErpSupplierOutboundGoods) {
                        String warehousingId = tErpSupplierOutboundGood.getWarehousingId();
                        TErpClinicWarehousing tErpSupplierWarehousing = erpClinicWarehousingMapper.selectById(warehousingId);
                        String goodsId = tErpSupplierInventoryGood.getGoodsId();
                        TErpGoods goods = erpGoodsMapper.selectById(goodsId);
                        if (goods.getGoodsSource() == 1) {
                            TCrmSupplier supplier = crmSupplierMapper.selectById(goods.getSupplierClinicId());
                            if (supplier != null) {
                                inventoryDetailVo.setSupplierName(supplier.getSupplierName());
                            }
                        } else {
                            inventoryDetailVo.setSupplierName(goods.getSupplierName());
                        }
                        inventoryDetailVo.setGoodsName(goods.getGoodsName());
                        TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId());
                        if(tErpGoodsUnit!=null){
                            inventoryDetailVo.setUnitName(tErpGoodsUnit.getUnitName());
                        }
                        inventoryDetailVo.setWarehouseNo(tErpSupplierWarehousing.getWarehouseNo());
                        TErpClinicOutboundGoods outboundGoods = erpClinicOutboundGoodsMapper.selectById(tErpSupplierInventoryGood.getWarehousingBatchId());
                        TErpClinicWarehousingBatch tErpSupplierWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(outboundGoods.getWarehousingBatchId());
                        inventoryDetailVo.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber());
                        List<TErpClinicWarehousingBatch> tErpSupplierWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpClinicWarehousingBatch>().eq(TErpClinicWarehousingBatch::getId, outboundGoods.getWarehousingBatchId()));
                        int sum = tErpSupplierWarehousingBatches.stream().mapToInt(TErpClinicWarehousingBatch::getWarehousingNumber).sum();
                        List<String> collect = tErpSupplierWarehousingBatches.stream().map(TErpClinicWarehousingBatch::getId).collect(Collectors.toList());
                        if (!collect.isEmpty()) {
                            List<TErpClinicOutboundGoods> tErpSupplierOutboundGoods1 = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpClinicOutboundGoods>().in(TErpClinicOutboundGoods::getWarehousingBatchId, collect));
                            int sum1 = tErpSupplierOutboundGoods1.stream().mapToInt(TErpClinicOutboundGoods::getOutboundCount).sum();
                            sum = sum - sum1;
                        }
                        // sum = 剩余库存
                        inventoryDetailVo.setNowCount(sum);
                        inventoryDetailVo.setInventoryCount(tErpSupplierInventoryGood.getInventoryCount());
                        inventoryDetailVo.setInventoryType(tErpSupplierInventoryGood.getInventoryType());
                        inventoryDetailVo.setDamagedCount(tErpSupplierInventoryGood.getDamagedCount());
                    }
                }
                inventoryDetailVos.add(inventoryDetailVo);
@@ -195,39 +260,37 @@
    }
    @Override
    public PageInfo<ValidityPeriodWarningVo> validityPeriodWarning(ValidityPeriodWarningQuery query, SysUser user) {
        Integer roleType = user.getRoleType();
        String supplierClinicId = null;
        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();
        }
        // 获取当前时间后9个月的时间
        LocalDateTime nineMonthLater = LocalDateTime.now().plusMonths(9);
        // 获取当前时间后1个月的时间
        LocalDateTime nineMonthLater = LocalDateTime.now().plusMonths(1);
        PageInfo<ValidityPeriodWarningVo> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        return this.baseMapper.validityPeriodWarning(pageInfo,query,user,nineMonthLater,supplierClinicId);
        return this.baseMapper.validityPeriodWarning(pageInfo, query, user, nineMonthLater, supplierClinicId);
    }
    @Override
    public PageInfo<TErpGoodsVO> pageList(TErpGoodsQuery query, SysUser user) {
        PageInfo<TErpGoodsVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpGoodsVO> list = this.baseMapper.pageList(query,pageInfo,user);
        if(list.isEmpty()){
        List<TErpGoodsVO> list = this.baseMapper.pageList(query, pageInfo, user);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> typeIds = list.stream().map(TErpGoods::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()));
@@ -237,5 +300,228 @@
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public List<TErpGoodsVO> pageList1(TErpGoodsQuery query, SysUser user) {
        List<TErpGoodsVO> list = this.baseMapper.pageList1(query, user);
        if (list.isEmpty()) {
            return list;
        }
        List<String> typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList());
        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()));
                tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName());
            }
        }
        return list;
    }
    @Override
    public PageInfo<TErpClinicWarehousePageListVO> warehousePageList(TClinicWarehouseQuery query, SysUser user, String supplierClinicId) {
        String time = query.getTime();
        String sTime = null;
        String eTime = null;
        if (time != null && !time.isEmpty()) {
            String[] split = time.split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpClinicWarehousePageListVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpClinicWarehousePageListVO> list = this.baseMapper.warehousePageList(query, pageInfo, user, supplierClinicId, sTime, eTime);
        if (list.isEmpty()) {
            return pageInfo;
        }
        List<String> collect = list.stream().map(TErpClinicWarehousePageListVO::getId).collect(Collectors.toList());
        List<TErpClinicWarehousingBatch> tErpClinicWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpClinicWarehousingBatch>().in(TErpClinicWarehousingBatch::getWarehousingId, collect));
        for (TErpClinicWarehousePageListVO tErpClinicWarehousePageListVO : list) {
            int size = tErpClinicWarehousingBatches.stream().filter(e -> e.getWarehousingId().equals(tErpClinicWarehousePageListVO.getId())).map(TErpClinicWarehousingBatch::getGoodsId).collect(Collectors.toSet()).size();
            tErpClinicWarehousePageListVO.setTypeNum(size);
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public PageInfo<TErpClinicOutboundPageListVO> outboundPageList(TClinicOutboundQuery query, SysUser user, String supplierClinicId) {
        String time = query.getTime();
        String sTime = null;
        String eTime = null;
        if (time != null && !time.isEmpty()) {
            String[] split = time.split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        PageInfo<TErpClinicOutboundPageListVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TErpClinicOutboundPageListVO> list = this.baseMapper.outboundPageList(query, pageInfo, user, supplierClinicId, sTime, eTime);
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void warehouseGoods(WarehouseGoodsDto dtos, SysUser user, String supplierClinicId) {
        // dtos根据供应商分组
//        Map<String, List<clinicWarehouseGoodsDto>> collect = dtos.stream().collect(Collectors.groupingBy(clinicWarehouseGoodsDto::getSupplierId));
//        for (Map.Entry<String, List<clinicWarehouseGoodsDto>> stringListEntry : collect.entrySet()) {
        if(dtos.getStatus()==null || dtos.getStatus()!=1 ){
            // 草稿-》正式 移除所有草稿
            this.baseMapper.delete(new LambdaQueryWrapper<TErpClinicWarehousing>().eq(TErpClinicWarehousing::getClinicId,supplierClinicId).eq(TErpClinicWarehousing::getStatus,1));
        }
        TErpClinicWarehousing tErpClinicWarehousing = new TErpClinicWarehousing();
        tErpClinicWarehousing.setClinicId(supplierClinicId);
        tErpClinicWarehousing.setCreateId(user.getUserId().toString());
        TErpClinicWarehousing last = this.getOne(new LambdaQueryWrapper<TErpClinicWarehousing>().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicWarehousing::getWarehouseNo).last("LIMIT 1"));
        if(last==null){
            tErpClinicWarehousing.setWarehouseNo("G" + DateUtils.dateTimeNow()+"00001");
        }else{
            String lastProcurementCode = last.getWarehouseNo();
            String datePart = "G" + DateUtils.dateTimeNow();
            // 直接获取后五位数字并+1,不考虑是否同一天
            try {
                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
                int number = Integer.parseInt(numberPart);
                if (number < 99999) {
                    number++;
                } else {
                    number = 2; // 达到99999后重置为00002
                }
                tErpClinicWarehousing.setWarehouseNo(datePart + String.format("%05d", number));
            } catch (NumberFormatException e) {
                // 如果解析失败,默认从00001开始
                tErpClinicWarehousing.setWarehouseNo(datePart + "00001");
            }
        }
        tErpClinicWarehousing.setCreateTime(LocalDateTime.now());
        tErpClinicWarehousing.setType(3);
        tErpClinicWarehousing.setStatus(dtos.getStatus());
        tErpClinicWarehousing.setWarehousingType(2);
        try {
            this.save(tErpClinicWarehousing);
        }catch (Exception e){
            throw new RuntimeException("网络繁忙,请重试");
        }
        BigDecimal totalPrice = new BigDecimal(0);
        ArrayList<String> names = new ArrayList<>();
        for (clinicWarehouseGoodsDto dto : dtos.getDtos()) {
            TErpClinicWarehousingBatch tErpClinicWarehousingBatch = new TErpClinicWarehousingBatch();
            tErpClinicWarehousingBatch.setWarehousingId(tErpClinicWarehousing.getId());
            tErpClinicWarehousingBatch.setWarehousingNumber(dto.getPurchaseCount());
            tErpClinicWarehousingBatch.setBatchNumber(dto.getBatchNumber());
            tErpClinicWarehousingBatch.setProductionDate(dto.getProductionDate().atStartOfDay());
            tErpClinicWarehousingBatch.setExpiryDate(dto.getExpiryDate().atStartOfDay());
            tErpClinicWarehousingBatch.setGoodsId(dto.getId());
            TErpGoods goods = erpGoodsMapper.selectById(dto.getId());
            String supplierName = goods.getSupplierName();
            names.add(supplierName);
            tErpClinicWarehousingBatch.setGoodsName(goods.getGoodsName());
            tErpClinicWarehousingBatch.setQuasiNumber(goods.getQuasiNumber());
            tErpClinicWarehousingBatch.setUnitAmount(dto.getUnitAmount());
            tErpClinicWarehousingBatch.setSalesAmount(dto.getSalesAmount());
            if (dto.getSalesAmount() == null) {
                tErpClinicWarehousingBatch.setSalesAmount(goods.getClinicPurchasePrice());
            }
            tErpClinicWarehousingBatch.setPurchaseCount(dto.getPurchaseCount());
            erpClinicWarehousingBatchMapper.insert(tErpClinicWarehousingBatch);
            if (goods.getMaintenanceInterval() != null && !goods.getMaintenanceInterval().isEmpty()) {
                TErpMaintenanceReminder tErpMaintenanceReminder = new TErpMaintenanceReminder();
                tErpMaintenanceReminder.setClinicSupplierId(supplierClinicId);
                tErpMaintenanceReminder.setMaintenanceType(2);
                tErpMaintenanceReminder.setWarehousingBatchId(tErpClinicWarehousingBatch.getId());
                Integer day = Integer.valueOf(goods.getMaintenanceInterval());
                // 当前时间+day天
                LocalDateTime time1 = LocalDateTime.now().plusDays(day);
                tErpMaintenanceReminder.setMaintenanceTime(time1);
                erpMaintenanceReminderMapper.insert(tErpMaintenanceReminder);
            }
            totalPrice = totalPrice.add(dto.getUnitAmount().multiply(new BigDecimal(dto.getPurchaseCount())));
        }
        tErpClinicWarehousing.setTotalPrice(totalPrice);
        String name = names.stream().collect(Collectors.joining(","));
        tErpClinicWarehousing.setSupplierName(name);
        this.updateById(tErpClinicWarehousing);
//        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outboundGoods(ClinicOutboundGoodsDto dto, SysUser user, String supplierClinicId) {
        TErpClinicOutbound tErpClinicOutbound = new TErpClinicOutbound();
        tErpClinicOutbound.setClinicId(supplierClinicId);
        tErpClinicOutbound.setCreateId(user.getUserId().toString());
        TErpClinicOutbound last = erpClinicOutboundMapper.selectOne(new LambdaQueryWrapper<TErpClinicOutbound>().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicOutbound::getOutboundNumber).last("limit 1"));
        if(last==null){
            tErpClinicOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001");
        }else{
            String lastProcurementCode = last.getOutboundNumber();
            String datePart = "G" + DateUtils.dateTimeNow();
            // 直接获取后五位数字并+1,不考虑是否同一天
            try {
                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
                int number = Integer.parseInt(numberPart);
                if (number < 99999) {
                    number++;
                } else {
                    number = 2; // 达到99999后重置为00002
                }
                tErpClinicOutbound.setOutboundNumber(datePart + String.format("%05d", number));
            } catch (NumberFormatException e) {
                // 如果解析失败,默认从00001开始
                tErpClinicOutbound.setOutboundNumber(datePart + "00001");
            }
        }
        tErpClinicOutbound.setCreateTime(LocalDateTime.now());
        tErpClinicOutbound.setOutboundType(dto.getOutboundType());
        tErpClinicOutbound.setTotalMoney(dto.getTotalPrice());
        tErpClinicOutbound.setOutboundReason(dto.getOutboundReason());
        tErpClinicOutbound.setTypeNum(dto.getTypeNum());
        try {
            erpClinicOutboundMapper.insert(tErpClinicOutbound);
        }catch (Exception e){
            throw new RuntimeException("网络繁忙,请重试");
        }
        List<TErpClinicOutboundGoods> tErpClinicOutboundGoods = new ArrayList<>();
        for (OutboundGoodsNextDto outboundGoodsNextDto : dto.getOutboundGoodsNextDtos()) {
            TErpClinicOutboundGoods tErpClinicOutboundGoods1 = new TErpClinicOutboundGoods();
            tErpClinicOutboundGoods1.setOutboundId(tErpClinicOutbound.getId());
            String batchId = outboundGoodsNextDto.getBatchId();
            TErpClinicWarehousingBatch tErpClinicWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(batchId);
            tErpClinicOutboundGoods1.setWarehousingId(tErpClinicWarehousingBatch.getWarehousingId());
            tErpClinicOutboundGoods1.setWarehousingBatchId(batchId);
            tErpClinicOutboundGoods1.setOutboundCount(outboundGoodsNextDto.getNum());
            if(tErpClinicWarehousingBatch.getUnitAmount()==null){
                tErpClinicOutboundGoods1.setTotalPrice(tErpClinicWarehousingBatch.getSalesAmount().multiply(BigDecimal.valueOf(outboundGoodsNextDto.getNum())));
            }else {
                tErpClinicOutboundGoods1.setTotalPrice(tErpClinicWarehousingBatch.getUnitAmount().multiply(BigDecimal.valueOf(outboundGoodsNextDto.getNum())));
            }
            tErpClinicOutboundGoods1.setGoodsId(tErpClinicWarehousingBatch.getGoodsId());
            tErpClinicOutboundGoods.add(tErpClinicOutboundGoods1);
            erpClinicOutboundGoodsMapper.insert(tErpClinicOutboundGoods1);
        }
    }
}