package com.fuyuanshen.web.controller.device;

import com.fuyuanshen.app.domain.dto.APPReNameDTO;
import com.fuyuanshen.app.domain.dto.AppRealTimeStatusDto;
import com.fuyuanshen.app.domain.vo.APPDeviceTypeVo;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.dto.AppDeviceBo;
import com.fuyuanshen.equipment.domain.dto.InstructionRecordDto;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.vo.*;
import com.fuyuanshen.web.service.device.DeviceBizService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * web后台:设备控制中心
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/device")
public class DeviceControlCenterController extends BaseController {

    private final DeviceBizService appDeviceService;


    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    public TableDataInfo<WebDeviceVo> list(DeviceQueryCriteria bo, PageQuery pageQuery) {
        return appDeviceService.queryWebDeviceList(bo, pageQuery);
    }


    /**
     * 绑定设备
     */
    @PostMapping("/bind")
    public R<Void> bind(@RequestBody AppDeviceBo bo) {
        return toAjax(appDeviceService.bindDevice(bo));
    }


    /**
     * 解绑设备
     */
    @DeleteMapping("/unBind")
    public R<Void> unBind(Long id) {
        return toAjax(appDeviceService.unBindDevice(id));
    }


    /**
     * 查询设备类型列表
     */
    @GetMapping(value = "/typeList")
    public R<List<APPDeviceTypeVo>> getTypeList() {
        List<APPDeviceTypeVo> typeList = appDeviceService.getTypeList();
        return R.ok(typeList);
    }


    /**
     * 重命名设备
     *
     * @param reNameDTO
     * @return
     */
    @PostMapping(value = "/reName")
    public R<String> reName(@Validated @RequestBody APPReNameDTO reNameDTO) {
        appDeviceService.reName(reNameDTO);
        return R.ok("重命名成功！！！");
    }


    @GetMapping("/realTimeStatus")
    public R<Map<String, Object>> getRealTimeStatus(AppRealTimeStatusDto statusDto) {
        Map<String, Object> status = appDeviceService.getRealTimeStatus(statusDto);
        return R.ok(status);
    }

    /**
     * 根据mac查询设备信息
     */
    @GetMapping("/getDeviceInfoByDeviceMac")
    public R<AppDeviceVo> getDeviceInfo(String deviceMac) {
        return R.ok(appDeviceService.getDeviceInfo(deviceMac));
    }

    /**
     * 指令下发记录
     */
    @GetMapping("/instructionRecord")
    public TableDataInfo<InstructionRecordVo> getInstructionRecord(InstructionRecordDto dto, PageQuery pageQuery) {
        return appDeviceService.getInstructionRecord(dto, pageQuery);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(InstructionRecordDto dto, PageQuery pageQuery, HttpServletResponse response) {
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(2000);
        TableDataInfo<InstructionRecordVo> instructionRecord = appDeviceService.getInstructionRecord(dto, pageQuery);
        if (instructionRecord.getRows() == null) {
            return;
        }
        ExcelUtil.exportExcel(instructionRecord.getRows(), "设备操作日志", InstructionRecordVo.class, response);
    }


    /**
     * 历史轨迹查询
     */
    @GetMapping("/locationHistory")
    public TableDataInfo<LocationHistoryVo> getLocationHistory(InstructionRecordDto dto, PageQuery pageQuery) {
        return appDeviceService.getLocationHistory(dto, pageQuery);
    }

    /**
     * 历史轨迹导出
     */
    @GetMapping("/locationHistoryExport")
    public void locationHistoryExport(InstructionRecordDto dto, PageQuery pageQuery, HttpServletResponse response) {
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(2000);
        TableDataInfo<LocationHistoryVo> result = appDeviceService.getLocationHistory(dto, pageQuery);
        if (result.getRows() == null) {
            return;
        }
        ExcelUtil.exportExcel(result.getRows(), "历史轨迹记录", LocationHistoryVo.class, response);
    }

    /**
     * 历史轨迹播放
     */
    @GetMapping("/getLocationHistoryDetail")
    public R<List<LocationHistoryDetailVo>> getLocationHistoryDetail(Long id) {
        return R.ok(appDeviceService.getLocationHistoryDetail(id));
    }

}
