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.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.Digests;
import com.huimv.admin.common.utils.HttpClientSSLUtils;
import com.huimv.admin.entity.*;
import com.huimv.admin.entity.zengxindto.*;
import com.huimv.admin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.util.*;

@Configuration
@EnableScheduling
public class ShenChanTimer {

    //生产数据
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IProdStockService prodStockService;


    @Scheduled(cron = "0 0 */4 * * ? ")
//    @Scheduled(cron = "0 * * * * ?")
    @GetMapping("/test")
    private void getShenChan() throws Exception {
        Integer farmId = 21;
        List<Integer> farmIds = new ArrayList();
        farmIds.add(21);
        farmIds.add(23);
//        farmIds.add(24);
        farmIds.add(26);
        farmIds.add(27);
        String time = LocalDate.now().toString();
        String token = login();
//        System.out.println("token"+token);
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        System.out.println(token);

        String url = "http://test.htpig.cn/rest/db/storedproc";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("db_name","MSSQL");
        map.put("proc_name","rep_yz_kc_hz");
        map.put("method","open_proc");
        HashMap<String, Object> maps = new HashMap<>();
        List list = new ArrayList();
        maps.put("name","@fdate");
        maps.put("value",time);
        list.add(maps);

        map.put("params",list);
        String params = JSON.toJSONString(map);
        System.out.println(" ====== getStock请求参数：【" + params + "】");
        String data = HttpClientSSLUtils.doPost(url+"?token="+token+"&data=", params);
        ProdStockDto prodStockDto = JSONUtil.toBean(data, ProdStockDto.class);
        List<List<Object>> stockData = prodStockDto.getData();
        System.out.println(prodStockDto.toString());
        for (Integer id : farmIds) {
            ProdStock prodStock = prodStockService.getOne(new QueryWrapper<ProdStock>().ge("creat_time",dateTime).eq("farm_id", id).orderByDesc("creat_time").last("limit 1"));

            for (List stock : stockData) {
                if ("正康".equals(stock.get(0))){
                    if (ObjectUtil.isNotEmpty(prodStock)){
                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(21);
                        prodStock.setDisplayType(0);
                        prodStockService.updateById(prodStock);
                    }else {
                        prodStock = new ProdStock();
                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setMzcl(100);
                        prodStock.setGzcl(200);
                        prodStock.setBrzcl(452);
                        prodStock.setHbzcl(100);
                        prodStock.setRszcl(30);
                        prodStock.setBrmzcl(20);
                        prodStock.setKhzcl(212);
                        prodStock.setSpzcl(1020);
                        prodStock.setCreatTime(new Date());
                        prodStock.setDisplayType(0);
                        prodStock.setFarmId(21);
                        prodStockService.save(prodStock);
                    }

                }

              /*  if ("东阳".equals(stock.get(0))){
                    if (ObjectUtil.isNotEmpty(prodStock)){
//                    prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(5433);
//                    prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(6760);
//                    prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(4361);
//                    prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(16554);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(24);
                        prodStockService.updateById(prodStock);
                        System.out.println("东阳--》");
                    }else {
                        prodStock = new ProdStock();
                        //                    prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(5433);
//                    prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(6760);
//                    prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(4361);
//                    prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(16554);
                        prodStock.setMzcl(100);
                        prodStock.setGzcl(200);
                        prodStock.setBrzcl(452);
                        prodStock.setHbzcl(100);
                        prodStock.setRszcl(30);
                        prodStock.setBrmzcl(20);
                        prodStock.setKhzcl(212);
                        prodStock.setSpzcl(1020);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(24);
                        prodStockService.save(prodStock);
                        System.out.println("东阳");
                    }
                }*/
                if ("浦江".equals(stock.get(0))){
                    if (ObjectUtil.isNotEmpty(prodStock)){
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(23);
                        prodStock.setDisplayType(0);
                        prodStockService.updateById(prodStock);
                        System.out.println("浦江--》");
                    }else {
                        prodStock = new ProdStock();
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setMzcl(100);
                        prodStock.setGzcl(200);
                        prodStock.setBrzcl(452);
                        prodStock.setHbzcl(100);
                        prodStock.setRszcl(30);
                        prodStock.setBrmzcl(20);
                        prodStock.setKhzcl(212);
                        prodStock.setSpzcl(1020);
                        prodStock.setCreatTime(new Date());
                        prodStock.setDisplayType(0);
                        prodStock.setFarmId(23);
                        prodStockService.save(prodStock);
                        System.out.println("浦江--》");
                    }

                }

                if ("横路".equals(stock.get(0))){
                    if (ObjectUtil.isNotEmpty(prodStock)){
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(26);
                        prodStock.setDisplayType(0);
                        prodStockService.updateById(prodStock);
                        System.out.println("横路--》");
                    }else {
                        prodStock = new ProdStock();
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setMzcl(100);
                        prodStock.setGzcl(200);
                        prodStock.setBrzcl(452);
                        prodStock.setHbzcl(100);
                        prodStock.setRszcl(30);
                        prodStock.setBrmzcl(20);
                        prodStock.setKhzcl(212);
                        prodStock.setSpzcl(1020);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(26);
                        prodStock.setDisplayType(0);
                        prodStockService.save(prodStock);
                        System.out.println("横路--》");
                    }

                }
                if ("天台".equals(stock.get(0))){
                    if (ObjectUtil.isNotEmpty(prodStock)){
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setCreatTime(new Date());
                        prodStock.setDisplayType(0);
                        prodStock.setFarmId(27);
                        prodStockService.updateById(prodStock);
                        System.out.println("天台--》");
                    }else {
                        prodStock = new ProdStock();
//                        prodStock.setZzcl(Integer.parseInt(stock.get(11).toString()));
                        prodStock.setZzcl(9980);
//                        prodStock.setByzcl(Integer.parseInt(stock.get(13).toString()));
                        prodStock.setByzcl(16384);
//                        prodStock.setYfzcl(Integer.parseInt(stock.get(14).toString()));
                        prodStock.setYfzcl(32999);
//                        prodStock.setZcl(Integer.parseInt(stock.get(15).toString()));
                        prodStock.setZcl(66381);
                        prodStock.setMzcl(100);
                        prodStock.setGzcl(200);
                        prodStock.setBrzcl(452);
                        prodStock.setHbzcl(100);
                        prodStock.setRszcl(30);
                        prodStock.setBrmzcl(20);
                        prodStock.setKhzcl(212);
                        prodStock.setSpzcl(1020);
                        prodStock.setCreatTime(new Date());
                        prodStock.setFarmId(27);
                        prodStock.setDisplayType(0);
                        prodStockService.save(prodStock);
                        System.out.println("天台--》");
                    }

                }
            }
        }
    }


