package cn.iocoder.yudao.module.system.controller.admin.robot;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.robot.dto.RobotStatusDTO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotInformationDO;
import cn.iocoder.yudao.module.system.service.robot.RobotInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 车辆信息")
@RestController
@RequestMapping("/system/robot/information")
@Validated
public class RobotInformationController {

    @Resource
    private RobotInformationService informationService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆信息")
    @PreAuthorize("@ss.hasPermission('robot:information:create')")
    public CommonResult<Long> createInformation(@Valid @RequestBody RobotInformationSaveReqVO createReqVO) {
        return success(informationService.createInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆信息")
    @PreAuthorize("@ss.hasPermission('robot:information:update')")
    public CommonResult<Boolean> updateInformation(@Valid @RequestBody RobotInformationSaveReqVO updateReqVO) {
        informationService.updateInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:information:delete')")
    public CommonResult<Boolean> deleteInformation(@RequestParam("id") Long id) {
        informationService.deleteInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:information:query')")
    public CommonResult<RobotInformationRespVO> getInformation(@RequestParam("id") Long id) {
        RobotInformationRespVO information = informationService.getInformation(id);
        return success(BeanUtils.toBean(information, RobotInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆信息分页")
    @PreAuthorize("@ss.hasPermission('robot:information:query')")
    public CommonResult<PageResult<RobotInformationPageRespVO>> getInformationPage(@Valid RobotInformationPageReqVO pageReqVO) {
        PageResult<RobotInformationPageRespVO> pageResult = informationService.getInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotInformationPageRespVO.class));
    }


    @GetMapping("/getListByMapId")
    @Operation(summary = "根据地图id获取车辆列表")
    public CommonResult<List<RobotInformationRespVO>> getListByMapId(@RequestParam Long mapId) {
        List<RobotInformationDO> list = informationService.getListByMapId(mapId);
        return success(BeanUtils.toBean(list, RobotInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆信息 Excel")
    @PreAuthorize("@ss.hasPermission('robot:information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInformationExcel(@Valid RobotInformationPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotInformationPageRespVO> list = informationService.getInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆信息.xls", "数据", RobotInformationPageRespVO.class,
                BeanUtils.toBean(list, RobotInformationPageRespVO.class));
    }

    @PostMapping("/statistics")
    @Operation(summary = "统计车辆待命/任务中/离线")
    @PreAuthorize("@ss.hasPermission('robot:information:statistics')")
    public CommonResult<RobotInformationStatisticsVO> statisticsInformation() {
        return success(informationService.statisticsInformation());
    }

    @PostMapping("/getCanUseRobot")
    @Operation(summary = "查询能正常使用的车辆")
    @PreAuthorize("@ss.hasPermission('robot:information:getCanUseRobot')")
    public CommonResult<List<RobotInformationRespVO>> getCanUseRobot() {
        List<RobotInformationDO> result = informationService.getCanUseRobot();
        return success(BeanUtils.toBean(result, RobotInformationRespVO.class));
    }

    @PostMapping("/getAllRobot")
    @Operation(summary = "查询所有车辆")
    @PreAuthorize("@ss.hasPermission('robot:information:getAllRobot')")
    public CommonResult<List<RobotInformationRespVO>> getAllRobot() {
        List<RobotInformationRespVO> result = informationService.getAllRobotAndErrorMsg();
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获得车辆信息分页")
    @PreAuthorize("@ss.hasPermission('robot:information:list')")
    public CommonResult<PageResult<RobotInformationPageRespVO>> getRobotList(@Valid RobotInformationPageReqVO pageReqVO) {
        PageResult<RobotInformationPageRespVO> pageResult = informationService.getInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotInformationPageRespVO.class));
    }

    @PostMapping("/test")
    @Operation(summary = "测试")
    @PermitAll
    public CommonResult<Boolean> test(@RequestBody RobotStatusDTO dto) {
        informationService.test(dto);
        return success(true);
    }

    @PostMapping("/cleanTrafficManagement")
    @Operation(summary = "清除交管")
    @PreAuthorize("@ss.hasPermission('robot:information:cleanTrafficManagement')")
    public CommonResult<Boolean> cleanTrafficManagement(@RequestParam("robotNo") String robotNo) {
        informationService.cleanTrafficManagement(robotNo);
        return success(true);
    }

    @PostMapping("/doTaskContinue")
    @Operation(summary = "恢复任务")
    @PreAuthorize("@ss.hasPermission('robot:information:doTaskContinue')")
    public CommonResult<Boolean> doTaskContinue(@RequestParam("robotNo") String robotNo) {
        informationService.doTaskContinue(robotNo,false);
        return success(true);
    }

    @PostMapping("/getRobotByFloorAndArea")
    @Operation(summary = "获取楼层区域对应的机器人编号")
    @PreAuthorize("@ss.hasPermission('robot:information:getRobotByFloorAndArea')")
    public CommonResult<List<String>> getRobotByFloorAndArea(@RequestParam("floor") String floor, @RequestParam("area") String area) {
        return success(informationService.getRobotByFloorAndArea(floor,area));
    }


    @PostMapping("/stopRobot")
    @Operation(summary = "暂停车辆")
    @PreAuthorize("@ss.hasPermission('robot:information:stopRobot')")
    public CommonResult<Boolean> stopRobot(@RequestParam("robotNo") String robotNo) {
        informationService.stopRobot(robotNo);
        return success(true);
    }

    @PostMapping("/continueRobot")
    @Operation(summary = "恢复车辆")
    @PreAuthorize("@ss.hasPermission('robot:information:continueRobot')")
    public CommonResult<Boolean> continueRobot(@RequestParam("robotNo") String robotNo) {
        informationService.continueRobot(robotNo);
        return success(true);
    }
}
