package com.zd.bottle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zd.bottle.domain.RfidTag;
import com.zd.bottle.domain.UseRecord;
import com.zd.bottle.service.RfidTagService;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
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.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * RFID标签管理 前端控制器
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@RestController
@Api(tags = "【RFID标签管理】")
@RequestMapping("/rfidTag")
public class RfidTagController extends BaseController<RfidTag> {

    @Resource
    private RfidTagService service;

    /**
     * 查询RFID标签管理列表
     * @param rfidTag 参数实体
     * @return TableDataInfo<RfidTag>
     */
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询RFID标签管理列表")
    public TableDataInfo<RfidTag> list(RfidTag rfidTag) {
        startPage("create_time","descending");
        List<RfidTag> list = service.list(rfidTag);
        return getDataTable(list);
    }

    /**
     * 导出RFID标签管理列表
     * @param response 响应对象
     * @param rfidTag 参数实体
     * @throws IOException 异常
     */
    @ApiOperation(value = "导出RFID标签管理列表")
//    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.EXPORT)
    @Log(title = "RFID标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RfidTag rfidTag) throws IOException {
        LambdaQueryWrapper<RfidTag> wrapper = Wrappers.lambdaQuery();
        wrapper.setEntity(rfidTag);
        List<RfidTag> list = service.list(wrapper);
        ExcelUtil<RfidTag> util = new ExcelUtil<>(RfidTag.class);
        util.exportExcel(response, list, "RFID标签管理数据");
    }

    /**
     * 获取RFID标签管理详细信息
     * @param id 主键
     * @return RfidTag
     */
    @ApiOperation(value = "获取RFID标签管理详细信息")
//    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<RfidTag> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(service.getById(id));
    }

    /**
     * 新增RFID标签管理
     * @param rfidTag 参数实体
     * @return Boolean
     */
    @ApiOperation(value = "新增RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.ADD)
    @Log(title = "RFID标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<RfidTag> add(@RequestBody RfidTag rfidTag) {
        String tagCode = rfidTag.getTagCode();
        LambdaQueryWrapper<RfidTag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RfidTag::getTagCode,tagCode);
        List<RfidTag> tags = service.list(queryWrapper);
        if (!tags.isEmpty()){
            return ResultData.fail("该标签已经存在，请勿重复录入，如需补打二维码请点击下方按钮。");
        }
        return ResultData.success(service.add(rfidTag));
    }

    /**
     * 修改RFID标签管理
     * @param rfidTag 参数实体
     * @return Boolean
     */
    @ApiOperation(value = "修改RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.EDIT)
    @Log(title = "RFID标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody RfidTag rfidTag) {
        return ResultData.result(service.updateById(rfidTag));
    }

    /**
     * 删除RFID标签管理
     * @param ids 主键集合
     * @return Boolean
     */
    @ApiOperation(value = "删除RFID标签管理")
    @PreAuthorize(hasPermi = PerPrefix.BOTTLE_TAG+ PerFun.REMOVE)
    @Log(title = "RFID标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        LambdaQueryWrapper<RfidTag> wrapper = Wrappers.lambdaQuery(RfidTag.class);
        wrapper.in(RfidTag::getId,idList).eq(RfidTag::getIsBind,1);
        List<RfidTag> list = service.list(wrapper);
        if (!list.isEmpty()){
            List<String> authCodeList = list.stream().map(RfidTag::getAuthCode).collect(Collectors.toList());
            throw new ServiceException(String.join(",", authCodeList)+"已被绑定");
        }
        return ResultData.result(service.removeBatchByIds(idList));
    }

}
