|
|
@@ -0,0 +1,115 @@
|
|
|
+package com.usoftchina.saas.transfers.task;
|
|
|
+
|
|
|
+import com.usoftchina.inquiry.api.PublicProductApi;
|
|
|
+import com.usoftchina.saas.account.api.CompanyApi;
|
|
|
+import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
+import com.usoftchina.saas.document.api.ProductApi;
|
|
|
+import com.usoftchina.saas.document.dto.ProductUploadDTO;
|
|
|
+import com.usoftchina.saas.transfers.dto.MessageInfo;
|
|
|
+import com.usoftchina.saas.transfers.service.SendService;
|
|
|
+import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
+import com.usoftchina.saas.utils.DateUtils;
|
|
|
+import com.usoftchina.saas.utils.JsonUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 物料上传
|
|
|
+ * @Author chenwei
|
|
|
+ * @Date 2019/01/09
|
|
|
+ */
|
|
|
+public class SendProductTask extends Executable{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductApi productApi;
|
|
|
+ @Autowired
|
|
|
+ private PublicProductApi publicProductApi;
|
|
|
+ @Autowired
|
|
|
+ private CompanyApi companyApi;
|
|
|
+ @Autowired
|
|
|
+ private SendService sendService;
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(SendProductTask.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void execute(MessageInfo messageInfo) {
|
|
|
+ Long enUU = companyApi.getCompanyById(BaseContextHolder.getCompanyId()).getData().getUu();
|
|
|
+ //需要上传的物料信息
|
|
|
+ List<ProductUploadDTO> productUploadDTOList = productApi.getUploadData().getData();
|
|
|
+ //上传结果反馈
|
|
|
+ List<Long> successIds = new ArrayList<Long>();
|
|
|
+ List<Long> failedIds = new ArrayList<Long>();
|
|
|
+ if (!CollectionUtils.isEmpty(productUploadDTOList)){
|
|
|
+ //分批次上传,每次50条. 物料服务提供接口采用@requestParam接收物料数据,存在数据长度限制
|
|
|
+ if (productUploadDTOList.size() >= 50){
|
|
|
+ int count = (int) Math.ceil(productUploadDTOList.size() / 50.0);
|
|
|
+ CountDownLatch latch = new CountDownLatch(count);
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ //第loop次循环
|
|
|
+ int loop = i;
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ int startPoint = loop * 50;
|
|
|
+ int endPoint = 0;
|
|
|
+ if (loop + 1 == count) {
|
|
|
+ endPoint = productUploadDTOList.size() - 1;
|
|
|
+ }else{
|
|
|
+ endPoint = (loop + 1) * 50;
|
|
|
+ }
|
|
|
+ List<ProductUploadDTO> subList = productUploadDTOList.subList(startPoint, endPoint);
|
|
|
+ try {
|
|
|
+ publicProductApi.saveProducts(JsonUtils.toJsonString(subList), enUU);
|
|
|
+ subList.forEach(ProductUploadDTO -> successIds.add(ProductUploadDTO.getPr_id()));
|
|
|
+ LOGGER.info("本批次第{}至{},物料上传成功, 发起时间 {}",startPoint, endPoint, DateUtils.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
|
|
|
+ }catch (Exception e){
|
|
|
+ subList.forEach(ProductUploadDTO -> failedIds.add(ProductUploadDTO.getPr_id()));
|
|
|
+ LOGGER.info("本批次第{}至{},物料上传失败,原因:{}, 发起时间 {}",startPoint, endPoint, e.getMessage(), DateUtils.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
|
|
|
+ }finally {
|
|
|
+ latch.countDown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ latch.await();
|
|
|
+ LOGGER.info("物料上传完成, 成功{}条,失败{}条", successIds.size(), failedIds.size());
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ try {
|
|
|
+ publicProductApi.saveProducts(JsonUtils.toJsonString(productUploadDTOList), enUU);
|
|
|
+ productUploadDTOList.forEach(productUploadDTO -> successIds.add(productUploadDTO.getPr_id()));
|
|
|
+ LOGGER.info(",物料上传成功,条数{}, 发起时间 {}",productUploadDTOList.size(), DateUtils.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
|
|
|
+ }catch (Exception e){
|
|
|
+ productUploadDTOList.forEach(productUploadDTO -> failedIds.add(productUploadDTO.getPr_id()));
|
|
|
+ LOGGER.info("物料上传失败,条数{},原因:{}, 发起时间 {}", productUploadDTOList.size(), e.getMessage(), DateUtils.format(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //更新物料状态
|
|
|
+ if (successIds.size() > 0) {
|
|
|
+ Iterator<Long> it = successIds.iterator();
|
|
|
+ StringBuilder success = new StringBuilder();
|
|
|
+ while (it.hasNext()){
|
|
|
+ success.append(it.next() + ",");
|
|
|
+ }
|
|
|
+ productApi.updateStatus(success.substring(0, success.length() - 1), "已上传");
|
|
|
+ }
|
|
|
+ if (failedIds.size() > 0){
|
|
|
+ Iterator<Long> it = failedIds.iterator();
|
|
|
+ StringBuilder failed = new StringBuilder();
|
|
|
+ while (it.hasNext()){
|
|
|
+ failed.append(it.next() + ",");
|
|
|
+ }
|
|
|
+ String failedStr = failed.substring(0, failed.length() - 1);
|
|
|
+ MessageInfo newMessageInfo = new MessageInfo(messageInfo.getUserId(), messageInfo.getBizType(), failedStr, messageInfo.getCompanyId());
|
|
|
+ sendService.sendMessage(newMessageInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|