/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.DistributionProdDto;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.po.DistributionProdPO;
import com.yami.shop.distribution.comment.service.DistributionProdService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分销商品接口
 */
@RestController
@RequestMapping("/p/distribution/prod")
@Api(tags = "分销商品接口")
@AllArgsConstructor
public class DistributionProdController {

    private final DistributionProdService distributionProdService;

    private final DistributionUserService distributionUserService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查看分销商品", notes = "根据店铺和分销员id获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", dataType = "Long"),
            @ApiImplicitParam(name = "prodName", value = "商品名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序(1时间排序 2销量排序 3价格排序) 默认1", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "orderBy", value = "排序(0升序 1降序) 默认1", required = false, dataType = "Integer"),
    })
    public ResponseEntity<IPage<DistributionProdDto>> page(@RequestParam(value = "shopId", required = false, defaultValue = "0") Long shopId,
                                                           @RequestParam(value = "prodName") String prodName,
                                                           @RequestParam(value = "sort", defaultValue = "1") Integer sort,
                                                           @RequestParam(value = "orderBy", defaultValue = "1") Integer orderBy,
                                                           PageParam<DistributionProdDto> page) {
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        page.setRecords(distributionProdService.distributionProdDtoPage(page, prodName, sort, orderBy, distributionUser));
        return ResponseEntity.ok(page);
    }

    @GetMapping("/isDistributionProd")
    @ApiOperation(value = "根据商品id查看该商品是否为分销商品", notes = "根据商品id获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prodId", value = "商品id", required = true, dataType = "Long")
    })
    public ResponseEntity<Boolean> isDistributionProd(Long prodId) {

        DistributionProdPO distributionProdPO = distributionProdService.getDistributionProdPOByProdId(prodId);

        return ResponseEntity.ok(distributionProdPO != null);
    }


}
