| | |
| | | package cn.gistack.sm.sjztods.service.impl; |
| | | |
| | | import cn.gistack.common.utils.SpringContextUtil; |
| | | import cn.gistack.sm.sjztods.constant.ZtApiUrlConstant; |
| | | import cn.gistack.sm.sjztods.constant.ZtConfigConstant; |
| | | import cn.gistack.sm.sjztods.entity.SkxsQkTj; |
| | | import cn.gistack.sm.sjztods.excel.SkxsQkTjExcel; |
| | | import cn.gistack.sm.sjztods.mapper.SkxsQkTjMapper; |
| | | import cn.gistack.sm.sjztods.service.ISkxsQkTjService; |
| | | import cn.gistack.sm.sjztods.vo.SkxsQkTjVO; |
| | | import cn.gistack.system.user.entity.User; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.parser.Feature; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @ClassName SkxsQkTjServiceImpl |
| | |
| | | @Service |
| | | @DS("ztznwh") |
| | | public class SkxsQkTjServiceImpl extends ServiceImpl<SkxsQkTjMapper, SkxsQkTj> implements ISkxsQkTjService { |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | /** |
| | | * 上报 |
| | |
| | | public List<SkxsQkTjExcel> exportSkxsQkTjList(SkxsQkTjVO skxsQkTj) { |
| | | return baseMapper.exportSkxsQkTjList(skxsQkTj); |
| | | } |
| | | |
| | | /** |
| | | * 中台接口数据调用,数据入库 |
| | | * @param skxsQkTj |
| | | */ |
| | | @Override |
| | | public void saveOrUpdateData(SkxsQkTjVO skxsQkTj) { |
| | | // 参数拼接 |
| | | StringBuilder builder = new StringBuilder("?"); |
| | | builder.append("pageNo=1").append("&pageSize=1000"); |
| | | // 拼接日期,必传 |
| | | builder.append("&dt=").append(skxsQkTj.getQueryTime()); |
| | | String params = builder.toString(); |
| | | // 查询 |
| | | JSONObject jsonObject = getZtData(params, ZtApiUrlConstant.cap_water_detail); |
| | | // 保留 null值 |
| | | JSONObject jsonData = jsonObject.getJSONObject("data"); |
| | | JSONArray jsonArray = jsonData.getJSONArray("data"); |
| | | List<SkxsQkTj> list = new ArrayList<>(); |
| | | // 遍历,写入数据 |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | String res_cd = jsonArray.getJSONObject(i).getString("res_cd"); |
| | | // 查询库里是否已存在数据 |
| | | SkxsQkTj skxs = baseMapper.selectSkxsQkTjBy(res_cd,jsonArray.getJSONObject(i).getString("res_nm"),skxsQkTj.getQueryTime()); |
| | | if (null!=skxs){ |
| | | // 跳出本次循环 |
| | | continue; |
| | | } |
| | | SkxsQkTj qkTj = new SkxsQkTj(); |
| | | // 设置各项值 |
| | | setSkxsQkTjInfo(qkTj,jsonArray,i,skxsQkTj.getQueryTime()); |
| | | // 加入集合 |
| | | list.add(qkTj); |
| | | } |
| | | // 批量插入 |
| | | saveBatch(list); |
| | | } |
| | | |
| | | /** |
| | | * 设置水库库容统计值 |
| | | * @param skxs |
| | | * @param jsonArray |
| | | * @param i 第几个 |
| | | * @param time 统计日期 |
| | | */ |
| | | private void setSkxsQkTjInfo(SkxsQkTj skxs,JSONArray jsonArray,int i,String time) { |
| | | try { |
| | | skxs.setStatisticTime(new SimpleDateFormat("yyyy-MM-dd").parse(time)); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | skxs.setCreateUser(AuthUtil.getUserId()); |
| | | skxs.setUpdateUser(AuthUtil.getUserId()); |
| | | skxs.setCreateTime(new Date()); |
| | | skxs.setUpdateTime(new Date()); |
| | | skxs.setSysResource("zt"); |
| | | |
| | | skxs.setDeadCap(jsonArray.getJSONObject(i).getDouble("dead_cap")); |
| | | skxs.setResCode(jsonArray.getJSONObject(i).getString("res_cd")); |
| | | skxs.setResName(jsonArray.getJSONObject(i).getString("res_nm")); |
| | | skxs.setTotalCap(jsonArray.getJSONObject(i).getDouble("tot_cap")); |
| | | skxs.setRz(jsonArray.getJSONObject(i).getDouble("rz")); |
| | | skxs.setWs(jsonArray.getJSONObject(i).getDouble("now_cap")); |
| | | skxs.setBenResCap(jsonArray.getJSONObject(i).getDouble("ben_res_cap")); |
| | | skxs.setLastYearWs(jsonArray.getJSONObject(i).getDouble("last_year_cap")); |
| | | skxs.setHisWs(jsonArray.getJSONObject(i).getDouble("avg_cap")); |
| | | skxs.setUpMonthWs(jsonArray.getJSONObject(i).getDouble("last_month_cap")); |
| | | skxs.setTotalWsDif(jsonArray.getJSONObject(i).getString("diff_his")); |
| | | } |
| | | |
| | | /** |
| | | * 调用中台接口查询数据 |
| | | * |
| | | * @param params |
| | | * @param url |
| | | * @return |
| | | */ |
| | | public JSONObject getZtData(String params, String url) { |
| | | // 获取环境 |
| | | String activeProfile = SpringContextUtil.getActiveProfile(); |
| | | if (activeProfile.equals("dev")){ |
| | | url = ZtApiUrlConstant.url_prefix_dev + url; |
| | | } |
| | | if (activeProfile.equals("prod")){ |
| | | url = ZtApiUrlConstant.url_prefix_prod + url; |
| | | } |
| | | if (activeProfile.equals("test")){ |
| | | url = ZtApiUrlConstant.url_prefix_test + url; |
| | | } |
| | | //设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add(ZtConfigConstant.header_key, ZtConfigConstant.header_value); |
| | | //封装请求头 |
| | | HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers); |
| | | try { |
| | | //有请求头,有参数请求 |
| | | ResponseEntity<String> responseEntity = |
| | | restTemplate.exchange(url + params, |
| | | HttpMethod.GET, |
| | | formEntity, |
| | | String.class); |
| | | // Feature.IgnoreNotMatch 保留null值的属性 |
| | | JSONObject jsonObject = JSON.parseObject(responseEntity.getBody(),Feature.IgnoreNotMatch); |
| | | // 返回 |
| | | return jsonObject; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | } |
| | | |