|
|
@@ -1,21 +1,30 @@
|
|
|
package com.dk.mnls_mp.service.mnls;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.dk.mnls_mp.infrastructure.base.ResponseCodeEnum;
|
|
|
import com.dk.mnls_mp.infrastructure.config.Config;
|
|
|
+import com.dk.mnls_mp.infrastructure.convert.IBOSSGoodsCodeConvert;
|
|
|
import com.dk.mnls_mp.infrastructure.util.HttpHelper;
|
|
|
import com.dk.mnls_mp.mapper.basic.CodeFacRecordMapper;
|
|
|
+import com.dk.mnls_mp.model.dto.FacCodeDetailDto;
|
|
|
+import com.dk.mnls_mp.model.dto.FacCodeDto;
|
|
|
import com.dk.mnls_mp.model.pojo.basic.*;
|
|
|
+import com.dk.mnls_mp.model.pojo.mnls.FacCodeQuery;
|
|
|
+import com.dk.mnls_mp.model.pojo.mnls.IBOSSGoodsCode;
|
|
|
+import com.dongke.base.exceptionHandler.BaseBusinessException;
|
|
|
import com.dongke.base.exceptionHandler.ResponseResultUtil;
|
|
|
import com.dongke.base.exceptionHandler.ResponseResultVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @desc : 拉取厂家商品编码业务类
|
|
|
@@ -33,42 +42,127 @@ public class PullFacGoodsCodeService {
|
|
|
@Autowired
|
|
|
CodeFacRecordMapper pullFacCodeRecordMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ IBOSSGoodsCodeConvert convert;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IBOSSGoodsCodeService IBOSSGoodsCodeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ CodeFacRecordMapper codeFacRecordMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 厂家商品编码明细集合
|
|
|
+ */
|
|
|
+ List<FacCodeDetailDto> facCodeDetailDtoList;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定义抓取履历
|
|
|
+ */
|
|
|
+ Map pullRecord=new HashMap();
|
|
|
+
|
|
|
/**
|
|
|
* @desc : 拉取厂家商品编码
|
|
|
* @author : 张潇木
|
|
|
* @date : 2022/9/23 16:54
|
|
|
*/
|
|
|
public ResponseResultVO pullFacGoodsCode() {
|
|
|
+ //初始化商品编码结果集
|
|
|
+ facCodeDetailDtoList=new ArrayList<>();
|
|
|
+ //初始化抓取履历
|
|
|
+ pullRecord.put("CreateTime",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
|
|
|
+ pullRecord.put("Status",0);
|
|
|
try {
|
|
|
- //定义上次拉取成功时间
|
|
|
- String historyDate= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
|
|
|
- //查询上次拉取成功时间
|
|
|
- List<CodeFacRecord> historyDateList = pullFacCodeRecordMapper.selectCodeHistoryTime();
|
|
|
- //上次拉取成功时间赋值
|
|
|
- if (historyDateList != null && historyDateList.size() > 0) {
|
|
|
- historyDate = historyDateList.get(0).getCreateTime();
|
|
|
- }
|
|
|
+ //todo 调用了接口但是异常了。status应该是0 而不是没有履历记录
|
|
|
+ int syncId=this.recordPullDetail(pullRecord);
|
|
|
+ //组装查询参数
|
|
|
+ FacCodeQuery facCodeQuery = this.packageQuery(null,null);
|
|
|
+ //定义开始时间
|
|
|
Date startTime = new Date();
|
|
|
- //todo
|
|
|
- ResponseResultVO responseResultVO = HttpHelper.postMY(config.getMyGoodsCodeUrl(), null);
|
|
|
+ //调用getFacCode方法
|
|
|
+ this.getFacCode(facCodeQuery);
|
|
|
+ //定义结束时间
|
|
|
Date endTime = new Date();
|
|
|
- log.info("任务{},耗时{}毫秒(ms)", "厂家产品主信息", endTime.getTime() - startTime.getTime());
|
|
|
-
|
|
|
- return ResponseResultUtil.success();
|
|
|
+ log.info("拉取厂家商品编码成功,耗时=>{}毫秒(ms)", endTime.getTime() - startTime.getTime());
|
|
|
+ //拉取到数据后,插入中台数据库
|
|
|
+ if(facCodeDetailDtoList.size()>0){
|
|
|
+ this.insertGoodsCode(facCodeDetailDtoList);
|
|
|
+ }
|
|
|
+ //将履历表状态修改为成功
|
|
|
+ codeFacRecordMapper.updateCodeHistoryStatusByID(new HashMap(){{put("Status",1);put("SynchID",syncId);}});
|
|
|
+ //处理返回值
|
|
|
+ return ResponseResultUtil.success(ResponseCodeEnum.SUCCESS.getCode(),"拉取厂家商品编码,并插入中台数据库成功");
|
|
|
} catch (Exception ex) {
|
|
|
- TransactionAspectSupport.currentTransactionStatus()
|
|
|
- .setRollbackOnly();
|
|
|
log.error(ex.getMessage(), ex);
|
|
|
- //返回错误数据
|
|
|
- return ResponseResultUtil.error(666, "抓取并更新标准商品编码失败");
|
|
|
+ throw new BaseBusinessException(666,"拉取厂家商品编码失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 插入商品编码
|
|
|
+ * @author : 张潇木
|
|
|
+ * @date : 2022/9/25 13:44
|
|
|
+ */
|
|
|
+ public void insertGoodsCode(List<FacCodeDetailDto> list) {
|
|
|
+ List<IBOSSGoodsCode> ibossGoodsCodes = convert.facDTO2IBOSSGoodsCodePO(list);
|
|
|
+ IBOSSGoodsCodeService.insertBatch(ibossGoodsCodes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 组装查询参数
|
|
|
+ * @author : 张潇木
|
|
|
+ * @date : 2022/9/25 10:40
|
|
|
+ */
|
|
|
+ public FacCodeQuery packageQuery(Integer pageIndex,Integer pageSize){
|
|
|
+ //pageIndex默认为1 pageSize默认为1000
|
|
|
+ FacCodeQuery facCodeQuery=new FacCodeQuery().setPageIndex(Optional.ofNullable(pageIndex).orElse(1)).setPageSize(Optional.ofNullable(pageSize).orElse(1000));
|
|
|
+ //查询上次拉取成功时间
|
|
|
+ List<CodeFacRecord> historyDateList = pullFacCodeRecordMapper.selectCodeHistoryTime();
|
|
|
+ //定义本次拉取成功时间
|
|
|
+ String endDate= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
|
|
|
+ //定义上次拉取成功时间赋值
|
|
|
+ String startDate = historyDateList != null && historyDateList.size() > 0?historyDateList.get(0).getCreateTime():endDate;
|
|
|
+ //设置查询条件
|
|
|
+ facCodeQuery.setParam(new JSONObject(){{put("startDate",startDate);put("endDate",endDate);}});
|
|
|
+ return facCodeQuery;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 调用厂家商品编码接口,并整理返回体
|
|
|
+ * @author : 张潇木
|
|
|
+ * @date : 2022/9/25 10:40
|
|
|
+ */
|
|
|
+ public void getFacCode(FacCodeQuery query){
|
|
|
+ ResponseResultVO resultVO = HttpHelper.postMY(config.getMyGoodsCodeUrl(), query);
|
|
|
+ if (ResponseCodeEnum.SUCCESS.getCode()==resultVO.getCode()) {
|
|
|
+ //返回值转换DTO
|
|
|
+ FacCodeDto facCodeDto= JSON.parseObject(resultVO.getData().toString(),FacCodeDto.class);
|
|
|
+ if(ResponseCodeEnum.SUCCESS.getCode().equals(facCodeDto.getCode())&&facCodeDto.getRecords()!=null){
|
|
|
+ facCodeDetailDtoList.addAll(facCodeDto.getRecords());
|
|
|
+ }else{
|
|
|
+ log.error("getFacCode 接口调用失败,接口返回code不等于200",JSON.toJSONString(resultVO));
|
|
|
+ throw new BaseBusinessException(ResponseCodeEnum.MY_GOODS_CODE_POST_FAIL.getCode(),ResponseCodeEnum.MY_GOODS_CODE_POST_FAIL.getMessage());
|
|
|
+ }
|
|
|
+ //如果不是最后一页,继续调用
|
|
|
+ if(facCodeDto.getPages()!=null&&facCodeDto.getPages()!=0&&query.getPageIndex()!=facCodeDto.getPages()){
|
|
|
+ //页码+1
|
|
|
+ this.getFacCode(query.setPageIndex(query.getPageIndex()+1));
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ log.error("getFacCode 接口调用失败,http状态不等于200",JSON.toJSONString(resultVO));
|
|
|
+ throw new BaseBusinessException(ResponseCodeEnum.MY_GOODS_CODE_POST_FAIL.getCode(),ResponseCodeEnum.MY_GOODS_CODE_POST_FAIL.getMessage());
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* @desc : 记录拉取履历
|
|
|
* @author : 张潇木
|
|
|
* @date : 2022/9/23 16:54
|
|
|
*/
|
|
|
- public void recordPullDetail(){
|
|
|
-
|
|
|
+ public int recordPullDetail(Map pullRecord){
|
|
|
+ return codeFacRecordMapper.insertHistory(pullRecord);
|
|
|
}
|
|
|
}
|