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.TDevDownspout;
import com.ruoyi.project.patrol.dev.service.ITDevDownspoutService;
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/downspout")
public class TDevDownspoutController extends BaseController {
    @Autowired
    private ITDevDownspoutService tDevDownspoutService;

    /**
     * 查询落水管列表
     */
    @PreAuthorize("@ss.hasPermi('dev:downspout:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevDownspout tDevDownspout) {
        startPage();
        List<TDevDownspout> list = tDevDownspoutService.selectTDevDownspoutList(tDevDownspout);
        return getDataTable(list);
    }

    /**
     * 导出落水管列表
     */
    
    @Log(title = "落水管", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevDownspout tDevDownspout) {
        List<TDevDownspout> list = tDevDownspoutService.selectTDevDownspoutList(tDevDownspout);
        ExcelUtil<TDevDownspout> util = new ExcelUtil<TDevDownspout>(TDevDownspout.class);
        return util.exportExcel(list,  "落水管数据");
    }

    @Log(title = "落水管二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response,  TDevDownspout tDevDownspout) {
        List<TDevDownspout> list = tDevDownspoutService.selectTDevDownspoutList(tDevDownspout);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevDownspout item : list) {
            qrCodeEntities.add(new QRCodeEntity(item.getId(), item.getPosition(), item.getPatrolType() ));
        }
        QRCodeUtil<QRCodeEntity> util = new QRCodeUtil<>(QRCodeEntity.class);
        util.exportQRCode(response, qrCodeEntities);
    }

    /**
     * 获取落水管详细信息
     */
    @PreAuthorize("@ss.hasPermi('dev:downspout:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevDownspoutService.selectTDevDownspoutById(id));
    }

    /**
     * 新增落水管
     */
    @PreAuthorize("@ss.hasPermi('dev:downspout:add')")
    @Log(title = "落水管", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevDownspout tDevDownspout) {
        return toAjax(tDevDownspoutService.insertTDevDownspout(tDevDownspout));
    }

    /**
     * 修改落水管
     */
    @PreAuthorize("@ss.hasPermi('dev:downspout:edit')")
    @Log(title = "落水管", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevDownspout tDevDownspout) {
        return toAjax(tDevDownspoutService.updateTDevDownspout(tDevDownspout));
    }

    /**
     * 删除落水管
     */
    @PreAuthorize("@ss.hasPermi('dev:downspout:remove')")
    @Log(title = "落水管", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevDownspoutService.deleteTDevDownspoutByIds(ids));
    }
}
