mitao
2024-12-18 ae265295dd977b0d64309b0471e8d3b0d626ccf3
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
package com.sinata.system.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.core.domain.entity.SysDictData;
import com.sinata.common.entity.PageDTO;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
import com.sinata.system.domain.MwProtectionEquipment;
import com.sinata.system.domain.MwProtectionEquipmentRecord;
import com.sinata.system.domain.dto.MwProtectionEquipmentDTO;
import com.sinata.system.domain.dto.MwProtectionEquipmentRecordDTO;
import com.sinata.system.domain.query.MwProtectionEquipmentQuery;
import com.sinata.system.domain.vo.MwProtectionEquipmentVO;
import com.sinata.system.mapper.MwProtectionEquipmentMapper;
import com.sinata.system.service.ISysDictDataService;
import com.sinata.system.service.MwProtectionEquipmentRecordService;
import com.sinata.system.service.MwProtectionEquipmentService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.Objects;
 
/**
 * <p>
 * 防护器具 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class MwProtectionEquipmentServiceImpl extends ServiceImpl<MwProtectionEquipmentMapper, MwProtectionEquipment> implements MwProtectionEquipmentService {
    private final SysDepartmentService sysDepartmentService;
    private final ISysDictDataService sysDictDataService;
    private final MwProtectionEquipmentRecordService mwProtectionEquipmentRecordService;
 
    /**
     * 防护器具分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwProtectionEquipmentVO> pageList(MwProtectionEquipmentQuery query) {
        String treeCode = sysDepartmentService.getTreeCode(query.getDepartmentId());
        Page<MwProtectionEquipmentVO> page = baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode);
        return PageDTO.of(page);
    }
 
    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MwProtectionEquipmentVO detail(Long id) {
        return BeanUtils.copyBean(this.getById(id), MwProtectionEquipmentVO.class);
    }
 
    /**
     * 新增防护器具
     *
     * @param dto
     * @return
     */
    @Override
    public void add(MwProtectionEquipmentDTO dto) {
        MwProtectionEquipment mwProtectionEquipment = BeanUtils.copyBean(dto, MwProtectionEquipment.class);
        SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionEquipmentType()).one();
        if (Objects.nonNull(dictData)) {
            mwProtectionEquipment.setProtectionEquipmentTypeStr(dictData.getDictLabel());
        }
        save(mwProtectionEquipment);
    }
 
    /**
     * 编辑
     *
     * @param dto
     * @return
     */
    @Override
    public void edit(MwProtectionEquipmentDTO dto) {
        if (Objects.isNull(dto.getId())) {
            throw new ServiceException("防护器具id不能为空");
        }
        MwProtectionEquipment mwProtectionEquipment = BeanUtils.copyBean(dto, MwProtectionEquipment.class);
        SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionEquipmentType()).one();
        if (Objects.nonNull(dictData)) {
            mwProtectionEquipment.setProtectionEquipmentTypeStr(dictData.getDictLabel());
        }
        updateById(mwProtectionEquipment);
    }
 
    @Override
    public void addStock(MwProtectionEquipmentRecordDTO dto) {
        MwProtectionEquipment equipment = getById(dto.getProtectionEquipmentId());
        if (Objects.isNull(equipment)) {
            throw new ServiceException("防护器具不存在");
        }
        equipment.setStock(equipment.getStock() + dto.getChangeQuantity());
        updateById(equipment);
        // 新增库存记录
        MwProtectionEquipmentRecord mwProtectionEquipmentRecord = BeanUtils.copyBean(dto, MwProtectionEquipmentRecord.class);
        mwProtectionEquipmentRecord.setType(1);
        mwProtectionEquipmentRecordService.save(mwProtectionEquipmentRecord);
    }
}