package com.sooka.sponest.monitor.meiqiguan.gps.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.GasTankDevices;
import com.sooka.sponest.monitor.meiqiguan.gps.domain.vo.GasTankDevicesVo;
import com.sooka.sponest.monitor.meiqiguan.gps.service.IGasTankDevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 煤气罐设备信息Controller
 *
 * @author ruoyi
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/gps/devices")
public class GasTankDevicesController extends BaseController {
    @Autowired
    private IGasTankDevicesService gasTankDevicesService;

    /**
     * 查询煤气罐设备信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GasTankDevices gasTankDevices) {
        startPage();
        List<GasTankDevices> list = gasTankDevicesService.selectGasTankDevicesList(gasTankDevices);
        return getDataTable(list);
    }

    /**
     * 根据(用户权限)部门id查询部门及部门以下的数据，没有则默认查询所有
     * @param gasTankDevices
     * @param
     * @return
     */

    @GetMapping("/getGasTankListByDeptId")
    public R listDept(GasTankDevices gasTankDevices) {
        if (StringUtils.isEmpty(gasTankDevices.getDeptId())) {
            if (SecurityUtils.getLoginUser() == null) {
                // 未登录时返回错误信息
                return R.ok("用户未登录或Token无效");
            }
            //获取当前登录用户信息 及以下部门信息
            List<SysDept> user = SecurityUtils.getLoginUser().getSysUser().getDepts();
            List<String> gps = new ArrayList<>();
            for (SysDept dep : user) {
                gps.add(dep.getDeptId().toString());
            }
            List<GasTankDevices> list = gasTankDevicesService.selectGasTankDevicesListDpet(gps);
            return R.ok(list);
        } else {
            List<GasTankDevices> list = gasTankDevicesService.selectGasTankDevicesList(gasTankDevices);
            return R.ok(list);
        }
    }
    /**
     * 查询煤气罐设备列表与轨迹
     * @param gasTankDevices
     * @param
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(GasTankDevices gasTankDevices) {
        List<GasTankDevicesVo> list = gasTankDevicesService.selectGasTankDevicesListAll(gasTankDevices);
        return AjaxResult.success(list);
    }

    /**
     * 导出煤气罐设备信息列表
     */
    @Log(title = "煤气罐设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GasTankDevices gasTankDevices) {
        List<GasTankDevices> list = gasTankDevicesService.selectGasTankDevicesList(gasTankDevices);
        ExcelUtil<GasTankDevices> util = new ExcelUtil<GasTankDevices>(GasTankDevices.class);
        util.exportExcel(response, list, "煤气罐设备信息数据");
    }

    /**
     * 获取煤气罐设备信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gasTankDevicesService.selectGasTankDevicesById(id));
    }

    /**
     * 新增煤气罐设备信息
     */
    @Log(title = "煤气罐设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GasTankDevices gasTankDevices) {
        return toAjax(gasTankDevicesService.insertGasTankDevices(gasTankDevices));
    }

    /**
     * 修改煤气罐设备信息
     */
    @Log(title = "煤气罐设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GasTankDevices gasTankDevices) {
        return toAjax(gasTankDevicesService.updateGasTankDevices(gasTankDevices));
    }

    /**
     * 删除煤气罐设备信息
     */
    @Log(title = "煤气罐设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gasTankDevicesService.deleteGasTankDevicesByIds(ids));
    }

    /**
     * 获取最新一次行动轨迹时间的设备id列表
     * @param
     * @return
     */
    @GetMapping("/selectGasTankDevicesNewEndTime")
    public AjaxResult selectGasTankDevicesNewEndTime() {
        List<GasTankDevices> list = gasTankDevicesService.selectGasTankDevicesNewEndTime();
        return AjaxResult.success(list);
    }

}
