package org.jeecg.modules.warning.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.common.util.DateUtils;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.warning.entity.HttpResultItem;
import org.jeecg.modules.warning.entity.Ltd;
import org.jeecg.modules.warning.entity.WellNewTable;
import org.jeecg.modules.warning.mapper.LtdMapper;
import org.jeecg.modules.warning.service.ILtdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
//@DS("chartDataSource")
@DS("yjDataSource")
public class LtdServiceImpl extends ServiceImpl<LtdMapper, Ltd> implements ILtdService{
    @Autowired
    private LtdMapper ltdMapper;
    @Override
    @Cacheable(cacheNames="ltd2",unless = "#result.size() == 0 or !#map.containsKey('timeEnd')")
    public List<Ltd> selectList(Map<String, Object> map) {
        List<Ltd> result = ltdMapper.selectList(map);
        return result;
    }

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

    @Override
    public List<Ltd> selectNewOne(String table) {
        return ltdMapper.selectNewOne(table);
    }

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

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

    @Override
    public List<String> selectNewTable(){
        return ltdMapper.selectNewTable();
    }

    /**
     *
     * @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;
        }

    }
}
