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.TDevEyewash;
import com.ruoyi.project.patrol.dev.service.ITDevEyewashService;
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 ruoyi
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/dev/eyewash")
public class TDevEyewashController extends BaseController
{
    @Autowired
    private ITDevEyewashService tDevEyewashService;

    /**
     * 查询洗眼器列表
     */
    @PreAuthorize("@ss.hasPermi('dev:eyewash:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevEyewash tDevEyewash)
    {
        startPage();
        List<TDevEyewash> list = tDevEyewashService.selectTDevEyewashList(tDevEyewash);
        return getDataTable(list);
    }

    /**
     * 导出洗眼器列表
     */
    
    @Log(title = "洗眼器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevEyewash tDevEyewash)
    {
        List<TDevEyewash> list = tDevEyewashService.selectTDevEyewashList(tDevEyewash);
        ExcelUtil<TDevEyewash> util = new ExcelUtil<TDevEyewash>(TDevEyewash.class);
        return util.exportExcel(list,  "洗眼器数据");
    }

    @Log(title = "洗眼器", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevEyewash tDevEyewash){
        List<TDevEyewash> list = tDevEyewashService.selectTDevEyewashList(tDevEyewash);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevEyewash 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:eyewash:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tDevEyewashService.selectTDevEyewashById(id));
    }

    /**
     * 新增洗眼器
     */
    @PreAuthorize("@ss.hasPermi('dev:eyewash:add')")
    @Log(title = "洗眼器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevEyewash tDevEyewash)
    {
        return toAjax(tDevEyewashService.insertTDevEyewash(tDevEyewash));
    }

    /**
     * 修改洗眼器
     */
    @PreAuthorize("@ss.hasPermi('dev:eyewash:edit')")
    @Log(title = "洗眼器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevEyewash tDevEyewash)
    {
        return toAjax(tDevEyewashService.updateTDevEyewash(tDevEyewash));
    }

    /**
     * 删除洗眼器
     */
    @PreAuthorize("@ss.hasPermi('dev:eyewash:remove')")
    @Log(title = "洗眼器", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDevEyewashService.deleteTDevEyewashByIds(ids));
    }
}
