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.TDevManholeCover;
import com.ruoyi.project.patrol.dev.service.ITDevManholeCoverService;
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/cover")
public class TDevManholeCoverController extends BaseController {
    @Autowired
    private ITDevManholeCoverService tDevManholeCoverService;

    /**
     * 查询地井盖列表
     */
    @PreAuthorize("@ss.hasPermi('dev:cover:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevManholeCover tDevManholeCover) {
        startPage();
        List<TDevManholeCover> list = tDevManholeCoverService.selectTDevManholeCoverList(tDevManholeCover);
        return getDataTable(list);
    }

    /**
     * 导出地井盖列表
     */
    
    @Log(title = "地井盖", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevManholeCover tDevManholeCover) {
        List<TDevManholeCover> list = tDevManholeCoverService.selectTDevManholeCoverList(tDevManholeCover);
        ExcelUtil<TDevManholeCover> util = new ExcelUtil<TDevManholeCover>(TDevManholeCover.class);
        return util.exportExcel(list,  "地井盖数据");
    }

    @Log(title = "地井盖二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevManholeCover tDevManholeCover) {
        List<TDevManholeCover> list = tDevManholeCoverService.selectTDevManholeCoverList(tDevManholeCover);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevManholeCover 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:cover:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevManholeCoverService.selectTDevManholeCoverById(id));
    }

    /**
     * 新增地井盖
     */
    @PreAuthorize("@ss.hasPermi('dev:cover:add')")
    @Log(title = "地井盖", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevManholeCover tDevManholeCover) {
        return toAjax(tDevManholeCoverService.insertTDevManholeCover(tDevManholeCover));
    }

    /**
     * 修改地井盖
     */
    @PreAuthorize("@ss.hasPermi('dev:cover:edit')")
    @Log(title = "地井盖", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevManholeCover tDevManholeCover) {
        return toAjax(tDevManholeCoverService.updateTDevManholeCover(tDevManholeCover));
    }

    /**
     * 删除地井盖
     */
    @PreAuthorize("@ss.hasPermi('dev:cover:remove')")
    @Log(title = "地井盖", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevManholeCoverService.deleteTDevManholeCoverByIds(ids));
    }
}
