package com.ruoyi.project.patrol.dev.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.QRCodeEntity;
import com.ruoyi.common.utils.poi.QRCodeUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.patrol.dev.service.ITDevAlarmService;
import com.ruoyi.project.patrol.dev.domain.TDevAlarm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 现场可燃气体检测器/火灾报警系统Controller
 *
 * @author admin
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/dev/alarm")
public class TDevAlarmController extends BaseController {
    @Autowired
    private ITDevAlarmService tDevAlarmService;

    /**
     * 查询现场可燃气体检测器/火灾报警系统列表
     */
    @PreAuthorize("@ss.hasPermi('dev:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevAlarm tDevAlarm) {
        startPage();
        List<TDevAlarm> list = tDevAlarmService.selectTDevAlarmList(tDevAlarm);
        return getDataTable(list);
    }

    /**
     * 导出现场可燃气体检测器/火灾报警系统列表
     */
    
    @Log(title = "现场可燃气体检测器/火灾报警系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevAlarm tDevAlarm) {
        List<TDevAlarm> list = tDevAlarmService.selectTDevAlarmList(tDevAlarm);
        ExcelUtil<TDevAlarm> util = new ExcelUtil<TDevAlarm>(TDevAlarm.class);
        return util.exportExcel(list,  "现场可燃气体检测器/火灾报警系统数据");
    }

    @Log(title = "现场可燃气体检测器/火灾报警系统", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevAlarm tDevAlarm) {
        List<TDevAlarm> list = tDevAlarmService.selectTDevAlarmList(tDevAlarm);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevAlarm item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), item.getDevNo(), item.getPatrolType(), item.getPosition()));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }


    /**
     * 获取现场可燃气体检测器/火灾报警系统详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevAlarmService.selectTDevAlarmById(id));
    }

    /**
     * 新增现场可燃气体检测器/火灾报警系统
     */
    @PreAuthorize("@ss.hasPermi('dev:alarm:add')")
    @Log(title = "现场可燃气体检测器/火灾报警系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevAlarm tDevAlarm) {
        return toAjax(tDevAlarmService.insertTDevAlarm(tDevAlarm));
    }

    /**
     * 修改现场可燃气体检测器/火灾报警系统
     */
    @PreAuthorize("@ss.hasPermi('dev:alarm:edit')")
    @Log(title = "现场可燃气体检测器/火灾报警系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevAlarm tDevAlarm) {
        return toAjax(tDevAlarmService.updateTDevAlarm(tDevAlarm));
    }

    /**
     * 删除现场可燃气体检测器/火灾报警系统
     */
    @PreAuthorize("@ss.hasPermi('dev:alarm:remove')")
    @Log(title = "现场可燃气体检测器/火灾报警系统", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevAlarmService.deleteTDevAlarmByIds(ids));
    }
}
