| | |
| | | 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.entity.XsfsQktj; |
| | | import cn.gistack.sm.sjztods.excel.XsfsQktjExcel; |
| | | import cn.gistack.sm.sjztods.mapper.XsfsQktjMapper; |
| | | import cn.gistack.sm.sjztods.service.IXsfsQktjService; |
| | | import cn.gistack.sm.sjztods.vo.XsfsQktjVO; |
| | | 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.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | @Service |
| | | @DS("ztznwh") |
| | | public class XsfsQktjServiceImpl extends ServiceImpl<XsfsQktjMapper, XsfsQktj> implements IXsfsQktjService { |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | /** |
| | | * 上报 |
| | |
| | | public List<XsfsQktjExcel> exportXsfsQktjList(XsfsQktjVO xsfsQktj) { |
| | | return baseMapper.exportXsfsQktjList(xsfsQktj); |
| | | } |
| | | |
| | | /** |
| | | * 中台接口数据调用,数据入库 |
| | | * @param xsfsQktj |
| | | */ |
| | | @Override |
| | | public void saveOrUpdateData(XsfsQktjVO xsfsQktj) { |
| | | // 参数拼接 |
| | | StringBuilder builder = new StringBuilder("?"); |
| | | // 拼接日期,必传 |
| | | builder.append("?dt=").append(xsfsQktj.getQueryTime()); |
| | | if (null!=xsfsQktj.getType() && !xsfsQktj.getType().equals("")) { |
| | | builder.append("&").append(xsfsQktj.getType()).append("=").append(xsfsQktj.getTypeValue()); |
| | | } |
| | | String params = builder.toString(); |
| | | // 查询 |
| | | JSONObject jsonObject = getZtData(params, ZtApiUrlConstant.cap_tot_status); |
| | | // 解析数据 |
| | | JSONArray jsonArray = jsonObject.getJSONArray("data"); |
| | | List<XsfsQktj> list = new ArrayList<>(); |
| | | // 遍历,写入数据 |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | String ad_nm = ""; |
| | | if (xsfsQktj.getType().equals("city_cd")){ |
| | | ad_nm = jsonArray.getJSONObject(i).getString("county_nm"); |
| | | }else if (xsfsQktj.getType().equals("county_cd")){ |
| | | ad_nm = jsonArray.getJSONObject(i).getString("town_nm"); |
| | | }else { |
| | | ad_nm = jsonArray.getJSONObject(i).getString("city_nm"); |
| | | } |
| | | // 查询库里是否已存在数据 |
| | | XsfsQktj xsfsQktjBy = baseMapper.selectXsfsQktjBy(ad_nm,xsfsQktj.getQueryTime()); |
| | | if (null!=xsfsQktjBy){ |
| | | // 跳出本次循环 |
| | | continue; |
| | | } |
| | | XsfsQktj qkTj = new XsfsQktj(); |
| | | qkTj.setAdName(ad_nm); |
| | | // 设置各项值 |
| | | setXsfsQktjInfo(qkTj,jsonArray,i,xsfsQktj.getQueryTime()); |
| | | // 加入集合 |
| | | list.add(qkTj); |
| | | } |
| | | // 批量插入 |
| | | saveBatch(list); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 设置水库库容统计值 |
| | | * @param skxs |
| | | * @param jsonArray |
| | | * @param i 第几个 |
| | | * @param time 统计日期 |
| | | */ |
| | | private void setXsfsQktjInfo(XsfsQktj 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.setZxsTotal(jsonArray.getJSONObject(i).getDouble("tot_cap")); |
| | | skxs.setZxsSc(jsonArray.getJSONObject(i).getString("diff_last_day")); |
| | | skxs.setZxsLastYearTq(jsonArray.getJSONObject(i).getString("rate_last_year_cap")); |
| | | skxs.setZxsLastYearTqzj(jsonArray.getJSONObject(i).getString("diff_last_year")); |
| | | skxs.setZxsHisTq(jsonArray.getJSONObject(i).getString("diff_his_cap")); |
| | | skxs.setZxsHisTqzj(jsonArray.getJSONObject(i).getString("rate_avg_cap")); |
| | | // 大型 |
| | | skxs.setDxskXsTotal(jsonArray.getJSONObject(i).getDouble("big_tot_cap")); |
| | | skxs.setDxskXsHis(jsonArray.getJSONObject(i).getString("big_diff_his")); |
| | | skxs.setDxskXsLastYear(jsonArray.getJSONObject(i).getString("big_diff_last_year")); |
| | | // 中型 |
| | | skxs.setZxskXsTotal(jsonArray.getJSONObject(i).getDouble("mid_tot_cap")); |
| | | skxs.setZxskXsHis(jsonArray.getJSONObject(i).getString("mid_diff_his")); |
| | | skxs.setZxskXsLastYear(jsonArray.getJSONObject(i).getString("mid_diff_last_year")); |
| | | // 小型 |
| | | skxs.setXxskXsTotal(jsonArray.getJSONObject(i).getDouble("small_tot_cap")); |
| | | } |
| | | |
| | | /** |
| | | * 调用中台接口查询数据 |
| | | * |
| | | * @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; |
| | | } |
| | | } |
| | | |