package com.ruoyi.system.service.impl; 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 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.CodeGenerateUtils; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.uuid.IdUtils; import com.ruoyi.system.dto.InventoryDto; import com.ruoyi.system.dto.InventoryGoodsDto; import com.ruoyi.system.mapper.*; import com.ruoyi.system.model.*; import com.ruoyi.system.query.*; import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.TErpGoodsService; import com.ruoyi.system.service.TErpSupplierInventoryGoodsService; import com.ruoyi.system.vo.*; import org.springframework.beans.factory.annotation.Autowired; 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 java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** *

* erp商品 服务实现类 *

* * @author xiaochen * @since 2025-08-20 */ @Service public class TErpGoodsServiceImpl extends ServiceImpl implements TErpGoodsService { @Resource private TErpGoodsTypeMapper erpGoodsTypeMapper; @Resource private TErpProcurementGoodsMapper erpProcurementGoodsMapper; @Resource private TErpGoodsMapper erpGoodsMapper; @Resource private TErpGoodsUnitMapper erpGoodsUnitMapper; @Resource private TErpSupplierInventoryMapper erpSupplierInventoryMapper; @Resource private TErpClinicInventoryMapper erpClinicInventoryMapper; @Resource private TErpSupplierInventoryGoodsMapper erpSupplierInventoryGoodsMapper; @Resource private TErpClinicInventoryGoodsMapper erpClinicInventoryGoodsMapper; @Resource private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper; @Resource private TErpClinicWarehousingMapper erpClinicWarehousingMapper; @Resource private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper; @Resource private TErpClinicWarehousingBatchMapper erpClinicWarehousingBatchMapper; @Resource private TErpSupplierOutboundMapper erpSupplierOutboundMapper; @Resource private TErpClinicOutboundMapper erpClinicOutboundMapper; @Resource private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper; @Resource private TErpClinicOutboundGoodsMapper erpClinicOutboundGoodsMapper; @Resource private TCrmClinicMapper crmClinicMapper; @Resource private TCrmSupplierMapper crmSupplierMapper; @Resource private TErpProcurementMapper erpProcurementMapper; @Autowired private ISysUserService sysUserService; @Override public PageInfo pageList(TErpGoodsQuery query, SysUser user) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageList(query, pageInfo, user); if (list.isEmpty()) { return pageInfo; } List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List 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 collect = list.stream().filter(e -> e.getGoodsSource() == 1).map(TErpGoods::getSupplierClinicId).collect(Collectors.toList()); if (!collect.isEmpty()) { List crmClinics = crmSupplierMapper.selectBatchIds(collect); for (TErpGoodsVO tErpGoodsVO : list) { crmClinics.stream().filter(t -> t.getId().equals(tErpGoodsVO.getSupplierClinicId())).findFirst().ifPresent(t -> tErpGoodsVO.setSupplierName(t.getSupplierName())); } } for (TErpGoodsVO tErpGoodsVO : list) { if(tErpGoodsVO.getIsPrescriptionDrug()!=null && tErpGoodsVO.getIsPrescriptionDrug()==3){ tErpGoodsVO.setIsPrescriptionDrug(null); } if(user.getRoleType()==5 && (tErpGoodsVO.getContentState()==null || tErpGoodsVO.getContentState()!=1)){ tErpGoodsVO.setContent(null); } } pageInfo.setRecords(list); return pageInfo; } @Override public PageInfo pageList1(TErpGoodsQuery query, SysUser user) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageList(query, pageInfo, user); if (list.isEmpty()) { return pageInfo; } List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); } } pageInfo.setRecords(list); return pageInfo; } @Override public PageInfo pageAdminList(TErpGoodsAdminQuery query) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageAdminList(query, pageInfo); if (list.isEmpty()) { return pageInfo; } List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); } } pageInfo.setRecords( list); return pageInfo; } @Override public List pageAddList(Page tErpGoodsPage, String clinicSupplierId, PageAddListQuery query) { return this.baseMapper.pageAddList(tErpGoodsPage, clinicSupplierId, query); } @Override public PageInfo pageShopList(TErpGoodsShopQuery query) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageShopList(query, pageInfo); if (list.isEmpty()){ return new PageInfo<>(); } List goodsIds = list.stream().map(TErpGoods::getId).collect(Collectors.toList()); Map> goodsSalesMap = erpProcurementGoodsMapper.selectList(new LambdaQueryWrapper() .in(TErpProcurementGoods::getGoodsId, goodsIds)).stream() .collect(Collectors.groupingBy(TErpProcurementGoods::getGoodsId)); List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsVO tErpGoodsVO : list) { List tErpProcurementGoods = goodsSalesMap.get(tErpGoodsVO.getId()); if(!CollectionUtils.isEmpty(tErpProcurementGoods)){ // 计算采购数量 tErpGoodsVO.setSalesCount(tErpProcurementGoods.stream().filter(e->e.getPurchaseCount()!=null).mapToInt(TErpProcurementGoods::getPurchaseCount).sum()); typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); }else { tErpGoodsVO.setSalesCount(0); } } pageInfo.setRecords(list); return pageInfo; } @Override public List pageShopListAll(TErpGoodsShopQuery query) { List list = this.baseMapper.pageShopListAll(query); if (list.isEmpty()){ return new ArrayList<>(); } List goodsIds = list.stream().map(TErpGoods::getId).collect(Collectors.toList()); Map> goodsSalesMap = erpProcurementGoodsMapper.selectList(new LambdaQueryWrapper() .in(TErpProcurementGoods::getGoodsId, goodsIds)).stream() .collect(Collectors.groupingBy(TErpProcurementGoods::getGoodsId)); List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsVO tErpGoodsVO : list) { List tErpProcurementGoods = goodsSalesMap.get(tErpGoodsVO.getId()); if(!CollectionUtils.isEmpty(tErpProcurementGoods)){ // 计算采购数量 tErpGoodsVO.setSalesCount(tErpProcurementGoods.stream().filter(e->e.getPurchaseCount()!=null).mapToInt(TErpProcurementGoods::getPurchaseCount).sum()); typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); }else { tErpGoodsVO.setSalesCount(0); } } return list; } @Override public List listExport(TErpGoodsQuery query, SysUser user) { List list = this.baseMapper.listExport(query, user); if (list.isEmpty()) { return list; } List typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List 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()); } } List lowUnitIds = list.stream().map(TErpGoods::getLowUnitId).collect(Collectors.toList()); if (!lowUnitIds.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(lowUnitIds); for (TErpGoodsVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getLowUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setLowUnitName(t.getUnitName())); } } List packingUnitId = list.stream().map(TErpGoods::getPackingUnitId).collect(Collectors.toList()); if (!packingUnitId.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoodsVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName())); } } return list; } @Override public boolean isExit(String goodsIdCode, String quasiNumber,String id) { Long size = this.baseMapper.selectCount(new LambdaQueryWrapper<>(TErpGoods.class).eq(TErpGoods::getGoodsSource,1).ne(id!=null,TErpGoods::getId, id).eq(TErpGoods::getGoodsIdCode, goodsIdCode)); if (size > 0) { return true; } return false; } @Override public boolean isExitOne(String goodsIdCode, String quasiNumber,String id) { Long size = this.baseMapper.selectCount(new LambdaQueryWrapper<>(TErpGoods.class).eq(TErpGoods::getGoodsSource,2).ne(id!=null,TErpGoods::getId, id).eq(TErpGoods::getQuasiNumber, quasiNumber)); if (size > 0) { return true; } return false; } @Override public PageInfo pageInventoryGoodsPageList(TErpGoodsInventoryQuery query, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } // 当前时间一个月后 Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1); PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageInventoryGoodsPageList(query, pageInfo, user,supplierClinicId,endDate); if (list.isEmpty()) { return pageInfo; } List typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsInventoryVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName()); } } List packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList()); if (!packingUnitId.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoodsInventoryVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName())); } } pageInfo.setRecords(list); return pageInfo; } @Override public PageInfo pageInventoryGoodsPageList1(TErpGoodsInventoryQuery query, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } // 当前时间一个月后 Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1); PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = this.baseMapper.pageInventoryGoodsPageList1(query, pageInfo, user,supplierClinicId,endDate); if (list.isEmpty()) { return pageInfo; } List typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsInventoryVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName()); } } List packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList()); if (!packingUnitId.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoodsInventoryVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName())); } } pageInfo.setRecords(list); return pageInfo; } @Override public List pageInventoryGoodsList(String warehouseId, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } // 当前时间一个月后 Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1); List list = this.baseMapper.pageInventoryGoodsList(warehouseId, user,supplierClinicId,endDate); if (list.isEmpty()) { return list; } List typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsInventoryVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName()); } } List packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList()); if (!packingUnitId.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoodsInventoryVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName())); } } return list; } @Override public List pageInventoryGoodsList1(String warehouseId, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } // 当前时间一个月后 Date endDate = DateUtils.addMonths(DateUtils.getNowDate(), 1); List list = this.baseMapper.pageInventoryGoodsList1(warehouseId, user,supplierClinicId,endDate); if (list.isEmpty()) { return list; } List typeIds = list.stream().map(TErpGoodsInventoryVO::getTypeId).collect(Collectors.toList()); if (!typeIds.isEmpty()) { List typeList = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoodsInventoryVO tErpGoodsVO : list) { typeList.stream().filter(t -> t.getId().equals(tErpGoodsVO.getTypeId())).findFirst().ifPresent(t -> tErpGoodsVO.setTypeName(t.getTypeName())); tErpGoodsVO.setTypeName(tErpGoodsVO.getTypeName()); } } List packingUnitId = list.stream().map(TErpGoodsInventoryVO::getPackingUnitId).collect(Collectors.toList()); if (!packingUnitId.isEmpty()) { List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoodsInventoryVO tErpGoodsVO : list) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoodsVO.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoodsVO.setPackingUnitName(t.getUnitName())); } } return list; } @Override public Page warehouseGoodsPageList(TClinicGoodsWarehouseQuery query, SysUser user, String supplierClinicId) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(BaseModel::getDisabled,0); wrapper.eq(TErpGoods::getSupplierClinicId,supplierClinicId).eq(TErpGoods::getGoodsSource,2); if(query.getGoodsName()!=null && !query.getGoodsName().isEmpty()){ wrapper.like(TErpGoods::getGoodsName,query.getGoodsName()); } if(query.getGoodsIdCode()!=null && !query.getGoodsIdCode().isEmpty()){ wrapper.eq(TErpGoods::getGoodsIdCode,query.getGoodsIdCode()); } if(query.getQuasiNumber()!=null && !query.getQuasiNumber().isEmpty()){ wrapper.eq(TErpGoods::getQuasiNumber,query.getQuasiNumber()); } if(query.getTypeId()!=null && !query.getTypeId().isEmpty()){ wrapper.eq(TErpGoods::getTypeId,query.getTypeId()); } wrapper.orderByDesc(BaseModel::getCreateTime); Page page = this.page(new Page<>(query.getPageNum(), query.getPageSize()), wrapper); List records = page.getRecords(); if(records.isEmpty()){ return page; } List packingUnitId = records.stream().map(TErpGoods::getPackingUnitId).collect(Collectors.toList()); if(!packingUnitId.isEmpty()){ List tErpGoodsUnits = erpGoodsUnitMapper.selectBatchIds(packingUnitId); for (TErpGoods tErpGoods : records) { tErpGoodsUnits.stream().filter(t -> t.getId().equals(tErpGoods.getPackingUnitId())).findFirst().ifPresent(t -> tErpGoods.setPackingUnitName(t.getUnitName())); } } List typeIds = records.stream().map(TErpGoods::getTypeId).collect(Collectors.toList()); if(!typeIds.isEmpty()){ List tErpGoodsTypes = erpGoodsTypeMapper.selectBatchIds(typeIds); for (TErpGoods tErpGoods : records) { tErpGoodsTypes.stream().filter(t -> t.getId().equals(tErpGoods.getTypeId())).findFirst().ifPresent(t -> tErpGoods.setTypeName(t.getTypeName())); } } page.setRecords( records); return page; } @Override public WarehouseGoodsDetailVo warehouseGoodsDetail(String id) { WarehouseGoodsDetailVo warehouseGoodsDetailVo = new WarehouseGoodsDetailVo(); TErpClinicWarehousing tErpClinicWarehousing = erpClinicWarehousingMapper.selectById(id); warehouseGoodsDetailVo.setCreateTime(tErpClinicWarehousing.getCreateTime()); warehouseGoodsDetailVo.setCreateBy(tErpClinicWarehousing.getCreateBy()); if(tErpClinicWarehousing.getType()==1){ TErpProcurement tErpProcurement = erpProcurementMapper.selectById(tErpClinicWarehousing.getProcurementId()); warehouseGoodsDetailVo.setInventoryNumber(tErpProcurement.getProcurementCode()); } if(tErpClinicWarehousing.getType()==2){ TErpClinicInventory tErpClinicInventory = erpClinicInventoryMapper.selectById(tErpClinicWarehousing.getInventoryId()); warehouseGoodsDetailVo.setInventoryNumber(tErpClinicInventory.getInventoryNumber()); } List tErpClinicWarehousingBatches = erpClinicWarehousingBatchMapper.selectList(new LambdaQueryWrapper().eq(TErpClinicWarehousingBatch::getWarehousingId, id)); for (TErpClinicWarehousingBatch tErpClinicWarehousingBatch : tErpClinicWarehousingBatches) { String goodsId = tErpClinicWarehousingBatch.getGoodsId(); TErpGoods tErpGoods = erpGoodsMapper.selectById(goodsId); tErpClinicWarehousingBatch.setGoodsName(tErpGoods.getGoodsName()); String packingUnitId = tErpGoods.getPackingUnitId(); TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(packingUnitId); if(tErpGoodsUnit!=null){ tErpClinicWarehousingBatch.setUnitName(tErpGoodsUnit.getUnitName()); } tErpClinicWarehousingBatch.setPurchaseCount(tErpClinicWarehousingBatch.getWarehousingNumber()); } warehouseGoodsDetailVo.setList(tErpClinicWarehousingBatches); return warehouseGoodsDetailVo; } @Override public OutboundGoodsDetailVo outboundGoodsDetail(String id) { OutboundGoodsDetailVo outboundGoodsDetailVo = new OutboundGoodsDetailVo(); TErpClinicOutbound tErpClinicOutbound = erpClinicOutboundMapper.selectById(id); outboundGoodsDetailVo.setCreateTime(tErpClinicOutbound.getCreateTime()); outboundGoodsDetailVo.setCreateBy(tErpClinicOutbound.getCreateBy()); outboundGoodsDetailVo.setOutboundType(tErpClinicOutbound.getOutboundType()); outboundGoodsDetailVo.setOrderNumber(tErpClinicOutbound.getOrderNumber()); outboundGoodsDetailVo.setOutboundReason(tErpClinicOutbound.getOutboundReason()); if(tErpClinicOutbound.getOutboundType()==6){ TErpClinicInventory tErpClinicInventory = erpClinicInventoryMapper.selectById(tErpClinicOutbound.getInventoryId()); outboundGoodsDetailVo.setInventoryNumber(tErpClinicInventory.getInventoryNumber()); } ArrayList tErpClinicOutboundGoods1 = new ArrayList<>(); List tErpClinicOutboundGoods = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper().eq(TErpClinicOutboundGoods::getOutboundId, id)); for (TErpClinicOutboundGoods outboundGoods : tErpClinicOutboundGoods) { TErpClinicWarehousing tErpClinicWarehousing = erpClinicWarehousingMapper.selectById(outboundGoods.getWarehousingId()); TErpClinicWarehousingBatch tErpClinicWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(outboundGoods.getWarehousingBatchId()); OutboundGoodsDetailNextVo outboundGoodsDetailNextVo = new OutboundGoodsDetailNextVo(); outboundGoodsDetailNextVo.setWarehouseNo(tErpClinicWarehousing.getWarehouseNo()); outboundGoodsDetailNextVo.setGoodsId(outboundGoods.getGoodsId()); TErpGoods goods = erpGoodsMapper.getGoodsById(tErpClinicWarehousingBatch.getGoodsId()); outboundGoodsDetailNextVo.setGoodsName(goods.getGoodsName()); outboundGoodsDetailNextVo.setTypeId(goods.getTypeId()); TErpGoodsType tErpGoodsType = erpGoodsTypeMapper.selectById(goods.getTypeId()); outboundGoodsDetailNextVo.setTypeName(tErpGoodsType.getTypeName()); outboundGoodsDetailNextVo.setQuasiNumber(goods.getQuasiNumber()); outboundGoodsDetailNextVo.setBatchNumber(tErpClinicWarehousingBatch.getBatchNumber()); TErpGoodsUnit tErpGoodsUnit = erpGoodsUnitMapper.selectById(goods.getPackingUnitId()); outboundGoodsDetailNextVo.setUnitName(tErpGoodsUnit.getUnitName()); List tErpClinicOutboundGoods2 = erpClinicOutboundGoodsMapper.selectList(new LambdaQueryWrapper().eq(TErpClinicOutboundGoods::getWarehousingBatchId, outboundGoods.getWarehousingBatchId())); int sum = tErpClinicOutboundGoods2.stream().mapToInt(TErpClinicOutboundGoods::getOutboundCount).sum(); int count = tErpClinicWarehousingBatch.getWarehousingNumber() - sum; outboundGoodsDetailNextVo.setSurplusCount(count); outboundGoodsDetailNextVo.setUnitAmount(tErpClinicWarehousingBatch.getUnitAmount()); if(outboundGoodsDetailNextVo.getUnitAmount()==null){ outboundGoodsDetailNextVo.setUnitAmount(tErpClinicWarehousingBatch.getSalesAmount()); } outboundGoodsDetailNextVo.setOutboundCount(outboundGoods.getOutboundCount()); outboundGoodsDetailNextVo.setExpiryDate(tErpClinicWarehousingBatch.getExpiryDate()); outboundGoodsDetailNextVo.setTotalAmount(outboundGoodsDetailNextVo.getUnitAmount().multiply(new BigDecimal(outboundGoods.getOutboundCount()))); tErpClinicOutboundGoods1.add(outboundGoodsDetailNextVo); } outboundGoodsDetailVo.setList(tErpClinicOutboundGoods1); return outboundGoodsDetailVo; } @Override @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void inventoryGoods(InventoryDto dto, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } TErpSupplierInventory tErpSupplierInventory = new TErpSupplierInventory(); tErpSupplierInventory.setCreateBy(user.getNickName()); TErpSupplierInventory last1 = erpSupplierInventoryMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpSupplierInventory::getInventoryNumber).last("limit 1")); if(last1==null){ tErpSupplierInventory.setInventoryNumber("P" + DateUtils.dateTimeNow()+"00001"); }else { String lastProcurementCode = last1.getInventoryNumber(); String datePart = "P" + 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 } tErpSupplierInventory.setInventoryNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierInventory.setInventoryNumber(datePart + "00001"); } } tErpSupplierInventory.setWarehouseId(dto.getWarehouseId()); tErpSupplierInventory.setSupplierId(supplierClinicId); try { erpSupplierInventoryMapper.insert(tErpSupplierInventory); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } List dtos = dto.getDtos(); for (InventoryGoodsDto inventoryGoodsDto : dtos) { // 根据入库批次id查询入库信息 TErpSupplierWarehousing tErpSupplierWarehousing = new TErpSupplierWarehousing(); TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch = erpSupplierWarehousingBatchMapper.selectById(inventoryGoodsDto.getId()); String warehousingId = tErpSupplierWarehousingBatch.getWarehousingId(); TErpSupplierWarehousing tErpSupplierWarehousing1 = erpSupplierWarehousingMapper.selectById(warehousingId); TErpGoods erpGoods = this.getById(tErpSupplierWarehousing1.getGoodsId()); // 盘点是盘亏盘盈 赢 if (inventoryGoodsDto.getNum() < inventoryGoodsDto.getInventoryCount()) { // 添加入库信息 tErpSupplierWarehousing.setWarehouseId(dto.getWarehouseId()); tErpSupplierWarehousing.setSupplierId(supplierClinicId); tErpSupplierWarehousing.setGoodsId(tErpSupplierWarehousing1.getGoodsId()); tErpSupplierWarehousing.setGoodsName(erpGoods.getGoodsName()); tErpSupplierWarehousing.setGoodsCount(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum()); tErpSupplierWarehousing.setUnitAmount(tErpSupplierWarehousing1.getUnitAmount()); tErpSupplierWarehousing.setTotalPrice(tErpSupplierWarehousing1.getUnitAmount().multiply(new BigDecimal(tErpSupplierWarehousing.getGoodsCount()))); // 当前年月日时分秒 TErpSupplierWarehousing last = erpSupplierWarehousingMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpSupplierWarehousing::getWarehouseNo).last("LIMIT 1")); if(last==null){ tErpSupplierWarehousing.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 } tErpSupplierWarehousing.setWarehouseNo(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierWarehousing.setWarehouseNo(datePart + "00001"); } } tErpSupplierWarehousing.setType(2); try { erpSupplierWarehousingMapper.insert(tErpSupplierWarehousing); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } // 添加批次信息 TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch1 = new TErpSupplierWarehousingBatch(); tErpSupplierWarehousingBatch1.setWarehousingId(tErpSupplierWarehousing.getId()); tErpSupplierWarehousingBatch1.setWarehousingNumber(tErpSupplierWarehousing.getGoodsCount()); tErpSupplierWarehousingBatch1.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber()); tErpSupplierWarehousingBatch1.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate()); tErpSupplierWarehousingBatch1.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate()); erpSupplierWarehousingBatchMapper.insert(tErpSupplierWarehousingBatch1); // 添加盘点信息 TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods(); tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId()); tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierWarehousing.getId()); tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch1.getId()); tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount()); tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount()); tErpSupplierInventoryGoods.setInventoryType(1); tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId()); erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods); } else { int i =1; TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch1 = erpSupplierWarehousingBatchMapper.selectById(inventoryGoodsDto.getId()); String warehousingId1 = tErpSupplierWarehousingBatch1.getWarehousingId(); TErpSupplierWarehousing tErpSupplierWarehousing2 = erpSupplierWarehousingMapper.selectById(warehousingId1); if(inventoryGoodsDto.getDamagedCount()!=null && inventoryGoodsDto.getDamagedCount()>0){ TErpSupplierOutbound tErpSupplierOutbound = new TErpSupplierOutbound(); tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId()); tErpSupplierOutbound.setSupplierId(supplierClinicId); tErpSupplierOutbound.setOutboundType(1); TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).last("limit 1")); if(last==null){ tErpSupplierOutbound.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 } tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierOutbound.setOutboundNumber(datePart + "00001"); } } int count = inventoryGoodsDto.getDamagedCount(); tErpSupplierOutbound.setTotalMoney(tErpSupplierWarehousing1.getUnitAmount().multiply(new BigDecimal(count))); tErpSupplierOutbound.setGoodsId(tErpSupplierWarehousing2.getGoodsId()); try { erpSupplierOutboundMapper.insert(tErpSupplierOutbound); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods(); tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId()); tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId()); tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId()); tErpSupplierOutboundGoods.setOutboundCount(count); tErpSupplierOutboundGoods.setTotalPrice(tErpSupplierWarehousing1.getUnitAmount().multiply(new BigDecimal(count))); erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods); i=2; } TErpSupplierOutbound tErpSupplierOutbound = new TErpSupplierOutbound(); tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId()); tErpSupplierOutbound.setSupplierId(supplierClinicId); tErpSupplierOutbound.setOutboundType(6); TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).last("limit 1")); if(last==null){ tErpSupplierOutbound.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+=i; } else { number = 2; // 达到99999后重置为00002 } tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierOutbound.setOutboundNumber(datePart + "00001"); } } int count = inventoryGoodsDto.getNum() - inventoryGoodsDto.getInventoryCount(); tErpSupplierOutbound.setTotalMoney(tErpSupplierWarehousing1.getUnitAmount().multiply(new BigDecimal(count))); tErpSupplierOutbound.setGoodsId(tErpSupplierWarehousing2.getGoodsId()); try { erpSupplierOutboundMapper.insert(tErpSupplierOutbound); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods(); tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId()); tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId()); tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId()); tErpSupplierOutboundGoods.setOutboundCount(count); tErpSupplierOutboundGoods.setTotalPrice(tErpSupplierWarehousing1.getUnitAmount().multiply(new BigDecimal(count))); erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods); // 添加盘点信息 TErpSupplierInventoryGoods tErpSupplierInventoryGoods = new TErpSupplierInventoryGoods(); tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId()); tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierOutbound.getId()); tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierOutboundGoods.getId()); tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount()); tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount()); tErpSupplierInventoryGoods.setInventoryType(2); tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousing1.getGoodsId()); erpSupplierInventoryGoodsMapper.insert(tErpSupplierInventoryGoods); } } } @Override @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void inventoryGoods1(InventoryDto dto, SysUser user) { Integer roleType = user.getRoleType(); String supplierClinicId = null; if(roleType == 4){ // 供应商 TCrmSupplier crmSupplier = crmSupplierMapper.selectOne(Wrappers.lambdaQuery(TCrmSupplier.class) .eq(TCrmSupplier::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmSupplier)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmSupplier = crmSupplierMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmSupplier.getId(); } if(roleType == 5){ // 诊所 TCrmClinic crmClinic = crmClinicMapper.selectOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, user.getUserId()) .last("LIMIT 1")); // *** if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(user.getUserId()); crmClinic = crmClinicMapper.selectById(sysUser.getObjectId()); } supplierClinicId =crmClinic.getId(); } TErpClinicInventory tErpSupplierInventory = new TErpClinicInventory(); tErpSupplierInventory.setCreateBy(user.getNickName()); TErpClinicInventory last = erpClinicInventoryMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicInventory::getInventoryNumber).last("limit 1")); if(last==null){ tErpSupplierInventory.setInventoryNumber("P" + DateUtils.dateTimeNow()+"00001"); }else { String lastProcurementCode = last.getInventoryNumber(); String datePart = "P" + 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 } tErpSupplierInventory.setInventoryNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierInventory.setInventoryNumber(datePart + "00001"); } } tErpSupplierInventory.setClinicId(supplierClinicId); try { erpClinicInventoryMapper.insert(tErpSupplierInventory); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } List dtos = dto.getDtos(); for (InventoryGoodsDto inventoryGoodsDto : dtos) { // 根据入库批次id查询入库信息 TErpClinicWarehousing tErpSupplierWarehousing = new TErpClinicWarehousing(); TErpClinicWarehousingBatch tErpSupplierWarehousingBatch = erpClinicWarehousingBatchMapper.selectById(inventoryGoodsDto.getId()); String warehousingId = tErpSupplierWarehousingBatch.getWarehousingId(); TErpClinicWarehousing tErpSupplierWarehousing1 = erpClinicWarehousingMapper.selectById(warehousingId); TErpGoods erpGoods = this.getById(tErpSupplierWarehousingBatch.getGoodsId()); // 盘点是盘亏盘盈 赢 if (inventoryGoodsDto.getNum() < inventoryGoodsDto.getInventoryCount()) { // 添加入库信息 tErpSupplierWarehousing.setClinicId(supplierClinicId); // tErpSupplierWarehousing.setGoodsId(tErpSupplierWarehousing1.getGoodsId()); // tErpSupplierWarehousing.setGoodsName(erpGoods.getGoodsName()); // tErpSupplierWarehousing.setPurchaseCount(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum()); // tErpSupplierWarehousing.setUnitAmount(erpGoods.getSalesAmount()); if(tErpSupplierWarehousingBatch.getUnitAmount()==null){ tErpSupplierWarehousing.setTotalPrice(tErpSupplierWarehousingBatch.getSalesAmount().multiply(new BigDecimal(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum()))); }else { tErpSupplierWarehousing.setTotalPrice(tErpSupplierWarehousingBatch.getUnitAmount().multiply(new BigDecimal(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum()))); } // 当前年月日时分秒 TErpClinicWarehousing last1 = erpClinicWarehousingMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicWarehousing::getWarehouseNo).last("limit 1")); if(last1==null){ tErpSupplierWarehousing.setWarehouseNo("G" + DateUtils.dateTimeNow()+"00001"); }else { String lastProcurementCode = last1.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 } tErpSupplierWarehousing.setWarehouseNo(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierWarehousing.setWarehouseNo(datePart + "00001"); } } tErpSupplierWarehousing.setType(2); tErpSupplierWarehousing.setInventoryId(tErpSupplierInventory.getId()); if(erpGoods.getGoodsSource()==1){ TCrmSupplier supplier = crmSupplierMapper.selectById(erpGoods.getSupplierClinicId()); tErpSupplierWarehousing.setSupplierName(supplier.getSupplierName()); }else { tErpSupplierWarehousing.setSupplierName(erpGoods.getSupplierName()); } try { erpClinicWarehousingMapper.insert(tErpSupplierWarehousing); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } // 添加批次信息 TErpClinicWarehousingBatch tErpSupplierWarehousingBatch1 = new TErpClinicWarehousingBatch(); tErpSupplierWarehousingBatch1.setWarehousingId(tErpSupplierWarehousing.getId()); tErpSupplierWarehousingBatch1.setWarehousingNumber(inventoryGoodsDto.getInventoryCount() - inventoryGoodsDto.getNum()); tErpSupplierWarehousingBatch1.setBatchNumber(tErpSupplierWarehousingBatch.getBatchNumber()); tErpSupplierWarehousingBatch1.setProductionDate(tErpSupplierWarehousingBatch.getProductionDate()); tErpSupplierWarehousingBatch1.setExpiryDate(tErpSupplierWarehousingBatch.getExpiryDate()); tErpSupplierWarehousingBatch1.setGoodsId(tErpSupplierWarehousingBatch.getGoodsId()); tErpSupplierWarehousingBatch1.setQuasiNumber(tErpSupplierWarehousingBatch.getQuasiNumber()); tErpSupplierWarehousingBatch1.setPurchaseCount(tErpSupplierWarehousingBatch.getPurchaseCount()); tErpSupplierWarehousingBatch1.setSalesAmount(tErpSupplierWarehousingBatch.getSalesAmount()); tErpSupplierWarehousingBatch1.setUnitAmount(tErpSupplierWarehousingBatch.getUnitAmount()); erpClinicWarehousingBatchMapper.insert(tErpSupplierWarehousingBatch1); // 添加盘点信息 TErpClinicInventoryGoods tErpSupplierInventoryGoods = new TErpClinicInventoryGoods(); tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId()); tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierWarehousing.getId()); tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch1.getId()); tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount()); tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount()); tErpSupplierInventoryGoods.setInventoryType(1); tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousingBatch.getGoodsId()); erpClinicInventoryGoodsMapper.insert(tErpSupplierInventoryGoods); } else { int i=1; if(inventoryGoodsDto.getDamagedCount()!=null && inventoryGoodsDto.getDamagedCount()>0){ TErpClinicOutbound tErpSupplierOutbound = new TErpClinicOutbound(); // tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId()); // tErpSupplierOutbound.setSupplierId(supplierClinicId); tErpSupplierOutbound.setClinicId(supplierClinicId); tErpSupplierOutbound.setCreateId(user.getUserId().toString()); tErpSupplierOutbound.setCreateBy(user.getNickName()); tErpSupplierOutbound.setOutboundType(1); TErpClinicOutbound last2 = erpClinicOutboundMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicOutbound::getOutboundNumber).last("limit 1")); if(last2==null){ tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001"); }else { String lastProcurementCode = last2.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 } tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierOutbound.setOutboundNumber(datePart + "00001"); } } int count = inventoryGoodsDto.getDamagedCount(); tErpSupplierOutbound.setTotalMoney(erpGoods.getSalesAmount().multiply(new BigDecimal(count))); tErpSupplierOutbound.setInventoryId(tErpSupplierInventory.getId()); try { erpClinicOutboundMapper.insert(tErpSupplierOutbound); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } TErpClinicOutboundGoods tErpSupplierOutboundGoods = new TErpClinicOutboundGoods(); tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId()); tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId()); tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId()); tErpSupplierOutboundGoods.setOutboundCount(count); tErpSupplierOutboundGoods.setTotalPrice(erpGoods.getSalesAmount().multiply(new BigDecimal(count))); erpClinicOutboundGoodsMapper.insert(tErpSupplierOutboundGoods); i=2; } TErpClinicOutbound tErpSupplierOutbound = new TErpClinicOutbound(); // tErpSupplierOutbound.setWarehouseId(dto.getWarehouseId()); // tErpSupplierOutbound.setSupplierId(supplierClinicId); tErpSupplierOutbound.setCreateId(user.getUserId().toString()); tErpSupplierOutbound.setClinicId(supplierClinicId); tErpSupplierOutbound.setOutboundType(6); tErpSupplierOutbound.setCreateBy(user.getNickName()); TErpClinicOutbound last4 = erpClinicOutboundMapper.selectOne(new LambdaQueryWrapper().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpClinicOutbound::getOutboundNumber).last("limit 1")); if(last4==null){ tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001"); }else { String lastProcurementCode = last4.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+=i; } else { number = 2; // 达到99999后重置为00002 } tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number)); } catch (NumberFormatException e) { // 如果解析失败,默认从00001开始 tErpSupplierOutbound.setOutboundNumber(datePart + "00001"); } } int count = inventoryGoodsDto.getNum() - inventoryGoodsDto.getInventoryCount(); tErpSupplierOutbound.setTotalMoney(tErpSupplierWarehousingBatch.getSalesAmount().multiply(new BigDecimal(count))); tErpSupplierOutbound.setInventoryId(tErpSupplierInventory.getId()); try { erpClinicOutboundMapper.insert(tErpSupplierOutbound); }catch (Exception e){ throw new RuntimeException("网络繁忙,请重试"); } TErpClinicOutboundGoods tErpSupplierOutboundGoods = new TErpClinicOutboundGoods(); tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId()); tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing1.getId()); tErpSupplierOutboundGoods.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId()); tErpSupplierOutboundGoods.setOutboundCount(count); tErpSupplierOutboundGoods.setTotalPrice(tErpSupplierWarehousingBatch.getSalesAmount().multiply(new BigDecimal(count))); erpClinicOutboundGoodsMapper.insert(tErpSupplierOutboundGoods); // 添加盘点信息 TErpClinicInventoryGoods tErpSupplierInventoryGoods = new TErpClinicInventoryGoods(); tErpSupplierInventoryGoods.setInventoryId(tErpSupplierInventory.getId()); tErpSupplierInventoryGoods.setWarehousingId(tErpSupplierOutbound.getId()); tErpSupplierInventoryGoods.setWarehousingBatchId(tErpSupplierOutboundGoods.getId()); tErpSupplierInventoryGoods.setInventoryCount(inventoryGoodsDto.getInventoryCount()); tErpSupplierInventoryGoods.setDamagedCount(inventoryGoodsDto.getDamagedCount()); tErpSupplierInventoryGoods.setInventoryType(2); tErpSupplierInventoryGoods.setGoodsId(tErpSupplierWarehousingBatch.getGoodsId()); erpClinicInventoryGoodsMapper.insert(tErpSupplierInventoryGoods); } } } }