|
|
|
|
@@ -0,0 +1,164 @@
|
|
|
|
|
package com.czg.service.product.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.hutool.core.bean.copier.CopyOptions;
|
|
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.czg.exception.CzgException;
|
|
|
|
|
import com.czg.product.dto.ConsStockFlowDTO;
|
|
|
|
|
import com.czg.product.entity.ConsInfo;
|
|
|
|
|
import com.czg.product.entity.ConsStockFlow;
|
|
|
|
|
import com.czg.product.enums.InOutItemEnum;
|
|
|
|
|
import com.czg.product.enums.InOutTypeEnum;
|
|
|
|
|
import com.czg.product.param.ConsCheckStockParam;
|
|
|
|
|
import com.czg.product.param.ConsInOutStockHeadParam;
|
|
|
|
|
import com.czg.product.service.ConsStockFlowService;
|
|
|
|
|
import com.czg.product.vo.ConsCheckStockRecordVo;
|
|
|
|
|
import com.czg.sa.StpKit;
|
|
|
|
|
import com.czg.service.product.mapper.ConsInfoMapper;
|
|
|
|
|
import com.czg.service.product.mapper.ConsStockFlowMapper;
|
|
|
|
|
import com.czg.utils.PageUtil;
|
|
|
|
|
import com.mybatisflex.core.query.QueryWrapper;
|
|
|
|
|
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 耗材库存变动记录
|
|
|
|
|
*
|
|
|
|
|
* @author Tankaikai tankaikai@aliyun.com
|
|
|
|
|
* @since 1.0 2025-02-21
|
|
|
|
|
*/
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
@Service
|
|
|
|
|
public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, ConsStockFlow> implements ConsStockFlowService {
|
|
|
|
|
|
|
|
|
|
private final ConsInfoMapper consInfoMapper;
|
|
|
|
|
|
|
|
|
|
private QueryWrapper buildQueryWrapper(ConsStockFlowDTO param) {
|
|
|
|
|
QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
|
|
|
|
|
/*if (StrUtil.isNotEmpty(param.getName())) {
|
|
|
|
|
queryWrapper.like(ConsStockFlow::getName, param.getName());
|
|
|
|
|
}*/
|
|
|
|
|
Long shopId = StpKit.USER.getShopId(0L);
|
|
|
|
|
queryWrapper.eq(ConsStockFlow::getShopId, shopId);
|
|
|
|
|
queryWrapper.orderBy(ConsStockFlow::getId, false);
|
|
|
|
|
return queryWrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void inStock(ConsInOutStockHeadParam param) {
|
|
|
|
|
Long shopId = StpKit.USER.getShopId(0L);
|
|
|
|
|
Long createUserId = StpKit.USER.getLoginIdAsLong();
|
|
|
|
|
String createUserName = StpKit.USER.getAccount();
|
|
|
|
|
ConsStockFlow head = BeanUtil.copyProperties(param, ConsStockFlow.class);
|
|
|
|
|
List<ConsStockFlow> entityList = BeanUtil.copyToList(param.getBodyList(), ConsStockFlow.class);
|
|
|
|
|
List<ConsStockFlow> insertList = new ArrayList<>();
|
|
|
|
|
List<ConsInfo> updateStockList = new ArrayList<>();
|
|
|
|
|
for (ConsStockFlow entity : entityList) {
|
|
|
|
|
BeanUtil.copyProperties(head, entity, CopyOptions.create().ignoreNullValue());
|
|
|
|
|
entity.setShopId(shopId);
|
|
|
|
|
entity.setInOutType(InOutTypeEnum.IN.value());
|
|
|
|
|
entity.setInOutItem(InOutItemEnum.MANUAL_IN.value());
|
|
|
|
|
entity.setCreateUserId(createUserId);
|
|
|
|
|
entity.setCreateUserName(createUserName);
|
|
|
|
|
Long conId = entity.getConId();
|
|
|
|
|
ConsInfo consInfo = consInfoMapper.selectOneById(conId);
|
|
|
|
|
if (consInfo == null) {
|
|
|
|
|
throw new CzgException(StrUtil.format("耗材{}不存在", entity.getConName()));
|
|
|
|
|
}
|
|
|
|
|
entity.setBeforeNumber(consInfo.getStockNumber());
|
|
|
|
|
entity.setAfterNumber(NumberUtil.add(entity.getBeforeNumber(), entity.getInOutNumber()));
|
|
|
|
|
insertList.add(entity);
|
|
|
|
|
consInfo.setStockNumber(entity.getAfterNumber());
|
|
|
|
|
updateStockList.add(consInfo);
|
|
|
|
|
}
|
|
|
|
|
super.saveBatch(insertList);
|
|
|
|
|
updateStockList.parallelStream().forEach(consInfoMapper::update);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void outStock(ConsInOutStockHeadParam param) {
|
|
|
|
|
Long shopId = StpKit.USER.getShopId(0L);
|
|
|
|
|
Long createUserId = StpKit.USER.getLoginIdAsLong();
|
|
|
|
|
String createUserName = StpKit.USER.getAccount();
|
|
|
|
|
ConsStockFlow head = BeanUtil.copyProperties(param, ConsStockFlow.class);
|
|
|
|
|
List<ConsStockFlow> entityList = BeanUtil.copyToList(param.getBodyList(), ConsStockFlow.class);
|
|
|
|
|
List<ConsStockFlow> insertList = new ArrayList<>();
|
|
|
|
|
List<ConsInfo> updateStockList = new ArrayList<>();
|
|
|
|
|
for (ConsStockFlow entity : entityList) {
|
|
|
|
|
BeanUtil.copyProperties(head, entity, CopyOptions.create().ignoreNullValue());
|
|
|
|
|
entity.setInOutNumber(NumberUtil.sub(BigDecimal.ZERO, entity.getInOutNumber()));
|
|
|
|
|
entity.setShopId(shopId);
|
|
|
|
|
entity.setInOutType(InOutTypeEnum.OUT.value());
|
|
|
|
|
entity.setInOutItem(InOutItemEnum.MANUAL_OUT.value());
|
|
|
|
|
entity.setCreateUserId(createUserId);
|
|
|
|
|
entity.setCreateUserName(createUserName);
|
|
|
|
|
Long conId = entity.getConId();
|
|
|
|
|
ConsInfo consInfo = consInfoMapper.selectOneById(conId);
|
|
|
|
|
if (consInfo == null) {
|
|
|
|
|
throw new CzgException(StrUtil.format("耗材{}不存在", entity.getConName()));
|
|
|
|
|
}
|
|
|
|
|
entity.setBeforeNumber(consInfo.getStockNumber());
|
|
|
|
|
entity.setAfterNumber(NumberUtil.add(entity.getBeforeNumber(), entity.getInOutNumber()));
|
|
|
|
|
insertList.add(entity);
|
|
|
|
|
consInfo.setStockNumber(entity.getAfterNumber());
|
|
|
|
|
updateStockList.add(consInfo);
|
|
|
|
|
}
|
|
|
|
|
super.saveBatch(insertList);
|
|
|
|
|
updateStockList.parallelStream().forEach(consInfoMapper::update);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void checkStock(ConsCheckStockParam param) {
|
|
|
|
|
Long shopId = StpKit.USER.getShopId(0L);
|
|
|
|
|
Long createUserId = StpKit.USER.getLoginIdAsLong();
|
|
|
|
|
String createUserName = StpKit.USER.getAccount();
|
|
|
|
|
ConsStockFlow entity = new ConsStockFlow();
|
|
|
|
|
entity.setCreateUserId(createUserId);
|
|
|
|
|
entity.setCreateUserName(createUserName);
|
|
|
|
|
entity.setShopId(shopId);
|
|
|
|
|
entity.setConId(param.getConId());
|
|
|
|
|
entity.setConName(param.getConName());
|
|
|
|
|
entity.setPurchasePrice(param.getPrice());
|
|
|
|
|
ConsInfo consInfo = consInfoMapper.selectOneById(param.getConId());
|
|
|
|
|
if (consInfo == null) {
|
|
|
|
|
throw new CzgException(StrUtil.format("耗材{}不存在", entity.getConName()));
|
|
|
|
|
}
|
|
|
|
|
BigDecimal winLossNumber = NumberUtil.sub(param.getActualNumber(), param.getStockNumber());
|
|
|
|
|
if (!NumberUtil.equals(winLossNumber, param.getWinLossNumber())) {
|
|
|
|
|
throw new CzgException(StrUtil.format("耗材{}存在发生变动,请刷新后重试", entity.getConName()));
|
|
|
|
|
}
|
|
|
|
|
entity.setBeforeNumber(consInfo.getStockNumber());
|
|
|
|
|
entity.setInOutNumber(winLossNumber);
|
|
|
|
|
entity.setAfterNumber(NumberUtil.add(entity.getBeforeNumber(), entity.getInOutNumber()));
|
|
|
|
|
if (NumberUtil.isLess(winLossNumber, BigDecimal.ZERO)) {
|
|
|
|
|
entity.setInOutType(InOutTypeEnum.OUT.value());
|
|
|
|
|
entity.setInOutItem(InOutItemEnum.LOSS_OUT.value());
|
|
|
|
|
} else {
|
|
|
|
|
entity.setInOutType(InOutTypeEnum.IN.value());
|
|
|
|
|
entity.setInOutItem(InOutItemEnum.WIN_IN.value());
|
|
|
|
|
}
|
|
|
|
|
entity.setSubTotal(NumberUtil.mul(winLossNumber, param.getPrice()));
|
|
|
|
|
entity.setRemark(param.getRemark());
|
|
|
|
|
super.save(entity);
|
|
|
|
|
consInfo.setStockNumber(entity.getAfterNumber());
|
|
|
|
|
consInfoMapper.update(consInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<ConsCheckStockRecordVo> getCheckStockRecordList(Long conId) {
|
|
|
|
|
Long shopId = StpKit.USER.getShopId(0L);
|
|
|
|
|
return super.mapper.selectListByQueryAs(query().eq(ConsStockFlow::getShopId, shopId).eq(ConsStockFlow::getConId, conId).orderBy(ConsStockFlow::getId, true), ConsCheckStockRecordVo.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|