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.TDevStandpipe;
import com.ruoyi.project.patrol.dev.service.ITDevStandpipeService;
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-10-31
 */
@RestController
@RequestMapping("/dev/standpipe")
public class TDevStandpipeController extends BaseController {
    @Autowired
    private ITDevStandpipeService tDevStandpipeService;

    /**
     * 查询消防竖管列表
     */
    @PreAuthorize("@ss.hasPermi('dev:standpipe:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevStandpipe tDevStandpipe) {
        startPage();
        List<TDevStandpipe> list = tDevStandpipeService.selectTDevStandpipeList(tDevStandpipe);
        return getDataTable(list);
    }

    /**
     * 导出消防竖管列表
     */
    
    @Log(title = "消防竖管", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevStandpipe tDevStandpipe) {
        List<TDevStandpipe> list = tDevStandpipeService.selectTDevStandpipeList(tDevStandpipe);
        ExcelUtil<TDevStandpipe> util = new ExcelUtil<TDevStandpipe>(TDevStandpipe.class);
        return util.exportExcel(list,  "消防竖管数据");
    }

    @Log(title = "消防竖管二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevStandpipe tDevStandpipe) {
        List<TDevStandpipe> list = tDevStandpipeService.selectTDevStandpipeList(tDevStandpipe);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevStandpipe 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:standpipe:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevStandpipeService.selectTDevStandpipeById(id));
    }

    /**
     * 新增消防竖管
     */
    @PreAuthorize("@ss.hasPermi('dev:standpipe:add')")
    @Log(title = "消防竖管", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevStandpipe tDevStandpipe) {
        return toAjax(tDevStandpipeService.insertTDevStandpipe(tDevStandpipe));
    }

    /**
     * 修改消防竖管
     */
    @PreAuthorize("@ss.hasPermi('dev:standpipe:edit')")
    @Log(title = "消防竖管", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevStandpipe tDevStandpipe) {
        return toAjax(tDevStandpipeService.updateTDevStandpipe(tDevStandpipe));
    }

    /**
     * 删除消防竖管
     */
    @PreAuthorize("@ss.hasPermi('dev:standpipe:remove')")
    @Log(title = "消防竖管", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevStandpipeService.deleteTDevStandpipeByIds(ids));
    }
}
