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 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 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 codes = tLibraryCodeMapper.selectTLibraryCodeList(code); if (StringUtils.isNull(codes) || codes.size() == 0) { insertTLibraryCode(libraryCode); successNum++; successMsg.append("
" + successNum + "、报告字典 " + libraryCode.getLibraryCode() + " 导入成功"); } else if (updateSupport) { libraryCode.setId(codes.get(0).getId()); this.updateTLibraryCode(libraryCode); successNum++; successMsg.append("
" + successNum + "、报告字典 " + libraryCode.getLibraryCode() + " 更新成功"); } else { failureNum++; failureMsg.append("
" + failureNum + "、报告字典 " + libraryCode.getLibraryCode() + " 已存在"); } } catch (Exception e) { failureNum++; String msg = "
" + 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(); } }