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.TDevCctv;
import com.ruoyi.project.patrol.dev.service.ITDevCctvService;
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;

/**
 * CCTV系统Controller
 *
 * @author admin
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/dev/cctv")
public class TDevCctvController extends BaseController {
    @Autowired
    private ITDevCctvService tDevCctvService;

    /**
     * 查询CCTV系统列表
     */
    @PreAuthorize("@ss.hasPermi('dev:cctv:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevCctv tDevCctv) {
        startPage();
        List<TDevCctv> list = tDevCctvService.selectTDevCctvList(tDevCctv);
        return getDataTable(list);
    }

    /**
     * 导出CCTV系统列表
     */
    
    @Log(title = "CCTV系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevCctv tDevCctv) {
        List<TDevCctv> list = tDevCctvService.selectTDevCctvList(tDevCctv);
        ExcelUtil<TDevCctv> util = new ExcelUtil<TDevCctv>(TDevCctv.class);
        return util.exportExcel(list,  "CCTV系统数据");
    }

    @Log(title = "CCTV系统二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevCctv tDevCctv) {
        List<TDevCctv> list = tDevCctvService.selectTDevCctvList(tDevCctv);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevCctv item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), item.getDevNo(), item.getPatrolType(), ""));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取CCTV系统详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:cctv:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevCctvService.selectTDevCctvById(id));
    }

    /**
     * 新增CCTV系统
     */
    @PreAuthorize("@ss.hasPermi('dev:cctv:add')")
    @Log(title = "CCTV系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevCctv tDevCctv) {
        return toAjax(tDevCctvService.insertTDevCctv(tDevCctv));
    }

    /**
     * 修改CCTV系统
     */
    @PreAuthorize("@ss.hasPermi('dev:cctv:edit')")
    @Log(title = "CCTV系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevCctv tDevCctv) {
        return toAjax(tDevCctvService.updateTDevCctv(tDevCctv));
    }

    /**
     * 删除CCTV系统
     */
    @PreAuthorize("@ss.hasPermi('dev:cctv:remove')")
    @Log(title = "CCTV系统", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevCctvService.deleteTDevCctvByIds(ids));
    }
}
