package com.huimv.admin.jinghongtimer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.EnergyEnvDevice;
import com.huimv.admin.jinghongtimer.Vo.EneryDataVo;
import com.huimv.admin.jinghongtimer.Vo.EneryDataVo3;
import com.huimv.admin.jinghongtimer.Vo.TokenVo;
import com.huimv.admin.mapper.EnergyDataMapper;
import com.huimv.admin.mapper.EnergyEnvDeviceMapper;
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.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;


//@RestController
//@RequestMapping("/test")
@Configuration
@EnableScheduling
public class EnergyFeedTimer {
    @Autowired
    private EnergyEnvDeviceMapper energyEnvDeviceMapper;
    @Autowired
    private EnergyDataMapper energyDataMapper;
    @Autowired
    private RestTemplate restTemplate;


//    @GetMapping("/test3")
    @Scheduled(cron = "0 0 * * * ? ")
    public void action(){
        DateTime dateTime = DateUtil.offsetHour(new Date(),-1);
        DateTime endOfMonth = new DateTime();
        List<EnergyEnvDevice> remark = energyEnvDeviceMapper.selectList(new QueryWrapper<EnergyEnvDevice>().eq("remark", 3));
        String url = "http://api.infoexdata.cn/foreignApps/foreignerDevice/getDeviceInventoryList";
        String token = getToke();
        for (EnergyEnvDevice energyEnvDevice : remark) {

            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
            formData.add("pageNum", "1");
            formData.add("pageSize", "20");
            formData.add("uniqueNo", energyEnvDevice.getDeviceCode());
            formData.add("type", "2");
            formData.add("compensate", "0");
            formData.add("startTime", dateTime.toString("yyyy-MM-dd HH:mm:ss"));
            formData.add("endTime", endOfMonth.toString("yyyy-MM-dd HH:mm:ss"));

            // 设置请求头，指定内容类型为 form-data
            HttpHeaders headers = new HttpHeaders();
            headers.set("token",token);
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            // 创建 HttpEntity
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
            // 发送 POST 请求
            ResponseEntity<EneryDataVo> response = restTemplate.postForEntity(url, requestEntity, EneryDataVo.class);
            EneryDataVo body = response.getBody();
            System.out.println(body);
            if (ObjectUtil.isEmpty(body) || ObjectUtil.isEmpty(body.getData())){
                continue;
            }
            if (body.getData().getRecords() !=null){
                List<EneryDataVo3> records = body.getData().getRecords();
                for (EneryDataVo3 record : records) {
                    EnergyData energyData = new EnergyData();
//                    energyData.setCreateDate(DateUtil.parse(record.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
                    energyData.setCreateDate(record.getEndTime());
                    energyData.setDisplayType(0);
                    energyData.setFeedValue(record.getNum()+"");
                    energyData.setUnitId(energyEnvDevice.getUnitId());
                    energyData.setUnitName(energyEnvDevice.getUnitName());
                    energyData.setFarmId(energyEnvDevice.getFarmId());
                    energyData.setOriginalFeedValue(record.getBeforeWeight()+"");
                    energyDataMapper.insert(energyData);
                }
            }
        }

    }


    private  String getToke() {
        String url = "http://api.infoexdata.cn/foreignApps/foreignApplications/getToken?appId=farnek23dxr5yk8ymvwuhbmup&appKey=x6re4jt63kcpyt3jhyjzb4ckj";
        TokenVo forObject = restTemplate.getForObject(url, TokenVo.class);
        if (ObjectUtil.isNotEmpty(forObject) && forObject.getCode() == 200) {
            return forObject.getData();
        } else {
            return null;
        }
    }
}


    //华统料塔
