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.TDevAnnihilator;
import com.ruoyi.project.patrol.dev.service.ITDevAnnihilatorService;
import com.ruoyi.project.patrol.patrol.domain.TPatrolMain;
import com.ruoyi.project.patrol.patrol.service.ITPatrolMainService;
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 ruoyi
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/dev/annihilator")
public class TDevAnnihilatorController extends BaseController {
    @Autowired
    private ITDevAnnihilatorService tDevAnnihilatorService;
    @Autowired
    private ITPatrolMainService tPatrolMainService;

    /**
     * 查询灭火器材列表
     */
    @PreAuthorize("@ss.hasPermi('dev:annihilator:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDevAnnihilator tDevAnnihilator) {
        startPage();
        List<TDevAnnihilator> list = tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator);
        return getDataTable(list);
    }

    /**
     * 导出灭火器材列表
     */
    
    @Log(title = "灭火器材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TDevAnnihilator tDevAnnihilator) {
        List<TDevAnnihilator> list = tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator);
        ExcelUtil<TDevAnnihilator> util = new ExcelUtil<TDevAnnihilator>(TDevAnnihilator.class);
        return util.exportExcel(list,  "灭火器材数据");
    }

    @Log(title = "手提式/小推车式灭火器/灭火器材箱", businessType = BusinessType.EXPORT)
    @PostMapping("/exportQRCode")
    public void exportQRCode(HttpServletResponse response, TDevAnnihilator tDevAnnihilator) {
        List<TDevAnnihilator> list = tDevAnnihilatorService.selectTDevAnnihilatorList(tDevAnnihilator);
        List<QRCodeEntity> qrCodeEntities = new ArrayList<>();
        for (TDevAnnihilator 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:annihilator:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDevAnnihilatorService.selectTDevAnnihilatorById(id));
    }

    /**
     * 新增灭火器材
     */
    @PreAuthorize("@ss.hasPermi('dev:annihilator:add')")
    @Log(title = "灭火器材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDevAnnihilator tDevAnnihilator) {
        String num = "{1}/{2}/{3}";
        if (tDevAnnihilatorService.insertTDevAnnihilator(tDevAnnihilator) > 0) {
            num = getNum(num);
        }
        return getAjaxResult(num);
    }

    /**
     * 修改灭火器材
     */
    @PreAuthorize("@ss.hasPermi('dev:annihilator:edit')")
    @Log(title = "灭火器材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDevAnnihilator tDevAnnihilator) {
        String num = "{1}/{2}/{3}";
        if (tDevAnnihilatorService.updateTDevAnnihilator(tDevAnnihilator) > 0) {
            num = getNum(num);
        }
        return getAjaxResult(num);
    }

    /**
     * 删除灭火器材
     */
    @PreAuthorize("@ss.hasPermi('dev:annihilator:remove')")
    @Log(title = "灭火器材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        String num = "{1}/{2}/{3}";
        if (tDevAnnihilatorService.deleteTDevAnnihilatorByIds(ids) > 0) {
            num = getNum(num);
        }
        return getAjaxResult(num);
    }

    private AjaxResult getAjaxResult(String num) {
        num = num.replace("{1}", "0");
        num = num.replace("{2}", "0");
        num = num.replace("{3}", "0");
        TPatrolMain tPatrolMain = new TPatrolMain();
        tPatrolMain.setPatrolType("1");
        tPatrolMain.setDevNum(num);
        return toAjax(tPatrolMainService.updateTPatrolMainByType(tPatrolMain));
    }

    private String getNum(String num) {
        for (TDevAnnihilator devAnnihilator : tDevAnnihilatorService.selectTDevAnnihilatorNum()) {
            if ("1".equals(devAnnihilator.getDevType())) {
                num = num.replace("{1}", devAnnihilator.getAnniNum());
            } else if ("2".equals(devAnnihilator.getDevType())) {
                num = num.replace("{2}", devAnnihilator.getAnniNum());
            } else if ("3".equals(devAnnihilator.getDevType())) {
                num = num.replace("{3}", devAnnihilator.getAnniNum());
            }
        }
        return num;
    }
}
