派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
package com.qxueyou.scc.sys.service.impl;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.IFileUploadService;
import com.qxueyou.scc.base.service.impl.CommonAppService;
import com.qxueyou.scc.base.util.CollectionUtils;
import com.qxueyou.scc.base.util.TraceUtils;
import com.qxueyou.scc.sys.model.SysAttachment;
import com.qxueyou.scc.sys.model.SysFileUploadTrace;
import com.qxueyou.scc.sys.service.ISysAttachmentService;
 
/**
 * 数据库配置服务
 * @author kevin
 *
 */
@Service
public class SysAttachmentService extends CommonAppService implements ISysAttachmentService {
    
    private final Logger logger = LogManager.getLogger(SysAttachmentService.class);
    
    @Autowired
    IFileUploadService fileUploadService;
 
    @Override
    public Result querySysAttachement(String relaedObjectId) {
        
        String hql = "from SysAttachment s where s.realatedObjectId = ? and s.deleteFlag is false order by s.createTime desc";
 
        List<SysAttachment> lstAttachements = this.find(hql, CollectionUtils.newList(relaedObjectId), SysAttachment.class);
 
        return new Result(true, "",lstAttachements);
    }
 
    @Override
    public Result insertSysAttachment(String attachmentName, String attachmentPath,long fileSize , String relatedObjectId) {
        SysAttachment attachement = null;
        String fileSuffix = null;
        Result result = new Result(false);
        try {
            fileSuffix = StringUtils.substring(attachmentPath, attachmentPath.lastIndexOf('.'));
            attachement = new SysAttachment();
            TraceUtils.setCreateTrace(attachement);
            attachement.setAttachmentId(null);
            attachement.setAttachmentName(attachmentName);
            attachement.setAttachmentPath(attachmentPath);
            attachement.setFileSize(fileSize);
            attachement.setRealatedObjectId(relatedObjectId);
            attachement.setFileSuffix(fileSuffix);
            attachement.setAttachmentType(0);
            this.save(attachement);
            
            // 更新上传文件使用轨迹
            fileUploadService.updateUploadTrace(attachmentPath, "sys_attachment_file", SysFileUploadTrace.FILE_USE, attachement.getAttachmentId());            
            result =new Result(true);
            Map<String,Object> attrMap = new HashMap<String,Object>();
            attrMap.put("attachmentId", attachement.getAttachmentId());
            result.setData(attrMap);
        } catch (Exception ex) {
            logger.error("新增附件出错。。attachmentName="+attachmentName,ex);
        }
        
        return result;
    }
 
    @Override
    public Result deleteSysAttachement(String attachementIds) {
        Result result = new Result(false);
        SysAttachment tempAttach = null;
        String[] arrAttachementId = null;
        try {
            // 保存到服务器
            if(StringUtils.isEmpty(attachementIds)){
                return new Result(false,"参数错误,需要删除的附件不存在");
            }
            
            arrAttachementId = attachementIds.split(",");
            for (String tempId : arrAttachementId) {
                tempAttach = read(SysAttachment.class, tempId);
                tempAttach.setDeleteFlag(true);
                TraceUtils.setUpdateTrace(tempAttach);
                this.save(tempAttach);
            }
            result = new Result(true);
        } catch (Exception ex) {
            logger.error("删除附件出错。。attachementIds="+attachementIds,ex);
        }
        return result;
    }
}