/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/9/12
 */
package org.springblade.modules.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.IsValidEnum;
import org.springblade.modules.baseinfo.monitorsite.dto.MonitorSiteInfoDTO;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteRelEntity;
import org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity;
import org.springblade.modules.business.monitor.service.IWaterFlowRateHistoryService;
import org.springblade.modules.share.entity.DataShareLogEntity;
import org.springblade.utils.HttpRequestUtil;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/***
 * Date:2023/9/12
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@Slf4j
@RequestMapping("galaxy-test/test")
@AllArgsConstructor
@Api(value = "运维单位管理", tags = "运维单位管理")
public class TestController extends BladeController {

    private final IWaterFlowRateHistoryService waterFlowRateHistoryService;

    @GetMapping("/js/test")
    public R jsTest() {
        try {
            JSONObject post = new JSONObject();
            post.put("pageSize", 20);
            post.put("pageNum", 1);
            String url = "https://qysgl.mwr.cn/wr/datasync/fac/info/findPage";
            // 请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("area", BusinessConstant.DATA_SHARE_AREA);
            headers.put("key", BusinessConstant.DATA_SHARE_KEY);
            JSONObject res = HttpRequestUtil.doHttpPostRequest("计量设施数据同步", url, headers, post.toJSONString(), 30000);
            if (Func.notNull(res)) {
                log.info("接口返回 {}", res.toJSONString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("计量设施同步任务异常 {}", e.getMessage());
        }
        return R.success("ok");
    }

    @GetMapping("/js/test2")
    public R jsTest2(@RequestParam(name = "measId") String measId, @RequestParam(name = "dateStr") String dateStr) {
        try {
            JSONObject post = new JSONObject();
            post.put("pageSize", 20);
            post.put("pageNum", 1);
            post.put("measId", measId);
            post.put("dateStr", dateStr);
            String url = "https://qysgl.mwr.cn/wr/datasync/wiwrDayWr/water/findPage";
            // 请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("area", BusinessConstant.DATA_SHARE_AREA);
            headers.put("key", BusinessConstant.DATA_SHARE_KEY);
            JSONObject res = HttpRequestUtil.doHttpPostRequest("日水量数据同步", url, headers, post.toJSONString(), 30000);
            if (Func.notNull(res)) {
                log.info("接口返回 {}", res.toJSONString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("计量设施同步任务异常 {}", e.getMessage());
        }
        return R.success("ok");
    }

    @GetMapping("/js/test3")
    public R jsTest3() {
        double dayAccw = waterFlowRateHistoryService.getDayAccw("1522002011001");
        List<JSONObject> datas = new ArrayList<>();
        JSONObject data = new JSONObject();
        data.put("dayW", dayAccw);
        data.put("dayW2", 0.0);
        datas.add(data);
        JSONObject post = new JSONObject();
        post.put("data", datas);
        log.info("json post {}", post.toJSONString());
        return R.success(post.toJSONString());
    }

    @GetMapping("/map/json")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取配置项", notes = "获取配置项")
    public R<List<JSONObject>> getMapJson() {
        List<JSONObject> area = new ArrayList<>();

        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(new FileInputStream("D:\\tmp\\sh.json"), JSONObject.class);
            log.info(jsonObject.toJSONString());
            JSONArray features = jsonObject.getJSONArray("features");
            for (int j = 0; j < features.size(); j++) {
                JSONObject d = new JSONObject();
                List<JSONObject> datas = new ArrayList<>();
                JSONObject d1 = features.getJSONObject(j);
                JSONObject geometry = d1.getJSONObject("geometry");
                JSONObject pp = d1.getJSONObject("properties");
                JSONArray center = pp.getJSONArray("center");
                JSONArray coordinates = geometry.getJSONArray("coordinates");
                JSONArray locations = coordinates.getJSONArray(0).getJSONArray(0);
                for (int i = 0; i < locations.size(); i++) {
                    JSONArray l = locations.getJSONArray(i);
                    JSONObject p = new JSONObject();
                    p.put("longitude", l.getDoubleValue(0));
                    p.put("latitude", l.getDoubleValue(1));
                    datas.add(p);
                }
                log.info(coordinates.toJSONString());
                d.put("data", datas);
                d.put("center", center);
                d.put("name", pp.getString("name"));
                d.put("id", j);
                area.add(d);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.data(area);
    }
}
