package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.locationLog.domain.LocationLogQuery;
import com.ruoyi.bst.locationLog.domain.LocationLogVO;
import com.ruoyi.bst.locationLog.service.LocationLogService;
import com.ruoyi.bst.locationLog.service.LocationLogValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 定位日志Controller
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/bst/locationLog")
public class LocationLogController extends BaseController
{
    @Autowired
    private LocationLogService locationLogService;

    @Autowired
    private LocationLogValidator locationLogValidator;


    /**
     * 查询定位日志列表
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(LocationLogQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        List<LocationLogVO> list = locationLogService.selectLocationLogList(query);
        return getDataTable(list);
    }

    /**
     * 查询全部，不分页
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(LocationLogQuery query)
    {
        if (query.getDeviceId() == null && query.getEqMac() == null && query.getOrderId() == null) {
            return error("设备ID、MAC、订单ID不能同时为空");
        }
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        List<LocationLogVO> list = locationLogService.selectLocationLogList(query);
        return success(list);
    }

    /**
     * 根据时间段查询全部定位日志
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:list')")
    @GetMapping("/listByTime")
    public AjaxResult listByTime(LocationLogQuery query) {
        if (query.getStartTime() == null || query.getEndTime() == null) {
            return error("开始时间和结束时间不能为空");
        }
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        List<LocationLogVO> list = locationLogService.selectLocationLogList(query);
        return success(list);
    }

    /**
     * 导出定位日志列表
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:export')")
    @Log(title = "定位日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LocationLogQuery query)
    {
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        List<LocationLogVO> list = locationLogService.selectLocationLogList(query);
        ExcelUtil<LocationLogVO> util = new ExcelUtil<LocationLogVO>(LocationLogVO.class);
        util.exportExcel(response, list, "定位日志数据");
    }

    /**
     * 获取定位日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(locationLogService.selectLocationLogById(id, true));
    }

    /**
     * 删除定位日志
     */
    @PreAuthorize("@ss.hasPermi('bst:locationLog:remove')")
    @Log(title = "定位日志", businessType = BusinessType.DELETE, bizIdName = "arg0", bizType = LogBizType.LOCATION_LOG)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        if (!locationLogValidator.canDeleteAll(ids)) {
            return error("您无权删除ID为" + ids + "的定位日志");
        }
        return toAjax(locationLogService.deleteLocationLogByIds(ids));
    }
}
