| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package com.jeesite.modules.mes.service;
- import java.util.List;
- import javax.validation.ConstraintViolation;
- import javax.validation.ConstraintViolationException;
- import com.jeesite.common.config.Global;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.multipart.MultipartFile;
- import com.jeesite.common.entity.Page;
- import com.jeesite.common.lang.StringUtils;
- 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.MesStationPiecePriceDao;
- import com.jeesite.modules.mes.entity.MesStationPiecePrice;
- /**
- * 工位计件单价Service
- */
- @Service
- public class MesStationPiecePriceService extends CrudService<MesStationPiecePriceDao, MesStationPiecePrice> {
- @Override
- public MesStationPiecePrice get(MesStationPiecePrice mesStationPiecePrice) {
- return super.get(mesStationPiecePrice);
- }
- @Override
- public Page<MesStationPiecePrice> findPage(MesStationPiecePrice mesStationPiecePrice) {
- return super.findPage(mesStationPiecePrice);
- }
- @Override
- public List<MesStationPiecePrice> findList(MesStationPiecePrice mesStationPiecePrice) {
- return super.findList(mesStationPiecePrice);
- }
- @Override
- @Transactional
- public void save(MesStationPiecePrice mesStationPiecePrice) {
- trimKeyFields(mesStationPiecePrice);
- super.save(mesStationPiecePrice);
- }
- @Transactional
- public String importData(MultipartFile file) {
- if (file == null) {
- throw new ServiceException(text("\u8bf7\u9009\u62e9\u5bfc\u5165\u7684\u6570\u636e\u6587\u4ef6\uff01"));
- }
- int successNum = 0;
- int failureNum = 0;
- StringBuilder successMsg = new StringBuilder();
- StringBuilder failureMsg = new StringBuilder();
- try (ExcelImport ei = new ExcelImport(file, 2, 0)) {
- List<MesStationPiecePrice> list = ei.getDataList(MesStationPiecePrice.class);
- for (MesStationPiecePrice row : list) {
- try {
- trimKeyFields(row);
- MesStationPiecePrice old = dao.findByLineSnAndOprno(row.getLineSn(), row.getOprno());
- if (old != null) {
- row.setId(old.getId());
- row.setIsNewRecord(false);
- }
- ValidatorUtils.validateWithException(row);
- this.save(row);
- successNum++;
- successMsg.append("<br/>").append(successNum).append("\u3001")
- .append(row.getLineSn()).append(" / ").append(row.getOprno())
- .append(" \u5bfc\u5165\u6210\u529f");
- } catch (Exception e) {
- failureNum++;
- StringBuilder msg = new StringBuilder("<br/>").append(failureNum).append("\u3001")
- .append(StringUtils.defaultString(row.getLineSn())).append(" / ")
- .append(StringUtils.defaultString(row.getOprno())).append(" \u5bfc\u5165\u5931\u8d25\uff1a");
- if (e instanceof ConstraintViolationException) {
- ConstraintViolationException cve = (ConstraintViolationException)e;
- for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
- msg.append(Global.getText(violation.getMessage())).append(" (")
- .append(violation.getPropertyPath()).append(")");
- }
- } else {
- msg.append(e.getMessage());
- }
- failureMsg.append(msg);
- logger.error(msg.toString(), e);
- }
- }
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- failureMsg.append(e.getMessage());
- return failureMsg.toString();
- }
- if (failureNum > 0) {
- failureMsg.insert(0, "\u5f88\u62b1\u6b49\uff0c\u5bfc\u5165\u5931\u8d25\uff01\u5171 " + failureNum + " \u6761\u6570\u636e\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u9519\u8bef\u5982\u4e0b\uff1a");
- throw new ServiceException(failureMsg.toString());
- }
- successMsg.insert(0, "\u606d\u559c\u60a8\uff0c\u6570\u636e\u5df2\u5168\u90e8\u5bfc\u5165\u6210\u529f\uff01\u5171 " + successNum + " \u6761\uff0c\u6570\u636e\u5982\u4e0b\uff1a");
- return successMsg.toString();
- }
- private void trimKeyFields(MesStationPiecePrice price) {
- if (StringUtils.isNotBlank(price.getLineSn())) {
- price.setLineSn(price.getLineSn().trim());
- }
- if (StringUtils.isNotBlank(price.getOprno())) {
- price.setOprno(price.getOprno().trim());
- }
- if (StringUtils.isNotBlank(price.getOprnoName())) {
- price.setOprnoName(price.getOprnoName().trim());
- }
- if (StringUtils.isBlank(price.getStatus())) {
- price.setStatus(MesStationPiecePrice.STATUS_NORMAL);
- }
- }
- @Override
- @Transactional
- public void updateStatus(MesStationPiecePrice mesStationPiecePrice) {
- super.updateStatus(mesStationPiecePrice);
- }
- @Override
- @Transactional
- public void delete(MesStationPiecePrice mesStationPiecePrice) {
- super.delete(mesStationPiecePrice);
- }
- }
|