//package com.huimv.admin.timer;
//
//
//import cn.hutool.core.codec.Base64;
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.http.HttpRequest;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONUtil;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.huimv.admin.common.utils.HttpClientSSLUtils;
//import com.huimv.admin.common.utils.NumberUtils;
//import com.huimv.admin.common.utils.Result;
//import com.huimv.admin.entity.*;
//import com.huimv.admin.entity.zengxindto.*;
//import com.huimv.admin.mapper.FeedAreaMapper;
//import com.huimv.admin.mapper.FeedPigMapper;
//import com.huimv.admin.mapper.FeedingDataMapper;
//import com.huimv.admin.mapper.FeedingEnvMapper;
//import com.huimv.admin.service.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.client.RestTemplate;
//
//import java.text.DecimalFormat;
//import java.text.NumberFormat;
//import java.text.SimpleDateFormat;
//import java.time.Duration;
//import java.time.LocalDateTime;
//import java.util.*;
//import java.util.concurrent.ThreadLocalRandom;
//
//@Configuration
//@EnableScheduling
//public class FeedTimer {
//
//    @Autowired
//    private FeedAreaMapper feedAreaMapper;
//    @Autowired
//    private FeedingEnvMapper envMapper;
//    @Autowired
//    private FeedingDataMapper dataMapper;
//    @Autowired
//    private FeedPigMapper pigMapper;
//
//
//    //    @Scheduled(cron = "0 0 */1 * * ? ")
//    @Scheduled(cron = "0 0/59 * * * ? ")
//    @Transactional
//    public void getFeed() throws Exception {
//        QueryWrapper<FeedArea> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("farm_id", 26);
//        List<FeedArea> feedAreas = feedAreaMapper.selectList(queryWrapper);
////        Date now = new Date(); // 获取当前时间
////        long oneHour = 1 * 60 * 60 * 1000; // 1小时的毫秒数
////        Date oneHourAgo = new Date(now.getTime() - oneHour); // 获取1小时前的时间
////        List<String> time = new ArrayList<>();
////        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        time.add(sdf.format(oneHourAgo));
////        time.add(sdf.format(new Date()));
////        System.out.println("这个是传入的时间数组：" + time);
//        for (FeedArea feedArea : feedAreas) {
//            HashMap<String, String> paramMap = new HashMap<>();
//            JSONObject jsonObject = new JSONObject();
////            jsonObject.put("pageNum", "1");
////            jsonObject.put("pageSize", "50");
////            jsonObject.put("createTime", time);
//            jsonObject.put("fdQrcode", "");
//            jsonObject.put("fenceNum", "");
//            jsonObject.put("gatewayNo", feedArea.getGatewayNo());
//            jsonObject.put("areaId", feedArea.getAreaId());
//            paramMap.put("Content-Type", "application/json;charset=utf-8");
//            paramMap.put("AuthorizationF", getToken());
//            String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/mqtt/pageQryRealtime?pageNum=1&pageSize=50").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
//            JSONObject jsonObject1 = JSONObject.parseObject(post);
//            Map map = (Map) jsonObject1.get("data");
//            List<Map<String, Object>> list = (List) map.get("records");
//            System.out.println("取到了记录：" + list);
//            if (null != list) {
//                for (Map<String, Object> stringMap : list) {
//                    FeedingData data = new FeedingData();
//                    data.setFarmId(26);
//                    data.setUnitName(feedArea.getName());
//                    data.setDeviceCode(stringMap.get("fdQrcode").toString());
//
//                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                    data.setUploadTime(dateFormat.parse(stringMap.get("createTime").toString()));
//                    data.setBlanking(stringMap.get("zkAlram").toString());
//                    data.setDeviceType(feedArea.getType());
//
//                    QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("device_code", stringMap.get("fdQrcode"));
//                    FeedingEnv env = envMapper.selectOne(queryWrapper1);
//                    if (ObjectUtil.isNotEmpty(env)) {
//                        data.setEartag(env.getEartag());
//                    }
//                    data.setMeals(Integer.parseInt(stringMap.get("dayDeliver").toString()));
//                    Integer intFdWeight = Integer.parseInt(stringMap.get("intFdWeight").toString());
//                    Double feed = Double.valueOf(intFdWeight) / 100;
//                    DecimalFormat def = new DecimalFormat("0.00");
//                    data.setFoodIntake(def.format(feed));
//                    data.setWater(stringMap.get("intDwWeight").toString());
//                    data.setDayNum(stringMap.get("dayOld").toString());
//                    data.setBodySize(stringMap.get("backfat").toString());
//                    dataMapper.insert(data);
//                }
//            }
//
//        }
//    }
//
//    public String getToken() {
//        HashMap<String, String> paramMap = new HashMap<>();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("username", "ruanzong");
//        jsonObject.put("password", "123456");
//        paramMap.put("Content-Type", "application/json;charset=utf-8");
//        String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/admin/appLogin").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
//        JSONObject jsonObject1 = JSONObject.parseObject(post);
//        Map map = (Map) jsonObject1.get("data");
//        String s = map.get("tokenHead") + " " + map.get("token");
//        return s;
//    }
//
//    public static void main(String[] args) {
////        Date now = new Date(); // 获取当前时间
////        long oneHour = 1 * 30 * 1000; // 1小时的毫秒数
////        Date oneHourAgo = new Date(now.getTime() - oneHour); // 获取1小时前的时间
////        List<String> time = new ArrayList<>();
////        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        time.add(sdf.format(oneHourAgo));
////        time.add(sdf.format(new Date()));
////        HashMap<String, String> paramMap = new HashMap<>();
////        JSONObject jsonObject = new JSONObject();
////        jsonObject.put("pageNum", 1);
////        jsonObject.put("pageSize", 20);
////        jsonObject.put("createTime", time);
////        jsonObject.put("areaId","1017517");
////        paramMap.put("Content-Type", "application/json;charset=utf-8");
////        paramMap.put("AuthorizationF","Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJoZW5sdSIsImNyZWF0ZWQiOjE2OTUzNjg1OTE4MjEsImV4cCI6MTY5NTQ1NDk5MX0.63A5FNJD714MVLp2cdH9HLyN25lYtAzsXHysXLEp1eR-FfkQZel6FnuoLfkIoFbgaht1OsEN-2ERabw0Lby7Pg");
////        String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/mqtt/pageQryRealtime").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
////        JSONObject jsonObject1 = JSONObject.parseObject(post);
////        Map map = (Map) jsonObject1.get("data");
////        System.out.println(map);
////        List<Map<String, String>> list = (List) map.get("records");
////        System.out.println(list);
////        List<String> jsonArray = (ArrayList) map.get("records");
////        System.out.println(jsonArray);
//    }
//
//
//    //饲养器添加
//    @Scheduled(cron = "0 0/57 * * * ? ")
//    @Transactional
//    public void getFeedEnv() throws Exception {
//        QueryWrapper<FeedArea> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("farm_id", 26);
//        List<FeedArea> feedAreas = feedAreaMapper.selectList(queryWrapper);
//        for (FeedArea feedArea : feedAreas) {
//            HashMap<String, String> paramMap = new HashMap<>();
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("pageNum", "1");
//            jsonObject.put("pageSize", "200");
//            jsonObject.put("areaId", feedArea.getAreaId());
//            paramMap.put("Content-Type", "application/json;charset=utf-8");
//            paramMap.put("AuthorizationF", getToken());
//            String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/api/v1/feeder/pageQry?pageNum=1&pageSize=50").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
//            JSONObject jsonObject1 = JSONObject.parseObject(post);
//            Map map = (Map) jsonObject1.get("data");
//            List<Map<String, Object>> list = (List) map.get("records");
//            for (Map<String, Object> stringMap : list) {
//                QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
//                queryWrapper1.eq("farm_id", 26).eq("device_code", stringMap.get("qrcode"));
//                if (ObjectUtil.isEmpty(envMapper.selectOne(queryWrapper1))) {
//                    FeedingEnv feedingEnv = new FeedingEnv();
//                    feedingEnv.setFarmId(26);
//                    feedingEnv.setUnitName(feedArea.getName());
//                    feedingEnv.setUnitId(Integer.parseInt(feedArea.getUnitId()));
//                    feedingEnv.setDeviceType(feedArea.getType());
//                    feedingEnv.setDeviceCode(stringMap.get("qrcode").toString());
//                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                    feedingEnv.setLastTime(dateFormat.parse((String) stringMap.get("createTime")));
//                    feedingEnv.setOnlineStatus((Integer) stringMap.get("feedStatus"));
//                    envMapper.insert(feedingEnv);
//                } else {
//                    if (StringUtils.isNotBlank(stringMap.get("earTag").toString())) {
//                        FeedingEnv feedingEnv = envMapper.selectOne(queryWrapper1);
//                        feedingEnv.setEartag(stringMap.get("earTag").toString());
//                        envMapper.updateById(feedingEnv);
//                    }
//                }
//
//            }
//        }
//    }
//
//
//    //猪群数量变动
//    @Scheduled(cron = "0 0 1 * * ? ")
//    @Transactional
//    public void getPigCount() throws Exception {
//        List<FeedingEnv> envs = envMapper.selectList(null);
//        Integer number = 0;
//        String type = "";
//        for (FeedingEnv env : envs) {
//            String url = "http://znsw.zxkj2006.com/feed/api/api/v1/pig/list/" + env.getEartag();
//            Map<String, String> heads = new HashMap<>();
//            heads.put("Authorizationf", getToken());
//            String body = HttpRequest.get(url).addHeaders(heads).execute().body();
//            JSONObject jsonObject1 = JSONObject.parseObject(body);
//            System.out.println("这个是转换为json的" + jsonObject1);
//            List<JSONObject> data = (List<JSONObject>) jsonObject1.get("data");
//            System.out.println(data);
//            if (data.size() > 0) {
//                JSONObject jsonObject = data.get(0);
//                String num = jsonObject.get("litterNum").toString();
//                number = number + Integer.parseInt(num);
//                type = jsonObject.get("pigType").toString();//类型
//            }
//        }
//        List<FeedPig> feedPigs = pigMapper.selectList(null);
//        for (FeedPig feedPig : feedPigs) {
//            if (type.equals(feedPig.getType())) {
//                feedPig.setNumber(number.toString());
//                pigMapper.updateById(feedPig);
//            }
////            else {
////                FeedPig pig = new FeedPig();
////                pig.setType(type);
////                pig.setNumber(number.toString());
////                pigMapper.insert(pig);
////            }
//        }
//
//    }
//
//
//    //饲养器状态修改
//    @Scheduled(cron = "0 0/5 * * * ? ")
//    @Transactional
//    public void getFeedEnvStatus() throws Exception {
//        QueryWrapper<FeedArea> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("farm_id", 26);
//        List<FeedArea> feedAreas = feedAreaMapper.selectList(queryWrapper);
//        for (FeedArea feedArea : feedAreas) {
//            HashMap<String, String> paramMap = new HashMap<>();
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("pageNum", "1");
//            jsonObject.put("pageSize", "20");
//            jsonObject.put("areaId", feedArea.getAreaId());
//            paramMap.put("Content-Type", "application/json;charset=utf-8");
//            paramMap.put("AuthorizationF", getToken());
//            String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/api/v1/feeder/pageQry?pageNum=1&pageSize=50").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
//            JSONObject jsonObject1 = JSONObject.parseObject(post);
//            Map map = (Map) jsonObject1.get("data");
//            List<Map<String, Object>> list = (List) map.get("records");
//            for (Map<String, Object> stringMap : list) {
//                QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
//                queryWrapper1.eq("farm_id", 26).eq("device_code", stringMap.get("qrcode"));
//                if (ObjectUtil.isEmpty(envMapper.selectOne(queryWrapper1))) {
//                    FeedingEnv feedingEnv = new FeedingEnv();
//                    feedingEnv.setFarmId(26);
//                    feedingEnv.setUnitName(feedArea.getName());
//                    feedingEnv.setUnitId(Integer.parseInt(feedArea.getUnitId()));
//                    feedingEnv.setDeviceType(feedArea.getType());
//                    feedingEnv.setDeviceCode(stringMap.get("qrcode").toString());
//                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//                    feedingEnv.setLastTime(dateFormat.parse((String) stringMap.get("createTime")));
//                    feedingEnv.setOnlineStatus((Integer) stringMap.get("feedStatus"));
//                    envMapper.insert(feedingEnv);
//                } else {
//                    if (StringUtils.isNotBlank(stringMap.get("earTag").toString())) {
//                        FeedingEnv feedingEnv = envMapper.selectOne(queryWrapper1);
//                        feedingEnv.setEartag(stringMap.get("earTag").toString());
//                        String status = stringMap.get("swAlram").toString();
//                        if ("0".equals(status)) {
//                            feedingEnv.setOnlineStatus(0);
//                        } else {
//                            feedingEnv.setOnlineStatus(1);
//                        }
//                        envMapper.updateById(feedingEnv);
//                    }
//
//                }
//
//            }
//        }
//    }
//}