|
|
|
|
package com.glxp.api.service.collect;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.core.util.XmlUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
import com.glxp.api.dao.collect.RelCodeBatchMapper;
|
|
|
|
|
import com.glxp.api.entity.auth.AuthAdmin;
|
|
|
|
|
import com.glxp.api.entity.collect.RelCodeBatch;
|
|
|
|
|
import com.glxp.api.entity.collect.RelCodeDetail;
|
|
|
|
|
import com.glxp.api.exception.JsonException;
|
|
|
|
|
import com.glxp.api.req.collect.RelCodeBatchRequest;
|
|
|
|
|
import com.glxp.api.req.collect.RelCodeDetailRequest;
|
|
|
|
|
import com.glxp.api.res.collect.RelCodeBatchResponse;
|
|
|
|
|
import com.glxp.api.service.auth.CustomerService;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
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.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class RelCodeBatchService extends ServiceImpl<RelCodeBatchMapper, RelCodeBatch> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private RelCodeDetailService relCodeDetailService;
|
|
|
|
|
@Resource
|
|
|
|
|
private CustomerService customerService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传保存关联信息
|
|
|
|
|
* @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文件解析错误");
|
|
|
|
|
}
|
|
|
|
|
AuthAdmin user = customerService.getUserBean();
|
|
|
|
|
for (int i = 0; i < batch.getLength(); i++) {
|
|
|
|
|
Node batchNode = batch.item(i);
|
|
|
|
|
RelCodeBatch relCodeBatch = new RelCodeBatch();
|
|
|
|
|
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);
|
|
|
|
|
String batchNo = batchE.getAttribute("batchNo");
|
|
|
|
|
relCodeBatch.setBatchNo(batchNo);
|
|
|
|
|
String madeDate = batchE.getAttribute("madeDate");
|
|
|
|
|
relCodeBatch.setMadeDate(madeDate);
|
|
|
|
|
// relCodeBatch.setMadeDate(DateUtil.parse(madeDate,"yyyy-MM-dd"));
|
|
|
|
|
String validateDate = batchE.getAttribute("validateDate");
|
|
|
|
|
// relCodeBatch.setValidateDate(DateUtil.offsetDay(DateUtil.parse(validateDate,"yyyy-MM-dd"),1));
|
|
|
|
|
relCodeBatch.setValidateDate(String.valueOf(DateUtil.offsetDay(DateUtil.parse(validateDate,"yyyy-MM-dd"),1)));
|
|
|
|
|
String workShop = batchE.getAttribute("workShop");
|
|
|
|
|
relCodeBatch.setWorkShop(workShop);
|
|
|
|
|
String lineName = batchE.getAttribute("lineName");
|
|
|
|
|
relCodeBatch.setLineName(lineName);
|
|
|
|
|
String lineManager = batchE.getAttribute("lineManager");
|
|
|
|
|
relCodeBatch.setLineManager(lineManager);
|
|
|
|
|
relCodeBatch.setCreateTime(new Date());
|
|
|
|
|
relCodeBatch.setCreateUser(user.getUserName());
|
|
|
|
|
relCodeBatch.setUpdateTime(new Date());
|
|
|
|
|
relCodeBatch.setUpdateUser(user.getUserName());
|
|
|
|
|
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(StrUtil.isNotBlank(parentCode)?parentCode:"0");
|
|
|
|
|
String flag = codeE.getAttribute("flag");
|
|
|
|
|
relCodeDetail.setFlag(Integer.valueOf(flag));
|
|
|
|
|
relCodeDetails.add(relCodeDetail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
relCodeDetailService.saveBatch(relCodeDetails);
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
throw new JsonException("上传失败:"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 手动添加
|
|
|
|
|
* @param relCodeBatchRequest
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void add(RelCodeBatchRequest relCodeBatchRequest) {
|
|
|
|
|
List<RelCodeDetailRequest> detailList = relCodeBatchRequest.getDetailList();
|
|
|
|
|
if(CollUtil.isEmpty(detailList)){
|
|
|
|
|
throw new JsonException("关联明细不能为空!");
|
|
|
|
|
}
|
|
|
|
|
AuthAdmin user = customerService.getUserBean();
|
|
|
|
|
RelCodeBatch relCodeBatch = new RelCodeBatch();
|
|
|
|
|
BeanUtils.copyProperties(relCodeBatchRequest,relCodeBatch);
|
|
|
|
|
relCodeBatch.setCreateTime(new Date());
|
|
|
|
|
relCodeBatch.setCreateUser(user.getUserName());
|
|
|
|
|
relCodeBatch.setUpdateTime(new Date());
|
|
|
|
|
relCodeBatch.setUpdateUser(user.getUserName());
|
|
|
|
|
this.save(relCodeBatch);
|
|
|
|
|
List<RelCodeDetail> relCodeDetails = new ArrayList<>();
|
|
|
|
|
for(RelCodeDetailRequest request : detailList){
|
|
|
|
|
RelCodeDetail relCodeDetail = new RelCodeDetail();
|
|
|
|
|
String parentCode = request.getParentCode();
|
|
|
|
|
request.setParentCode(StrUtil.isNotBlank(parentCode)?parentCode:"0");
|
|
|
|
|
BeanUtils.copyProperties(request,relCodeDetail);
|
|
|
|
|
relCodeDetail.setBatchIdFk(relCodeBatch.getId());
|
|
|
|
|
relCodeDetails.add(relCodeDetail);
|
|
|
|
|
}
|
|
|
|
|
relCodeDetailService.saveBatch(relCodeDetails);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void delete(Long id) {
|
|
|
|
|
if(id == null){
|
|
|
|
|
throw new JsonException("id 不能为空");
|
|
|
|
|
}
|
|
|
|
|
relCodeDetailService.remove(new LambdaQueryWrapper<RelCodeDetail>().eq(RelCodeDetail::getBatchIdFk,id));
|
|
|
|
|
this.removeById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void addRelCodeSpellBox(List<RelCodeBatchRequest> relCodeBatchRequests) {
|
|
|
|
|
if (CollUtil.isEmpty(relCodeBatchRequests)){
|
|
|
|
|
throw new JsonException("单据信息异常!");
|
|
|
|
|
}
|
|
|
|
|
relCodeBatchRequests.forEach( item -> {
|
|
|
|
|
this.add(item);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void deleteDetail(String curCode) {
|
|
|
|
|
List<RelCodeDetail> list = relCodeDetailService.list(new LambdaQueryWrapper<RelCodeDetail>().eq(RelCodeDetail::getParentCode, curCode));
|
|
|
|
|
if (CollUtil.isNotEmpty(list)){
|
|
|
|
|
List<Integer> batchIds = list.stream().map(RelCodeDetail::getBatchIdFk).collect(Collectors.toList());
|
|
|
|
|
this.removeBatchByIds(batchIds);
|
|
|
|
|
relCodeDetailService.remove(new LambdaQueryWrapper<RelCodeDetail>().in(RelCodeDetail::getBatchIdFk,batchIds));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void deleteAllDetail(List<RelCodeDetailRequest> detailList) {
|
|
|
|
|
if (CollUtil.isNotEmpty(detailList)){
|
|
|
|
|
detailList.forEach( item -> {
|
|
|
|
|
String curCode = item.getCurCode();
|
|
|
|
|
this.deleteDetail(curCode);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|