ProbeDetailQueryServiceImpl

  1 import com.alibaba.fastjson.JSONArray;
  2 import com.alibaba.fastjson.JSONObject;
  3 import lombok.extern.slf4j.Slf4j;
  4 import org.apache.commons.lang.StringUtils;
  5 import org.springframework.beans.factory.annotation.Autowired;
  6 import org.springframework.beans.factory.annotation.Value;
  7 import org.springframework.stereotype.Service;
  8 
  9 import javax.annotation.Resource;
 10 import java.io.IOException;
 11 import java.math.BigDecimal;
 12 import java.math.RoundingMode;
 13 import java.text.ParseException;
 14 import java.text.SimpleDateFormat;
 15 import java.util.Arrays;
 16 import java.util.Calendar;
 17 import java.util.Map;
 18 import java.util.stream.Collectors;
 19 
 20 import static com.huawei.it.regioc.service.helper.ReportQueryHelper.getHeaderMap;
 21 
 22 
 23 @Slf4j
 24 @Service
 25 public class ProbeDetailQueryServiceImpl implements IProbeDetailQueryService {
 26     @Value("${request.application.appId}")
 27     private String appId;
 28 
 29     @Value("${request.appSecret}")
 30     private String appSecret;
 31 
 32     @Resource
 33     private HttpConfig httpConfig;
 34 
 35     @Autowired
 36     private MybatisQuery mybatisQuery;
 37 
 38     @Override
 39     public TargetAttainment queryTargetAttainment(Condition condition) throws IOException {
 40         // 构建查询参数
 41         String probeId = condition.getFilters().stream().filter(item -> "probeId".equals(item.getFilterName())).collect(Collectors.toList()).get(0).getFilterValue();
 42         // 查询探针基础值
 43         ProbeInfo probeInfo = getProbeInfoVal(probeId,condition);
 44         ApiProbeParam apiParam = ReportQueryHelper.buildProbeRegionParam(condition);
 45         apiParam.setPeriodId(condition.getTime());
 46         apiParam.setProbeId(probeId);
 47         apiParam.setThresholdVal(probeInfo.getThresholdVal());
 48         apiParam.setTargetVal(probeInfo.getTargetVal());
 49         apiParam.setBaseVal(probeInfo.getBaseVal());
 50         // 调用外部API,API由xml解析后放入静态变量Container,从其中获取
 51         Map<String, String> headerMap = getHeaderMap(httpConfig.getAppId(), httpConfig.getAppSecret());
 52         String url = "";
 53         String titile = "";
 54         // 发未收汇总比例
 55         if ("overdue_not_received".equals(probeId)) {
 56             url = httpConfig.getTargetAttainmentNotReceived();
 57             titile = "风险备货积压目标达成";
 58         } else if ("overdue_not_checked".equals(probeId)){
 59             // 收未验汇总比例
 60             url = httpConfig.getTargetAttainmentNotCheck();
 61             titile = "风险备货积压目标达成";
 62         }
 63         String result = HttpUtils.postRequest(url, JSONObject.toJSONString(apiParam), headerMap);
 64         JSONObject resultObject = JSONObject.parseObject(result);
 65         JSONArray jsonArray = resultObject.getJSONArray("list");
 66         JSONObject data = jsonArray.getJSONObject(0);
 67         return new TargetAttainment(titile, invertPercent(data.getString("PERCENTAGE_OF_REGION_CN_NAME_7")), invertPercent(data.getString("同比")), invertPercent(data.getString("环比")),"环比");
 68     }
 69 
 70     @Override
 71     public BasicChart queryAchievementTrend(Condition condition) throws IOException, ParseException {
 72         // 构建查询参数
 73         String probeId = condition.getFilters().stream().filter(item -> "probeId".equals(item.getFilterName())).collect(Collectors.toList()).get(0).getFilterValue();
 74         // 1-当月 2-累月
 75         String type = condition.getFilters().stream().filter(item -> "type".equals(item.getFilterName())).collect(Collectors.toList()).get(0).getFilterValue();
 76         // 查询探针基础值
 77         ProbeInfo probeInfo = getProbeInfoVal(probeId,condition);
 78         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
 79         String endTime = condition.getTime().substring(0, 6);
 80         Calendar calendar = Calendar.getInstance();
 81         calendar.setTime(sdf.parse(endTime));
 82         calendar.add(Calendar.YEAR, -1);
 83         String startTime = sdf.format(calendar.getTime());
 84         ApiParam apiParam = ReportQueryHelper.buildApiParam(condition, "7");
 85         apiParam.setPeriodId(condition.getTime());
 86         apiParam.setBeginTime(startTime);
 87         apiParam.setEndTime(endTime);
 88         apiParam.setRepOfficeCnName("ALL");
 89         apiParam.setCountryCnName("ALL");
 90         apiParam.setThresholdVal(probeInfo.getThresholdVal());
 91         apiParam.setTargetVal(probeInfo.getTargetVal());
 92         apiParam.setBaseVal(probeInfo.getBaseVal());
 93         Map<String, String> headerMap = getHeaderMap(httpConfig.getAppId(), httpConfig.getAppSecret());
 94         String url = "";
 95         String titile = "";
 96         String xName = "";
 97         String yName = "";
 98         // 发未收汇总
 99         if ("overdue_not_received".equals(probeId)) {
100             url = httpConfig.getAchievementTrendNotReceived();
101             xName = "PERIOD_ID";
102             if ("2".equals(type)) {
103                 titile = "达成趋势-累月值";
104                 yName = "PERCENTAGE_OF_REGION_CN_NAME_7_LJ";
105             } else {
106                 titile = "达成趋势-当月值";
107                 yName = "PERCENTAGE_OF_REGION_CN_NAME_7";
108             }
109         } else if ("overdue_not_checked".equals(probeId)){
110             // 收未验汇总
111             url = httpConfig.getAchievementTrendNotCheck();
112             xName = "PERIOD_ID";
113             if ("2".equals(type)) {
114                 titile = "达成趋势-累月值";
115                 yName = "PERCENTAGE_OF_REGION_CN_NAME_7_LJ";
116             } else {
117                 titile = "达成趋势-当月值";
118                 yName = "PERCENTAGE_OF_REGION_CN_NAME_7";
119             }
120         }
121         String result = HttpUtils.postRequest(url, JSONObject.toJSONString(apiParam), headerMap);
122         JSONObject resultObject = JSONObject.parseObject(result);
123         return buildBasicChart(resultObject, titile, xName, yName);
124     }
125 
126     @Override
127     public Distribution queryDistribution(Condition condition) throws IOException {
128         // 构建查询参数
129         String probeId = condition.getFilters().stream().filter(item -> "probeId".equals(item.getFilterName())).collect(Collectors.toList()).get(0).getFilterValue();
130         // 查询探针基础值
131         ProbeInfo probeInfo = getProbeInfoVal(probeId,condition);
132         ApiParam apiParam = ReportQueryHelper.buildApiParam(condition, probeInfo.getThresholdVal());
133         apiParam.setPeriodId(condition.getTime());
134         apiParam.setRepOfficeCnName("ALL");
135         apiParam.setCountryCnName("ALL");
136         Map<String, String> headerMap = getHeaderMap(httpConfig.getAppId(), httpConfig.getAppSecret());
137         String url = "";
138         String titile = "";
139         String xName = "";
140         String yName = "";
141         // 发未收
142         if ("overdue_not_received".equals(probeId)) {
143             url = httpConfig.getDistributionNotReceived();
144             titile = "项目风险分布";
145             xName = "proj_en_name";
146             yName = "PERCENTAGE_OF_REGION_CN_NAME_7";
147         } else if ("overdue_not_checked".equals(probeId)){
148             // 收未验汇总
149             url = httpConfig.getDistributionNotCheck();
150             titile = "项目风险分布";
151             xName = "proj_en_name";
152             yName = "PERCENTAGE_OF_REGION_CN_NAME_7";
153         }
154         String result = HttpUtils.postRequest(url, JSONObject.toJSONString(apiParam), headerMap);
155         JSONObject resultObject = JSONObject.parseObject(result);
156         // 处理返回结果
157         Distribution distribution = new Distribution();
158         distribution.getBasicChartList().add(buildBasicChart(resultObject, titile, xName, yName));
159         return distribution;
160     }
161 
162     @Override
163     public TableDetail queryTableDetail(Condition condition) throws IOException {
164         // 构建查询参数
165         ApiParam apiParam = ReportQueryHelper.buildDetailApiParam(condition, "7");
166         String probeId = condition.getFilters()
167                 .stream()
168                 .filter(item -> item.getFilterName().equals("probe_id"))
169                 .collect(Collectors.toList())
170                 .get(0)
171                 .getFilterValue();
172         Map<String, String> headerMap = getHeaderMap(appId, appSecret);
173         String[] header = {};
174         String url = "";
175         if ("issued_not_received".equals(probeId)) {
176             url = "http://apigw.huawei.com/api/data-subject/app_000000035085/D103144";
177             header =
178                     new String[]{"REPOFFICE_EN_NAME", "CUST_CLASS_CN_NAME", "DW_CREATED_BY", "MORE_THAN_7DAY", "PERIOD_ID",
179                             "LESS_THAN_7DAY", "PURPOSE_OF_DELIVERY", "MODE1", "DT", "REPOFFICE_CN_NAME", "DW_LAST_UPDATED_BY",
180                             "PERIOD_WEEK_ID", "DW_LAST_UPDATED_DATE", "FIRST_SUBWH_SIGN_DATE", "RANGE_TYPE", "REGION_CODE",
181                             "ABNORMAL_DESC", "REGION_EN_NAME", "COUNTRY_EN_NAME", "OVER_FLAG", "PROJ_NUM",
182                             "SUBCON_INBOUND_DATE", "REGION_CN_NAME", "PROJ_EN_NAME", "COUNTRY_CN_NAME", "DW_CREATED_DATE",
183                             "ORIGINAL_BILL_NO", "HW_CONTRACT_NUM", "SUBCON_OUTBOUND_DATE", "OVER_DAYS", "PROJ_CN_NAME",
184                             "REPOFFICE_CODE"};
185         }
186         String jsonString = JSONObject.toJSONString(apiParam);
187         
188         // 发送查询请求
189         String result = HttpUtils.postRequest(url, jsonString, headerMap);
190         
191         // 组装返回数据
192         JSONObject jsonObject = JSONObject.parseObject(result);
193         
194         TableDetail tableDetail = new TableDetail();
195         tableDetail.setHeader(Arrays.asList(header));
196         JSONArray jsonArray = jsonObject.getJSONArray("list");
197         tableDetail.setList(jsonArray);
198         return tableDetail;
199     }
200 
201     public static BasicChart buildBasicChart(JSONObject result, String title, String xName, String yName) {
202         BasicChart basicChart = new BasicChart();
203         basicChart.setTitle(title);
204         JSONArray jsonArray = result.getJSONArray("list");
205         for (int i = 0; i < jsonArray.size(); i++) {
206             JSONObject item = jsonArray.getJSONObject(i);
207             if (StringUtils.isNotBlank(item.getString(xName)) && StringUtils.isNotBlank(item.getString(yName))) {
208                 basicChart.getXAxis().add(item.getString(xName));
209                 basicChart.getYAxis().add(Double.valueOf(item.getString(yName)));
210             }
211         }
212         return basicChart;
213     }
214 
215     /**
216      * 转百分比(保留一位小数)
217      * @param val
218      * @return
219      */
220     private String invertPercent(String val) {
221         String result = "";
222         if (StringUtils.isNotBlank(val)) {
223             result = new BigDecimal(100).multiply(new BigDecimal(val)).setScale(1, RoundingMode.HALF_UP).toString();
224         }
225         return result;
226     }
227 
228     /**
229      * 查询探针基础值
230      * @param probeId
231      * @param condition
232      * @return
233      */
234     private ProbeInfo getProbeInfoVal(String probeId, Condition condition) {
235         ApiProbeParam probeParam = new ApiProbeParam();
236         probeParam.setProbeId(probeId);
237         probeParam.setRegionCnName(condition.getRegion().getRegionOrg());
238         probeParam.setRepoffceCnName(condition.getRegion().getRepOffice());
239         // 查询探针基础值
240         return mybatisQuery.getProbeInfo(probeParam).get(0);
241 
242     }
243 }

 

posted @ 2022-11-08 20:56  MING5135  阅读(28)  评论(0)    收藏  举报