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.TDevSafetyBelt;
import com.ruoyi.project.patrol.dev.service.ITDevSafetyBeltService;
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/safetyBelt")
public class TDevSafetyBeltController extends BaseController {
    @Autowired
    private ITDevSafetyBeltService tDevSafetyBeltService;

    /**
     * 查询安全带列表
     */
    @PreAuthorize("@ss.hasPermi('dev:safetyBelt:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevSafetyBelt tDevSafetyBelt) {
        startPage();
        List<TDevSafetyBelt> list = tDevSafetyBeltService.selectTDevSafetyBeltList(tDevSafetyBelt);
        return getDataTable(list);
    }

    /**
     * 导出安全带列表
     */
    
    @Log(title = "安全带", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevSafetyBelt tDevSafetyBelt) {
        List<TDevSafetyBelt> list = tDevSafetyBeltService.selectTDevSafetyBeltList(tDevSafetyBelt);
        ExcelUtil<TDevSafetyBelt> util = new ExcelUtil<TDevSafetyBelt>(TDevSafetyBelt.class);
        return util.exportExcel(list,  "安全带数据");
    }

    @Log(title = "安全带二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevSafetyBelt tDevSafetyBelt) {
        List<TDevSafetyBelt> list = tDevSafetyBeltService.selectTDevSafetyBeltList(tDevSafetyBelt);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevSafetyBelt 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:safetyBelt:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevSafetyBeltService.selectTDevSafetyBeltById(id));
    }

    /**
     * 新增安全带
     */
    @PreAuthorize("@ss.hasPermi('dev:safetyBelt:add')")
    @Log(title = "安全带", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevSafetyBelt tDevSafetyBelt) {
        return toAjax(tDevSafetyBeltService.insertTDevSafetyBelt(tDevSafetyBelt));
    }

    /**
     * 修改安全带
     */
    @PreAuthorize("@ss.hasPermi('dev:safetyBelt:edit')")
    @Log(title = "安全带", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevSafetyBelt tDevSafetyBelt) {
        return toAjax(tDevSafetyBeltService.updateTDevSafetyBelt(tDevSafetyBelt));
    }

    /**
     * 删除安全带
     */
    @PreAuthorize("@ss.hasPermi('dev:safetyBelt:remove')")
    @Log(title = "安全带", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevSafetyBeltService.deleteTDevSafetyBeltByIds(ids));
    }
}
