package com.jkcredit.invoice.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.jkcredit.invoice.common.CommonConstants; import com.jkcredit.invoice.model.entity.SearchInvoiceResult; import com.jkcredit.invoice.model.entity.SearchInvoiceResultTemp; import com.jkcredit.invoice.model.entity.WayBillTest; import com.jkcredit.invoice.service.SearchInvoiceResultService; import com.jkcredit.invoice.service.SearchInvoiceResultTempService; import com.jkcredit.invoice.util.HttpUtil; import com.jkcredit.invoice.util.ReadExcelUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; /** * @description: * @author: xusonglin * @create: 2019/12/13 13:21 * @version: V1.0 **/ @RestController @RequestMapping("/temp") @Slf4j public class TempController { @Autowired private HttpUtil httpUtil; @Autowired private SearchInvoiceResultService searchInvoiceResultService; @Autowired private SearchInvoiceResultTempService tempService; @Autowired private RedisTemplate redisTemplate; @RequestMapping("/temp") public void temp(@RequestParam MultipartFile file) { // 查询所有运单 List wayBillTestList = ReadExcelUtil.readTempExcel(file, WayBillTest.class); //线程分割数 int count = 20000; //获取list大小 int listSize = wayBillTestList.size(); //线程数 int threadSize = (listSize / count) + 1; ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-numInvoice-%d").build(); ExecutorService executor = new ThreadPoolExecutor(threadSize, threadSize, 200L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory); CountDownLatch countDownLatch = new CountDownLatch(threadSize); List newList; for (int i = 0; i < threadSize; i++) { if ((i + 1) == threadSize) { int startIndex = (i * count); int endIndex = wayBillTestList.size(); newList = wayBillTestList.subList(startIndex, endIndex); } else { int startIndex = i * count; int endIndex = (i + 1) * count; newList = wayBillTestList.subList(startIndex, endIndex); } List finalNewList = newList; executor.execute(new Thread(() -> { if (null != finalNewList) { log.info("传入数据是:{}", JSON.toJSONString(finalNewList)); finalNewList.stream().forEach(numInvoice -> { Map map = new HashMap<>(4); map.put("companyNum","10004616"); map.put("num", numInvoice.getWayBillNum()); log.info("运单编号num={}", numInvoice.getWayBillNum()); //调用接口 String registerResult = httpUtil.getCheckResult(map, CommonConstants.WAY_BILL_NUM_FIND_INVOICE_API); // String registerResult = getTestResult(); log.info("运单查询发票接口返回:{},请求参数:{}", registerResult, JSON.toJSONString(map)); //转换结果为jsonobject JSONObject registerResultJson = JSON.parseObject(registerResult); //获取返回data值 String dataCode = registerResultJson.getString("data"); //判断返回值是否为1 如果是1 设置状态为成功 if ("1".equals(dataCode)) { String returnStr = registerResultJson.getString("msg"); JSONObject returnStrJson = JSON.parseObject(returnStr); //获取接口返回结果 String numResult = returnStrJson.getString("result"); //获取接口状态 String waybillStatus = returnStrJson.getString("waybillStatus"); //转换为结果实体对象 并插入数据库 实现计费功能 List ts = JSONArray.parseArray(numResult, SearchInvoiceResult.class); List newSearchInvoiceResultList; if (ts.size() > 0) { //去重列表 newSearchInvoiceResultList = ts.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(SearchInvoiceResult::getTransactionId))), ArrayList::new)); //计费 newSearchInvoiceResultList.stream().forEach(t -> { if (!redisTemplate.hasKey(CommonConstants.NUM_FIND_INVOICE_KEY + t.getInvoiceNum() + "_" + t.getInvoiceCode())) { SearchInvoiceResultTemp temp = new SearchInvoiceResultTemp(); BeanUtils.copyProperties(t, temp); tempService.save(temp); //存入redis发票信息 redisTemplate.opsForValue().set(CommonConstants.NUM_FIND_INVOICE_KEY + t.getInvoiceNum() + "_" + t.getInvoiceCode(), JSON.toJSONString(t)); //存入redis交易id redisTemplate.opsForValue().set(CommonConstants.TRANSACTION_ID_KEY + t.getTransactionId(), t.getTransactionId()); } }); } } else { //设置失败 log.error("返回code不等于1的结果={}", registerResult); } }); } countDownLatch.countDown(); })); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } executor.shutdown(); } }