package com.xhpc.charging.station.controller;

import com.xhpc.charging.station.service.IXhpcChargingStationService;
import com.xhpc.common.api.dto.XhpcChargingStationDto;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.domain.XhpcChargingStation;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 电站Controller
 *
 * @author yuyang
 * @date 2021-07-19
 */
@RestController
@RequestMapping("/station")
public class XhpcChargingStationController extends BaseController {

    @Autowired
    private IXhpcChargingStationService xhpcChargingStationService;


    @Log(title = "充电站状态编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/editStationStatus")
    public AjaxResult editStationStatus(@RequestBody Map<String, Object> map) {

        //空值判断
        if (map.get("chargingStationId") == null && map.get("pushStatus") == null) {
            return AjaxResult.error("chargingStationId or pushStatus or stationName is null");
        }
        Integer integer = xhpcChargingStationService.editStationsStatus((List<Integer>) map.get("chargingStationId"), (Integer) map.get("pushStatus"));
        return AjaxResult.success("充电站状态编辑成功", null);
    }

    @Log(title = "充电站状态保存", businessType = BusinessType.UPDATE)
    @PostMapping("/saveStationStatus")
    public AjaxResult saveStationStatus(@RequestBody Map<String, Object> map) {

        //空值判断
        if (map.get("chargingStationId") == null && map.get("pushStatus") == null && map.get("stationName") == null) {

            return AjaxResult.error("chargingStationId or pushStatus or stationName is null");
        }
        Boolean result = xhpcChargingStationService.findOneByStationId((Integer) map.get("chargingStationId"));
        //电站表中是否有对应的电站存在
        if (!result) {
            return AjaxResult.error("chargingStationId is not exist");
        }
        Integer integer = xhpcChargingStationService.saveStationStatus((Integer) map.get("chargingStationId"), (String) map.get("stationName"), (Integer) map.get("pushStatus"));
        //是否重复判断，保证唯一
        if (integer == 0) {
            return AjaxResult.error("chargingStationId doesn't repeat");
        }
        return AjaxResult.success("充电站状态保存成功", null);
    }

