package com.zd.airbottle.controller;

import com.zd.airbottle.domain.QpRfidTag;
import com.zd.airbottle.service.IQpRfidTagService;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * RFID标签管理Controller
 *
 * @author zd
 * @date 2022-06-06
 */
@RestController
@Api(tags = "【RFID标签管理】")
@RequestMapping("/tag")
public class QpRfidTagController extends BaseController<QpRfidTag> {
    @Autowired
    private IQpRfidTagService qpRfidTagService;

    /**
     * 查询RFID标签管理列表
     */
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询RFID标签管理列表")
    public TableDataInfo<QpRfidTag> list(QpRfidTag qpRfidTag) {
        startPage();
        List<QpRfidTag> list = qpRfidTagService.selectQpRfidTagList(qpRfidTag);
        return getDataTable(list);
    }

    /**
     * 导出RFID标签管理列表
     */
    @ApiOperation(value = "导出RFID标签管理列表")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.EXPORT)
    @Log(title = "RFID标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QpRfidTag qpRfidTag) throws IOException {
        List<QpRfidTag> list = qpRfidTagService.selectQpRfidTagList(qpRfidTag);
        ExcelUtil<QpRfidTag> util = new ExcelUtil<>(QpRfidTag.class);
        util.exportExcel(response, list, "RFID标签管理数据");
    }

    /**
     * 获取RFID标签管理详细信息
     */
    @ApiOperation(value = "获取RFID标签管理详细信息")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<QpRfidTag> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(qpRfidTagService.selectQpRfidTagById(id));
    }

    /**
     * 新增RFID标签管理
     */
    @ApiOperation(value = "新增RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.ADD)
    @Log(title = "RFID标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody QpRfidTag qpRfidTag) {
        String tagCode = qpRfidTag.getTagCode();
        if (qpRfidTagService.selectCountTag(tagCode.toUpperCase())>0){
           return ResultData.fail("该标签已经存在，请勿重复录入，如需补打二维码请点击下方按钮。");
        }
        return ResultData.result(qpRfidTagService.insertQpRfidTag(qpRfidTag));
    }

    /**
     * 修改RFID标签管理
     */
    @ApiOperation(value = "修改RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.EDIT)
    @Log(title = "RFID标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody QpRfidTag qpRfidTag) {
        return ResultData.result(qpRfidTagService.updateQpRfidTag(qpRfidTag));
    }

    /**
     * 删除RFID标签管理
     */
    @ApiOperation(value = "删除RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.AIRBOTTLE_TAG+ PerFun.REMOVE)
    @Log(title = "RFID标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        return ResultData.result(qpRfidTagService.deleteQpRfidTagByIds(ids));
    }
}
