package com.iailab.module.data.http.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.module.data.common.utils.HttpsRequest; import com.iailab.module.data.common.utils.PageUtils; import com.iailab.module.data.common.utils.Query; import com.iailab.module.data.http.dao.HttpTagDao; import com.iailab.module.data.http.dto.*; import com.iailab.module.data.http.entity.HttpApiEntity; import com.iailab.module.data.http.entity.HttpTagEntity; import com.iailab.module.data.http.service.HttpApiService; import com.iailab.module.data.http.service.HttpTagService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.*; /** * @author Houzhongjian * @Description * @createTime 2024年04月10日 17:35:00 */ @Slf4j @Service public class HttpTagServiceImpl extends BaseServiceImpl implements HttpTagService { @Resource private HttpsRequest httpsRequest; @Resource private HttpApiService httpApiService; /** * 分页查询tag * * @param params */ @Override public PageUtils queryPage(Map params) { String httpApiCode = (String) params.get("httpApiCode"); String tagType = (String) params.get("tagType"); String tagCode = (String) params.get("tagCode"); String tagName = (String) params.get("tagName"); IPage page = baseDao.selectPage( new Query().getPage(params), new QueryWrapper() .like(StringUtils.isNotBlank(httpApiCode), "http_api_code", httpApiCode) .like(StringUtils.isNotBlank(tagType), "tag_type", tagType) .like(StringUtils.isNotBlank(tagCode), "tag_code", tagCode) .like(StringUtils.isNotBlank(tagName), "tag_name", tagName) .orderByDesc("create_time") ); return new PageUtils(page); } @Override public List selectList(Map params) { String httpId = (String) params.get("httpId"); HttpApiEntity httpApiEntity = httpApiService.selectById(httpId); return baseDao.selectList(new QueryWrapper() .eq("http_api_code", httpApiEntity.getCode()) .orderByDesc("create_time")); } @Override public List getByCode(String code) { return baseDao.selectList(new QueryWrapper() .eq("http_api_code", code) .orderByDesc("create_time")); } @Override public List getByTagType(String deviceId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("tag_type", deviceId); List entityList = baseDao.selectList(queryWrapper); List list = new ArrayList<>(); if(ObjectUtils.isNotEmpty(entityList)) { entityList.stream().forEach(entity -> { String tagCode = entity.getTagCode(); list.add(tagCode); }); } return list; } @Override public List list(Map params) { List list = baseDao.getList(params); return list; } @Override public Map getTagsValues(String httpApiCode, List tags) { Map stringBigDecimalMap = new HashMap<>(); if (CommonConstant.YEAR_PEI_HTTP_TAG.equals(httpApiCode)) { stringBigDecimalMap = yearPeiTagsValues(tags); } else if(CommonConstant.CURRENT_PERFORMANCE_HTTP_TAG.equals(httpApiCode)) { stringBigDecimalMap = currentPerformanceTagsValues(tags); } else if(CommonConstant.CURRENT_SALE_HTTP_TAG.equals(httpApiCode)) { stringBigDecimalMap = currentSaleTagsValues(tags); } else if(CommonConstant.PRD_TIME_DIST_HTTP_TAG.equals(httpApiCode)) { stringBigDecimalMap = prdTimeDistTagsValues(tags); } return stringBigDecimalMap; } private Map yearPeiTagsValues(List tags) { Map result = new HashMap<>(tags.size()); Calendar calendar = Calendar.getInstance(); int currentMonth = calendar.get(Calendar.MONTH) + 1; // String url = httpApiService.getByCode(API_CODE).getUrl(); // String responseStr = httpsRequest.doGet(url, params,"utf-8", ""); String url = "http://192.168.55.121/Sunny/Action/BdApi/Invoke?code=Prd.YearPEI"; String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"yearData\":{\"year\":2024,\"xxPlan\":21850000.00,\"xxPerformance\":6794492.00,\"zqPlan\":12500000.00,\"zqPerformance\":3149110.41},\"monthData\":[{\"month\":1,\"xxPlan\":1400000.00,\"xxPerformance\":2022725.00,\"zqPlan\":800000.00,\"zqPerformance\":955066.76},{\"month\":2,\"xxPlan\":1400000.00,\"xxPerformance\":1829671.00,\"zqPlan\":800000.00,\"zqPerformance\":809494.02},{\"month\":3,\"xxPlan\":1700000.00,\"xxPerformance\":2225923.00,\"zqPlan\":1000000.00,\"zqPerformance\":971406.22},{\"month\":4,\"xxPlan\":1620000.00,\"xxPerformance\":716173.00,\"zqPlan\":1000000.00,\"zqPerformance\":345324.75},{\"month\":5,\"xxPlan\":0.0,\"xxPerformance\":0.00,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":6,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":7,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":8,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":9,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":10,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":11,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":12,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0}]}}"; if (StringUtils.isNotBlank(responseStr)) { TagYearPeiJsonDto yearPeiJsonDto = parseYearPeiDto(responseStr); tags.stream().forEach( item -> { String tagType = item.getTagType(); String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1]; TagYearPeiJsonDto.DATA res = yearPeiJsonDto.getRes(); TagYearPeiJsonDto.DATA.YEAR yearData = res.getYearData(); List monthDataList = res.getMonthData(); TagYearPeiJsonDto.DATA.MONTH month = monthDataList.get(currentMonth - 1); Class yearPeiClass = res.getClass(); String name = yearPeiClass.getName(); try { BigDecimal value; Class yearPeiTypeClass1 = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase()); Field field = yearPeiTypeClass1.getDeclaredField(tagCode); field.setAccessible(true); if(CommonConstant.YEAR.equals(tagType)){ value = new BigDecimal((String) field.get(yearData)); result.put(item.getTagCode(), value); } else if(CommonConstant.MONTH.equals(tagType)){ value = new BigDecimal((String) field.get(month)); result.put(item.getTagCode(), value); } } catch (NoSuchFieldException e) { log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode()); } catch (IllegalAccessException e) { log.info("没有反射权限"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } ); } return result; } private Map currentPerformanceTagsValues(List tags) { Map result = new HashMap<>(tags.size()); Map params = new HashMap<>(2); // String url = httpApiService.getByCode(API_CODE).getUrl(); // String responseStr = httpsRequest.doGet(url, params,"utf-8", ""); String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"today\":{\"xxPerformance\":30866.00,\"zqPerformance\":0.0},\"yesterday\":{\"xxPerformance\":65748.00,\"zqPerformance\":36263.20},\"currentMonth\":{\"xxPerformance\":716173.00,\"zqPerformance\":345324.75},\"currentYear\":{\"xxPerformance\":6794492.00,\"zqPerformance\":3149110.41}}}"; if (StringUtils.isNotBlank(responseStr)) { TagCurrentPerformanceJsonDto currentPerformanceJsonDto = parseCurrentPerformanceDto(responseStr); tags.stream().forEach( item -> { String tagType = item.getTagType(); String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1]; Class currentPerformanceClass = currentPerformanceJsonDto.getRes().getClass(); String name = currentPerformanceClass.getName(); try { Field declaredField = currentPerformanceClass.getDeclaredField(tagType); Class typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase()); Field field = typeClass.getDeclaredField(tagCode); field.setAccessible(true); declaredField.setAccessible(true); BigDecimal value = new BigDecimal((String) field.get(declaredField.get(currentPerformanceJsonDto.getRes()))); result.put(item.getTagCode(), value); } catch (NoSuchFieldException e) { log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode()); } catch (IllegalAccessException e) { log.info("没有反射权限"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } ); } return result; } private Map currentSaleTagsValues(List tags) { Map result = new HashMap<>(tags.size()); Map params = new HashMap<>(2); // String url = httpApiService.getByCode(API_CODE).getUrl(); // String responseStr = httpsRequest.doGet(url, params,"utf-8", ""); String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"today\":{\"groundsales\":0.0,\"medblock\":0.0,\"nubmeasure\":0.0,\"gangue\":0.0,\"reshipped\":0.0,\"trainTon\":0.0,\"trainCount\":0},\"yesterday\":{\"groundsales\":15696.00,\"medblock\":14757.00,\"nubmeasure\":0.00,\"gangue\":4384.00,\"reshipped\":2447.00,\"trainTon\":36263.20,\"trainCount\":10},\"currentMonth\":{\"groundsales\":0.0,\"medblock\":0.0,\"nubmeasure\":0.0,\"gangue\":0.0,\"reshipped\":0.0,\"trainTon\":0.0,\"trainCount\":0},\"currentYear\":{\"groundsales\":521638.00,\"medblock\":266372.00,\"nubmeasure\":0.00,\"gangue\":143475.00,\"reshipped\":139695.00,\"trainTon\":0.0,\"trainCount\":0}}}"; if (StringUtils.isNotBlank(responseStr)) { TagCurrentSaleJsonDto currentPerformanceJsonDto = parseCurrentSaleDto(responseStr); tags.stream().forEach( item -> { String tagType = item.getTagType(); String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1]; Class currentSaleClass = currentPerformanceJsonDto.getRes().getClass(); String name = currentSaleClass.getName(); try { Field declaredField = currentSaleClass.getDeclaredField(tagType); Class typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase()); Field field = typeClass.getDeclaredField(tagCode); field.setAccessible(true); declaredField.setAccessible(true); BigDecimal value = new BigDecimal((String) field.get(declaredField.get(currentPerformanceJsonDto.getRes()))); result.put(item.getTagCode(), value); } catch (NoSuchFieldException e) { log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode()); } catch (IllegalAccessException e) { log.info("没有反射权限"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } ); } return result; } private Map prdTimeDistTagsValues(List tags) { Map result = new HashMap<>(tags.size()); Map params = new HashMap<>(2); // String url = httpApiService.getByCode(API_CODE).getUrl(); // String responseStr = httpsRequest.doGet(url, params,"utf-8", ""); String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"xx\":{\"runMinutes\":1120.0,\"overhaulMinutes\":305.0,\"affectMinutes\":15.0},\"zq\":{\"runMinutes\":645.0,\"overhaulMinutes\":223.0,\"affectMinutes\":572.0}}}"; if (StringUtils.isNotBlank(responseStr)) { TagPrdTimeDistJsonDto prdTimeDistJsonDto = parsePrdTimeDistDto(responseStr); tags.stream().forEach( item -> { String tagType = item.getTagType(); String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1]; Class prdTimeDistClass = prdTimeDistJsonDto.getRes().getClass(); String name = prdTimeDistClass.getName(); try { Field declaredField = prdTimeDistClass.getDeclaredField(tagType); Class typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase()); Field field = typeClass.getDeclaredField(tagCode); field.setAccessible(true); declaredField.setAccessible(true); BigDecimal value = new BigDecimal((String) field.get(declaredField.get(prdTimeDistJsonDto.getRes()))); result.put(item.getTagCode(), value); } catch (NoSuchFieldException e) { log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode()); } catch (IllegalAccessException e) { log.info("没有反射权限"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } ); } return result; } private TagYearPeiJsonDto parseYearPeiDto(String responseStr) { TagYearPeiJsonDto result = new TagYearPeiJsonDto(); if (!StringUtils.isEmpty(responseStr)) { JSONObject items = JSONObject.parseObject(responseStr); result = items.toJavaObject(TagYearPeiJsonDto.class); } return result; } private TagCurrentPerformanceJsonDto parseCurrentPerformanceDto(String responseStr) { TagCurrentPerformanceJsonDto result = new TagCurrentPerformanceJsonDto(); if (!StringUtils.isEmpty(responseStr)) { JSONObject items = JSONObject.parseObject(responseStr); result = items.toJavaObject(TagCurrentPerformanceJsonDto.class); } return result; } private TagCurrentSaleJsonDto parseCurrentSaleDto(String responseStr) { TagCurrentSaleJsonDto result = new TagCurrentSaleJsonDto(); if (!StringUtils.isEmpty(responseStr)) { JSONObject items = JSONObject.parseObject(responseStr); result = items.toJavaObject(TagCurrentSaleJsonDto.class); } return result; } private TagPrdTimeDistJsonDto parsePrdTimeDistDto(String responseStr) { TagPrdTimeDistJsonDto result = new TagPrdTimeDistJsonDto(); if (!StringUtils.isEmpty(responseStr)) { JSONObject items = JSONObject.parseObject(responseStr); result = items.toJavaObject(TagPrdTimeDistJsonDto.class); } return result; } }