liu
2 天以前 5d34bdd3c426bb2ca5f6627424ae67984f745603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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));
    }
 
}