package com.mcwl.web.controller.myInvitation;


import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.myInvitation.domain.Invitation;
import com.mcwl.myInvitation.domain.dto.EarningsDisplayDto;
import com.mcwl.myInvitation.service.InvitationService;
import com.mcwl.myInvitation.domain.vo.EarningsDisplayVO;
import com.mcwl.system.domain.dto.WithdrawalPageRes;
import com.mcwl.system.service.ISysUserPayAccountLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

import static com.mcwl.common.core.domain.AjaxResult.success;


/**
 * 我的邀请
 */
@RestController()
@RequiredArgsConstructor
@RequestMapping("/invitation")
@Api(tags = "我的邀请")
public class InvitationController {

    private final InvitationService invitationService;

    private final ISysUserPayAccountLogService sysUserPayAccountLogService;


    /**
     * 获取邀请码
     *
     * @return 邀请码
     */
    @GetMapping("/getInvitationCode")
    @ApiOperation(value = "获取邀请码")
    public R<String> getInvitationCode() {
        // 获取当前用户
        Long userId = SecurityUtils.getUserId();
        String invitationCode = invitationService.getInvitationCode(userId);
        if (StringUtils.isEmpty(invitationCode)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"获取邀请码失败");
        }
        return R.ok("操作成功", invitationCode);
    }


    /**
     * 获取邀请列表
     *
     * @param userId 用户id
     * @return 邀请列表
     */

//    @GetMapping("/list")
//    @ApiOperation(value = "获取邀请列表")
//    public R<List<Invitation>> list(@Valid
//                                    @NotNull(message = "用户id不能为空")
//                                    @ApiParam("用户id") Long userId) {
//        List<Invitation> list = invitationService.lambdaQuery()
//                .eq(Invitation::getUserId, userId)
//                .list();
//        return R.ok(list);
//    }


    /**
     * 邀请人收益展示
     */
    @GetMapping("earningsDisplay")
    @ApiOperation(value = "邀请人收益展示")
    public R<EarningsDisplayVO> earningsDisplay() {
        Long userId = SecurityUtils.getUserId();

        EarningsDisplayVO earningsDisplayVO = new EarningsDisplayVO();

        // 获取邀请人收益
        Double totalAmount = invitationService.getTotalAmount(userId);

        // 获取我的团员
        List<EarningsDisplayDto> earningsDisplay = invitationService.getEarningsDisplay(userId);
        if (earningsDisplay == null || earningsDisplay.isEmpty()) {
            return R.ok(new EarningsDisplayVO(totalAmount, Collections.emptyList()));
        }

        earningsDisplayVO.setTotalAmount(totalAmount);
        earningsDisplayVO.setEarningsDisplayList(earningsDisplay);

        return R.ok(earningsDisplayVO);
    }


    /**
     * 累计收入金额
     */
    @GetMapping("totalAmount")
    @ApiOperation(value = "累计收入金额")
    public R<Double> totalAmount() {
        Long userId = SecurityUtils.getUserId();
        Double totalAmount = invitationService.getTotalAmount(userId);
        if (Objects.isNull(totalAmount)) {
            totalAmount = 0.0;
        }
        return R.ok(totalAmount);
    }

    /**
     * 提现记录
     */
    @PostMapping("withdrawalRecord")
    @ApiOperation(value = "提现记录")
    public TableDataInfo withdrawalRecord(@Valid @RequestBody WithdrawalPageRes withdrawalPageRes) {

        return sysUserPayAccountLogService.getWithdrawalRecord(withdrawalPageRes);
    }


}
