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

/**
 * PAGAController
 *
 * @author admin
 * @date 2023-11-02
 */
@RestController
@RequestMapping("/dev/paga")
public class TDevPagaController extends BaseController {
    @Autowired
    private ITDevPagaService tDevPagaService;

    /**
     * 查询PAGA列表
     */
    @PreAuthorize("@ss.hasPermi('dev:paga:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevPaga tDevPaga) {
        startPage();
        List<TDevPaga> list = tDevPagaService.selectTDevPagaList(tDevPaga);
        return getDataTable(list);
    }

    /**
     * 导出PAGA列表
     */
    
    @Log(title = "PAGA", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevPaga tDevPaga) {
        List<TDevPaga> list = tDevPagaService.selectTDevPagaList(tDevPaga);
        ExcelUtil<TDevPaga> util = new ExcelUtil<TDevPaga>(TDevPaga.class);
        return util.exportExcel(list,  "PAGA数据");
    }

    @Log(title = "PAGA二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevPaga tDevPaga) {
        List<TDevPaga> list = tDevPagaService.selectTDevPagaList(tDevPaga);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevPaga 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);
    }

    /**
     * 获取PAGA详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:paga:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevPagaService.selectTDevPagaById(id));
    }

    /**
     * 新增PAGA
     */
    @PreAuthorize("@ss.hasPermi('dev:paga:add')")
    @Log(title = "PAGA", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevPaga tDevPaga) {
        return toAjax(tDevPagaService.insertTDevPaga(tDevPaga));
    }

    /**
     * 修改PAGA
     */
    @PreAuthorize("@ss.hasPermi('dev:paga:edit')")
    @Log(title = "PAGA", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevPaga tDevPaga) {
        return toAjax(tDevPagaService.updateTDevPaga(tDevPaga));
    }

    /**
     * 删除PAGA
     */
    @PreAuthorize("@ss.hasPermi('dev:paga:remove')")
    @Log(title = "PAGA", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevPagaService.deleteTDevPagaByIds(ids));
    }
}
