package com.mcwl.web.controller.myInvitation;


import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.myInvitation.domain.CommissionRatio;
import com.mcwl.myInvitation.domain.Invitation;
import com.mcwl.myInvitation.domain.dto.EarningsDisplayDto;
import com.mcwl.myInvitation.domain.vo.CommissionRatioVo;
import com.mcwl.myInvitation.domain.vo.EarningsDisplayVO;
import com.mcwl.myInvitation.service.CommissionRatioService;
import com.mcwl.myInvitation.service.InvitationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 提成比例
 */
@RestController()
@RequiredArgsConstructor
@RequestMapping("/commissionRatio")
@Api(tags = "我的邀请")
public class CommissionRatioController {

    private final CommissionRatioService commissionRatioService;


    /**
     * 提成列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "提成列表")
    public R<List<CommissionRatioVo>> list() {
        List<CommissionRatioVo> list = commissionRatioService.ratioList();
        return R.ok(list);
    }

    /**
     * 提成详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "提成详情")
    public R<CommissionRatioVo> detail(@Valid @NotNull(message = "提成比例id不能为空") Long id) {
        CommissionRatioVo detail = commissionRatioService.detail(id);
        if (detail == null) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"提成比例不存在");
        }
        return R.ok(detail);
    }

    /**
     * 修改提成
     */
    @GetMapping("/update")
    @ApiOperation(value = "修改提成")
    public R<CommissionRatioVo> update(@Valid
                                       @NotNull(message = "提成比例id不能为空")
                                       Long id,
                                       @Valid
                                       @NotNull(message = "提成比例不能为空")
                                               @NumberFormat
                                       Double ratio) {

        if (ratio < 0 || ratio > 1) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"提成比例必须在0-1之间");
        }

        CommissionRatioVo detail = commissionRatioService.updateRatio(id, ratio);
        if (detail == null) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"提成比例不存在");
        }

        return R.ok(detail);
    }


}
