package org.king.modules.ad.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.king.common.api.vo.Result;
import org.king.modules.ad.entity.*;
import org.king.modules.ad.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/staticalCount")
public class StaticalController {

    @Autowired
    private StaticalService staticalService;

    @Autowired
    private IAdDeviceFaultReportService adDeviceFaultReportService;

    @Autowired
    private IAdShipFaultReportService adShipFaultReportService;

    @Autowired
    private IAdChargingRecordService adChargingRecordService;

    @Autowired
    private IBaseShipInfoService baseShipInfoService;

    @GetMapping(value = "/getDeviceCount")
    public Result<?> getDeviceCount() {
        AdDeviceCount count =staticalService.getDeviceCount();
        return Result.ok(count);
    }

    @GetMapping(value = "/getChargeData")
    public Result<?> getChargeData() {
        List<ChargeCount> list =staticalService.getChargeData();
        return Result.ok(list);
    }

    @GetMapping(value = "/getDeviceFault")
    public Result<?> getDeviceFault() {
        List<AdDeviceFaultReport> list =staticalService.getlist();
        return Result.ok(list);
    }

    @GetMapping(value = "/getShipFault")
    public Result<?> getShipFault() {
        List<AdShipFaultReport> list =adShipFaultReportService.list(Wrappers.<AdShipFaultReport>lambdaQuery().orderByDesc(AdShipFaultReport::getCreateTime));
        return Result.ok(list);
    }

    @GetMapping(value = "/getChargeRecord")
    public Result<?> getChargeRecord(){
        String json = HttpUtil.get("http://36.154.210.106:8090/ybl/czs/ship/anchorOverTwoHours");
        List<BaseShipInfo> shipInfos = new ArrayList<BaseShipInfo>();
//        String json = readJsonFile("d://ad.json");
        JSONObject jsonObject = JSONObject.parseObject(json);
        if(jsonObject.get("code").toString().equals("200")){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                List<String> list = new ArrayList<String>();
                List<AdChargingRecord> chargelist = adChargingRecordService.list(Wrappers.<AdChargingRecord>lambdaQuery().apply("date_format(create_time,'%Y-%m-%d') = '"+df.format(new Date())+"'"));
                List<AdShipFaultReport> shiplist =adShipFaultReportService.list(Wrappers.<AdShipFaultReport>lambdaQuery().apply("date_format(create_time,'%Y-%m-%d') = '"+df.format(new Date())+"'"));
                for(AdChargingRecord obj:chargelist){
                    list.add(obj.getShipId());
                }
                for(AdShipFaultReport obj:shiplist){
                    list.add(obj.getShipId());
                }
                JSONArray result= JSONArray.parseArray(jsonObject.get("result").toString());
                 String[] ships = new String[result.size()];
                for(int i=0;i<result.size();i++) {
//                    System.out.println(result.getJSONObject(i).get("id"));
                    ships[i] = result.getJSONObject(i).get("id").toString();
                }
                List<BaseShipInfo> yujinList = baseShipInfoService.getListBymmsi(ships);
                for(BaseShipInfo baseShipInfo:yujinList ){
                    if(baseShipInfo.getShipId() != null){
                        int i = 0;
                        for(String shipid:list ){
                            if(baseShipInfo.getShipId().equals(shipid)){
                                i = 1;
                            }
                        }
                        if(i == 0){
                            shipInfos.add(baseShipInfo);
                        }
                    }
//                    shipInfos.add(baseShipInfo);
                }
                return Result.ok(shipInfos);
        }else{
            return Result.error("预警接口访问失败，错误码"+jsonObject.get("code").toString());
        }
    }

    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
