|  |  |  | @ -1,12 +1,143 @@ | 
			
		
	
		
			
				
					|  |  |  |  | package com.glxp.api.service.collect; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import cn.hutool.core.date.DateUtil; | 
			
		
	
		
			
				
					|  |  |  |  | import cn.hutool.core.io.FileUtil; | 
			
		
	
		
			
				
					|  |  |  |  | import cn.hutool.core.util.XmlUtil; | 
			
		
	
		
			
				
					|  |  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
			
		
	
		
			
				
					|  |  |  |  | import com.github.pagehelper.PageHelper; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.entity.collect.RelCodeDetail; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.exception.JsonException; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.req.collect.RelCodeBatchRequest; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.res.collect.RelCodeBatchResponse; | 
			
		
	
		
			
				
					|  |  |  |  | import lombok.extern.slf4j.Slf4j; | 
			
		
	
		
			
				
					|  |  |  |  | import org.springframework.stereotype.Service; | 
			
		
	
		
			
				
					|  |  |  |  | import javax.annotation.Resource; | 
			
		
	
		
			
				
					|  |  |  |  | import java.util.List; | 
			
		
	
		
			
				
					|  |  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.dao.collect.RelCodeBatchMapper; | 
			
		
	
		
			
				
					|  |  |  |  | import com.glxp.api.entity.collect.RelCodeBatch; | 
			
		
	
		
			
				
					|  |  |  |  | import org.springframework.transaction.annotation.Transactional; | 
			
		
	
		
			
				
					|  |  |  |  | import org.springframework.web.multipart.MultipartFile; | 
			
		
	
		
			
				
					|  |  |  |  | import org.w3c.dom.Document; | 
			
		
	
		
			
				
					|  |  |  |  | import org.w3c.dom.Element; | 
			
		
	
		
			
				
					|  |  |  |  | import org.w3c.dom.Node; | 
			
		
	
		
			
				
					|  |  |  |  | import org.w3c.dom.NodeList; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import javax.annotation.Resource; | 
			
		
	
		
			
				
					|  |  |  |  | import java.util.ArrayList; | 
			
		
	
		
			
				
					|  |  |  |  | import java.util.Collections; | 
			
		
	
		
			
				
					|  |  |  |  | import java.util.List; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | @Service | 
			
		
	
		
			
				
					|  |  |  |  | @Slf4j | 
			
		
	
		
			
				
					|  |  |  |  | public class RelCodeBatchService extends ServiceImpl<RelCodeBatchMapper, RelCodeBatch> { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     @Resource | 
			
		
	
		
			
				
					|  |  |  |  |     private RelCodeDetailService relCodeDetailService; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * 上传保存关联信息 | 
			
		
	
		
			
				
					|  |  |  |  |      * @param file | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     @Transactional(rollbackFor = Exception.class) | 
			
		
	
		
			
				
					|  |  |  |  |     public void xmlUpload(MultipartFile file) { | 
			
		
	
		
			
				
					|  |  |  |  |         try { | 
			
		
	
		
			
				
					|  |  |  |  |             String fileName = file.getOriginalFilename(); | 
			
		
	
		
			
				
					|  |  |  |  |             String suffix = FileUtil.getSuffix(fileName); | 
			
		
	
		
			
				
					|  |  |  |  |             if(! "xml".equals(suffix)){ | 
			
		
	
		
			
				
					|  |  |  |  |                 throw new JsonException("仅支持XML,文件类型错误"+suffix); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             log.info(file.getSize()+"文件长度"); | 
			
		
	
		
			
				
					|  |  |  |  |             if(file.getSize() > 5 * 1024 *1024){ | 
			
		
	
		
			
				
					|  |  |  |  |                 throw new JsonException("上传文件超过5M"); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             List<RelCodeDetail> relCodeDetails = new ArrayList<>(); | 
			
		
	
		
			
				
					|  |  |  |  |             Document document = XmlUtil.readXML(file.getInputStream()); | 
			
		
	
		
			
				
					|  |  |  |  |             //获取根
 | 
			
		
	
		
			
				
					|  |  |  |  |             Element rootElement = XmlUtil.getRootElement(document); | 
			
		
	
		
			
				
					|  |  |  |  |             //截取Relation
 | 
			
		
	
		
			
				
					|  |  |  |  |             NodeList relation = rootElement.getElementsByTagName("Relation"); | 
			
		
	
		
			
				
					|  |  |  |  |             if(relation == null){ | 
			
		
	
		
			
				
					|  |  |  |  |                 throw new JsonException("XML文件解析错误"); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             Element relationE = (Element) relation.item(0); | 
			
		
	
		
			
				
					|  |  |  |  |             String productCode = relationE.getAttribute("productCode"); | 
			
		
	
		
			
				
					|  |  |  |  |             String subTypeNo = relationE.getAttribute("subTypeNo"); | 
			
		
	
		
			
				
					|  |  |  |  |             String cascade = relationE.getAttribute("cascade"); | 
			
		
	
		
			
				
					|  |  |  |  |             String packageSpec = relationE.getAttribute("packageSpec"); | 
			
		
	
		
			
				
					|  |  |  |  |             String comment = relationE.getAttribute("comment"); | 
			
		
	
		
			
				
					|  |  |  |  |             //截取Batch
 | 
			
		
	
		
			
				
					|  |  |  |  |             NodeList batch = rootElement.getElementsByTagName("Batch"); | 
			
		
	
		
			
				
					|  |  |  |  |             if(batch == null){ | 
			
		
	
		
			
				
					|  |  |  |  |                 throw new JsonException("XML文件解析错误"); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             for (int i = 0; i < batch.getLength(); i++) { | 
			
		
	
		
			
				
					|  |  |  |  |                 Node batchNode = batch.item(i); | 
			
		
	
		
			
				
					|  |  |  |  |                 RelCodeBatch relCodeBatch = new RelCodeBatch(); | 
			
		
	
		
			
				
					|  |  |  |  |                 String batchNo; | 
			
		
	
		
			
				
					|  |  |  |  |                 if (batchNode.getNodeType() == Node.ELEMENT_NODE) { | 
			
		
	
		
			
				
					|  |  |  |  |                     Element batchE = (Element) batchNode; | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setProductCode(productCode); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setSubTypeNo(subTypeNo); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setCascadeRatio(cascade); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setPackageSpec(packageSpec); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setComment(comment); | 
			
		
	
		
			
				
					|  |  |  |  |                     batchNo = batchE.getAttribute("batchNo"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setBatchNo(batchNo); | 
			
		
	
		
			
				
					|  |  |  |  |                     String madeDate = batchE.getAttribute("madeDate"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setMadeDate(DateUtil.parse(madeDate,"yyyy-MM-dd")); | 
			
		
	
		
			
				
					|  |  |  |  |                     String validateDate = batchE.getAttribute("validateDate"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setValidateDate(DateUtil.parse(validateDate,"yyyy-MM-dd")); | 
			
		
	
		
			
				
					|  |  |  |  |                     String workShop = batchE.getAttribute("workShop"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setWorkShop(workShop); | 
			
		
	
		
			
				
					|  |  |  |  |                     String lineName = batchE.getAttribute("lineName"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setLineName(lineName); | 
			
		
	
		
			
				
					|  |  |  |  |                     String lineManager = batchE.getAttribute("lineManager"); | 
			
		
	
		
			
				
					|  |  |  |  |                     relCodeBatch.setLineManager(lineManager); | 
			
		
	
		
			
				
					|  |  |  |  |                     this.save(relCodeBatch); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |                 //batch下的节点code
 | 
			
		
	
		
			
				
					|  |  |  |  |                 NodeList code = batchNode.getChildNodes(); | 
			
		
	
		
			
				
					|  |  |  |  |                 for (int j = 0; j < code.getLength(); j++) { | 
			
		
	
		
			
				
					|  |  |  |  |                     Node codeNode = code.item(j); | 
			
		
	
		
			
				
					|  |  |  |  |                     RelCodeDetail relCodeDetail = new RelCodeDetail(); | 
			
		
	
		
			
				
					|  |  |  |  |                     if (codeNode.getNodeType() == Node.ELEMENT_NODE) { | 
			
		
	
		
			
				
					|  |  |  |  |                         Element codeE = (Element) codeNode; | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetail.setBatchIdFk(relCodeBatch.getId()); | 
			
		
	
		
			
				
					|  |  |  |  |                         String curCode = codeE.getAttribute("curCode"); | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetail.setCurCode(curCode); | 
			
		
	
		
			
				
					|  |  |  |  |                         String packLayer = codeE.getAttribute("packLayer"); | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetail.setPackLayer(Integer.valueOf(packLayer)); | 
			
		
	
		
			
				
					|  |  |  |  |                         String parentCode = codeE.getAttribute("parentCode"); | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetail.setParentCode(parentCode); | 
			
		
	
		
			
				
					|  |  |  |  |                         String flag = codeE.getAttribute("flag"); | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetail.setFlag(Integer.valueOf(flag)); | 
			
		
	
		
			
				
					|  |  |  |  |                         relCodeDetails.add(relCodeDetail); | 
			
		
	
		
			
				
					|  |  |  |  |                     } | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             relCodeDetailService.saveBatch(relCodeDetails); | 
			
		
	
		
			
				
					|  |  |  |  |         }catch (Exception e){ | 
			
		
	
		
			
				
					|  |  |  |  |             throw new JsonException("上传失败:"+e.getMessage()); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public List<RelCodeBatchResponse> filterList(RelCodeBatchRequest relCodeBatchRequest) { | 
			
		
	
		
			
				
					|  |  |  |  |         if (relCodeBatchRequest == null) { | 
			
		
	
		
			
				
					|  |  |  |  |             return Collections.emptyList(); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         if (relCodeBatchRequest.getPage() != null) { | 
			
		
	
		
			
				
					|  |  |  |  |             int offset = (relCodeBatchRequest.getPage() - 1) * relCodeBatchRequest.getLimit(); | 
			
		
	
		
			
				
					|  |  |  |  |             PageHelper.offsetPage(offset, relCodeBatchRequest.getLimit()); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return this.baseMapper.filterList(relCodeBatchRequest); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public void delete(Long id) { | 
			
		
	
		
			
				
					|  |  |  |  |         if(id == null){ | 
			
		
	
		
			
				
					|  |  |  |  |             throw new JsonException("id 不能为空"); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         relCodeDetailService.remove(new LambdaQueryWrapper<RelCodeDetail>().eq(RelCodeDetail::getBatchIdFk,id)); | 
			
		
	
		
			
				
					|  |  |  |  |         this.removeById(id); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
	
		
			
				
					|  |  |  | 
 |