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.TDevCabinet;
import com.ruoyi.project.patrol.dev.service.ITDevCabinetService;
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-07
 */
@RestController
@RequestMapping("/dev/cabinet")
public class TDevCabinetController extends BaseController {
    @Autowired
    private ITDevCabinetService tDevCabinetService;

    /**
     * 查询应急事故柜列表
     */
    @PreAuthorize("@ss.hasPermi('dev:cabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevCabinet tDevCabinet) {
        startPage();
        List<TDevCabinet> list = tDevCabinetService.selectTDevCabinetList(tDevCabinet);
        return getDataTable(list);
    }

    /**
     * 导出应急事故柜列表
     */
    
    @Log(title = "应急事故柜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevCabinet tDevCabinet) {
        List<TDevCabinet> list = tDevCabinetService.selectTDevCabinetList(tDevCabinet);
        ExcelUtil<TDevCabinet> util = new ExcelUtil<TDevCabinet>(TDevCabinet.class);
        return util.exportExcel(list,  "应急事故柜数据");
    }

    @Log(title = "应急事故柜二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response,TDevCabinet tDevCabinet) {
        tDevCabinet.setDevType(null);
        List<TDevCabinet> list = tDevCabinetService.selectTDevCabinetList(tDevCabinet);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevCabinet item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), "事故柜"+item.getDevType()+"："+item.getDevName() ,item.getPatrolType()));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取应急事故柜详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:cabinet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevCabinetService.selectTDevCabinetById(id));
    }

    /**
     * 新增应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('dev:cabinet:add')")
    @Log(title = "应急事故柜", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevCabinet tDevCabinet) {
        return toAjax(tDevCabinetService.insertTDevCabinet(tDevCabinet));
    }

    /**
     * 修改应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('dev:cabinet:edit')")
    @Log(title = "应急事故柜", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevCabinet tDevCabinet) {
        return toAjax(tDevCabinetService.updateTDevCabinet(tDevCabinet));
    }

    /**
     * 删除应急事故柜
     */
    @PreAuthorize("@ss.hasPermi('dev:cabinet:remove')")
    @Log(title = "应急事故柜", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevCabinetService.deleteTDevCabinetByIds(ids));
    }
}
