|  |  |  | 
|---|
|  |  |  | import cn.hutool.core.util.IdUtil; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.aps.common.core.constant.SecurityConstants; | 
|---|
|  |  |  | import com.aps.common.core.utils.DateUtils; | 
|---|
|  |  |  | import com.aps.common.core.utils.StringUtils; | 
|---|
|  |  |  | import com.aps.common.core.utils.uuid.IdUtils; | 
|---|
|  |  |  | import com.aps.job.domain.ApsBomHeaderJob; | 
|---|
|  |  |  | import com.aps.job.domain.ApsBomLineJob; | 
|---|
|  |  |  | import com.aps.job.domain.ApsWorkOrderJobLog; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.aps.job.mapper.ApsBomLineJobMapper; | 
|---|
|  |  |  | import com.aps.job.mapper.ApsWorkOrderJobLogMapper; | 
|---|
|  |  |  | import com.aps.job.service.IApsBomHeaderJobService; | 
|---|
|  |  |  | import com.aps.system.api.RemoteCoreService; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.http.HttpEntity; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @date 2025-05-08 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class ApsBomHeaderJobServiceImpl implements IApsBomHeaderJobService | 
|---|
|  |  |  | { | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RestTemplate restTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RemoteCoreService remoteCoreService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询BOM Header 数据管理 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean syncBomData(Integer pageIndex, Integer pageSize, String orgCode, String itemCodeList) { | 
|---|
|  |  |  | JSONObject requestBody = new JSONObject(); | 
|---|
|  |  |  | ResponseEntity<String> response = null; | 
|---|
|  |  |  | ApsWorkOrderJobLog jobLog = new ApsWorkOrderJobLog(); | 
|---|
|  |  |  | String batchNum = ""; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 设置请求头 | 
|---|
|  |  |  | HttpHeaders headers = new HttpHeaders(); | 
|---|
|  |  |  | headers.setContentType(MediaType.APPLICATION_JSON); | 
|---|
|  |  |  | // 设置请求体 | 
|---|
|  |  |  | while (true) { | 
|---|
|  |  |  | ApsWorkOrderJobLog jobLog = new ApsWorkOrderJobLog(); | 
|---|
|  |  |  | JSONObject requestBody = new JSONObject(); | 
|---|
|  |  |  | batchNum = IdUtils.fastSimpleUUID(); | 
|---|
|  |  |  | jobLog = new ApsWorkOrderJobLog(); | 
|---|
|  |  |  | requestBody = new JSONObject(); | 
|---|
|  |  |  | requestBody.put("PageIndex", pageIndex); | 
|---|
|  |  |  | requestBody.put("PageSize", pageSize); | 
|---|
|  |  |  | if(!StringUtils.isEmpty(orgCode)){ | 
|---|
|  |  |  | 
|---|
|  |  |  | // 创建HttpEntity对象 | 
|---|
|  |  |  | HttpEntity<String> request = new HttpEntity<>(requestBody.toJSONString(), headers); | 
|---|
|  |  |  | // 发送POST请求 | 
|---|
|  |  |  | ResponseEntity<String> response = restTemplate.postForEntity(getBomUrl, request, String.class); | 
|---|
|  |  |  | response = restTemplate.postForEntity(getBomUrl, request, String.class); | 
|---|
|  |  |  | JSONObject responseBodyJson = JSONObject.parseObject(response.getBody()); | 
|---|
|  |  |  | if (response.getStatusCode().is2xxSuccessful() && "200".equals(responseBodyJson.getString("status"))) { | 
|---|
|  |  |  | JSONArray jsonArray = responseBodyJson.getJSONArray("data"); | 
|---|
|  |  |  | 
|---|
|  |  |  | insertBomHeader.setStartDate(DateUtils.parseDate(bomHeader.getString("HEffectiveDate"))); | 
|---|
|  |  |  | insertBomHeader.setEndDate(DateUtils.parseDate(bomHeader.getString("HDisableDate"))); | 
|---|
|  |  |  | insertBomHeader.setOrgCode(bomHeader.getString("HOrgName").contains("沈阳")?"FORTUNE":bomHeader.getString("HOrgName").contains("南通")?"FORTUNA":bomHeader.getString("HOrgName").contains("北京")?"FORTUBE":bomHeader.getString("HOrgName")); | 
|---|
|  |  |  | insertBomHeader.setCreateBy(batchNum); | 
|---|
|  |  |  | headerList.add(insertBomHeader); | 
|---|
|  |  |  | for(Object line : bomLines){ | 
|---|
|  |  |  | JSONObject lineJson = (JSONObject) line; | 
|---|
|  |  |  | 
|---|
|  |  |  | bomLine.setStartDate(DateUtils.parseDate(lineJson.getString("LEffectiveDate"))); | 
|---|
|  |  |  | bomLine.setEndDate(DateUtils.parseDate(lineJson.getString("LDisableDate"))); | 
|---|
|  |  |  | bomLine.setOrgCode(lineJson.getString("LOrgName").contains("沈阳")?"FORTUNE":lineJson.getString("LOrgName").contains("南通")?"FORTUNA":lineJson.getString("LOrgName").contains("北京")?"FORTUBE":lineJson.getString("LOrgName")); | 
|---|
|  |  |  | bomLine.setCreateBy(batchNum); | 
|---|
|  |  |  | lineList.add(bomLine); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | jobLog.setCreateTime(DateUtils.getNowDate()); | 
|---|
|  |  |  | jobLog.setBizType("bom"); | 
|---|
|  |  |  | jobLog.setResult("header:"+headerList.size() + " line:"+lineList.size()); | 
|---|
|  |  |  | jobLog.setCreateBy(batchNum); | 
|---|
|  |  |  | jobLogMapper.insertApsWorkOrderJobLog(jobLog); | 
|---|
|  |  |  | log.info("BOM同步:"+ JSON.toJSONString(jobLog)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | pageIndex++; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | 
|---|
|  |  |  | jobLog.setCreateTime(DateUtils.getNowDate()); | 
|---|
|  |  |  | jobLog.setBizType("bom"); | 
|---|
|  |  |  | jobLog.setResult("error"); | 
|---|
|  |  |  | jobLog.setCreateBy(batchNum); | 
|---|
|  |  |  | jobLogMapper.insertApsWorkOrderJobLog(jobLog); | 
|---|
|  |  |  | log.info("BOM同步:"+ JSON.toJSONString(jobLog)); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | jobLog.setRequestData(requestBody.toJSONString()); | 
|---|
|  |  |  | jobLog.setResponseData(response.getBody()); | 
|---|
|  |  |  | jobLog.setPageNum(Long.valueOf(pageIndex)); | 
|---|
|  |  |  | jobLog.setPageCount(Long.valueOf(pageSize)); | 
|---|
|  |  |  | jobLog.setCreateTime(DateUtils.getNowDate()); | 
|---|
|  |  |  | jobLog.setBizType("bom"); | 
|---|
|  |  |  | jobLog.setResult("error"); | 
|---|
|  |  |  | jobLog.setCreateBy(batchNum); | 
|---|
|  |  |  | jobLogMapper.insertApsWorkOrderJobLog(jobLog); | 
|---|
|  |  |  | log.info("BOM同步异常信息:"+ JSON.toJSONString(jobLog)); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean syncBomDataJob(Integer pageIndex, Integer pageSize, String orgCode, String itemCodeList){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | log.info("全量同步BOM数据定时任务开始"); | 
|---|
|  |  |  | log.info(" DELETE FROM aps_bom_header_job"); | 
|---|
|  |  |  | apsBomHeaderJobMapper.deleteApsBomHeaderJob(); | 
|---|
|  |  |  | log.info(" DELETE FROM aps_bom_line_job"); | 
|---|
|  |  |  | apsBomLineJobMapper.deleteApsBomLineJob(); | 
|---|
|  |  |  | boolean res = syncBomData(pageIndex, pageSize, orgCode, itemCodeList); | 
|---|
|  |  |  | if(!res){ | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | log.info("将BOM同步到Redis"); | 
|---|
|  |  |  | remoteCoreService.setBomDataToRedis(SecurityConstants.INNER); | 
|---|
|  |  |  | log.info("deleteApsBomHeader"); | 
|---|
|  |  |  | apsBomHeaderJobMapper.deleteApsBomHeader(); | 
|---|
|  |  |  | log.info("deleteApsBomLine"); | 
|---|
|  |  |  | apsBomLineJobMapper.deleteApsBomLine(); | 
|---|
|  |  |  | log.info("insertIntoApsBomHeader"); | 
|---|
|  |  |  | apsBomHeaderJobMapper.insertIntoApsBomHeader(); | 
|---|
|  |  |  | log.info("insertIntoApsBomLine"); | 
|---|
|  |  |  | apsBomLineJobMapper.insertIntoApsBomLine(); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|