| | |
| | | package com.chinaztt.mes.basic.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.lang.Console; |
| | | import cn.hutool.core.text.csv.CsvData; |
| | | import cn.hutool.core.text.csv.CsvReader; |
| | | import cn.hutool.core.text.csv.CsvRow; |
| | | import cn.hutool.core.text.csv.CsvUtil; |
| | | import cn.hutool.core.util.CharsetUtil; |
| | | import cn.hutool.http.HttpRequest; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | |
| | | import com.chinaztt.mes.basic.service.PartService; |
| | | import com.chinaztt.mes.basic.util.DictUtils; |
| | | import com.chinaztt.mes.basic.util.PartUtils; |
| | | import com.chinaztt.mes.basic.util.TransactionUtils; |
| | | import com.chinaztt.mes.basic.vo.PartVo; |
| | | import com.chinaztt.mes.common.util.JsonUtil; |
| | | import com.chinaztt.mes.common.util.MultipartFileToFile; |
| | | import com.chinaztt.ztt.common.core.util.R; |
| | | import com.chinaztt.ztt.common.security.util.SecurityUtils; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.common.collect.Lists; |
| | | import com.google.gson.Gson; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.apache.http.impl.client.HttpClientBuilder; |
| | | import org.apache.http.params.CoreConnectionPNames; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.cloud.client.discovery.DiscoveryClient; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.sql.Connection; |
| | | import java.sql.PreparedStatement; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | |
| | | /** |
| | | * 零件 |
| | |
| | | private DiscoveryClient discoveryClient; |
| | | private Environment environment; |
| | | private RedisTemplate redisTemplate; |
| | | private TransactionUtils transactionUtils; |
| | | |
| | | @Autowired |
| | | private ThreadPoolTaskExecutor threadPoolTaskExecutor; |
| | | ; |
| | | |
| | | |
| | | @Override |
| | | public void importPartExcel(List<PartData> list) { |
| | |
| | | throw new RuntimeException("零件号版本号重复:" + partByFind.getPartName() + "-" + partByFind.getPartNo()); |
| | | } |
| | | baseMapper.insert(part); |
| | | //对接IFS |
| | | // R result = ifsFeignClient.importInventPartStd(new JSONObject().fluentPut("BATCH_INFO", new JSONArray().fluentAdd(ifsDataMake(part))), true); |
| | | // if (result.getCode() == 1) { |
| | | // throw new RuntimeException("IFS错误——" + result.getMsg()); |
| | | // } |
| | | return true; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public Long syncNewIfs(Date date) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long syncNewIfs(String date) { |
| | | Map<String, String> dicmattype = dictUtils.getDicKey("material_type"); |
| | | Map<String, String> dicunit = dictUtils.getDicKey("unit"); |
| | | String key = "syncPart_lock" + SecurityUtils.getUser().getId(); |
| | |
| | | throw new RuntimeException("有同步任务正在处理"); |
| | | } |
| | | redisTemplate.opsForValue().set(key, 1, 1000 * 3600); |
| | | Map<String, Object>map=new HashMap<>(); |
| | | map.put("MODIFY_DATE", DateUtil.format(date,"yyyy-MM-dd")); |
| | | List<Part>partList=new ArrayList<Part>(); |
| | | Connection conn =null; |
| | | PreparedStatement pstmt =null; |
| | | try { |
| | | String url =""; |
| | | //判断日期有否 |
| | | if (Objects.isNull(date)){ |
| | | url = "http://192.168.20.47:8008/PurchService.ashx?contract=KJZS&contractKey=4ttDeLKNsZuhstjtROMcRE1USNFXKdFYE7lQ2p1m5Bo=&procedureName=QUERY_INVENTORY_PART_INFO_STD&userId=7632&inAttr={}"; |
| | | }else { |
| | | Map<String, Object>map=new HashMap<>(); |
| | | map.put("MODIFY_DATE", date); |
| | | url = "http://192.168.20.47:8008/PurchService.ashx?contract=KJZS&contractKey=4ttDeLKNsZuhstjtROMcRE1USNFXKdFYE7lQ2p1m5Bo=&procedureName=QUERY_INVENTORY_PART_INFO_STD&userId=7632&inAttr="+new ObjectMapper().writeValueAsString(map); |
| | | } |
| | | log.info("开始执行同步==========》"); |
| | | String url = "http://192.168.20.47:8008/PurchService.ashx?contract=ZTKJ&contractKey=4ttDeLKNsZuhstjtROMcRE1USNFXKdFYE7lQ2p1m5Bo=&procedureName=QUERY_INVENTORY_PART_INFO_STD&userId=7632&inAttr="+new ObjectMapper().writeValueAsString(map); |
| | | String body = HttpRequest.get(url).execute().body(); |
| | | log.info("执行同步结束===========》"); |
| | | Map<String, Object> mapResult = JsonUtil.jsonToPojo(body, Map.class); |
| | | List<Map<String, Object>> data = JsonUtil.jsonToPojo(JsonUtil.jsonToString(mapResult.get("INVENTORY_PART_INFO")), List.class); |
| | | System.out.println(data.size()); |
| | | //data.forEach(System.out::println); |
| | | List<String>no=new ArrayList<String>(); |
| | | data.forEach(l->{ |
| | | Part part = new Part(); |
| | |
| | | List<Part> selectList = baseMapper.selectList(queryWrapper); |
| | | //System.out.println(baseMapper.selectList(queryWrapper)); |
| | | partList.removeIf(mainObj -> selectList.stream().anyMatch(subObj -> subObj.getPartNo().equals(mainObj.getPartNo()))); |
| | | //System.out.println(partList); |
| | | partList.forEach(l -> { |
| | | System.out.println("过滤长度=============================》"+partList.size()); |
| | | partList.forEach(l->{ |
| | | baseMapper.insert(l); |
| | | }); |
| | | } |
| | |
| | | e.printStackTrace(); |
| | | throw new RuntimeException(e.getMessage()); |
| | | } finally { |
| | | //JDBCUtil.close(pstmt,conn); |
| | | redisTemplate.delete(key); |
| | | } |
| | | return (long) partList.size(); |
| | | } |
| | | |
| | | @Override |
| | | public void asyncUploadExcel(MultipartFile file) { |
| | | String key = "syncPart_lock" + SecurityUtils.getUser().getId(); |
| | | if (redisTemplate.hasKey(key)) { |
| | | throw new RuntimeException("有导入任务正在处理"); |
| | | } |
| | | redisTemplate.opsForValue().set(key, 1, 1000 * 3600); |
| | | RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(),true); |
| | | Map<String, String> dicmattype = dictUtils.getDicKey("material_type"); |
| | | Map<String, String> dicunit = dictUtils.getDicKey("unit"); |
| | | CompletableFuture.runAsync(()->{ |
| | | asyncExcel(file,dicmattype,dicunit); |
| | | redisTemplate.delete(key); |
| | | },threadPoolTaskExecutor); |
| | | } |
| | | |
| | | //触发零件 |
| | | @Override |
| | | public String triggerAdd(PartVo partVo) { |
| | | log.info("接收主数据平台数据为===================》"+JSONObject.toJSONString(partVo)); |
| | | Part part = new Part(); |
| | | BeanUtils.copyProperties(partVo,part); |
| | | //创建者 |
| | | part.setCreateUser(partVo.getCreate_name()); |
| | | //更新者 |
| | | part.setUpdateUser(partVo.getUpdate_name()); |
| | | //零件号 |
| | | part.setPartNo(partVo.getMaterial_number()); |
| | | //名称 |
| | | part.setPartName(partVo.getMaterial_description()); |
| | | //零件描述 |
| | | part.setDescription(partVo.getMaterial_name()); |
| | | //单位 |
| | | part.setUnit(partVo.getUnit()); |
| | | //一级分类 |
| | | part.setAssortment(partVo.getFirstCategoryName()); |
| | | //二级分类 |
| | | part.setCategory(partVo.getSecondCategoryName()); |
| | | //三级分类 |
| | | part.setThreeLevelClassification(partVo.getThirdCategoryName()); |
| | | //规格 |
| | | part.setSpecs(partVo.getMaterial_spec()); |
| | | //版本号 |
| | | part.setEngChgLevel(partVo.getTemplate_version().substring(1)); |
| | | //计划方法 |
| | | part.setPlanningMethod("A"); |
| | | //型号 |
| | | part.setPartModel(partVo.getMaterial_spec()); |
| | | if (partVo.getType().equals("add")){ |
| | | int insert = baseMapper.insert(part); |
| | | log.info("新增"+part.getPartNo()+"零件号成功"); |
| | | return insert>0?"1":"0" ; |
| | | }else { |
| | | int update = baseMapper.update(part, Wrappers.<Part>lambdaUpdate().eq(Part::getPartNo, part.getPartNo())); |
| | | log.info("修改"+part.getPartNo()+"零件号成功"); |
| | | return update>0?"1":"0" ; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public void asyncExcel(MultipartFile file,Map<String, String> dicmattype,Map<String, String> dicunit){ |
| | | File file1 = MultipartFileToFile.multipartFileToFile(file); |
| | | CsvReader reader = CsvUtil.getReader(); |
| | | reader.setFieldSeparator(','); |
| | | //从文件中读取CSV数据 |
| | | CsvData data = reader.read(file1, CharsetUtil.CHARSET_GBK); |
| | | List<CsvRow> rows = data.getRows(); |
| | | //遍历行 |
| | | List<Part>list = new ArrayList<>(); |
| | | List<String>no=new ArrayList<String>(); |
| | | for (CsvRow csvRow : rows) { |
| | | //getRawList返回一个List列表,列表的每一项为CSV中的一个单元格(既逗号分隔部分) |
| | | List<String> rawList = filter(csvRow.getRawList()); |
| | | //System.out.println(rawList); |
| | | if(rawList.get(0).equals("是否设备备件")){ |
| | | continue; |
| | | } |
| | | if(rawList.get(1).equals("88.118.1/A0172320")){ |
| | | Console.log(rawList); |
| | | for (int i = 0; i < rawList.size(); i++) { |
| | | System.out.println(i+"====="+rawList.get(i)); |
| | | } |
| | | Console.log(rawList.get(6)); |
| | | } |
| | | Part part = new Part(); |
| | | part.setIsEquipment(rawList.get(0)); |
| | | part.setPartNo(rawList.get(1)); |
| | | no.add(rawList.get(1)); |
| | | part.setPartName(rawList.get(2)); |
| | | part.setDescription(rawList.get(3)); |
| | | part.setDomainNo(rawList.get(4)); |
| | | part.setDomainName(rawList.get(5)); |
| | | part.setMaterialType(rawList.get(6)); |
| | | part.setUnit(rawList.get(8)); |
| | | part.setAssets(rawList.get(16)); |
| | | part.setAssetsType(rawList.get(17)); |
| | | part.setPlanningMethod(rawList.get(18)); |
| | | part.setSuppliesType(rawList.get(19)); |
| | | part.setAbcType(rawList.get(20)); |
| | | part.setHz(rawList.get(21)); |
| | | part.setCycle(rawList.get(22)); |
| | | part.setSunit(rawList.get(33)); |
| | | part.setVolume(rawList.get(35)); |
| | | part.setTotalNum(rawList.get(36)); |
| | | part.setGetNum(rawList.get(37)); |
| | | part.setDefaultMr(rawList.get(60)); |
| | | part.setDopR(rawList.get(61)); |
| | | part.setNetto(rawList.get(62)); |
| | | part.setNumCount(rawList.get(64)); |
| | | part.setIvm(rawList.get(66)); |
| | | part.setIcl(rawList.get(67)); |
| | | part.setSii(rawList.get(68)); |
| | | part.setZc(rawList.get(69)); |
| | | part.setEsca(rawList.get(72)); |
| | | list.add(part); |
| | | } |
| | | list.forEach(r->{ |
| | | r.setEngChgLevel("1"); |
| | | no.add(r.getPartNo()); |
| | | r.setMaterialType(dicmattype.get(r.getMaterialType())); |
| | | r.setUnit(dicunit.get(r.getUnit())); |
| | | r.setSunit(dicunit.get(r.getSunit())); |
| | | }); |
| | | list.stream().filter(r-> Objects.isNull(r.getMaterialType())).forEach(System.out::println); |
| | | if(list.size()>0) { |
| | | List<List<String>> partition = Lists.partition(no, 10000); |
| | | partition.forEach(l->{ |
| | | QueryWrapper<Part> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.lambda().select(Part::getPartNo).in(Part::getPartNo, l); |
| | | List<Part> selectList = baseMapper.selectList(queryWrapper); |
| | | list.removeIf(mainObj -> selectList.stream().anyMatch(subObj -> subObj.getPartNo().equals(mainObj.getPartNo()))); |
| | | }); |
| | | log.info("过滤后长度=============================》"+list.size()); |
| | | list.forEach(l->{ |
| | | try { |
| | | baseMapper.insert(l); |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | } |
| | | MultipartFileToFile.delteTempFile(file1); |
| | | } |
| | | |
| | | /** |
| | | * 过滤修改不正确数据格式 |
| | | * @param rawList |
| | | * @return |
| | | */ |
| | | public static List<String> filter( List<String> rawList){ |
| | | List<String>filters = new ArrayList<>(); |
| | | for (int i = 0; i < rawList.size(); i++) { |
| | | if (rawList.get(i).contains("|")&&rawList.get(i).contains(",")&&rawList.get(i).split(",").length>1) { |
| | | String[] split = rawList.get(i).split(","); |
| | | if (!Objects.equals(split[1], "") &&split[0].charAt(0)==split[1].charAt(0)){ |
| | | Arrays.stream(split).forEach(filters::add); |
| | | }else { |
| | | filters.add(rawList.get(i)); |
| | | } |
| | | }else { |
| | | filters.add(rawList.get(i)); |
| | | } |
| | | } |
| | | return filters; |
| | | } |
| | | } |