package com.fzm.mall.server.admin.goods_center.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.goods_center.entity.dto.ExternalTokenDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.ExternalTokenVO;
import com.fzm.mall.server.admin.goods_center.service.IExternalTokenService;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 外部商品通证 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-08-10
 */
@Api(tags = "商品中心-外部商品通证")
@RestController
@RequestMapping("/externalToken")
public class ExternalTokenController {

    @Autowired
    private IExternalTokenService externalTokenService;

    @Autowired
    private ResponseFactory resFac;

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "商品中心-外部商品通证-列表查询")
    @ApiOperationSupport(order = 1)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum,
                           @ApiParam(value = "标识名称") @RequestParam(required = false) String labelName,
                           @ApiParam(value = "绑定商品编号") @RequestParam(required = false) String goodsId,
                           @ApiParam(value = "绑定商品名称") @RequestParam(required = false) String goodsName,
                           @ApiParam(value = "状态(0.未绑定 1.已绑定)") @RequestParam(required = false) Integer status) {
        Page<ExternalTokenDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        Admin admin = adminService.getByAdminId(adminId);
        IPage<ExternalTokenDTO> result = externalTokenService.page(page, labelName, goodsId, goodsName, status, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "商品中心-外部商品通证-二维码查询")
    @ApiOperationSupport(order = 2)
    @GetMapping("/read")
    public ResponseVO read(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);

        ExternalTokenVO result = externalTokenService.read(admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }
}
