package com.sooka.sponest.dataexchange.sendChange.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.log.annotation.Log; import com.ruoyi.common.log.enums.BusinessType; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.RemoteConfigService; import com.sooka.sponest.dataexchange.remoteapi.service.center.data.RemoteDataBaseService; import com.sooka.sponest.dataexchange.sendChange.domian.CenterdataTKeyProjects; import com.sooka.sponest.dataexchange.sendChange.domian.CenterdataTKeyProjectsPictures; import com.sooka.sponest.dataexchange.util.RestUtil; import org.springframework.beans.factory.annotation.Value; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @Slf4j @Controller @RequestMapping("/sendChange") public class EngineeringController { @Resource private RemoteDataBaseService remoteDataBaseService; @Resource private RemoteConfigService remoteConfigService; @Value("${engineering.ip}") private String ip; @Value("${engineering.port}") private String port; @Value("${engineering.username}") private String username; @Value("${engineering.password}") private String password; /** * 重点工程 * @param * @return */ @PostMapping("/pushEngineeringData") @ResponseBody @Log(title = "重点工程向数据中心推送事件", businessType = BusinessType.OTHER) public R pushEngineeringData(@RequestBody List list) { log.info("centerdataTKeyProjects:{}",list); if (StringUtils.isNotEmpty(SecurityUtils.getToken())) { log.info("SecurityUtils.getToken():{}",SecurityUtils.getToken()); R result = remoteDataBaseService.receivedMessage(list); if (result.getCode() == 200) { return R.ok(null,"推送成功"); } else { return R.fail(result.getCode(),result.getMsg()); } } else { return R.fail(401,"登录状态已过期"); } } @PostMapping("/receiveEngineeringData") @ResponseBody @Log(title = "向重点工程推送事件图片", businessType = BusinessType.OTHER) public R receiveEngineeringData(@RequestBody List list) { // 获取token JSONObject params = new JSONObject(); params.put("username", username); params.put("password", password); JSONObject result; try { result = RestUtil.post(ip + ":" + port + "/api/getToken", params); } catch (Exception e) { log.info("获取token报错:" + e.getMessage()); return R.fail("获取token报错"); } log.info("List:{}",list); R appUrl = remoteConfigService.remotegetConfigKey("appUrl"); List requestList = new ArrayList<>(); for (CenterdataTKeyProjectsPictures var : list) { List pictureList = new ArrayList<>(); CenterdataTKeyProjectsPictures centerdataTKeyProjectsPictures = new CenterdataTKeyProjectsPictures(); centerdataTKeyProjectsPictures.setProjectId(var.getProjectId()); for (String str : var.getSchedulePictures()) { pictureList.add(appUrl.getData() + str); } centerdataTKeyProjectsPictures.setSchedulePictures(pictureList); requestList.add(var); } JSONObject variables = new JSONObject(); variables.put(HttpHeaders.COOKIE, result.getString("data")); try { JSONArray array = JSONArray.parseArray(JSON.toJSONString(requestList)); result = RestUtil.post(ip + ":" + port + "/api/receiveEngineeringPictures", variables, array); log.info(result+""); } catch (Exception e) { log.info("重点工程报错:" + e.getMessage()); return R.fail("重点工程报错"); } return R.ok(result,"接收成功"); } /** * 重点区域 * @param * @return */ // @PostMapping("/pushAreaData") // @ResponseBody // public R pushAreaData(@RequestBody DataExchangeEntity dataExchangeEntity) { // log.info("dataExchangeEntity:{}",dataExchangeEntity); // List pictureList = new ArrayList<>(); // if (StringUtils.isNotEmpty(SecurityUtils.getToken()) && !CollectionUtils.isEmpty(dataExchangeEntity.getEventPicture())) { // for (String picture : dataExchangeEntity.getEventPicture()) { // if (!"image".equals(FilePrefixUtils.getUrlSuffix(picture))) { // log.error("事件图片必须为图片格式---{}", dataExchangeEntity); // return R.fail("事件图片必须为图片格式"); // } else { // MultipartFile multipartFile = FilePrefixUtils.urlToMultipartFile(picture, System.currentTimeMillis() + ".jpg"); // SysFile sysFile = SpringUtils.getBean(RemoteFileService.class).upload(multipartFile).getData(); // pictureList.add(sysFile.getUrl()); // } // } // dataExchangeEntity.setPictureUrlList(pictureList); // R result = remoteDataBaseService.insert3rdSystemEvent(dataExchangeEntity); // if (result.getCode() == 200) { // return R.ok("推送成功"); // } else { // return R.fail(result.getMsg()); // } // } else { // return R.fail("推送失败"); // } // } // // // @PostMapping("/receiveAreaData") // @ResponseBody // public R receiveAreaData(@RequestBody DataExchangeEntity dataExchangeEntity) { // System.out.println(dataExchangeEntity); // R appUrl = remoteConfigService.remotegetConfigKey("appUrl"); // List pictureList = new ArrayList<>(); // for (String picture : dataExchangeEntity.getEventPicture()) { // pictureList.add(appUrl.getData().toString() + picture); // } // dataExchangeEntity.setEventPicture(pictureList); // // TODO: 2024/1/9 0009 待修改 // JSONObject jsonObject = new JSONObject(); // jsonObject.put("", dataExchangeEntity); // try { // RestUtil.post(url, jsonObject); // } catch (Exception e) { // log.info("远程调用119报错"); // } // return R.ok("接收成功"); // } }