    public static void main(String[] args) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        StockLoginDto stockLoginDto = restTemplate.getForObject("http://test.htpig.cn/rest/user/login?user_name=test&password=e10adc3949ba59abbe56e057f20f883e&db_name=MSSQL", StockLoginDto.class);
       String   token  = stockLoginDto.getToken();
        String time = LocalDate.now().toString();
        System.out.println(token);

        String url = "http://test.htpig.cn/rest/db/storedproc";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("db_name","MSSQL");
        map.put("proc_name","rep_yz_kc_hz");
        map.put("method","open_proc");
        HashMap<String, Object> maps = new HashMap<>();
        List list = new ArrayList();
        maps.put("name","@fdate");
        maps.put("value",time);
        list.add(maps);

        map.put("params",list);
        String params = JSON.toJSONString(map);
        System.out.println(" ====== getStock请求参数：【" + params + "】");
        String data = HttpClientSSLUtils.doPost(url+"?token="+token+"&data=", params);
        ProdStockDto prodStockDto = JSONUtil.toBean(data, ProdStockDto.class);
        System.out.println(prodStockDto.toString());
    }
    private String login() throws Exception {
        StockLoginDto stockLoginDto = restTemplate.getForObject("http://test.htpig.cn/rest/user/login?user_name=test&password=e10adc3949ba59abbe56e057f20f883e&db_name=MSSQL", StockLoginDto.class);
        if (stockLoginDto.getStatus() != 0){
            return null;
        }
        return stockLoginDto.getToken();
    }

}
