[测评系统]--测评系统核心代码库
linzhijie
2021-03-11 84fea994d2db7dc313ad1774f34eb12a45f8d6e7
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
package com.ots.project.exam.service.impl;
import com.ots.common.exception.BusinessException;
import com.ots.common.utils.DateUtils;
import com.ots.common.utils.StringUtils;
import com.ots.common.utils.text.Convert;
import com.ots.project.exam.domain.TLibraryCode;
import com.ots.project.exam.mapper.TLibraryCodeMapper;
import com.ots.project.exam.service.ITLibraryCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
 
@Slf4j
@Service(value = "iTLibraryCodeService")
public class TLibraryCodeServiceImpl implements ITLibraryCodeService {
    @Autowired
    private TLibraryCodeMapper tLibraryCodeMapper;
    
    @Override
    public TLibraryCode selectTLibraryCodeById(Long id) {
        return tLibraryCodeMapper.selectTLibraryCodeById(id);
    }
    
    @Override
    public List<TLibraryCode> selectTLibraryCodeList(TLibraryCode tLibraryCode) {
        return tLibraryCodeMapper.selectTLibraryCodeList(tLibraryCode);
    }
    
    @Override
    public int insertTLibraryCode(TLibraryCode tLibraryCode) {
        tLibraryCode.setCreateTime(DateUtils.getNowDate());
        return tLibraryCodeMapper.insertTLibraryCode(tLibraryCode);
    }
    
    @Override
    public int updateTLibraryCode(TLibraryCode tLibraryCode) {
        tLibraryCode.setUpdateTime(DateUtils.getNowDate());
        return tLibraryCodeMapper.updateTLibraryCode(tLibraryCode);
    }
    
    @Override
    public int deleteTLibraryCodeByIds(String ids) {
        return tLibraryCodeMapper.deleteTLibraryCodeByIds(Convert.toStrArray(ids));
    }
    
    public int deleteTLibraryCodeById(Long id) {
        return tLibraryCodeMapper.deleteTLibraryCodeById(id);
    }
    @Override
    public String importLibraryCodes(List<TLibraryCode> libraryCodes, boolean updateSupport) {
        if (StringUtils.isNull(libraryCodes) || libraryCodes.size() == 0) {
            throw new BusinessException("导入数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TLibraryCode libraryCode : libraryCodes) {
            try {
                
                TLibraryCode code = new TLibraryCode();
                code.setLibraryCode(libraryCode.getLibraryCode());
                List<TLibraryCode> codes = tLibraryCodeMapper.selectTLibraryCodeList(code);
                if (StringUtils.isNull(codes) || codes.size() == 0) {
                    insertTLibraryCode(libraryCode);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、报告字典 " + libraryCode.getLibraryCode() + " 导入成功");
                } else if (updateSupport) {
                    libraryCode.setId(codes.get(0).getId());
                    this.updateTLibraryCode(libraryCode);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、报告字典 " + libraryCode.getLibraryCode() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、报告字典 " + libraryCode.getLibraryCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、报告字典 " + libraryCode.getLibraryCode() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }
}