package com.xhpc.charging.station.controller;

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.charging.station.service.IXhpcTerminalService;
import com.xhpc.common.domain.XhpcTerminal;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/7/27 9:29
 */
@RestController
@RequestMapping("/terminal")
public class XhpcTerminalController extends BaseController {

    @Autowired
    private IXhpcTerminalService xhpcTerminalService;


    /**
     * 终端列表
     *
     * @param serialNumber      终端编号
     * @param type              桩类型
     * @param status            0启用 1停用
     * @param workStatus        0离线 1故障 2空闲 3充电
     * @param chargingStationId
     * @return
     */
    //@PreAuthorize(hasPermi = "system:station:list")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, String serialNumber, Integer type, Integer status, String workStatus, Long chargingStationId) {
        List<Map<String, Object>> list = xhpcTerminalService.getXhpcTerminalList(request,serialNumber, type, status, workStatus, chargingStationId);
        return getDataTable(list);
    }


    /**
     * 停用
     *
     * @return
     */
    @Log(title = "终端-停用", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateStatus")
    public AjaxResult updateStatus(@RequestBody XhpcTerminal xhpcTerminal) {
        if(null==xhpcTerminal.getTerminalId()){
            return AjaxResult.error("桩数据为空");
        }
        if(null==xhpcTerminal.getStatus()){
            return AjaxResult.error("桩数据状态为空");
        }
        if(xhpcTerminal.getStatus()==1){
            if(null==xhpcTerminal.getPrompt() || xhpcTerminal.getPrompt().length()>50){
                return AjaxResult.error("桩提示语为空且50字之内");
            }
        }
        return xhpcTerminalService.updateStatus(xhpcTerminal.getTerminalId(), xhpcTerminal.getStatus(),xhpcTerminal.getPrompt());
    }

    /**
     * 查询
     */
    @GetMapping(value = "/selectXhpcTerminalMessage")
    public AjaxResult selectXhpcTerminalMessage(Long terminalId) {

        return xhpcTerminalService.selectXhpcTerminalMessage(terminalId);
    }

    /**
     * 修改
     *
     * @return
     */
    @Log(title = "终端-修改", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateXhpcTerminalName")
    public AjaxResult updateXhpcTerminalName(@RequestBody XhpcTerminal xhpcTerminal) {

        return xhpcTerminalService.updateXhpcTerminalName(xhpcTerminal.getTerminalId(), xhpcTerminal.getName(), xhpcTerminal.getSerialNumber(),xhpcTerminal.getConnectorType());
    }

    /**
     * 终端详情
     */
    @GetMapping(value = "/selectXhpcTerminalPileMessage")
    public AjaxResult selectXhpcTerminalPileMessage(Long terminalId) {

        return xhpcTerminalService.selectXhpcTerminalPileMessage(terminalId);
    }

    /**
     * 查询桩名称、场站名称、终端编号
     * @param terminalSerialNumber    搜索参数：终端
     * @param pileName                  搜索参数：桩名
     * @param stationName               搜索参数：站名
     * @return list
     */
    @GetMapping(value = "/selectPileNameAndStationNameAndTerminalNumList")
    public TableDataInfo selectPileNameAndStationNameAndTerminalNumList(String terminalSerialNumber,String pileName,String stationName) {
        startPage();
        List<Map<String,Object>> list = xhpcTerminalService.getXhpcPileNameAndStationNameAndTerminalNumList(terminalSerialNumber,pileName,stationName);
        return getDataTable(list);
    }

//    @GetMapping("/list")
//    public TableDataInfo list(String serialNumber, Integer type, Integer status, String workStatus, Long chargingStationId) {
//
//        startPage();
//        List<Map<String, Object>> list = xhpcTerminalService.getXhpcTerminalList(serialNumber, type, status, workStatus, chargingStationId);
//        return getDataTable(list);
//    }


    /**
     * 停用
     *
     * @return
     */
    @Log(title = "终端-停用", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateStatusAll")
    public AjaxResult updateStatusAll(@RequestBody XhpcTerminal xhpcTerminal) {
        if(null==xhpcTerminal.getChargingStationId()){
            return AjaxResult.error("场站数据为空");
        }
        if(null==xhpcTerminal.getStatus()){
            return AjaxResult.error("桩数据状态为空");
        }
        if(xhpcTerminal.getStatus()==1){
            if(null==xhpcTerminal.getPrompt() || xhpcTerminal.getPrompt().length()>50){
                return AjaxResult.error("桩提示语为空且50字之内");
            }
            //停该场站所有桩
        }
        return xhpcTerminalService.updateStatusAll(xhpcTerminal.getChargingStationId(), xhpcTerminal.getStatus(),xhpcTerminal.getPrompt());
    }



}