//    @Scheduled(cron = "0 0 * * * ? ")
//    public void action(){
//        DateTime dateTime = DateUtil.offsetHour(new Date(),-1);
//        DateTime endOfMonth = new DateTime();
//        List<EnergyEnvDevice> remark = energyEnvDeviceMapper.selectList(new QueryWrapper<EnergyEnvDevice>().eq("remark", 3));
//        String url = "http://api.infoexdata.cn/foreignApps/foreignerDevice/getDeviceInventoryList";
//        String token = getToke();
//        for (EnergyEnvDevice energyEnvDevice : remark) {
//
//            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
//            formData.add("pageNum", "1");
//            formData.add("pageSize", "20");
//            formData.add("uniqueNo", energyEnvDevice.getDeviceCode());
//            formData.add("type", "2");
//            formData.add("compensate", "0");
//            formData.add("startTime", dateTime.toString("yyyy-MM-dd HH:mm:ss"));
//            formData.add("endTime", endOfMonth.toString("yyyy-MM-dd HH:mm:ss"));
//
//            // 设置请求头，指定内容类型为 form-data
//            HttpHeaders headers = new HttpHeaders();
//            headers.set("token",token);
//            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//            // 创建 HttpEntity
//            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
//            // 发送 POST 请求
//            ResponseEntity<EneryDataVo> response = restTemplate.postForEntity(url, requestEntity, EneryDataVo.class);
//            EneryDataVo body = response.getBody();
//            System.out.println(body);
//            if (ObjectUtil.isEmpty(body) || ObjectUtil.isEmpty(body.getData())){
//                continue;
//            }
//            if (body.getData().getRecords() !=null){
//                List<EneryDataVo3> records = body.getData().getRecords();
//                for (EneryDataVo3 record : records) {
//                    EnergyData energyData = new EnergyData();
////                    energyData.setCreateDate(DateUtil.parse(record.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
//                    energyData.setCreateDate(record.getEndTime());
//                    energyData.setDisplayType(0);
//                    energyData.setFeedValue(record.getNum()+"");
//                    energyData.setUnitId(energyEnvDevice.getUnitId());
//                    energyData.setUnitName(energyEnvDevice.getUnitName());
//                    energyData.setFarmId(energyEnvDevice.getFarmId());
//                    energyDataMapper.insert(energyData);
//                }
//            }
//        }
//
//    }
//
//
//    private  String getToke(){
//        String url = "http://api.infoexdata.cn/foreignApps/foreignApplications/getToken?appId=farnek23dxr5yk8ymvwuhbmup&appKey=x6re4jt63kcpyt3jhyjzb4ckj";
//        TokenVo forObject = restTemplate.getForObject(url, TokenVo.class);
//        if (ObjectUtil.isNotEmpty(forObject) &&forObject.getCode() ==200 ){
//            return forObject.getData();
//        }else {
//            return null;
//        }
//    }
//
//
//    //华统料塔
////    @Scheduled(cron = "0 0 * * * ? ")
////    private void action(){
////        BigDecimal feedConsume;
////        BasePigpen basePigpen = basePigpenMapper.selectOne(new QueryWrapper<BasePigpen>().lambda()
////                .eq(BasePigpen::getFType, 3)
////                .eq(BasePigpen::getFarmId, 26).last("Limit 1"));
////        if (ObjectUtil.isNotEmpty(basePigpen)){
////            EnergyData energyData = new EnergyData();
////            ProdStock prodStock = prodStockMapper.selectOne(new QueryWrapper<ProdStock>().lambda()
////                    .eq(ProdStock::getFarmId, 26)
////                    .eq(ProdStock::getDisplayType, 0).orderByDesc(ProdStock::getCreatTime).last("LIMIT 1"));
////            if (ObjectUtil.isNotEmpty(prodStock)){
////                feedConsume = BigDecimal.valueOf((800*2.5)*prodStock.getZcl()/1000/12);
////                feedConsume = feedConsume.setScale(2, RoundingMode.UP);
////                energyData.setFeedValue(feedConsume.toString());
////                energyData.setFarmId(26);
////                energyData.setCreateDate(new Date());
////                energyData.setDisplayType(1);
////                energyData.setUnitId(basePigpen.getId());
////                energyData.setUnitName(basePigpen.getBuildName());
////                energyDataMapper.insert(energyData);
////            }
////
////        }
////    }
//    //景宏料塔
//}
