package com.fuyuanshen.web.controller.device.fence;

import java.util.List;

import com.fuyuanshen.equipment.domain.bo.DeviceGeoFenceBo;
import com.fuyuanshen.equipment.domain.bo.FenceTerminalBo;
import com.fuyuanshen.equipment.domain.dto.FenceCheckResponse;
import com.fuyuanshen.equipment.domain.query.FenceCheckRequest;
import com.fuyuanshen.equipment.domain.vo.DeviceGeoFenceVo;
import com.fuyuanshen.equipment.service.IDeviceGeoFenceService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.ResponseEntity;
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-09-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/equipment/geoFence")
public class DeviceGeoFenceController extends BaseController {

    private final IDeviceGeoFenceService deviceGeoFenceService;


    /**
     * 查询电子围栏列表
     */
    @SaCheckPermission("fys-equipment:geoFence:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceGeoFenceVo> list(DeviceGeoFenceBo bo, PageQuery pageQuery) {
        return deviceGeoFenceService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出电子围栏列表
     */
    @SaCheckPermission("fys-equipment:geoFence:export")
    @Log(title = "导出电子围栏列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceGeoFenceBo bo, HttpServletResponse response) {
        List<DeviceGeoFenceVo> list = deviceGeoFenceService.queryList(bo);
        // 设置区域类型名称
        list.forEach(item -> {
            item.setAreaTypeNameByAreaType();
            item.setIsActiveNameByIsActive(); // 添加这行来设置激活状态名称
        });
        ExcelUtil.exportExcel(list, "电子围栏", DeviceGeoFenceVo.class, response);
    }


    /**
     * 获取电子围栏详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("fys-equipment:geoFence:query")
    @GetMapping("/{id}")
    public R<DeviceGeoFenceVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(deviceGeoFenceService.queryById(id));
    }

    /**
     * 新增电子围栏
     */
    @SaCheckPermission("fys-equipment:geoFence:add")
    @Log(title = "电子围栏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceGeoFenceBo bo) {
        return toAjax(deviceGeoFenceService.insertByBo(bo));
    }

    /**
     * 修改电子围栏
     */
    @SaCheckPermission("fys-equipment:geoFence:edit")
    @Log(title = "电子围栏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceGeoFenceBo bo) {
        return toAjax(deviceGeoFenceService.updateByBo(bo));
    }


    /**
     * 删除电子围栏
     *
     * @param ids 主键串
     */
    @SaCheckPermission("fys-equipment:geoFence:remove")
    @Log(title = "电子围栏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(deviceGeoFenceService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 电子围栏-位置检查
     *
     * @param request
     * @return
     */
    @PostMapping("/check")
    public ResponseEntity<FenceCheckResponse> checkPosition(
            @Valid @RequestBody FenceCheckRequest request) {
        FenceCheckResponse response = deviceGeoFenceService.checkPosition(request);
        return ResponseEntity.ok(response);
    }


    /**
     * 添加电子围栏终端
     *
     * @param bo
     * @return
     */
    @PostMapping("/addTerminal")
    public R<Void> addFenceTerminal(@RequestBody FenceTerminalBo bo) {
        return toAjax(deviceGeoFenceService.addFenceTerminal(bo));
    }
    /**
     * 删除电子围栏终端
     *
     * @param bo
     * @return
     */
    @PostMapping("/delTerminal")
    public R<Void> delFenceTerminal(@RequestBody FenceTerminalBo bo) {
        return toAjax(deviceGeoFenceService.delFenceTerminal(bo));
    }

}
