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.TDevSprinkler;
import com.ruoyi.project.patrol.dev.service.ITDevSprinklerService;
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/sprinkler")
public class TDevSprinklerController extends BaseController
{
    @Autowired
    private ITDevSprinklerService tDevSprinklerService;

    /**
     * 查询自动喷淋灭火系统列表
     */
    @PreAuthorize("@ss.hasPermi('dev:sprinkler:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevSprinkler tDevSprinkler)
    {
        startPage();
        List<TDevSprinkler> list = tDevSprinklerService.selectTDevSprinklerList(tDevSprinkler);
        return getDataTable(list);
    }

    /**
     * 导出自动喷淋灭火系统列表
     */
    
    @Log(title = "自动喷淋灭火系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevSprinkler tDevSprinkler)
    {
        List<TDevSprinkler> list = tDevSprinklerService.selectTDevSprinklerList(tDevSprinkler);
        ExcelUtil<TDevSprinkler> util = new ExcelUtil<TDevSprinkler>(TDevSprinkler.class);
        return util.exportExcel(list,  "自动喷淋灭火系统数据");
    }

    @Log(title = "自动喷淋灭火系统二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response,TDevSprinkler tDevSprinkler) {
        List<TDevSprinkler> list = tDevSprinklerService.selectTDevSprinklerList(tDevSprinkler);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevSprinkler 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:sprinkler:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tDevSprinklerService.selectTDevSprinklerById(id));
    }

    /**
     * 新增自动喷淋灭火系统
     */
    @PreAuthorize("@ss.hasPermi('dev:sprinkler:add')")
    @Log(title = "自动喷淋灭火系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevSprinkler tDevSprinkler)
    {
        return toAjax(tDevSprinklerService.insertTDevSprinkler(tDevSprinkler));
    }

    /**
     * 修改自动喷淋灭火系统
     */
    @PreAuthorize("@ss.hasPermi('dev:sprinkler:edit')")
    @Log(title = "自动喷淋灭火系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevSprinkler tDevSprinkler)
    {
        return toAjax(tDevSprinklerService.updateTDevSprinkler(tDevSprinkler));
    }

    /**
     * 删除自动喷淋灭火系统
     */
    @PreAuthorize("@ss.hasPermi('dev:sprinkler:remove')")
    @Log(title = "自动喷淋灭火系统", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDevSprinklerService.deleteTDevSprinklerByIds(ids));
    }
}
