package cn.iocoder.yudao.module.asset.service.item;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.asset.controller.admin.item.vo.AssetItemPageReqVO;
|
import cn.iocoder.yudao.module.asset.controller.admin.item.vo.AssetItemSaveReqVO;
|
import cn.iocoder.yudao.module.asset.dal.dataobject.item.AssetItemDO;
|
import cn.iocoder.yudao.module.asset.dal.mysql.item.AssetItemMapper;
|
import cn.iocoder.yudao.module.asset.dal.mysql.requisition.AssetRequisitionMapper;
|
import cn.iocoder.yudao.module.asset.dal.redis.AssetNoRedisDAO;
|
import jakarta.annotation.Resource;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.math.BigDecimal;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.module.asset.enums.ErrorCodeConstants.*;
|
|
/**
|
* 资产台账 Service 实现类
|
*/
|
@Service
|
@Validated
|
@Slf4j
|
public class AssetItemServiceImpl implements AssetItemService {
|
|
@Resource
|
private AssetItemMapper itemMapper;
|
|
@Resource
|
private AssetRequisitionMapper requisitionMapper;
|
|
@Resource
|
private AssetNoRedisDAO noRedisDAO;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Long createItem(AssetItemSaveReqVO reqVO) {
|
String assetNo = StrUtil.trimToNull(reqVO.getAssetNo());
|
if (assetNo == null) {
|
assetNo = noRedisDAO.generate(AssetNoRedisDAO.ASSET_ITEM_NO_PREFIX);
|
} else if (itemMapper.selectByAssetNo(assetNo) != null) {
|
throw exception(ASSET_ITEM_NO_EXISTS, assetNo);
|
}
|
final String finalAssetNo = assetNo;
|
AssetItemDO item = BeanUtils.toBean(reqVO, AssetItemDO.class, in -> in
|
.setAssetNo(finalAssetNo)
|
.setUnit(StrUtil.blankToDefault(reqVO.getUnit(), "个"))
|
.setQuantity(reqVO.getQuantity() == null ? 0 : reqVO.getQuantity())
|
.setTotalAmount(calcTotalAmount(reqVO.getUnitPrice(), reqVO.getQuantity())));
|
itemMapper.insert(item);
|
return item.getId();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void updateItem(AssetItemSaveReqVO reqVO) {
|
AssetItemDO old = validateItemExists(reqVO.getId());
|
String assetNo = StrUtil.trimToNull(reqVO.getAssetNo());
|
if (assetNo != null && !assetNo.equals(old.getAssetNo())) {
|
AssetItemDO exists = itemMapper.selectByAssetNo(assetNo);
|
if (exists != null && !exists.getId().equals(reqVO.getId())) {
|
throw exception(ASSET_ITEM_NO_EXISTS, assetNo);
|
}
|
}
|
AssetItemDO updateObj = BeanUtils.toBean(reqVO, AssetItemDO.class, in -> in
|
.setAssetNo(assetNo)
|
.setUnit(StrUtil.blankToDefault(reqVO.getUnit(), "个"))
|
.setTotalAmount(calcTotalAmount(reqVO.getUnitPrice(), reqVO.getQuantity())));
|
itemMapper.updateById(updateObj);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void deleteItem(Long id) {
|
AssetItemDO item = validateItemExists(id);
|
Long activeCount = requisitionMapper.selectCountByAssetIdAndNotReturned(id);
|
if (activeCount != null && activeCount > 0) {
|
throw exception(ASSET_ITEM_HAS_ACTIVE_REQUISITION, item.getName(), activeCount);
|
}
|
itemMapper.deleteById(id);
|
}
|
|
@Override
|
public AssetItemDO getItem(Long id) {
|
return itemMapper.selectById(id);
|
}
|
|
@Override
|
public List<AssetItemDO> getItemList(Collection<Long> ids) {
|
if (CollUtil.isEmpty(ids)) {
|
return Collections.emptyList();
|
}
|
return itemMapper.selectBatchIds(ids);
|
}
|
|
@Override
|
public List<AssetItemDO> getItemSimpleList() {
|
return itemMapper.selectList();
|
}
|
|
@Override
|
public PageResult<AssetItemDO> getItemPage(AssetItemPageReqVO reqVO) {
|
return itemMapper.selectPage(reqVO);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void decreaseStock(Long id, Integer quantity) {
|
if (quantity == null || quantity <= 0) {
|
throw exception(ASSET_ITEM_QUANTITY_INVALID);
|
}
|
AssetItemDO item = validateItemExists(id);
|
int rows = itemMapper.decreaseStock(id, quantity);
|
if (rows == 0) {
|
throw exception(ASSET_ITEM_STOCK_NOT_ENOUGH, item.getName(),
|
item.getQuantity() == null ? 0 : item.getQuantity(), quantity);
|
}
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void increaseStock(Long id, Integer quantity) {
|
if (quantity == null || quantity <= 0) {
|
throw exception(ASSET_ITEM_QUANTITY_INVALID);
|
}
|
validateItemExists(id);
|
itemMapper.increaseStock(id, quantity);
|
}
|
|
private AssetItemDO validateItemExists(Long id) {
|
AssetItemDO item = id != null ? itemMapper.selectById(id) : null;
|
if (item == null) {
|
throw exception(ASSET_ITEM_NOT_EXISTS);
|
}
|
return item;
|
}
|
|
private BigDecimal calcTotalAmount(BigDecimal unitPrice, Integer quantity) {
|
if (unitPrice == null || quantity == null) {
|
return BigDecimal.ZERO;
|
}
|
return unitPrice.multiply(BigDecimal.valueOf(quantity));
|
}
|
|
}
|