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.TDevWeathervane;
import com.ruoyi.project.patrol.dev.service.ITDevWeathervaneService;
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-06
 */
@RestController
@RequestMapping("/dev/weathervane")
public class TDevWeathervaneController extends BaseController {
    @Autowired
    private ITDevWeathervaneService tDevWeathervaneService;

    /**
     * 查询风向标/风向袋/新风系统正压计列表
     */
    @PreAuthorize("@ss.hasPermi('dev:weathervane:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevWeathervane tDevWeathervane) {
        startPage();
        List<TDevWeathervane> list = tDevWeathervaneService.selectTDevWeathervaneList(tDevWeathervane);
        return getDataTable(list);
    }

    /**
     * 导出风向标/风向袋/新风系统正压计列表
     */
    
    @Log(title = "风向标/风向袋/新风系统正压计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevWeathervane tDevWeathervane) {
        List<TDevWeathervane> list = tDevWeathervaneService.selectTDevWeathervaneList(tDevWeathervane);
        ExcelUtil<TDevWeathervane> util = new ExcelUtil<TDevWeathervane>(TDevWeathervane.class);
        return util.exportExcel(list,  "风向标/风向袋/新风系统正压计数据");
    }

    @Log(title = "风向标二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevWeathervane tDevWeathervane) {
        List<TDevWeathervane> list = tDevWeathervaneService.selectTDevWeathervaneList(tDevWeathervane);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevWeathervane 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:weathervane:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevWeathervaneService.selectTDevWeathervaneById(id));
    }

    /**
     * 新增风向标/风向袋/新风系统正压计
     */
    @PreAuthorize("@ss.hasPermi('dev:weathervane:add')")
    @Log(title = "风向标/风向袋/新风系统正压计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevWeathervane tDevWeathervane) {
        return toAjax(tDevWeathervaneService.insertTDevWeathervane(tDevWeathervane));
    }

    /**
     * 修改风向标/风向袋/新风系统正压计
     */
    @PreAuthorize("@ss.hasPermi('dev:weathervane:edit')")
    @Log(title = "风向标/风向袋/新风系统正压计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevWeathervane tDevWeathervane) {
        return toAjax(tDevWeathervaneService.updateTDevWeathervane(tDevWeathervane));
    }

    /**
     * 删除风向标/风向袋/新风系统正压计
     */
    @PreAuthorize("@ss.hasPermi('dev:weathervane:remove')")
    @Log(title = "风向标/风向袋/新风系统正压计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevWeathervaneService.deleteTDevWeathervaneByIds(ids));
    }
}
