package com.jsowell.lianlian;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.pile.dto.LianLianPushStationInfoDTO;
import com.jsowell.pile.dto.QueryEquipmentDTO;
import com.jsowell.pile.dto.QueryStartChargeDTO;
import com.jsowell.pile.dto.QueryStationInfoDTO;
import com.jsowell.thirdparty.domain.StationStatsInfo;
import com.jsowell.thirdparty.service.LianLianService;
import com.jsowell.thirdparty.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 对接联联平台controller
 *
 * @author JS-ZZA
 * @date 2023/4/10 14:58
 */
@Anonymous
@RestController
@RequestMapping("/LianLian")
public class LianLianController extends BaseController {

    @Autowired
    private LianLianService lianLianService;


    @PostMapping("/pushStationInfo")
    public RestApiResponse<?> pushStationInfo(@RequestBody LianLianPushStationInfoDTO dto) {
        logger.info("推送联联平台充电站信息 params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            lianLianService.pushStationInfo(dto);
            response = new RestApiResponse<>();
        }catch (BusinessException e) {
            logger.error("推送联联平台充电站信息 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e) {
            logger.error("推送联联平台充电站信息 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("推送联联平台充电站信息 result:{}", response);
        return response;
    }

    /**
     * 联联平台查询充电站信息
     * http://localhost:8080/LianLian/query_stations_info
     * @param dto
     * @return
     */
    @PostMapping("/query_stations_info")
    public RestApiResponse<?> query_stations_info(@RequestBody QueryStationInfoDTO dto) {
        logger.info("联联平台查询充电站信息 params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            LianLianPageResponse pageResponse = lianLianService.query_stations_info(dto);
            response = new RestApiResponse<>(pageResponse);
        }catch (BusinessException e) {
            logger.error("联联平台查询充电站信息 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e) {
            logger.error("联联平台查询充电站信息 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台查询充电站信息 result:{}", response);
        return response;
    }

    /**
     * 联联平台查询充电站状态信息
     * http://localhost:8080/LianLian/query_station_status
     * @param StationIDs
     * @return
     */
    @PostMapping("/query_station_status")
    public RestApiResponse<?> query_station_status(@RequestBody ArrayList<String> StationIDs) {
        logger.info("联联平台查询充电站状态信息 params:{}", StationIDs);
        RestApiResponse<?> response;
        try {
            if (CollectionUtils.isEmpty(StationIDs)) {
                return null;
            }
            LianLianPageResponse pageResponse = lianLianService.query_station_status(StationIDs);
            response = new RestApiResponse<>(pageResponse);
        }catch (BusinessException e) {
            logger.error("联联平台查询充电站状态信息 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e) {
            logger.error("联联平台查询充电站状态信息 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台查询充电站状态信息 result:{}", response);
        return response;
    }

    /**
     * 查询统计信息
     * http://localhost:8080/LianLian/query_station_stats
     *
     * @param dto
     * @return
     */
    @PostMapping("/query_station_stats")
    public RestApiResponse<?> query_station_stats(@RequestBody QueryStationInfoDTO dto) {
        logger.info("联联平台查询统计信息 params :{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response;
        try {
            StationStatsInfo stationStatsInfo = lianLianService.query_station_stats(dto);
            response = new RestApiResponse<>(stationStatsInfo);
        }catch (BusinessException e) {
            logger.error("联联平台查询统计信息 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("联联平台查询统计信息 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台查询统计信息 result :{}", response);
        return response;
    }

    /**
     * 请求设备认证
     * http://localhost:8080/LianLian/query_equip_auth
     * @param dto
     * @return
     */
    @PostMapping("/query_equip_auth")
    public RestApiResponse<?> query_equip_auth(@RequestBody QueryEquipmentDTO dto) {
        logger.info("联联平台请求设备认证 params :{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response;
        try {
            EquipmentAuthVO equipmentAuthVO = lianLianService.query_equip_auth(dto);
            response = new RestApiResponse<>(equipmentAuthVO);
        }catch (BusinessException e) {
            logger.error("联联平台请求设备认证 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("联联平台请求设备认证 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台请求设备认证 result :{}", response);
        return response;
    }

    /**
     * 请求启动充电
     * http://localhost:8080/LianLian/query_start_charge
     * @param dto
     * @return
     */
    @PostMapping("/query_start_charge")
    public RestApiResponse<?> query_start_charge(@RequestBody QueryStartChargeDTO dto) {
        logger.info("联联平台请求启动充电 params :{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response;
        try {
            QueryStartChargeVO queryStartChargeVO = lianLianService.query_start_charge(dto);
            response = new RestApiResponse<>(queryStartChargeVO);
        }catch (BusinessException e) {
          logger.error("联联平台请求启动充电 error",e);
          response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("联联平台请求启动充电 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台请求启动充电 result :{}", response);
        return response;
    }

    /**
     * 查询充电状态
     * http://localhost:8080/LianLian/query_equip_charge_status/{startChargeSeq}
     * @param startChargeSeq
     * @return
     */
    @GetMapping("/query_equip_charge_status/{startChargeSeq}")
    public RestApiResponse<?> query_equip_charge_status(@PathVariable("startChargeSeq") String startChargeSeq) {
        logger.info("联联平台查询充电状态 params :{}", startChargeSeq);
        RestApiResponse<?> response;
        try {
            QueryChargingStatusVO vo = lianLianService.query_equip_charge_status(startChargeSeq);
            response = new RestApiResponse<>(vo);
        }catch (BusinessException e) {
            logger.error("联联平台查询充电状态 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("联联平台查询充电状态 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台查询充电状态 result :{}", response);
        return response;
    }

    /**
     * 请求停止充电
     * http://localhost:8080/LianLian/query_stop_charge
     * @param dto
     * @return
     */
    @PostMapping("/query_stop_charge")
    public RestApiResponse<?> query_stop_charge(@RequestBody QueryStartChargeDTO dto) {
        logger.info("联联平台请求停止充电 params :{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response;
        try {
            QueryStopChargeVO queryStopChargeVO = lianLianService.query_stop_charge(dto);
            response = new RestApiResponse<>(queryStopChargeVO);
        }catch (BusinessException e) {
            logger.error("联联平台请求停止充电 error",e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("联联平台请求停止充电 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("联联平台请求停止充电 result :{}", response);
        return response;
    }


}
