|
@@ -1,11 +1,26 @@
|
|
|
package com.fjhx.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.base.BaseEntity;
|
|
|
+import com.fjhx.constants.ErrorMsgConstant;
|
|
|
+import com.fjhx.constants.LibrarySupplyLockConstant;
|
|
|
+import com.fjhx.constants.StatusConstant;
|
|
|
import com.fjhx.entity.ApplyPurchase;
|
|
|
+import com.fjhx.enums.ApplyPurchaseStatusEnum;
|
|
|
import com.fjhx.mapper.ApplyPurchaseMapper;
|
|
|
import com.fjhx.service.ApplyPurchaseService;
|
|
|
+import com.fjhx.utils.Assert;
|
|
|
+import org.springblade.core.redis.lock.RedisLockClient;
|
|
|
+import org.springblade.core.secure.utils.AuthUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 申购单 服务实现类
|
|
@@ -17,4 +32,184 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class ApplyPurchaseServiceImpl extends ServiceImpl<ApplyPurchaseMapper, ApplyPurchase> implements ApplyPurchaseService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisLockClient redisLockClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void add(ApplyPurchase applyPurchase) {
|
|
|
+ Assert.notEmpty(applyPurchase.getGoodsId(), "物品id不能为空");
|
|
|
+ Assert.notEmpty(applyPurchase.getQuantity(), "申购数量不能为空");
|
|
|
+ Assert.notEmpty(applyPurchase.getPlanArrivalTime(), "到货时间不能为空");
|
|
|
+
|
|
|
+ applyPurchase.setStatus(ApplyPurchaseStatusEnum.WAIT_START.getValue());
|
|
|
+
|
|
|
+ save(applyPurchase);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initiateSubscription(List<ApplyPurchase> applyPurchaseList, Long flowLinkNo) {
|
|
|
+
|
|
|
+ String tenantId = AuthUtil.getTenantId();
|
|
|
+
|
|
|
+ Boolean flag = redisLockClient.lockFair(LibrarySupplyLockConstant.APPLY_PURCHASE_CODE_LOCK + tenantId,
|
|
|
+ () -> {
|
|
|
+ String applyPurchaseCode = createApplyPurchaseCode();
|
|
|
+ applyPurchaseList.forEach(applyPurchase -> {
|
|
|
+ // 赋值申购单号
|
|
|
+ applyPurchase.setCode(applyPurchaseCode);
|
|
|
+ // 状态改为审批中
|
|
|
+ applyPurchase.setStatus(ApplyPurchaseStatusEnum.IN_APPROVAL.getValue());
|
|
|
+ // 赋值流程id
|
|
|
+ applyPurchase.setFlowLinkNo(flowLinkNo);
|
|
|
+ });
|
|
|
+ updateBatchById(applyPurchaseList);
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+
|
|
|
+ Assert.eqTrue(flag, ErrorMsgConstant.SYSTEM_BUSY_ERROR);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void flowPass(Long flowLinkNo) {
|
|
|
+ updateFlowStatus(flowLinkNo, ApplyPurchaseStatusEnum.PASS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void flowNotPass(Long flowLinkNo) {
|
|
|
+ updateFlowStatus(flowLinkNo, ApplyPurchaseStatusEnum.NOT_PASS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void bindingOrder(List<ApplyPurchase> applyPurchaseList, Long orderId, String orderCode, Long supplierId, Integer accountPeriod) {
|
|
|
+
|
|
|
+ for (ApplyPurchase applyPurchase : applyPurchaseList) {
|
|
|
+
|
|
|
+ Long id = applyPurchase.getId();
|
|
|
+ Assert.notEmpty(id, "申购单id不能为空");
|
|
|
+
|
|
|
+ BigDecimal unitPrice = applyPurchase.getUnitPrice();
|
|
|
+ Assert.notEmpty(unitPrice, "单价不能为空");
|
|
|
+
|
|
|
+ applyPurchase.setVersion(null);
|
|
|
+ applyPurchase.setOrderId(orderId);
|
|
|
+ applyPurchase.setOrderCode(orderCode);
|
|
|
+ applyPurchase.setSupplierId(supplierId);
|
|
|
+ applyPurchase.setAccountPeriod(accountPeriod);
|
|
|
+ applyPurchase.setStatus(ApplyPurchaseStatusEnum.PURCHASED.getValue());
|
|
|
+
|
|
|
+ // 申购金额
|
|
|
+ applyPurchase.setApplyPrice(unitPrice.multiply(applyPurchase.getQuantity()));
|
|
|
+
|
|
|
+ // 到货数量
|
|
|
+ applyPurchase.setArrivalQuantity(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ // 到货金额
|
|
|
+ applyPurchase.setArrivalPrice(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ updateBatchById(applyPurchaseList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void arrival(Long applyPurchaseId, BigDecimal arrivalQuantity, Boolean autoComplete) {
|
|
|
+
|
|
|
+ ApplyPurchase applyPurchase = getById(applyPurchaseId);
|
|
|
+ Assert.notEmpty(applyPurchase, "申购单号为空");
|
|
|
+
|
|
|
+ // 添加到货数量
|
|
|
+ BigDecimal totalArrivalQuantity = applyPurchase.getArrivalQuantity().add(arrivalQuantity);
|
|
|
+ applyPurchase.setArrivalQuantity(totalArrivalQuantity);
|
|
|
+
|
|
|
+ // 赋值到货金额
|
|
|
+ applyPurchase.setArrivalPrice(totalArrivalQuantity.multiply(applyPurchase.getUnitPrice()));
|
|
|
+
|
|
|
+ // 是否全部到货
|
|
|
+ BigDecimal quantity = applyPurchase.getQuantity();
|
|
|
+ if (totalArrivalQuantity.compareTo(quantity) >= 0) {
|
|
|
+ applyPurchase.setStatus(ApplyPurchaseStatusEnum.ARRIVAL.getValue());
|
|
|
+ if (autoComplete) {
|
|
|
+ applyPurchase.setCompleteStatus(StatusConstant.YES);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ applyPurchase.setStatus(ApplyPurchaseStatusEnum.PART_ARRIVAL.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean b = updateById(applyPurchase);
|
|
|
+
|
|
|
+ Assert.eqTrue(b, ErrorMsgConstant.SYSTEM_BUSY_ERROR);
|
|
|
+
|
|
|
+ // TODO 完成订单
|
|
|
+ if (totalArrivalQuantity.compareTo(quantity) >= 0 && autoComplete) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void endApplyPurchase(Long applyPurchaseId) {
|
|
|
+ update(Wrappers.<ApplyPurchase>lambdaUpdate()
|
|
|
+ .eq(ApplyPurchase::getId, applyPurchaseId)
|
|
|
+ .set(BaseEntity::getCreateTime, new Date())
|
|
|
+ .set(BaseEntity::getUpdateUser, AuthUtil.getUserId())
|
|
|
+ .set(ApplyPurchase::getCompleteStatus, StatusConstant.YES));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void discardApplyPurchase(Long applyPurchaseId) {
|
|
|
+ update(Wrappers.<ApplyPurchase>lambdaUpdate()
|
|
|
+ .eq(ApplyPurchase::getId, applyPurchaseId)
|
|
|
+ .set(BaseEntity::getCreateTime, new Date())
|
|
|
+ .set(BaseEntity::getUpdateUser, AuthUtil.getUserId())
|
|
|
+ .set(ApplyPurchase::getStatus, ApplyPurchaseStatusEnum.DISCARD.getValue()));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成申购单号
|
|
|
+ */
|
|
|
+ private String createApplyPurchaseCode() {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ builder.append("PA");
|
|
|
+ builder.append(DateUtil.format(new Date(), "yyyyMM"));
|
|
|
+ builder.append("-");
|
|
|
+
|
|
|
+ String codePrefix = builder.toString();
|
|
|
+
|
|
|
+ ApplyPurchase applyPurchase = getOne(Wrappers.<ApplyPurchase>lambdaQuery()
|
|
|
+ .select(ApplyPurchase::getCode)
|
|
|
+ .eq(BaseEntity::getTenantId, AuthUtil.getTenantId())
|
|
|
+ .likeRight(ApplyPurchase::getCode, codePrefix)
|
|
|
+ .orderByDesc(ApplyPurchase::getCode)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (applyPurchase == null) {
|
|
|
+ return builder.append("001").toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ int codeNum = Integer.parseInt(applyPurchase.getCode().replace(codePrefix, ""));
|
|
|
+
|
|
|
+ if (codeNum < 9) {
|
|
|
+ builder.append("00");
|
|
|
+ } else if (codeNum < 99) {
|
|
|
+ builder.append("0");
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.append(codeNum + 1).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改合同审批状态
|
|
|
+ */
|
|
|
+ private void updateFlowStatus(Long flowLinkNo, ApplyPurchaseStatusEnum applyPurchaseStatusEnum) {
|
|
|
+ update(Wrappers.<ApplyPurchase>lambdaUpdate()
|
|
|
+ .eq(ApplyPurchase::getFlowLinkNo, flowLinkNo)
|
|
|
+ .set(BaseEntity::getCreateTime, new Date())
|
|
|
+ .set(BaseEntity::getUpdateUser, AuthUtil.getUserId())
|
|
|
+ .set(ApplyPurchase::getStatus, applyPurchaseStatusEnum.getValue()));
|
|
|
+ }
|
|
|
+
|
|
|
}
|