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.TDevTester;
import com.ruoyi.project.patrol.dev.service.ITDevTesterService;
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-03
 */
@RestController
@RequestMapping("/dev/tester")
public class TDevTesterController extends BaseController {
    @Autowired
    private ITDevTesterService tDevTesterService;

    /**
     * 查询检测仪列表
     */
    @PreAuthorize("@ss.hasPermi('dev:tester:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevTester tDevTester) {
        startPage();
        List<TDevTester> list = tDevTesterService.selectTDevTesterList(tDevTester);
        return getDataTable(list);
    }

    /**
     * 导出检测仪列表
     */
    
    @Log(title = "检测仪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevTester tDevTester) {
        List<TDevTester> list = tDevTesterService.selectTDevTesterList(tDevTester);
        ExcelUtil<TDevTester> util = new ExcelUtil<TDevTester>(TDevTester.class);
        return util.exportExcel(list,  "检测仪数据");
    }

    @Log(title = "检测仪二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevTester tDevTester) {
        List<TDevTester> list = tDevTesterService.selectTDevTesterList(tDevTester);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevTester item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), "类型：" + item.getTesterType() + " 编号：" + item.getTesterNo(), item.getPatrolType()));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取检测仪详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:tester:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevTesterService.selectTDevTesterById(id));
    }

    /**
     * 新增检测仪
     */
    @PreAuthorize("@ss.hasPermi('dev:tester:add')")
    @Log(title = "检测仪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevTester tDevTester) {
        return toAjax(tDevTesterService.insertTDevTester(tDevTester));
    }

    /**
     * 修改检测仪
     */
    @PreAuthorize("@ss.hasPermi('dev:tester:edit')")
    @Log(title = "检测仪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevTester tDevTester) {
        return toAjax(tDevTesterService.updateTDevTester(tDevTester));
    }

    /**
     * 删除检测仪
     */
    @PreAuthorize("@ss.hasPermi('dev:tester:remove')")
    @Log(title = "检测仪", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevTesterService.deleteTDevTesterByIds(ids));
    }
}
