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.TDevEarplug;
import com.ruoyi.project.patrol.dev.service.ITDevEarplugService;
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-08
 */
@RestController
@RequestMapping("/dev/earplug")
public class TDevEarplugController extends BaseController {
    @Autowired
    private ITDevEarplugService tDevEarplugService;

    /**
     * 查询耳塞分配器列表
     */
    @PreAuthorize("@ss.hasPermi('dev:earplug:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevEarplug tDevEarplug) {
        startPage();
        List<TDevEarplug> list = tDevEarplugService.selectTDevEarplugList(tDevEarplug);
        return getDataTable(list);
    }

    /**
     * 导出耳塞分配器列表
     */
    
    @Log(title = "耳塞分配器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevEarplug tDevEarplug) {
        List<TDevEarplug> list = tDevEarplugService.selectTDevEarplugList(tDevEarplug);
        ExcelUtil<TDevEarplug> util = new ExcelUtil<TDevEarplug>(TDevEarplug.class);
        return util.exportExcel(list,  "耳塞分配器数据");
    }

    @Log(title = "耳塞分配器二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevEarplug tDevEarplug) {
        List<TDevEarplug> list = tDevEarplugService.selectTDevEarplugList(tDevEarplug);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevEarplug 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:earplug:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevEarplugService.selectTDevEarplugById(id));
    }

    /**
     * 新增耳塞分配器
     */
    @PreAuthorize("@ss.hasPermi('dev:earplug:add')")
    @Log(title = "耳塞分配器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevEarplug tDevEarplug) {
        return toAjax(tDevEarplugService.insertTDevEarplug(tDevEarplug));
    }

    /**
     * 修改耳塞分配器
     */
    @PreAuthorize("@ss.hasPermi('dev:earplug:edit')")
    @Log(title = "耳塞分配器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevEarplug tDevEarplug) {
        return toAjax(tDevEarplugService.updateTDevEarplug(tDevEarplug));
    }

    /**
     * 删除耳塞分配器
     */
    @PreAuthorize("@ss.hasPermi('dev:earplug:remove')")
    @Log(title = "耳塞分配器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevEarplugService.deleteTDevEarplugByIds(ids));
    }
}
