| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package com.jeesite.modules.mes.service;
- import java.util.List;
- import javax.validation.ConstraintViolation;
- import javax.validation.ConstraintViolationException;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.multipart.MultipartFile;
- import com.jeesite.common.config.Global;
- import com.jeesite.common.entity.Page;
- import com.jeesite.common.service.CrudService;
- import com.jeesite.common.service.ServiceException;
- import com.jeesite.common.utils.excel.ExcelImport;
- import com.jeesite.common.validator.ValidatorUtils;
- import com.jeesite.modules.mes.dao.MesSawDao;
- import com.jeesite.modules.mes.entity.MesSaw;
- /**
- * 锯切数据Service
- * @author system
- * @version 2026-06-11
- */
- @Service
- public class MesSawService extends CrudService<MesSawDao, MesSaw> {
- @Override
- public MesSaw get(MesSaw e) { return super.get(e); }
- @Override
- public Page<MesSaw> findPage(MesSaw e) { return super.findPage(e); }
- @Override
- public List<MesSaw> findList(MesSaw e) { return super.findList(e); }
- @Override
- @Transactional
- public void save(MesSaw e) { super.save(e); }
- @Override
- @Transactional
- public void updateStatus(MesSaw e) { super.updateStatus(e); }
- @Override
- @Transactional
- public void delete(MesSaw e) { super.delete(e); }
- @Transactional
- public String importData(MultipartFile file) {
- if (file == null) throw new ServiceException(text("请选择导入的数据文件!"));
- int successNum = 0; int failureNum = 0;
- StringBuilder successMsg = new StringBuilder();
- StringBuilder failureMsg = new StringBuilder();
- try (ExcelImport ei = new ExcelImport(file, 2, 0)) {
- List<MesSaw> list = ei.getDataList(MesSaw.class);
- for (MesSaw item : list) {
- try {
- ValidatorUtils.validateWithException(item);
- this.save(item);
- successNum++;
- successMsg.append("<br/>").append(successNum).append("、编号 ").append(item.getId()).append(" 导入成功");
- } catch (Exception ex) {
- failureNum++;
- String msg = "<br/>" + failureNum + "、编号 " + item.getId() + " 导入失败:";
- if (ex instanceof ConstraintViolationException) {
- for (ConstraintViolation<?> v : ((ConstraintViolationException) ex).getConstraintViolations()) {
- msg += Global.getText(v.getMessage()) + " (" + v.getPropertyPath() + ")";
- }
- } else {
- msg += ex.getMessage();
- }
- failureMsg.append(msg);
- logger.error(msg, ex);
- }
- }
- } catch (Exception ex) {
- logger.error(ex.getMessage(), ex);
- failureMsg.append(ex.getMessage());
- return failureMsg.toString();
- }
- if (failureNum > 0) {
- failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
- throw new ServiceException(failureMsg.toString());
- } else {
- successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
- }
- return successMsg.toString();
- }
- }
|