/*
 * 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.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.DistributionUserWalletBillDto;
import com.yami.shop.distribution.comment.dto.DistributionUserWalletDto;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.distribution.comment.service.DistributionUserWalletBillService;
import com.yami.shop.distribution.comment.service.DistributionUserWalletService;
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;

import java.util.Objects;

@RestController
@RequestMapping("/p/distribution/wallet")
@Api(tags = "分销钱包接口")
@AllArgsConstructor
public class DistributionUserWalletController {


    private final DistributionUserWalletService distributionUserWalletService;

    private final DistributionUserService distributionUserService;

    private final DistributionUserWalletBillService distributionUserWalletBillService;

    @GetMapping("/info")
    @ApiOperation(value = "查看分销员钱包数据", notes = "")
    public ResponseEntity<DistributionUserWalletDto> getDistributionUserWalletDtoByDistributionUserId() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            throw new GlobalException("您还不是销售员");
        }
        return ResponseEntity.ok(distributionUserWalletService.getDistributionUserWalletDtoByDistributionUserId(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/page")
    @ApiOperation(value = "查看分销员钱包流水记录", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "orderBy", value = "时间排序(0升序 1降序) 默认1", required = false, dataType = "Integer")
    })
    public ResponseEntity<IPage<DistributionUserWalletBillDto>> getDistributionUserWalletBillDtoPage(PageParam<DistributionUserWalletBillDto> page,
                                                                                                     @RequestParam(value = "orderBy", defaultValue = "1") Integer orderBy) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ResponseEntity.ok(distributionUserWalletBillService
                .getDistributionUserWalletBillDtoPage(page, distributionUser.getDistributionUserId(), orderBy));
    }


}
