jiangping
2025-06-06 77946261ec663aa1fe7f6f97e550532ed879f982
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.doumee.service.business.impl;
 
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.annotation.excel.ExcelImporter;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.LoginUserModel;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.CategoryMapper;
import com.doumee.dao.business.YwMaterialMapper;
import com.doumee.dao.business.YwOutinboundMapper;
import com.doumee.dao.business.YwOutinboundRecordMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.MultifileMapper;
import com.doumee.dao.system.dto.ImportMaterialDTO;
import com.doumee.dao.system.dto.ImportSystemUserDTO;
import com.doumee.dao.system.model.Multifile;
import com.doumee.service.business.YwMaterialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import javafx.scene.paint.Material;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 运维资产信息表Service实现
 * @author 江蹄蹄
 * @date 2025/01/06 11:05
 */
@Service
public class YwMaterialServiceImpl implements YwMaterialService {
 
    @Autowired
    private YwMaterialMapper ywMaterialMapper;
 
    @Autowired
    private MultifileMapper multifileMapper;
 
    @Autowired
    private YwOutinboundRecordMapper ywOutinboundRecordMapper;
 
    @Autowired
    private CategoryMapper categoryMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(YwMaterial ywMaterial) {
        //TODO rk 所属项目/房间需求问题
        if(Objects.isNull(ywMaterial)
            || StringUtils.isBlank(ywMaterial.getName())
                || StringUtils.isBlank(ywMaterial.getQrcode())
                || Objects.isNull(ywMaterial.getCateId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        ywMaterial.setId(null);
        if(StringUtils.isBlank(ywMaterial.getCode())){
            //自动生成 TODO 存在问题
            YwMaterial lastAutoMaterial = ywMaterialMapper.selectOne(new QueryWrapper<YwMaterial>().lambda().eq(YwMaterial::getAutoCode,Constants.ONE).last("limit 1 "));
            Long countCode = 0L;
            String nextCode = StringUtils.leftPad(Long.toString(countCode + 1 ),4,"0");
            if(Objects.nonNull(lastAutoMaterial)){
                Long maxCode = Long.valueOf(lastAutoMaterial.getCode().replace("P",""));
                nextCode = StringUtils.leftPad(Long.toString(maxCode + 1),4,"0");
                while (ywMaterialMapper.selectCount(new QueryWrapper<YwMaterial>().lambda().eq(YwMaterial::getCode, "P"+nextCode))>Constants.ZERO){
                    countCode = countCode + 1 ;
                    nextCode = StringUtils.leftPad(Long.toString(countCode),4,"0");
                }
            }
            ywMaterial.setCode("P"+nextCode);
            ywMaterial.setAutoCode(Constants.ONE);
        }else{
            //验证code是否存在
            if(ywMaterialMapper.selectCount(new QueryWrapper<YwMaterial>().lambda().eq(YwMaterial::getCode, ywMaterial.getCode()))>0){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前编码已存在");
            };
            ywMaterial.setAutoCode(Constants.ZERO);
        }
        LoginUserInfo loginUserInfo = ywMaterial.getLoginUserInfo();
        ywMaterial.setCreator(loginUserInfo.getId());
        ywMaterial.setCreateDate(new Date());
        ywMaterial.setIsdeleted(Constants.ZERO);
        ywMaterial.setStatus(Constants.ZERO);
        ywMaterialMapper.insert(ywMaterial);
 
        if(Objects.nonNull(ywMaterial.getMultifile())){
            if(org.apache.commons.lang3.StringUtils.isBlank(ywMaterial.getMultifile().getFileurl())
                    || org.apache.commons.lang3.StringUtils.isBlank(ywMaterial.getMultifile().getName())){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"附件信息错误");
            }
            ywMaterial.getMultifile().setCreator(loginUserInfo.getId());
            ywMaterial.getMultifile().setCreateDate(new Date());
            ywMaterial.getMultifile().setIsdeleted(Constants.ZERO);
            ywMaterial.getMultifile().setObjType(Constants.MultiFile.MATERIAL_FILE.getKey());
            ywMaterial.getMultifile().setObjId(ywMaterial.getId());
            multifileMapper.insert(ywMaterial.getMultifile());
        }
        return ywMaterial.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        if(ywOutinboundRecordMapper.selectCount(new QueryWrapper<YwOutinboundRecord>().lambda().eq(YwOutinboundRecord::getMaterialId,id))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前物料已有出入库记录,不可删除");
        }
        ywMaterialMapper.update(new UpdateWrapper<YwMaterial>().lambda().set(YwMaterial::getIsdeleted,Constants.ONE).eq(YwMaterial::getId,id));
    }
 
    @Override
    public void delete(YwMaterial ywMaterial) {
        UpdateWrapper<YwMaterial> deleteWrapper = new UpdateWrapper<>(ywMaterial);
        ywMaterialMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        ywMaterialMapper.deleteBatchIds(ids);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void updateById(YwMaterial ywMaterial) {
        //TODO rk 所属项目/房间需求问题
        if(Objects.isNull(ywMaterial)
                || StringUtils.isBlank(ywMaterial.getName())
                || StringUtils.isBlank(ywMaterial.getCode())
                || StringUtils.isBlank(ywMaterial.getQrcode())
                || Objects.isNull(ywMaterial.getCateId())
                || Objects.isNull(ywMaterial.getId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        ywMaterial.setCode(null);
        LoginUserInfo loginUserInfo = ywMaterial.getLoginUserInfo();
        ywMaterial.setEditor(loginUserInfo.getId());
        ywMaterial.setEditDate(new Date());
        ywMaterialMapper.updateById(ywMaterial);
        multifileMapper.delete(new QueryWrapper<Multifile>().lambda().eq(Multifile::getObjType,Constants.MultiFile.MATERIAL_FILE.getKey()).eq(Multifile::getObjId,ywMaterial.getId()));
        if(Objects.nonNull(ywMaterial.getMultifile())){
            if(org.apache.commons.lang3.StringUtils.isBlank(ywMaterial.getMultifile().getFileurl())
                    || org.apache.commons.lang3.StringUtils.isBlank(ywMaterial.getMultifile().getName())){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"附件信息错误");
            }
            ywMaterial.getMultifile().setCreator(loginUserInfo.getId());
            ywMaterial.getMultifile().setCreateDate(new Date());
            ywMaterial.getMultifile().setIsdeleted(Constants.ZERO);
            ywMaterial.getMultifile().setObjType(Constants.MultiFile.MATERIAL_FILE.getKey());
            ywMaterial.getMultifile().setObjId(ywMaterial.getId());
            multifileMapper.insert(ywMaterial.getMultifile());
        }
    }
 
    @Override
    public void updateByIdInBatch(List<YwMaterial> ywMaterials) {
        if (CollectionUtils.isEmpty(ywMaterials)) {
            return;
        }
        for (YwMaterial ywMaterial: ywMaterials) {
            this.updateById(ywMaterial);
        }
    }
 
    @Override
    public void updateStatus(YwMaterial ywMaterials) {
       if(Objects.isNull(ywMaterials)
           || Objects.isNull(ywMaterials.getId())
           || Objects.isNull(ywMaterials.getStatus())){
           throw new BusinessException(ResponseStatus.BAD_REQUEST);
       }
        ywMaterialMapper.update(new UpdateWrapper<YwMaterial>().lambda().eq(YwMaterial::getId,ywMaterials.getId()).set(YwMaterial::getStatus,ywMaterials.getStatus()));
    }
 
    @Override
    public YwMaterial findById(Integer id) {
        return ywMaterialMapper.selectById(id);
    }
 
    @Override
    public YwMaterial getDetail(Integer id) {
        YwMaterial ywMaterial = ywMaterialMapper.selectById(id);
        if(Objects.isNull(ywMaterial)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        Multifile multifile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda().eq(Multifile::getObjId,id).eq(Multifile::getObjType,Constants.MultiFile.MATERIAL_FILE.getKey()));
        if(Objects.nonNull(multifile)){
            String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode()
                    +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_MATERIAL).getCode();
            multifile.setFileurlFull(path + multifile.getFileurl());
            ywMaterial.setMultifile(multifile);
        }
        return ywMaterial;
    }
 
    @Override
    public YwMaterial findOne(YwMaterial ywMaterial) {
        QueryWrapper<YwMaterial> wrapper = new QueryWrapper<>(ywMaterial);
        return ywMaterialMapper.selectOne(wrapper);
    }
 
    @Override
    public List<YwMaterial> findList(YwMaterial ywMaterial) {
        QueryWrapper<YwMaterial> wrapper = new QueryWrapper<>(ywMaterial);
        return ywMaterialMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<YwMaterial> findPage(PageWrap<YwMaterial> pageWrap) {
        IPage<YwMaterial> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<YwMaterial> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        YwMaterial model = pageWrap.getModel();
        queryWrapper.selectAll(YwMaterial.class)
                .leftJoin(YwRoom.class,YwRoom::getId,YwMaterial::getRoomId)
                .and(StringUtils.isNotBlank(model.getCode()),i->i.like(YwMaterial::getCode,model.getCode()).or().like(YwMaterial::getName,model.getCode()))
                .like(StringUtils.isNotBlank(model.getAttr()),YwMaterial::getAttr,model.getAttr())
                .eq(Objects.nonNull(model.getProjectId()),YwRoom::getProjectId,model.getProjectId())
                .eq(Objects.nonNull(model.getStatus()),YwMaterial::getStatus,model.getStatus())
                .eq(Objects.nonNull(model.getRoomId()),YwMaterial::getRoomId,model.getRoomId())
                .eq(Objects.nonNull(model.getCateId()),YwMaterial::getCateId,model.getCateId())
                .eq(Objects.nonNull(model.getParentCateId()),YwMaterial::getParentCateId,model.getParentCateId())
                .eq(Objects.nonNull(model.getFloorId()),YwRoom::getFloor,model.getFloorId())
                .eq(Objects.nonNull(model.getBuildingId()),YwRoom::getBuildingId,model.getBuildingId())
                .eq(YwMaterial::getIsdeleted,Constants.ZERO)
                .orderByAsc(YwMaterial::getCode)
        ;
 
        IPage<YwMaterial> iPage = ywMaterialMapper.selectJoinPage(page,YwMaterial.class,queryWrapper);
        String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode()
                +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_MATERIAL).getCode();
        for (YwMaterial ywMaterial:iPage.getRecords()) {
            Multifile multifile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda().eq(Multifile::getObjId,ywMaterial.getId()).eq(Multifile::getObjType,Constants.MultiFile.MATERIAL_FILE.getKey()));
            if(Objects.nonNull(multifile)){
                multifile.setFileurlFull(path + multifile.getFileurl());
                ywMaterial.setMultifile(multifile);
            }
        }
        return PageData.from(iPage);
    }
 
    @Override
    public long count(YwMaterial ywMaterial) {
        QueryWrapper<YwMaterial> wrapper = new QueryWrapper<>(ywMaterial);
        return ywMaterialMapper.selectCount(wrapper);
    }
 
 
    @Override
    public Integer importMaterialBatch(MultipartFile file,LoginUserInfo loginUserInfo) {
        try {
            ExcelImporter ie = new ExcelImporter(file, 0, 0);
            List<ImportMaterialDTO> dataList = ie.getDataList(ImportMaterialDTO.class, null);
            if (CollectionUtils.isEmpty(dataList)) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,录入数据为空!");
            }
 
            List<Category> allCategory = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                    .eq(Category::getIsdeleted,Constants.ZERO).eq(Category::getIsdeleted,Constants.ZERO)
                    .eq(Category::getType,Constants.SEVEN)
            );
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(allCategory)){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,无分类基础数据!");
            }
            List<Category> categoryList = allCategory.stream().filter(i->Objects.isNull(i.getParentId())).collect(Collectors.toList());
            for (Category category: categoryList) {
                category.setChildCategoryList(
                        allCategory.stream().filter(i->Objects.nonNull(i.getParentId())&&Constants.equalsInteger(i.getParentId(),category.getId())).collect(Collectors.toList())
                );
            }
            List<YwMaterial> ywMaterialList = new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                ImportMaterialDTO importMaterialDTO = dataList.get(i);
                if(Objects.isNull(importMaterialDTO)
                    || StringUtils.isBlank(importMaterialDTO.getName())
                    || StringUtils.isBlank(importMaterialDTO.getCategoryName())
                    || StringUtils.isBlank(importMaterialDTO.getCategoryChildName())
                    || StringUtils.isBlank(importMaterialDTO.getQrcode())
                ){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第"+(i+1)+"行必填项缺失!");
                }
                YwMaterial ywMaterial = new YwMaterial();
                BeanUtils.copyProperties(importMaterialDTO,ywMaterial);
                Optional<Category> categoryOptional = categoryList.stream().filter(j->j.getName().equals(importMaterialDTO.getCategoryName())).findAny();
                if(categoryOptional.isPresent()){
                    Category category = categoryOptional.get();
                    ywMaterial.setParentCateId(category.getId());
                    Optional<Category> childCategoryOptional = category.getChildCategoryList().stream().filter(j->j.getName().equals(importMaterialDTO.getCategoryChildName())).findAny();
                    if(childCategoryOptional.isPresent()){
                        ywMaterial.setCateId(childCategoryOptional.get().getId());
                    }else{
                        throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第"+(i+1)+"行物料子分类未查询到!");
                    }
                }else{
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第"+(i+1)+"行物料分类未查询到!");
                }
                ywMaterialList.add(ywMaterial);
            }
 
            for (YwMaterial ywMaterial:ywMaterialList) {
                ywMaterial.setLoginUserInfo(loginUserInfo);
                this.create(ywMaterial);
            }
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),e.getMessage());
 
        }
        return null;
    }
}