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.TDevSandbox;
import com.ruoyi.project.patrol.dev.service.ITDevSandboxService;
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-02
 */
@RestController
@RequestMapping("/dev/sandbox")
public class TDevSandboxController extends BaseController
{
    @Autowired
    private ITDevSandboxService tDevSandboxService;

    /**
     * 查询应急沙箱列表
     */
    @PreAuthorize("@ss.hasPermi('dev:sandbox:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevSandbox tDevSandbox)
    {
        startPage();
        List<TDevSandbox> list = tDevSandboxService.selectTDevSandboxList(tDevSandbox);
        return getDataTable(list);
    }

    /**
     * 导出应急沙箱列表
     */
    
    @Log(title = "应急沙箱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevSandbox tDevSandbox)
    {
        List<TDevSandbox> list = tDevSandboxService.selectTDevSandboxList(tDevSandbox);
        ExcelUtil<TDevSandbox> util = new ExcelUtil<TDevSandbox>(TDevSandbox.class);
        return util.exportExcel(list,  "应急沙箱数据");
    }

    @Log(title = "应急沙箱二维码导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response,  TDevSandbox tDevSandbox) {
        List<TDevSandbox> list = tDevSandboxService.selectTDevSandboxList(tDevSandbox);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevSandbox 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:sandbox:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tDevSandboxService.selectTDevSandboxById(id));
    }

    /**
     * 新增应急沙箱
     */
    @PreAuthorize("@ss.hasPermi('dev:sandbox:add')")
    @Log(title = "应急沙箱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevSandbox tDevSandbox)
    {
        return toAjax(tDevSandboxService.insertTDevSandbox(tDevSandbox));
    }

    /**
     * 修改应急沙箱
     */
    @PreAuthorize("@ss.hasPermi('dev:sandbox:edit')")
    @Log(title = "应急沙箱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevSandbox tDevSandbox)
    {
        return toAjax(tDevSandboxService.updateTDevSandbox(tDevSandbox));
    }

    /**
     * 删除应急沙箱
     */
    @PreAuthorize("@ss.hasPermi('dev:sandbox:remove')")
    @Log(title = "应急沙箱", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDevSandboxService.deleteTDevSandboxByIds(ids));
    }
}
