package org.jeecg.modules.dataVue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.dataVue.entity.Mock;
import org.jeecg.modules.dataVue.mapper.MockMapper;
import org.jeecg.modules.dataVue.service.ILtdSourceService;
import jtznyj.engineer.vo.LtdRecord;
import org.jeecg.modules.warning.entity.HttpResultItem;
import org.jeecg.modules.warning.entity.Ltd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@DS("chartDataSource")
public class LtdSourceServiceImpl extends ServiceImpl<MockMapper, Mock> implements ILtdSourceService {

    @Resource
    private MockMapper mockMapper;
    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Override
    public JdbcTemplate getJdbc() {
        return jdbcTemplate;
    }

    @Override
    public List<Map<String, Object>> querySql(String sqlStr, String reserve) {
        return mockMapper.querySqlA(sqlStr);
    }

    @Override
    public List<LtdRecord> queryLtdRecordList(String sqlStr, String reserve) {
        return mockMapper.querySqlLTD(sqlStr);
    }

    @Cacheable(cacheNames = "ltdforjh", key = "#reserve")
    @Override
    public List<Map<String, Object>> querySqlCache(String sqlStr, String reserve) {
        return mockMapper.querySqlA(sqlStr);
    }

    @Override
    public int querySqlCount(String sqlStr, String reserve) {
        return mockMapper.queryCountA(sqlStr);
    }

    @Override
    @Cacheable(cacheNames = "ltd2", unless = "#result.size() == 0 or !#map.containsKey('timeEnd')")
    public List<Ltd> selectList(Map<String, Object> map) {
        return mockMapper.selectList(map);
    }

    @Override
    public List<Ltd> selectYData(Map<String, Object> map) {
        return mockMapper.selectYData(map);
    }

    @Override
    public Ltd selectDate(Map<String, Object> map) {
        return mockMapper.selectDate(map);
    }

    @Override
    public List<Map<String, Object>> selectAllDate(Map<String, Object> map) {
        return mockMapper.selectAllDate(map);
    }

    /**
     * @param jh
     * @param type h为历史数据
     * @return
     */
    @Override
    public List<Ltd> selectJwssList(String jh, String type) {
        String ltdParam = jh.replace('-', '_');
        String url = "http://10.68.224.20:9093/v1/structuredData/serviceData/jwss1h?app_key=meta";
        if (type.equals("h")) {
            url = "http://10.68.224.20:9093/v1/structuredData/serviceData/jwss?app_key=meta";
        }

        String jsonStr = String.format("{\r\n  \"jh\": {\r\n        \"condition\": [\r\n            {\r\n                \"operational\": \"in\",\r\n                \"value\": \"%s\",\r\n                \"relation\": \"and\"\r\n            }\r\n        ]\r\n    }\r\n}", ltdParam);
        String result2 = HttpRequest.post(url)
                .body(jsonStr)
                .execute().body();
        JSONObject json = JSONUtil.parseObj(result2);
        try {
            String statusCode = json.getStr("statusCode");
            Object item = json.get("data");
            HttpResultItem res = BeanUtil.toBean(item, HttpResultItem.class);
            List<Ltd> list = res.getRows();
            if (list.size() > 0) {
                for (Ltd item2 : list) {
                    item2.setTime(item2.getTime().substring(0, 19));
                }
            }
            return list;
        } catch (Exception ex) {
            return null;
        }

    }
}
