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

/**
 * CO2气体保护系统Controller
 *
 * @author admin
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/dev/shield")
public class TDevGasShieldController extends BaseController {
    @Autowired
    private ITDevGasShieldService tDevGasShieldService;

    /**
     * 查询CO2气体保护系统列表
     */
    @PreAuthorize("@ss.hasPermi('dev:shield:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevGasShield tDevGasShield) {
        startPage();
        List<TDevGasShield> list = tDevGasShieldService.selectTDevGasShieldList(tDevGasShield);
        return getDataTable(list);
    }

    /**
     * 导出CO2气体保护系统列表
     */
    
    @Log(title = "CO2气体保护系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevGasShield tDevGasShield) {
        List<TDevGasShield> list = tDevGasShieldService.selectTDevGasShieldList(tDevGasShield);
        ExcelUtil<TDevGasShield> util = new ExcelUtil<TDevGasShield>(TDevGasShield.class);
        return util.exportExcel(list,  "CO2气体保护系统数据");
    }

    @Log(title = "CO2气体保护系统", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevGasShield tDevGasShield) {
        List<TDevGasShield> list = tDevGasShieldService.selectTDevGasShieldList(tDevGasShield);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevGasShield 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);
    }

    /**
     * 获取CO2气体保护系统详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:shield:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevGasShieldService.selectTDevGasShieldById(id));
    }

    /**
     * 新增CO2气体保护系统
     */
    @PreAuthorize("@ss.hasPermi('dev:shield:add')")
    @Log(title = "CO2气体保护系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevGasShield tDevGasShield) {
        return toAjax(tDevGasShieldService.insertTDevGasShield(tDevGasShield));
    }

    /**
     * 修改CO2气体保护系统
     */
    @PreAuthorize("@ss.hasPermi('dev:shield:edit')")
    @Log(title = "CO2气体保护系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevGasShield tDevGasShield) {
        return toAjax(tDevGasShieldService.updateTDevGasShield(tDevGasShield));
    }

    /**
     * 删除CO2气体保护系统
     */
    @PreAuthorize("@ss.hasPermi('dev:shield:remove')")
    @Log(title = "CO2气体保护系统", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevGasShieldService.deleteTDevGasShieldByIds(ids));
    }
}
