package com.ruoyi.web.controller.reservation;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.reservation.bo.GatherBo;
import com.ruoyi.reservation.domain.Config;
import com.ruoyi.reservation.domain.Reservation;
import com.ruoyi.reservation.response.Result;
import com.ruoyi.reservation.service.IReservationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.List;



@RestController
@RequestMapping("/api")
@Slf4j
public class ReservationApiController {

    @Autowired
    private IReservationService reservationService;

    public static void main(String[] args) {


    }
    public static void writeJsonFile() {
        Config config = new Config("settingKey", "settingValue");
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(config);

        try (FileWriter writer = new FileWriter("config.json")) {
            writer.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void readJsonFile() {
        String filePath = System.getProperty("user.dir") + File.separator + "config.json";
        System.out.println(filePath);

        StringBuffer sb = new StringBuffer();
        try (InputStream input = new FileInputStream(filePath)) {
            byte[] buffer = new byte[1024];
            int length = 0;
            length = input.read(buffer);
            while (length != -1) {
                sb.append(new String(buffer, 0 ,length));
                length = input.read(buffer);
            }

            Config config = JSONObject.parseObject(sb.toString(), Config.class);
            System.out.println(config.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //转换成读取库，根据选择字段，配置接口文档
    @PostMapping("/reservation")
    public JSONObject reservation(@RequestBody JSONObject content) {
        log.info("**********预约接口API被调用开始**********");
        log.info("接口参数：{}",content);
        Reservation reservation = JSONObject.parseObject(content.toString(), Reservation.class);
        int save = reservationService.insertMasterReservation(reservation);

        Result result = new Result();
        if(save == 1) {
            result.setCode(200);
            result.setMessage("预约成功！");
            log.info("接口返回：{}",result.toString());
            log.info("**********预约接口API被调用结束**********");
            return JSONObject.from(result);
        }
        result.setCode(500);
        result.setMessage("预约失败！");
        log.info("接口返回：{}",result.toString());
        log.info("**********预约接口API被调用结束**********");
        return JSONObject.from(result);
    }

    @PostMapping("/passProcess")
    public JSONObject passProcess(@RequestBody String str) {
        log.info("**********车辆验放接口API被调用开始**********");
        log.info("接口参数：{}",str);
        GatherBo gatherBo = JSONObject.parseObject(str, GatherBo.class);

        Result result = new Result();
        result.setCode(500);
        result.setMessage("验放失败！");

        if(gatherBo.getGrossWt() < 1000) {
            result.setMessage("验放失败，车辆重量小于阈值1000kg！");
            log.info("接口返回：{}",result.toString());
            return JSONObject.from(result);
        }
        if (StringUtils.isEmpty(gatherBo.getVeLicenseNo())||StringUtils.equals("车牌",gatherBo.getVeLicenseNo())){
            result.setMessage("验放失败，车辆重量小于阈值1000kg！");
            log.info("接口返回：{}",result.toString());
            return JSONObject.from(result);
        }

        Reservation reservation = new Reservation();
        reservation.setCarNumber(gatherBo.getVeLicenseNo());
        //查本地主表是否有预约信息，并且返回结果给卡口系统
        List<Reservation> list = reservationService.selectMasterReservationList(reservation);
        if(list.size() == 1) {
            result.setCode(200);
            result.setMessage("验放成功，车辆放行！");
            log.info("接口返回：{}",result.toString());
            return JSONObject.from(result);
        }

        //把预约信息写进外部第三方数据库数据库
        int i = reservationService.saveSlaveReservation(reservation);
        log.info("**********车辆验放接口API被调用结束**********");
        return JSONObject.from(result);
    }
}
