|
@@ -2,13 +2,19 @@ package com.sd.business.service.purchase.impl;
|
|
|
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.file.utils.ObsFileUtil;
|
|
|
import com.fjhx.flow.enums.FlowStatusEnum;
|
|
|
import com.ruoyi.common.constant.StatusConstant;
|
|
|
import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import com.sd.business.entity.apply.po.ApplyBuy;
|
|
|
+import com.sd.business.entity.apply.po.ApplyBuyBom;
|
|
|
import com.sd.business.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.entity.in.po.InOutStorageBom;
|
|
|
import com.sd.business.entity.purchase.dto.PurchaseBomImportDataDto;
|
|
@@ -17,9 +23,11 @@ import com.sd.business.entity.purchase.dto.PurchaseSelectDto;
|
|
|
import com.sd.business.entity.purchase.enums.PurchaseStatusEnum;
|
|
|
import com.sd.business.entity.purchase.po.Purchase;
|
|
|
import com.sd.business.entity.purchase.po.PurchaseBom;
|
|
|
+import com.sd.business.entity.purchase.vo.PurchaseBomVo;
|
|
|
import com.sd.business.entity.purchase.vo.PurchaseVo;
|
|
|
import com.sd.business.entity.supplier.po.Supplier;
|
|
|
import com.sd.business.mapper.purchase.PurchaseMapper;
|
|
|
+import com.sd.business.service.apply.ApplyBuyBomService;
|
|
|
import com.sd.business.service.bom.BomSpecService;
|
|
|
import com.sd.business.service.purchase.PurchaseBomService;
|
|
|
import com.sd.business.service.purchase.PurchaseService;
|
|
@@ -32,10 +40,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -51,6 +56,9 @@ import java.util.stream.Collectors;
|
|
|
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {
|
|
|
|
|
|
@Autowired
|
|
|
+ private ApplyBuyBomService applyBuyBomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private PurchaseBomService purchaseBomService;
|
|
|
|
|
|
@Autowired
|
|
@@ -64,10 +72,46 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
|
|
|
Page<PurchaseVo> page = Sql.create(PurchaseVo.class)
|
|
|
.selectAll(Purchase.class)
|
|
|
+ .selectAs(Supplier::getName, PurchaseVo::getSupplierName)
|
|
|
+ .selectAs(ApplyBuy::getCode, PurchaseVo::getApplyBuyCode)
|
|
|
.from(Purchase.class)
|
|
|
+ .leftJoin(Supplier.class, Supplier::getId, Purchase::getSupplierId)
|
|
|
+ .leftJoin(ApplyBuy.class, ApplyBuy::getId, Purchase::getApplyBuyId)
|
|
|
+ .eq(Purchase::getFlowStatus, dto.getFlowStatus())
|
|
|
+ .eq(Purchase::getStatus, dto.getStatus())
|
|
|
+ .eq(Supplier::getName, dto.getSupplierName())
|
|
|
+ .and(StrUtil.isNotBlank(dto.getCode()),
|
|
|
+ q -> q.like(Purchase::getCode, dto.getCode())
|
|
|
+ .or().like(Purchase::getErpCode, dto.getCode()))
|
|
|
.orderByDesc(Purchase::getId)
|
|
|
.page(dto);
|
|
|
|
|
|
+ List<PurchaseVo> records = page.getRecords();
|
|
|
+ if (records.size() == 0) {
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 赋值采购明细
|
|
|
+ List<Long> idList = records.stream().map(PurchaseVo::getId).collect(Collectors.toList());
|
|
|
+ List<PurchaseBomVo> purchaseBomVoList = Sql.create(PurchaseBomVo.class)
|
|
|
+ .selectAll(PurchaseBom.class)
|
|
|
+ .selectAs(BomSpec::getCode, PurchaseBomVo::getBomSpecCode)
|
|
|
+ .selectAs(BomSpec::getName, PurchaseBomVo::getBomSpecName)
|
|
|
+ .selectAs(BomSpec::getColour, PurchaseBomVo::getBomSpecColour)
|
|
|
+ .selectAs(BomSpec::getLength, PurchaseBomVo::getBomSpecLength)
|
|
|
+ .selectAs(BomSpec::getWidth, PurchaseBomVo::getBomSpecWidth)
|
|
|
+ .selectAs(BomSpec::getHeight, PurchaseBomVo::getBomSpecHeight)
|
|
|
+ .from(PurchaseBom.class)
|
|
|
+ .innerJoin(Purchase.class, Purchase::getId, PurchaseBom::getPurchaseId)
|
|
|
+ .innerJoin(BomSpec.class, BomSpec::getId, PurchaseBom::getBomSpecId)
|
|
|
+ .in(Purchase::getId, idList)
|
|
|
+ .list();
|
|
|
+
|
|
|
+ Map<Long, List<PurchaseBomVo>> map = purchaseBomVoList.stream().collect(Collectors.groupingBy(PurchaseBom::getPurchaseId));
|
|
|
+ for (PurchaseVo record : records) {
|
|
|
+ record.setPurchaseBomList(map.getOrDefault(record.getId(), Collections.emptyList()));
|
|
|
+ }
|
|
|
+
|
|
|
return page;
|
|
|
}
|
|
|
|
|
@@ -82,22 +126,146 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
|
|
|
Assert.notNull(vo, "未知数据");
|
|
|
|
|
|
+ List<PurchaseBomVo> purchaseBomVoList = Sql.create(PurchaseBomVo.class)
|
|
|
+ .selectAll(PurchaseBom.class)
|
|
|
+ .selectAs(BomSpec::getCode, PurchaseBomVo::getBomSpecCode)
|
|
|
+ .selectAs(BomSpec::getName, PurchaseBomVo::getBomSpecName)
|
|
|
+ .selectAs(BomSpec::getColour, PurchaseBomVo::getBomSpecColour)
|
|
|
+ .selectAs(BomSpec::getLength, PurchaseBomVo::getBomSpecLength)
|
|
|
+ .selectAs(BomSpec::getWidth, PurchaseBomVo::getBomSpecWidth)
|
|
|
+ .selectAs(BomSpec::getHeight, PurchaseBomVo::getBomSpecHeight)
|
|
|
+ .selectAs("t1.purchase_quantity - t1.arrival_quantity + t1.return_quantity", PurchaseBomVo::getInTransitQuantity)
|
|
|
+ .from(PurchaseBom.class)
|
|
|
+ .innerJoin(Purchase.class, Purchase::getId, PurchaseBom::getPurchaseId)
|
|
|
+ .innerJoin(BomSpec.class, BomSpec::getId, PurchaseBom::getBomSpecId)
|
|
|
+ .eq(Purchase::getId, id)
|
|
|
+ .list();
|
|
|
+
|
|
|
+ vo.setPurchaseBomList(purchaseBomVoList);
|
|
|
+
|
|
|
return vo;
|
|
|
}
|
|
|
|
|
|
+ @DSTransactional
|
|
|
@Override
|
|
|
public void add(PurchaseDto dto) {
|
|
|
- save(dto);
|
|
|
+
|
|
|
+ // 保存采购合同
|
|
|
+ dto.setCode(CodeEnum.PURCHASE_CODE.getCode());
|
|
|
+ dto.setReturnAmount(BigDecimal.ZERO);
|
|
|
+ dto.setClosedAccountAmount(BigDecimal.ZERO);
|
|
|
+ dto.setDeductibleAmount(BigDecimal.ZERO);
|
|
|
+ dto.setPaymentStatus(StatusConstant.NO);
|
|
|
+ dto.setStorageStatus(StatusConstant.NO);
|
|
|
+ this.save(dto);
|
|
|
+
|
|
|
+ // 保存采购明细
|
|
|
+ Map<Long, ApplyBuyBom> applyBuyBomMap = applyBuyBomService.mapKEntity(
|
|
|
+ BaseIdPo::getId,
|
|
|
+ q -> q.eq(ApplyBuyBom::getApplyBuyId, dto.getApplyBuyId()));
|
|
|
+ List<PurchaseBom> purchaseBomList = dto.getPurchaseBomList();
|
|
|
+ purchaseBomList.forEach(item -> {
|
|
|
+ ApplyBuyBom applyBuyBom = applyBuyBomMap.get(item.getApplyBuyBomId());
|
|
|
+ if (applyBuyBom == null) {
|
|
|
+ throw new ServiceException("存在未知申购明细id");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal quantity = applyBuyBom.getQuantity();
|
|
|
+ BigDecimal purchaseQuantity = applyBuyBom.getPurchaseQuantity();
|
|
|
+ BigDecimal frozenQuantity = applyBuyBom.getFrozenQuantity();
|
|
|
+ BigDecimal itemPurchaseQuantity = item.getPurchaseQuantity();
|
|
|
+
|
|
|
+ if (quantity.compareTo(purchaseQuantity.add(frozenQuantity).add(itemPurchaseQuantity)) >= 0) {
|
|
|
+ applyBuyBom.setFrozenQuantity(frozenQuantity.add(itemPurchaseQuantity));
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("采购数量超过申购数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ item.setPurchaseId(dto.getId());
|
|
|
+ item.setReturnQuantity(BigDecimal.ZERO);
|
|
|
+ item.setPaidAmount(BigDecimal.ZERO);
|
|
|
+ });
|
|
|
+ purchaseBomService.saveBatch(purchaseBomList);
|
|
|
+
|
|
|
+ // 更新申购明细
|
|
|
+ ArrayList<ApplyBuyBom> applyBuyBomList = new ArrayList<>(applyBuyBomMap.values());
|
|
|
+ applyBuyBomService.updateBatchById(applyBuyBomList);
|
|
|
+
|
|
|
+ ObsFileUtil.saveFile(dto.getFileList(), dto.getId());
|
|
|
}
|
|
|
|
|
|
+ @DSTransactional
|
|
|
@Override
|
|
|
public void edit(PurchaseDto dto) {
|
|
|
- updateById(dto);
|
|
|
+
|
|
|
+ dto.setCode(null);
|
|
|
+
|
|
|
+ // 保存采购合同
|
|
|
+ this.updateById(dto);
|
|
|
+
|
|
|
+ Map<Long, ApplyBuyBom> applyBuyBomMap = applyBuyBomService.mapKEntity(
|
|
|
+ BaseIdPo::getId,
|
|
|
+ q -> q.eq(ApplyBuyBom::getApplyBuyId, dto.getApplyBuyId()));
|
|
|
+
|
|
|
+ Map<Long, PurchaseBom> purchaseBomMap = purchaseBomService.mapKEntity(
|
|
|
+ BaseIdPo::getId,
|
|
|
+ q -> q.eq(PurchaseBom::getPurchaseId, dto.getId()));
|
|
|
+
|
|
|
+ List<PurchaseBom> purchaseBomList = dto.getPurchaseBomList();
|
|
|
+ purchaseBomList.forEach(item -> {
|
|
|
+ item.setPurchaseId(dto.getId());
|
|
|
+
|
|
|
+ ApplyBuyBom applyBuyBom = applyBuyBomMap.get(item.getApplyBuyBomId());
|
|
|
+ if (applyBuyBom == null) {
|
|
|
+ throw new ServiceException("存在未知申购明细id");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal quantity = applyBuyBom.getQuantity();
|
|
|
+ BigDecimal purchaseQuantity = applyBuyBom.getPurchaseQuantity();
|
|
|
+ BigDecimal frozenQuantity = applyBuyBom.getFrozenQuantity();
|
|
|
+ BigDecimal itemPurchaseQuantity = item.getPurchaseQuantity();
|
|
|
+
|
|
|
+ if (item.getId() != null) {
|
|
|
+ PurchaseBom purchaseBom = purchaseBomMap.get(item.getId());
|
|
|
+ if (purchaseBom == null) {
|
|
|
+ throw new ServiceException("存在未知采购明细id");
|
|
|
+ }
|
|
|
+ frozenQuantity = frozenQuantity.subtract(purchaseBom.getPurchaseQuantity());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (quantity.compareTo(purchaseQuantity.add(frozenQuantity).add(itemPurchaseQuantity)) >= 0) {
|
|
|
+ applyBuyBom.setFrozenQuantity(frozenQuantity.add(itemPurchaseQuantity));
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("采购数量超过申购数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ purchaseBomService.editLinked(purchaseBomList, PurchaseBom::getPurchaseId, dto.getId());
|
|
|
+
|
|
|
+ ObsFileUtil.editFile(dto.getFileList(), dto.getId());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void delete(Long id) {
|
|
|
- removeById(id);
|
|
|
+ this.removeById(id);
|
|
|
+
|
|
|
+ Map<Long, PurchaseBom> purchaseBomMap = purchaseBomService.mapKEntity(
|
|
|
+ PurchaseBom::getApplyBuyBomId,
|
|
|
+ q -> q.eq(PurchaseBom::getPurchaseId, id));
|
|
|
+
|
|
|
+ List<ApplyBuyBom> applyBuyBomList = applyBuyBomService.list(q -> q.eq(ApplyBuyBom::getApplyBuyId, id));
|
|
|
+ applyBuyBomList.forEach(item -> {
|
|
|
+ PurchaseBom purchaseBom = purchaseBomMap.get(item.getId());
|
|
|
+ if (purchaseBom == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ item.setFrozenQuantity(item.getFrozenQuantity().subtract(purchaseBom.getPurchaseQuantity()));
|
|
|
+ });
|
|
|
+ applyBuyBomService.updateBatchById(applyBuyBomList);
|
|
|
+
|
|
|
+ purchaseBomService.remove(q -> q.eq(PurchaseBom::getPurchaseId, id));
|
|
|
+
|
|
|
+ ObsFileUtil.removeFile(id);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -135,6 +303,23 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public List<PurchaseVo> getPurchaseInStorageList() {
|
|
|
+ List<PurchaseVo> list = Sql.create(PurchaseVo.class)
|
|
|
+ .selectAll(Purchase.class)
|
|
|
+ .selectAs(Supplier::getName, PurchaseVo::getSupplierName)
|
|
|
+ .selectAs(ApplyBuy::getCode, PurchaseVo::getApplyBuyCode)
|
|
|
+ .from(Purchase.class)
|
|
|
+ .leftJoin(Supplier.class, Supplier::getId, Purchase::getSupplierId)
|
|
|
+ .leftJoin(ApplyBuy.class, ApplyBuy::getId, Purchase::getApplyBuyId)
|
|
|
+ .eq(Purchase::getFlowStatus, FlowStatusEnum.PASS.getKey())
|
|
|
+ .eq(Purchase::getStorageStatus, StatusConstant.NO)
|
|
|
+ .eq(Purchase::getStatus, PurchaseStatusEnum.UNDER_PURCHASE.getKey())
|
|
|
+ .orderByDesc(Purchase::getId)
|
|
|
+ .list();
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void purchaseTermination(Long id) {
|
|
|
Purchase purchase = this.getById(id);
|
|
|
if (purchase == null) {
|