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.TDevWalkieTalkie;
import com.ruoyi.project.patrol.dev.service.ITDevWalkieTalkieService;
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/walkieTalkie")
public class TDevWalkieTalkieController extends BaseController {
    @Autowired
    private ITDevWalkieTalkieService tDevWalkieTalkieService;

    /**
     * 查询对讲机列表
     */
    @PreAuthorize("@ss.hasPermi('dev:walkieTalkie:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevWalkieTalkie tDevWalkieTalkie) {
        startPage();
        List<TDevWalkieTalkie> list = tDevWalkieTalkieService.selectTDevWalkieTalkieList(tDevWalkieTalkie);
        return getDataTable(list);
    }

    /**
     * 导出对讲机列表
     */
    
    @Log(title = "对讲机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevWalkieTalkie tDevWalkieTalkie) {
        List<TDevWalkieTalkie> list = tDevWalkieTalkieService.selectTDevWalkieTalkieList(tDevWalkieTalkie);
        ExcelUtil<TDevWalkieTalkie> util = new ExcelUtil<TDevWalkieTalkie>(TDevWalkieTalkie.class);
        return util.exportExcel(list,  "对讲机数据");
    }

    @Log(title = "对讲机二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevWalkieTalkie tDevWalkieTalkie) {
        List<TDevWalkieTalkie> list = tDevWalkieTalkieService.selectTDevWalkieTalkieList(tDevWalkieTalkie);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevWalkieTalkie item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), item.getDevNo(), item.getPatrolType(), ""));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取对讲机详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:walkieTalkie:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevWalkieTalkieService.selectTDevWalkieTalkieById(id));
    }

    /**
     * 新增对讲机
     */
    @PreAuthorize("@ss.hasPermi('dev:walkieTalkie:add')")
    @Log(title = "对讲机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevWalkieTalkie tDevWalkieTalkie) {
        return toAjax(tDevWalkieTalkieService.insertTDevWalkieTalkie(tDevWalkieTalkie));
    }

    /**
     * 修改对讲机
     */
    @PreAuthorize("@ss.hasPermi('dev:walkieTalkie:edit')")
    @Log(title = "对讲机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevWalkieTalkie tDevWalkieTalkie) {
        return toAjax(tDevWalkieTalkieService.updateTDevWalkieTalkie(tDevWalkieTalkie));
    }

    /**
     * 删除对讲机
     */
    @PreAuthorize("@ss.hasPermi('dev:walkieTalkie:remove')")
    @Log(title = "对讲机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevWalkieTalkieService.deleteTDevWalkieTalkieByIds(ids));
    }
}