    /**
     * 查询电站列表
     */
    //@PreAuthorize(hasPermi = "system:station:list")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request,String tenantId,Long operatorId,Long chargingStationId,String name, String operatorName) {
        List<Map<String, Object>> list = xhpcChargingStationService.selectXhpcChargingStationList(request,tenantId,operatorId,chargingStationId,name, operatorName);
        return getDataTable(list);
    }

    /**
     * 导出电站列表
     */
    @PreAuthorize(hasPermi = "system:station:export")
    @Log(title = "电站", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhpcChargingStation xhpcChargingStation) throws IOException {
        //List<XhpcChargingStation> list = xhpcChargingStationService.selectXhpcChargingStationList(xhpcChargingStation);
        //ExcelUtil<XhpcChargingStation> util = new ExcelUtil<XhpcChargingStation>(XhpcChargingStation.class);
        //util.exportExcel(response, list, "电站数据");
    }

    /**
     * 获取电站详细信息
     */
    @PreAuthorize(hasPermi = "system:station:query")
    @GetMapping(value = "/{chargingStationId}")
    public AjaxResult getInfo(@PathVariable("chargingStationId") Long chargingStationId) {

        return AjaxResult.success(xhpcChargingStationService.selectXhpcChargingStationById(chargingStationId));
    }

    /**
     * 新增电站
     */
    @PreAuthorize(hasPermi = "system:station:add")
    @Log(title = "电站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XhpcChargingStation xhpcChargingStation) {

        return toAjax(xhpcChargingStationService.insertXhpcChargingStation(xhpcChargingStation));
    }

    /**
     * 修改电站
     */
    @PreAuthorize(hasPermi = "system:station:edit")
    @Log(title = "电站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XhpcChargingStation xhpcChargingStation) {

        return toAjax(xhpcChargingStationService.updateXhpcChargingStation(xhpcChargingStation));
    }

    /**
     * 删除电站
     */
    //@PreAuthorize(hasPermi = "system:station:remove")
    //@Log(title = "电站", businessType = BusinessType.DELETE)
    @Log(title = "删除电站", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chargingStationIds}")
    public AjaxResult remove(@PathVariable Long chargingStationIds) {

        return toAjax(xhpcChargingStationService.updateXhpcChargingStationById(chargingStationIds));
    }

    /**
     * 状态（启用 1停用）
     */
    //@PreAuthorize(hasPermi = "system:station:remove")
    //@Log(title = "电站", businessType = BusinessType.DELETE)
    @Log(title = "电站状态改变", businessType = BusinessType.UPDATE)
    @PostMapping("/status")
    public AjaxResult status(@RequestBody XhpcChargingStation xhpcChargingStation) {

        return toAjax(xhpcChargingStationService.status(xhpcChargingStation.getStatus(), xhpcChargingStation.getChargingStationId()));
    }

    /**
     * APP端、小程序是否可见
     *
     * @return 结果
     */
    //@PreAuthorize(hasPermi = "system:station:remove")
    //@Log(title = "电站", businessType = BusinessType.DELETE)
    @Log(title = "APP端、小程序是否可见", businessType = BusinessType.UPDATE)
    @PostMapping("/clientVisible")
    public AjaxResult clientVisible(@RequestBody XhpcChargingStation xhpcChargingStation) {

        return toAjax(xhpcChargingStationService.clientVisible(xhpcChargingStation.getType(), xhpcChargingStation.getClientVisible(), xhpcChargingStation.getChargingStationId()));
    }


    /**
     * 合作的电站
     */
    //@PreAuthorize(hasPermi = "system:station:remove")
    //@Log(title = "电站", businessType = BusinessType.DELETE)
    @GetMapping(value = "/stationInternetBlackList")
    public AjaxResult stationInternetBlackList(@RequestParam(value = "chargingStationId", required = true) Long chargingStationId) {

        return AjaxResult.success(xhpcChargingStationService.stationInternetBlackList(chargingStationId));
    }


    /**
     * 添加场站
     *
     * @param xhpcChargingStationDto
     * @return
     */
    @Log(title = "添加场站", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addXhpcChargingStation")
    public AjaxResult addXhpcChargingStation(HttpServletRequest request, @RequestBody XhpcChargingStationDto xhpcChargingStationDto) {
        return xhpcChargingStationService.addXhpcChargingStation(request, xhpcChargingStationDto);
    }

    /**
     * 场站详情接口
     *
     * @param chargingStationId
     * @param type              1编辑使用   2详情使用
     * @return
     */
    @GetMapping(value = "/getXhpcChargingStationMessage")
    public AjaxResult getXhpcChargingStationMessage(Long chargingStationId, Integer type) {

        return AjaxResult.success(xhpcChargingStationService.getXhpcChargingStationMessage(chargingStationId, type));
    }

    /**
     * 编辑电站（基本信息）
     *
     * @return
     */
    @Log(title = "编辑电站（基本信息）", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateXhpcTerminal")
    public AjaxResult updateXhpcTerminal(@RequestBody XhpcChargingStation xhpcChargingStation) {

        return xhpcChargingStationService.updateXhpcTerminal(xhpcChargingStation);
    }

    /**
     * 编辑费率
     *
     * @return
     */
    @Log(title = "编辑电站（编辑费率）", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateXhpcRateTime")
    public AjaxResult updateXhpcRateTime(HttpServletRequest request, @RequestBody XhpcChargingStationDto xhpcChargingStationDto) {

        return xhpcChargingStationService.updateXhpcRateTime(request, xhpcChargingStationDto);
    }

    /**
     * 组合场站信息（成都市平台）
     */
    @GetMapping("/getNotificationStationInfo")
    public R getNotificationStationInfo(Long stationId){
        return xhpcChargingStationService.getNotificationStationInfo(stationId);
    }

    /**
     * 定时获取场站允许充电方式
     */
    @GetMapping("/chargingMethod")
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void chargingMethod(){
        xhpcChargingStationService.getchargingMethodList();
    }


    @GetMapping("/getXhpcRateTimeOrder")
    public R getXhpcRateTimeOrder(Long rateModelId,String startTime, String endTime,String serialNumber){
        return xhpcChargingStationService.getXhpcRateTimeOrder(rateModelId,startTime,endTime,serialNumber);
    }

    @GetMapping("/getCrossDayXhpcRateTimeOrder")
    public R getCrossDayXhpcRateTimeOrder(Long rateModelId,String startTime, String endTime,String serialNumber){
        return xhpcChargingStationService.getCrossDayXhpcRateTimeOrder(rateModelId,startTime,endTime,serialNumber);
    }

}
