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
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.dnc.entity.DeviceInfo;
import org.jeecg.modules.dnc.entity.DocFile;
import org.jeecg.modules.dnc.entity.DocInfo;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DocFileMapper;
import org.jeecg.modules.dnc.mapper.DocInfoMapper;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.dnc.utils.file.DocVersionUtil;
import org.jeecg.modules.dnc.response.DocumentCode;
import org.jeecg.modules.dnc.service.IDocFileService;
import org.jeecg.modules.dnc.service.IDocInfoService;
import org.jeecg.modules.dnc.service.IDocRelativeService;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
public class DocFileServiceImpl extends ServiceImpl<DocFileMapper, DocFile> implements IDocFileService {
    @Autowired
    private DocInfoMapper docInfoMapper;
    @Autowired
    @Lazy
    private IDocRelativeService docRelativeService;
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDocFile(DocFile docFile) {
        if(docFile == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(docFile.getDocId()))
            ExceptionCast.cast(DocumentCode.DOC_FILE_DOC_ID_NONE);
        if(!ValidateUtil.validateString(docFile.getFileName()) || !ValidateUtil.validateString(docFile.getFileEncodeName()))
            ExceptionCast.cast(DocumentCode.DOCUMENT_NAME_NONE);
        if(!ValidateUtil.validateLong(docFile.getFileSize()))
            ExceptionCast.cast(DocumentCode.DOC_FILE_SIZE_NONE);
        /*if(!ValidateUtil.validateString(docFile.getFileSuffix()))
            ExceptionCast.cast(DocumentCode.DOC_SUFFIX_ERROR);*/
        if(!ValidateUtil.validateString(docFile.getFilePath()))
            ExceptionCast.cast(DocumentCode.DOC_FILE_PATH_NONE);
        DocFile nearestFile = getDocFileNearest(docFile.getDocId());
        if(nearestFile == null) {
            docFile.setDocVersion(DocVersionUtil.generateNewVersion(null));
        }else {
            docFile.setDocVersion(DocVersionUtil.generateNewVersion(nearestFile.getDocVersion()));
        }
        return super.save(docFile);
    }
 
    @Override
    public DocFile getDocFileNearest(String docId) {
        if(!ValidateUtil.validateString(docId))
            return null;
        LambdaQueryWrapper<DocFile> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DocFile::getDocId, docId).orderByDesc(DocFile::getCreateTime).orderByDesc(DocFile::getFileId);
        return super.getOne(lambdaQueryWrapper, false);
    }
 
    @Override
    public List<DocFile> findListByDocId(String docId) {
        DocInfo docInfo = docInfoMapper.selectById(docId);
        if(!ValidateUtil.validateString(docId))
            return null;
        List<DocFile> fileList=super.lambdaQuery().eq(DocFile::getDocId, docId).orderByDesc(DocFile::getFileId).list();
        fileList.forEach(item->{
            //对比版本
            item.setPublishFlag(docInfo.getPublishVersion().equals(item.getDocVersion()));
        });
        return fileList;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignFileVersion(String fileId) {
        if(!ValidateUtil.validateString(fileId))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        DocFile docFile = super.getById(fileId);
        if(docFile == null) {
            ExceptionCast.cast(DocumentCode.DOC_FILE_ERROR);
        }
        List<MdcEquipment> deviceList = docRelativeService.findDeviceByDocId(docFile.getDocId());
        if(deviceList != null && !deviceList.isEmpty())
            ExceptionCast.cast(DocumentCode.DOC_DEVICE_EXIST);
        DocInfo docInfo = new DocInfo();
        docInfo.setDocId(docFile.getDocId());
        docInfo.setPublishFileId(docFile.getFileId());
        docInfo.setPublishVersion(docFile.getDocVersion());
        return docInfoMapper.updateById(docInfo) >= 0;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByDocAttr(Integer type, String id) {
        return super.getBaseMapper().deleteByDocAttr(type, id) >= 0;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByDocId(String docId) {
        LambdaQueryWrapper<DocFile> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DocFile::getDocId, docId);
        return super.remove(lambdaQueryWrapper);
    }
}