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.TDevWaterPump;
import com.ruoyi.project.patrol.dev.service.ITDevWaterPumpService;
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/pump")
public class TDevWaterPumpController extends BaseController {
    @Autowired
    private ITDevWaterPumpService tDevWaterPumpService;

    /**
     * 查询水泵接合器列表
     */
    @PreAuthorize("@ss.hasPermi('dev:pump:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevWaterPump tDevWaterPump) {
        startPage();
        List<TDevWaterPump> list = tDevWaterPumpService.selectTDevWaterPumpList(tDevWaterPump);
        return getDataTable(list);
    }

    /**
     * 导出水泵接合器列表
     */
    
    @Log(title = "水泵接合器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevWaterPump tDevWaterPump) {
        List<TDevWaterPump> list = tDevWaterPumpService.selectTDevWaterPumpList(tDevWaterPump);
        ExcelUtil<TDevWaterPump> util = new ExcelUtil<TDevWaterPump>(TDevWaterPump.class);
        return util.exportExcel(list,  "水泵接合器数据");
    }

    @Log(title = "水泵接合器导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevWaterPump tDevWaterPump) {
        List<TDevWaterPump> list = tDevWaterPumpService.selectTDevWaterPumpList(tDevWaterPump);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevWaterPump 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:pump:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevWaterPumpService.selectTDevWaterPumpById(id));
    }

    /**
     * 新增水泵接合器
     */
    @PreAuthorize("@ss.hasPermi('dev:pump:add')")
    @Log(title = "水泵接合器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevWaterPump tDevWaterPump) {
        return toAjax(tDevWaterPumpService.insertTDevWaterPump(tDevWaterPump));
    }

    /**
     * 修改水泵接合器
     */
    @PreAuthorize("@ss.hasPermi('dev:pump:edit')")
    @Log(title = "水泵接合器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevWaterPump tDevWaterPump) {
        return toAjax(tDevWaterPumpService.updateTDevWaterPump(tDevWaterPump));
    }

    /**
     * 删除水泵接合器
     */
    @PreAuthorize("@ss.hasPermi('dev:pump:remove')")
    @Log(title = "水泵接合器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevWaterPumpService.deleteTDevWaterPumpByIds(ids));
    }
}
