package com.fuyuanshen.equipment.controller;

import java.util.List;

import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.bo.TerminalDelBo;
import com.fuyuanshen.equipment.domain.bo.TerminalDeviceBo;
import com.fuyuanshen.equipment.domain.bo.TerminalQueryBo;
import com.fuyuanshen.equipment.domain.bo.TrackServiceBo;
import com.fuyuanshen.equipment.domain.dto.TerminalDeviceDto;
import com.fuyuanshen.equipment.domain.vo.TrackServiceVo;
import com.fuyuanshen.equipment.service.ITrackServiceService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 轨迹服务
 *
 * @author Lion Li
 * @date 2025-11-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/trackService")
public class TrackServiceController extends BaseController {

    private final ITrackServiceService trackServiceService;

    /**
     * 查询轨迹服务列表
     */
    @SaCheckPermission("equipment:trackService:list")
    @GetMapping("/list")
    public TableDataInfo<TrackServiceVo> list(TrackServiceBo bo, PageQuery pageQuery) {
        return trackServiceService.queryPageList(bo, pageQuery);
    }

    /**
     * 轨迹服务列表
     */
    @SaCheckPermission("equipment:trackService:export")
    @Log(title = "轨迹服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TrackServiceBo bo, HttpServletResponse response) {
        List<TrackServiceVo> list = trackServiceService.queryList(bo);
        ExcelUtil.exportExcel(list, "轨迹服务", TrackServiceVo.class, response);
    }

    /**
     * 获取轨迹服务详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<TrackServiceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(trackServiceService.queryById(id));
    }

    /**
     * 新增轨迹服务
     */
    @SaCheckPermission("equipment:trackService:add")
    @Log(title = "轨迹服务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TrackServiceBo bo) {
        return toAjax(trackServiceService.insertByBo(bo));
    }

    /**
     * 修改轨迹服务
     */
    @SaCheckPermission("equipment:trackService:edit")
    @Log(title = "轨迹服务", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TrackServiceBo bo) {
        return toAjax(trackServiceService.updateByBo(bo));
    }

    /**
     * 删除轨迹服务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("equipment:trackService:remove")
    @Log(title = "轨迹服务", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/delete")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(trackServiceService.deleteWithValidByIds(List.of(ids), true));
    }

    @Log(title = "高德添加终端设备")
    @PostMapping(value = "/terminal")
    public R<Void> terminalDevice(@RequestBody TerminalDeviceBo model) {
        trackServiceService.terminal(model);
        return R.ok();
    }

    @Log(title = "高德移除终端设备")
    @DeleteMapping(value = "/delTerminal/{ids}")
    public R<Void> delTerminalDevice(@NotEmpty(message = "主键不能为空")
                                         @PathVariable Long[] ids) {
        return toAjax(trackServiceService.delTerminalDevice(List.of(ids), true));
    }

    @Log(title = "高德移除终端设备")
    @DeleteMapping(value = "/del/Terminal")
    public R<Void> delTerminalDevice(TerminalDelBo bo) {
        return toAjax(trackServiceService.delTerminalDevice(bo, true));
    }

    /**
     * 高德终端设备列表
     */
    @GetMapping("/listTerminal")
    public TableDataInfo<TerminalDeviceDto> listTerminal(TerminalQueryBo bo) {
        return trackServiceService.listTerminal(bo);
    }

    /**
     * 高德终端设备列表(关键字查询)
     */
    @GetMapping("/searchTerminal")
    public TableDataInfo<TerminalDeviceDto> searchTerminal(TerminalQueryBo bo) {
        return trackServiceService.searchTerminal(bo);
    }
}
