Commit 61005da1 by 丁伟峰

将根据地点、图层返回teamId分离到公共service中

1 parent 9649c7da
...@@ -15,4 +15,6 @@ public interface OrgTeamDao extends JpaRepository<OrgTeamEntity, Integer> { ...@@ -15,4 +15,6 @@ public interface OrgTeamDao extends JpaRepository<OrgTeamEntity, Integer> {
List<OrgTeamEntity> findAllByBranchId(String branchId); List<OrgTeamEntity> findAllByBranchId(String branchId);
List<OrgTeamEntity> findAllByGroupId(String groupId); List<OrgTeamEntity> findAllByGroupId(String groupId);
OrgTeamEntity getByTeamId(String teamId);
} }
...@@ -16,6 +16,7 @@ public class OrderCreateReqDTO { ...@@ -16,6 +16,7 @@ public class OrderCreateReqDTO {
private String faultDescribe; private String faultDescribe;
private String expectBegin; private String expectBegin;
private String expectEnd; private String expectEnd;
private String expectDesc;
private String priority; private String priority;
private String orderTags; private String orderTags;
private String description; private String description;
......
package com.alibaba.cloud.integration.order.service;
import com.alibaba.cloud.integration.order.dao.CapacityTeamStatDao;
import com.alibaba.cloud.integration.order.dao.MapBlockInfoDao;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Service
@Slf4j
public class CommonService {
@Autowired
private MapBlockInfoDao mapBlockInfoDao;
@Autowired
private CapacityTeamStatDao capacityTeamStatDao;
public String getTeamIdByInput(String location, String address, String layer) {
// todo 目前会随机兜底,后面将合理化
String teamId = null;
List<LayerAndArea> layerAndAreas = getLayerAndAreas(location, address);
if (layerAndAreas == null || layerAndAreas.size() == 0) {
// 分单接口没有查到,本地随机处理一下
teamId = capacityTeamStatDao.getRandomTeamIdByLayer(layer);
} else {
// 生成随机索引
int randomIndex = new Random().nextInt(layerAndAreas.size());
// 获取随机元素
LayerAndArea layerAndArea = layerAndAreas.get(randomIndex);
teamId = mapBlockInfoDao.getTeamIdByBlockIdAndLayer(layerAndArea.areaId, layer);
}
if (teamId == null || teamId.isBlank()) {
teamId = capacityTeamStatDao.getRandomTeamId();
}
log.info("teamId ==> {}", teamId);
assert (teamId != null && !teamId.isBlank());
return teamId;
}
/**
* 根据提供的地址信息,获取对应的片区图层定义的工作队;如果没有找到,返回为空
* 目前layer_info表中,area_data是保存位置围栏的json数据,后面可能会是对于其他图层的引用;
*
* @param location
* @return
*/
private List<LayerAndArea> getLayerAndAreas(String location, String address) {
// 根据坐标 查询分单接口,获得区块、图层列表 ===> 查询map_block_info表,根据优先级,确定 工作队;
// 根据工作队+图层,查询 capacity_team_stat,返回
// todo 由于saas的分单接口尚未完成,目前仅仅是调用分单获取图层、区块名称,对于我们的容量查询没有真正使用,目前采用随机返回方式
// 创建RestTemplate实例
log.info("===> getLayerAndAreas({}, {})", location, address);
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 构建请求参数
String url;
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("ak", "284c57cbabad4755a9c657885a8df3e2");
if (location != null && !location.isBlank()) {
url = "https://pea-test.bshg.com.cn/v2/xyfendan";
params.add("coordinate", location);
} else {
url = "https://pea-test.bshg.com.cn/v2/fendan";
params.add("addresses", String.format("[{\"address\":\"%s\"}]", address));
}
params.add("need_district", "false");
params.add("need_layer", "true");
// params.add("related_point_fields", "");
params.add("area_fields", "名称,唯一编号");
log.info("request params ==> {}", params);
// 构建请求实体
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
// 发送POST请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
// 获取响应结果
String responseBody = responseEntity.getBody();
log.info("after call saas ==> {}", responseEntity.getBody());
// 使用Jackson库解析JSON数据
ObjectMapper objectMapper = new ObjectMapper();
try {
List<LayerAndArea> results = new ArrayList<>();
JsonNode responseJson = objectMapper.readTree(responseBody);
// 获取areaResults[0]的值
for (JsonNode r : responseJson.get("result").get(0).get("areaResults")) {
// 分单接口暂时无图层返回
results.add(new LayerAndArea().setAreaId(r.get("field2").asText()).setAreaName(r.get("field1").asText()));
}
return results;
} catch (Exception e) {
return null;
}
}
@lombok.Data
@Accessors(chain = true)
private static class LayerAndArea {
private String layer;
private String areaId;
private String areaName;
}
}
...@@ -10,19 +10,13 @@ import com.alibaba.cloud.integration.order.entity.CapacityOrgStatEntity; ...@@ -10,19 +10,13 @@ import com.alibaba.cloud.integration.order.entity.CapacityOrgStatEntity;
import com.alibaba.cloud.integration.order.entity.CapacityTeamStatEntity; import com.alibaba.cloud.integration.order.entity.CapacityTeamStatEntity;
import com.alibaba.cloud.integration.order.entity.OrgGroupEntity; import com.alibaba.cloud.integration.order.entity.OrgGroupEntity;
import com.alibaba.cloud.integration.order.service.CapacityQueryService; import com.alibaba.cloud.integration.order.service.CapacityQueryService;
import com.fasterxml.jackson.databind.JsonNode; import com.alibaba.cloud.integration.order.service.CommonService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -44,8 +38,6 @@ public class CapacityQueryServiceImpl implements CapacityQueryService { ...@@ -44,8 +38,6 @@ public class CapacityQueryServiceImpl implements CapacityQueryService {
@Autowired @Autowired
private ProductCategoryDao productCategoryDao; private ProductCategoryDao productCategoryDao;
@Autowired
private MapBlockInfoDao mapBlockInfoDao;
@Autowired @Autowired
private OrgGroupDao orgGroupDao; private OrgGroupDao orgGroupDao;
...@@ -53,32 +45,17 @@ public class CapacityQueryServiceImpl implements CapacityQueryService { ...@@ -53,32 +45,17 @@ public class CapacityQueryServiceImpl implements CapacityQueryService {
@Autowired @Autowired
private OrgTeamDao orgTeamDao; private OrgTeamDao orgTeamDao;
@Autowired
private CommonService commonService;
@Override @Override
public Result<?> getOneCapacityData(CapacityOrderQueryReqDTO reqDTO) { public Result<?> getOneCapacityData(CapacityOrderQueryReqDTO reqDTO) {
/* /*
location ==> [layer_info] ==> team ==> (+skill) ==> [capacity_team_stat] location ==> [layer_info] ==> team ==> (+skill) ==> [capacity_team_stat]
*/ */
log.info("===> getOneCapacityData"); log.info("===> getOneCapacityData");
String teamId;
String layer = productCategoryDao.getLayerByBrandAndTypeAndSkill(reqDTO.getBrand(), reqDTO.getType(), reqDTO.getSkill()); String layer = productCategoryDao.getLayerByBrandAndTypeAndSkill(reqDTO.getBrand(), reqDTO.getType(), reqDTO.getSkill());
List<LayerAndArea> layerAndAreas = getLayerAndAreas(reqDTO.getLocation(), reqDTO.getAddress()); String teamId = commonService.getTeamIdByInput(reqDTO.getLocation(), reqDTO.getAddress(), layer);
log.info("=== getLayerAndAreas() ==> {}", layerAndAreas);
if (layerAndAreas == null || layerAndAreas.size() == 0) {
// 分单接口没有查到,本地随机处理一下
teamId = capacityTeamStatDao.getRandomTeamIdByLayer(layer);
} else {
// 生成随机索引
int randomIndex = new Random().nextInt(layerAndAreas.size());
// 获取随机元素
LayerAndArea layerAndArea = layerAndAreas.get(randomIndex);
teamId = mapBlockInfoDao.getTeamIdByBlockIdAndLayer(layerAndArea.areaId, layer);
}
if (teamId == null || teamId.isBlank()) {
teamId = capacityTeamStatDao.getRandomTeamId();
}
log.info("teamId ==> {}", teamId);
assert (teamId != null && !teamId.isBlank());
log.info("teamId[{}]layer[{}]", teamId, layer); log.info("teamId[{}]layer[{}]", teamId, layer);
CapacityQueryOrderRespDTO capacityQueryRespDTO = new CapacityQueryOrderRespDTO(); CapacityQueryOrderRespDTO capacityQueryRespDTO = new CapacityQueryOrderRespDTO();
capacityQueryRespDTO.setBeginDate(reqDTO.getBeginDate()).setEndDate(reqDTO.getEndDate()); capacityQueryRespDTO.setBeginDate(reqDTO.getBeginDate()).setEndDate(reqDTO.getEndDate());
...@@ -95,6 +72,7 @@ public class CapacityQueryServiceImpl implements CapacityQueryService { ...@@ -95,6 +72,7 @@ public class CapacityQueryServiceImpl implements CapacityQueryService {
legends.add(new CapacityQueryOrderRespDTO.LegendDTO().setType(3).setMemo("剩余30%以内")); legends.add(new CapacityQueryOrderRespDTO.LegendDTO().setType(3).setMemo("剩余30%以内"));
return legends; return legends;
} }
@Override @Override
public Result<?> getTeamStatData(CapacityStatQueryReqDTO capacityStatQueryReqDTO) { public Result<?> getTeamStatData(CapacityStatQueryReqDTO capacityStatQueryReqDTO) {
Page<?> stats = null; Page<?> stats = null;
...@@ -129,66 +107,6 @@ public class CapacityQueryServiceImpl implements CapacityQueryService { ...@@ -129,66 +107,6 @@ public class CapacityQueryServiceImpl implements CapacityQueryService {
return Result.success(data); return Result.success(data);
} }
/**
* 根据提供的地址信息,获取对应的片区图层定义的工作队;如果没有找到,返回为空
* 目前layer_info表中,area_data是保存位置围栏的json数据,后面可能会是对于其他图层的引用;
*
* @param location
* @return
*/
private List<LayerAndArea> getLayerAndAreas(String location, String address) {
// 根据坐标 查询分单接口,获得区块、图层列表 ===> 查询map_block_info表,根据优先级,确定 工作队;
// 根据工作队+图层,查询 capacity_team_stat,返回
// todo 由于saas的分单接口尚未完成,目前仅仅是调用分单获取图层、区块名称,对于我们的容量查询没有真正使用,目前采用随机返回方式
// 创建RestTemplate实例
log.info("===> getLayerAndAreas({}, {})", location, address);
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 构建请求参数
String url;
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("ak", "284c57cbabad4755a9c657885a8df3e2");
if (location != null && !location.isBlank()) {
url = "https://pea-test.bshg.com.cn/v2/xyfendan";
params.add("coordinate", location);
} else {
url = "https://pea-test.bshg.com.cn/v2/fendan";
params.add("addresses", String.format("[{\"address\":\"%s\"}]", address));
}
params.add("need_district", "false");
params.add("need_layer", "true");
// params.add("related_point_fields", "");
params.add("area_fields", "名称,唯一编号");
log.info("request params ==> {}", params);
// 构建请求实体
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
// 发送POST请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
// 获取响应结果
String responseBody = responseEntity.getBody();
log.info("after call saas ==> {}", responseEntity.getBody());
// 使用Jackson库解析JSON数据
ObjectMapper objectMapper = new ObjectMapper();
try {
List<LayerAndArea> results = new ArrayList<>();
JsonNode responseJson = objectMapper.readTree(responseBody);
// 获取areaResults[0]的值
for (JsonNode r : responseJson.get("result").get(0).get("areaResults")) {
// 分单接口暂时无图层返回
results.add(new LayerAndArea().setAreaId(r.get("field2").asText()).setAreaName(r.get("field1").asText()));
}
return results;
} catch (Exception e) {
return null;
}
}
private int getSpanType(int capLeft, int capTotal) { private int getSpanType(int capLeft, int capTotal) {
float ratio = (float) capLeft / capTotal; float ratio = (float) capLeft / capTotal;
...@@ -260,12 +178,4 @@ public class CapacityQueryServiceImpl implements CapacityQueryService { ...@@ -260,12 +178,4 @@ public class CapacityQueryServiceImpl implements CapacityQueryService {
DayOfWeek weekday = date.getDayOfWeek(); DayOfWeek weekday = date.getDayOfWeek();
return weekday.getDisplayName(TextStyle.SHORT, Locale.CHINA); return weekday.getDisplayName(TextStyle.SHORT, Locale.CHINA);
} }
@lombok.Data
@Accessors(chain = true)
private static class LayerAndArea {
private String layer;
private String areaId;
private String areaName;
}
} }
...@@ -19,13 +19,16 @@ package com.alibaba.cloud.integration.order.service.impl; ...@@ -19,13 +19,16 @@ package com.alibaba.cloud.integration.order.service.impl;
import com.alibaba.cloud.integration.common.Result; import com.alibaba.cloud.integration.common.Result;
import com.alibaba.cloud.integration.order.dao.OrderRequestDao; import com.alibaba.cloud.integration.order.dao.OrderRequestDao;
import com.alibaba.cloud.integration.order.dao.OrderTagStrategyDao; import com.alibaba.cloud.integration.order.dao.OrderTagStrategyDao;
import com.alibaba.cloud.integration.order.dao.OrgTeamDao;
import com.alibaba.cloud.integration.order.dao.ProductCategoryDao; import com.alibaba.cloud.integration.order.dao.ProductCategoryDao;
import com.alibaba.cloud.integration.order.dto.KeyValueDTO; import com.alibaba.cloud.integration.order.dto.KeyValueDTO;
import com.alibaba.cloud.integration.order.dto.LocationDTO; import com.alibaba.cloud.integration.order.dto.LocationDTO;
import com.alibaba.cloud.integration.order.dto.OrderCreateReqDTO; import com.alibaba.cloud.integration.order.dto.OrderCreateReqDTO;
import com.alibaba.cloud.integration.order.dto.ParameterRespDTO; import com.alibaba.cloud.integration.order.dto.ParameterRespDTO;
import com.alibaba.cloud.integration.order.entity.OrderRequestEntity; import com.alibaba.cloud.integration.order.entity.OrderRequestEntity;
import com.alibaba.cloud.integration.order.entity.OrgTeamEntity;
import com.alibaba.cloud.integration.order.entity.ProductCategoryEntity; import com.alibaba.cloud.integration.order.entity.ProductCategoryEntity;
import com.alibaba.cloud.integration.order.service.CommonService;
import com.alibaba.cloud.integration.order.service.OrderCreateService; import com.alibaba.cloud.integration.order.service.OrderCreateService;
import io.seata.core.context.RootContext; import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -50,6 +53,12 @@ public class OrderCreateServiceImpl implements OrderCreateService { ...@@ -50,6 +53,12 @@ public class OrderCreateServiceImpl implements OrderCreateService {
@Autowired @Autowired
private ProductCategoryDao productCategoryDao; private ProductCategoryDao productCategoryDao;
@Autowired
private OrgTeamDao orgTeamDao;
@Autowired
private CommonService commonService;
private List<KeyValueDTO> getPriorities() { private List<KeyValueDTO> getPriorities() {
List<KeyValueDTO> listPriorities = new ArrayList<>(); List<KeyValueDTO> listPriorities = new ArrayList<>();
int prioritiesLevels = 10; int prioritiesLevels = 10;
...@@ -101,15 +110,10 @@ public class OrderCreateServiceImpl implements OrderCreateService { ...@@ -101,15 +110,10 @@ public class OrderCreateServiceImpl implements OrderCreateService {
entity.setType(req.getType()); entity.setType(req.getType());
entity.setSkill(req.getSkill()); entity.setSkill(req.getSkill());
ProductCategoryEntity categoryEntity = productCategoryDao.getProductCategoryEntityByBrandAndTypeAndSkill(req.getBrand(), req.getType(), req.getSkill());
if (categoryEntity == null) {
throw new RuntimeException("产品代码不存在!");
}
String catalogId = categoryEntity.getProductCategoryId();
entity.setCategoryId(catalogId);
entity.setFaultDescribe(req.getFaultDescribe()); entity.setFaultDescribe(req.getFaultDescribe());
entity.setExpectTimeBegin(req.getExpectBegin()); entity.setExpectTimeBegin(req.getExpectBegin());
entity.setExpectTimeEnd(req.getExpectEnd()); entity.setExpectTimeEnd(req.getExpectEnd());
entity.setExpectTimeDesc(req.getExpectDesc());
entity.setDescription(req.getDescription()); entity.setDescription(req.getDescription());
// location // location
LocationDTO location = req.getLocation(); LocationDTO location = req.getLocation();
...@@ -117,6 +121,16 @@ public class OrderCreateServiceImpl implements OrderCreateService { ...@@ -117,6 +121,16 @@ public class OrderCreateServiceImpl implements OrderCreateService {
entity.setCity(location.getCity()); entity.setCity(location.getCity());
entity.setCounty(location.getCountry()); entity.setCounty(location.getCountry());
// 根据分单,填写clusterId/branchId/groupId/teamId等
String layer = productCategoryDao.getLayerByBrandAndTypeAndSkill(req.getBrand(), req.getType(), req.getSkill());
String teamId = commonService.getTeamIdByInput(req.getLocation().getLocation(), req.getAddress(), layer);
OrgTeamEntity teamEntity = orgTeamDao.getByTeamId(teamId);
entity.setOrgClusterId(teamEntity.getClusterId());
entity.setOrgBranchId(teamEntity.getBranchId());
entity.setOrgGroupId(teamEntity.getGroupId());
entity.setOrgTeamId(teamId);
// 根据orderTags, 解析保存到type、skill等字段 // 根据orderTags, 解析保存到type、skill等字段
String[] l = req.getLocation().getLocation().split(","); String[] l = req.getLocation().getLocation().split(",");
entity.setX(l[0]); entity.setX(l[0]);
...@@ -125,6 +139,13 @@ public class OrderCreateServiceImpl implements OrderCreateService { ...@@ -125,6 +139,13 @@ public class OrderCreateServiceImpl implements OrderCreateService {
entity.setStatus("OPEN"); entity.setStatus("OPEN");
entity.setAppointmentStatus("NOT_ASSIGNED"); entity.setAppointmentStatus("NOT_ASSIGNED");
entity.setAppointmentMethod("IMMEDIATE"); entity.setAppointmentMethod("IMMEDIATE");
ProductCategoryEntity categoryEntity = productCategoryDao.getProductCategoryEntityByBrandAndTypeAndSkill(req.getBrand(), req.getType(), req.getSkill());
if (categoryEntity == null) {
throw new RuntimeException("产品代码不存在!");
}
String catalogId = categoryEntity.getProductCategoryId();
entity.setCategoryId(catalogId);
orderRequestDao.save(entity); orderRequestDao.save(entity);
return Result.success(null); return Result.success(null);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!