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.TDevMovement;
import com.ruoyi.project.patrol.dev.service.ITDevMovementService;
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/movement")
public class TDevMovementController extends BaseController {
    @Autowired
    private ITDevMovementService tDevMovementService;

    /**
     * 查询移动平台及移动梯列表
     */
    @PreAuthorize("@ss.hasPermi('dev:movement:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevMovement tDevMovement) {
        startPage();
        List<TDevMovement> list = tDevMovementService.selectTDevMovementList(tDevMovement);
        return getDataTable(list);
    }

    /**
     * 导出移动平台及移动梯列表
     */
    
    @Log(title = "移动平台及移动梯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevMovement tDevMovement) {
        List<TDevMovement> list = tDevMovementService.selectTDevMovementList(tDevMovement);
        ExcelUtil<TDevMovement> util = new ExcelUtil<TDevMovement>(TDevMovement.class);
        return util.exportExcel(list,  "移动平台及移动梯数据");
    }

    @Log(title = "移动平台及移动梯二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevMovement tDevMovement) {
        List<TDevMovement> list = tDevMovementService.selectTDevMovementList(tDevMovement);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevMovement 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:movement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevMovementService.selectTDevMovementById(id));
    }

    /**
     * 新增移动平台及移动梯
     */
    @PreAuthorize("@ss.hasPermi('dev:movement:add')")
    @Log(title = "移动平台及移动梯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevMovement tDevMovement) {
        return toAjax(tDevMovementService.insertTDevMovement(tDevMovement));
    }

    /**
     * 修改移动平台及移动梯
     */
    @PreAuthorize("@ss.hasPermi('dev:movement:edit')")
    @Log(title = "移动平台及移动梯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevMovement tDevMovement) {
        return toAjax(tDevMovementService.updateTDevMovement(tDevMovement));
    }

    /**
     * 删除移动平台及移动梯
     */
    @PreAuthorize("@ss.hasPermi('dev:movement:remove')")
    @Log(title = "移动平台及移动梯", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDevMovementService.deleteTDevMovementByIds(ids));
    }
}
