| | |
| | | package com.ruoyi.productionPlan.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.http.HttpUtils; |
| | | import com.ruoyi.framework.config.AliDingConfig; |
| | | import com.ruoyi.productionPlan.dto.ProductionPlanDto; |
| | | import com.ruoyi.productionPlan.mapper.ProductionPlanMapper; |
| | | import com.ruoyi.productionPlan.pojo.ProductionPlan; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | | import java.time.OffsetDateTime; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <br> |
| | |
| | | @Slf4j |
| | | @Service |
| | | public class ProductionPlanServiceImpl extends ServiceImpl<ProductionPlanMapper, ProductionPlan> implements ProductionPlanService { |
| | | |
| | | @Autowired |
| | | private AliDingConfig aliDingConfig; |
| | | |
| | | @Autowired |
| | | private ProductionPlanMapper productionPlanMapper; |
| | | |
| | |
| | | public IPage<ProductionPlanDto> listPage(Page page, ProductionPlanDto productionPlanDto) { |
| | | return productionPlanMapper.listPage(page, productionPlanDto); |
| | | } |
| | | |
| | | /** |
| | | * 页面手动同步 |
| | | */ |
| | | @Override |
| | | public void loadProdData() { |
| | | syncProdData(1); |
| | | } |
| | | |
| | | /** |
| | | * 定时任务同步 |
| | | */ |
| | | @Override |
| | | public void syncProdDataJob() { |
| | | syncProdData(2); |
| | | } |
| | | |
| | | /** |
| | | * 同步数据 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void syncProdData(Integer dataSyncType) { |
| | | try { |
| | | // 获取AccessToken |
| | | String params = "appkey=" + aliDingConfig.getAppKey() |
| | | + "&appsecret=" + aliDingConfig.getAppSecret(); |
| | | |
| | | String tokenRes = HttpUtils.sendGet(aliDingConfig.getAccessTokenUrl(), params); |
| | | |
| | | JSONObject tokenObj = JSON.parseObject(tokenRes); |
| | | |
| | | String accessToken = tokenObj.getString("access_token"); |
| | | if (StringUtils.isEmpty(accessToken)) { |
| | | log.error("获取钉钉AccessToken失败: {}", tokenRes); |
| | | return; |
| | | } |
| | | |
| | | log.info("获取AccessToken成功: {}", accessToken); |
| | | |
| | | // 查询表单数据 |
| | | JSONObject searchParam = new JSONObject(); |
| | | searchParam.put("appType", aliDingConfig.getAppType()); |
| | | searchParam.put("systemToken", aliDingConfig.getSystemToken()); |
| | | searchParam.put("userId", aliDingConfig.getUserId()); |
| | | searchParam.put("formUuid", aliDingConfig.getFormUuid()); |
| | | |
| | | String dataRes = HttpUtils.sendPostJson( |
| | | aliDingConfig.getSearchFormDataUrl(), |
| | | searchParam.toJSONString(), |
| | | StandardCharsets.UTF_8.name(), |
| | | null, |
| | | accessToken |
| | | ); |
| | | |
| | | if (StringUtils.isEmpty(dataRes)) { |
| | | log.error("拉取宜搭数据为空"); |
| | | return; |
| | | } |
| | | |
| | | JSONObject resultObj = JSON.parseObject(dataRes); |
| | | JSONArray dataArr = resultObj.getJSONArray("data"); |
| | | |
| | | if (dataArr == null || dataArr.isEmpty()) { |
| | | log.warn("没有获取到表单数据"); |
| | | return; |
| | | } |
| | | |
| | | List<ProductionPlan> list = new ArrayList<>(); |
| | | |
| | | // 解析表单 |
| | | for (int i = 0; i < dataArr.size(); i++) { |
| | | |
| | | JSONObject item = dataArr.getJSONObject(i); |
| | | |
| | | String formInstanceId = item.getString("formInstanceId"); |
| | | String serialNo = item.getString("serialNo"); |
| | | // 发起人 |
| | | JSONObject originator = item.getJSONObject("originator"); |
| | | String originatorName = originator.getJSONObject("userName").getString("nameInChinese"); |
| | | |
| | | JSONObject formData = item.getJSONObject("formData"); |
| | | // 明细表 |
| | | JSONArray tableArr = formData.getJSONArray("tableField_l7fytfcn"); |
| | | if (tableArr == null || tableArr.isEmpty()) { |
| | | continue; |
| | | } |
| | | |
| | | // 解析明细 |
| | | for (int j = 0; j < tableArr.size(); j++) { |
| | | JSONObject row = tableArr.getJSONObject(j); |
| | | ProductionPlan plan = new ProductionPlan(); |
| | | |
| | | // 表单信息 |
| | | plan.setFormInstanceId(formInstanceId); |
| | | plan.setSerialNo(serialNo); |
| | | |
| | | // 表头字段 |
| | | plan.setApplyNo(formData.getString("textField_l7fytfco")); |
| | | plan.setCustomerName(formData.getString("textField_lbkozohg")); |
| | | |
| | | // 明细字段 |
| | | plan.setMaterialCode(row.getString("textField_l9xo62q5")); |
| | | plan.setProductName(row.getString("textField_l9xo62q7")); |
| | | plan.setProductSpec(row.getString("textField_l9xo62q8")); |
| | | plan.setLength(row.getInteger("numberField_lb7lgatg_value")); |
| | | plan.setWidth(row.getInteger("numberField_lb7lgath_value")); |
| | | plan.setHeight(row.getInteger("numberField_lb7lgati_value")); |
| | | plan.setQuantity(row.getInteger("numberField_lb7lgatj_value")); |
| | | plan.setVolume(row.getBigDecimal("numberField_l7fytfd3_value")); |
| | | plan.setStrength(row.getString("radioField_m9urarr2_id")); |
| | | |
| | | // 日期区间 |
| | | JSONArray dateArr = row.getJSONArray("cascadeDateField_lfxqqluw"); |
| | | |
| | | if (dateArr != null && dateArr.size() == 2) { |
| | | long start = Long.parseLong(dateArr.getString(0)); |
| | | long end = Long.parseLong(dateArr.getString(1)); |
| | | |
| | | plan.setStartDate(Instant.ofEpochMilli(start) |
| | | .atZone(ZoneId.systemDefault()) |
| | | .toLocalDateTime()); |
| | | |
| | | plan.setEndDate(Instant.ofEpochMilli(end) |
| | | .atZone(ZoneId.systemDefault()) |
| | | .toLocalDateTime()); |
| | | } |
| | | |
| | | // 提交人 |
| | | plan.setSubmitter(originatorName); |
| | | // 组织 |
| | | plan.setSubmitOrg("宁夏中创绿能实业集团有限公司"); |
| | | // 备注 |
| | | plan.setRemarkOne(formData.getString("textareaField_l7fytfcy")); |
| | | plan.setRemarkTwo(formData.getString("textField_l7fytfcx")); |
| | | // 创建人 |
| | | plan.setCreatorName(originatorName); |
| | | // 修改人 |
| | | JSONObject modifyUser = item.getJSONObject("modifyUser"); |
| | | if (modifyUser != null) { |
| | | plan.setModifierName(modifyUser.getJSONObject("userName").getString("nameInChinese")); |
| | | } |
| | | // 表单时间 |
| | | plan.setFormCreatedTime(parseUtcTime(item.getString("createdTimeGMT"))); |
| | | plan.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT"))); |
| | | // 同步信息 |
| | | plan.setDataSyncType(dataSyncType); |
| | | plan.setDataSourceType(1); |
| | | // 数据库时间 |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | plan.setCreateTime(now); |
| | | plan.setUpdateTime(now); |
| | | |
| | | list.add(plan); |
| | | } |
| | | } |
| | | |
| | | if (!list.isEmpty()) { |
| | | saveBatch(list); |
| | | } |
| | | log.info("生产计划同步完成,共 {} 条", list.size()); |
| | | } catch (Exception e) { |
| | | log.error("同步生产计划异常", e); |
| | | } |
| | | } |
| | | |
| | | private LocalDateTime parseUtcTime(String utcString) { |
| | | if (StringUtils.isEmpty(utcString)) { |
| | | return null; |
| | | } |
| | | try { |
| | | OffsetDateTime odt = OffsetDateTime.parse(utcString); |
| | | return odt.toLocalDateTime(); |
| | | } catch (DateTimeParseException ex) { |
| | | log.warn("解析时间 {} 失败: {}", utcString, ex.getMessage()); |
| | | return null; |
| | | } |
| | | } |
| | | } |