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.domain.TDevDoor;
import com.ruoyi.project.patrol.dev.service.ITDevDoorService;
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-07
 */
@RestController
@RequestMapping("/dev/door")
public class TDevDoorController extends BaseController {
    @Autowired
    private ITDevDoorService tDevDoorService;

    /**
     * 查询常闭式防爆门/防火门列表
     */
    @PreAuthorize("@ss.hasPermi('dev:door:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevDoor tDevDoor) {
        startPage();
        List<TDevDoor> list = tDevDoorService.selectTDevDoorList(tDevDoor);
        return getDataTable(list);
    }

    /**
     * 导出常闭式防爆门/防火门列表
     */
    
    @Log(title = "常闭式防爆门/防火门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevDoor tDevDoor) {
        List<TDevDoor> list = tDevDoorService.selectTDevDoorList(tDevDoor);
        ExcelUtil<TDevDoor> util = new ExcelUtil<TDevDoor>(TDevDoor.class);
        return util.exportExcel(list,  "常闭式防爆门及防火门数据");
    }

    @Log(title = "常闭式防爆门/防火门二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevDoor tDevDoor) {
        tDevDoor.setDevType(null);
        List<TDevDoor> list = tDevDoorService.selectTDevDoorList(tDevDoor);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevDoor item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), item.getDevNo(), item.getPatrolType(), item.getDevType()));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取常闭式防爆门/防火门详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:door:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevDoorService.selectTDevDoorById(id));
    }

    /**
     * 新增常闭式防爆门/防火门
     */
    @PreAuthorize("@ss.hasPermi('dev:door:add')")
    @Log(title = "常闭式防爆门/防火门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevDoor tDevDoor) {
        return toAjax(tDevDoorService.insertTDevDoor(tDevDoor));
    }

    /**
     * 修改常闭式防爆门/防火门
     */
    @PreAuthorize("@ss.hasPermi('dev:door:edit')")
    @Log(title = "常闭式防爆门/防火门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevDoor tDevDoor) {
        return toAjax(tDevDoorService.updateTDevDoor(tDevDoor));
    }

    /**
     * 删除常闭式防爆门/防火门
     */
    @PreAuthorize("@ss.hasPermi('dev:door:remove')")
    @Log(title = "常闭式防爆门/防火门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevDoorService.deleteTDevDoorByIds(ids));
    }
}
