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.TDevBicycle;
import com.ruoyi.project.patrol.dev.service.ITDevBicycleService;
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-09
 */
@RestController
@RequestMapping("/dev/bicycle")
public class TDevBicycleController extends BaseController {
    @Autowired
    private ITDevBicycleService tDevBicycleService;

    /**
     * 查询自行车列表
     */
    @PreAuthorize("@ss.hasPermi('dev:bicycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevBicycle tDevBicycle) {
        startPage();
        List<TDevBicycle> list = tDevBicycleService.selectTDevBicycleList(tDevBicycle);
        return getDataTable(list);
    }

    /**
     * 导出自行车列表
     */
    
    @Log(title = "自行车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevBicycle tDevBicycle) {
        List<TDevBicycle> list = tDevBicycleService.selectTDevBicycleList(tDevBicycle);
        ExcelUtil<TDevBicycle> util = new ExcelUtil<TDevBicycle>(TDevBicycle.class);
        return util.exportExcel(list,  "自行车数据");
    }

    /**
     * 导出自行车列表
     */
    @Log(title = "自行车二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevBicycle tDevBicycle) {
        List<TDevBicycle> list = tDevBicycleService.selectTDevBicycleList(tDevBicycle);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevBicycle 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:bicycle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevBicycleService.selectTDevBicycleById(id));
    }

    /**
     * 新增自行车
     */
    @PreAuthorize("@ss.hasPermi('dev:bicycle:add')")
    @Log(title = "自行车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevBicycle tDevBicycle) {
        return toAjax(tDevBicycleService.insertTDevBicycle(tDevBicycle));
    }

    /**
     * 修改自行车
     */
    @PreAuthorize("@ss.hasPermi('dev:bicycle:edit')")
    @Log(title = "自行车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevBicycle tDevBicycle) {
        return toAjax(tDevBicycleService.updateTDevBicycle(tDevBicycle));
    }

    /**
     * 删除自行车
     */
    @PreAuthorize("@ss.hasPermi('dev:bicycle:remove')")
    @Log(title = "自行车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevBicycleService.deleteTDevBicycleByIds(ids));
    }
}
