package com.miaxis.app.controller.extension;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.extension.dto.ExtensionSettleDTO;
import com.miaxis.extension.service.IWxExtensionIncomeService;
import com.miaxis.extension.vo.ExtensionIncomeVo;
import com.miaxis.spread.domain.WxSpreadRelation;
import com.miaxis.spread.service.IWxSpreadRelationService;
import com.miaxis.user.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/11/29 15:58
 */
@RestController
@RequestMapping(Constants.STUDENT_PREFIX + "/extension/income")
@Api(tags={"【H5-推广积分】"})
@AllArgsConstructor
public class WxExtensionIncomeController extends BaseController {

    private final IWxExtensionIncomeService extensionIncomeService;

    private final IUserInfoService userInfoService;

    private final IWxSpreadRelationService wxSpreadRelationService;



    /**
     * 查询收益金额（总金额、已提现金额、未提现金额）
     */
    @GetMapping(value = "/extensionIncomePrice")
    @ApiOperation("对下级结算")
    public Response extensionIncomePrice(ExtensionSettleDTO extensionSettleDTO) {
        UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
        //查询该下级用户的上级是否为当前用户
        WxSpreadRelation wxSpreadRelation = wxSpreadRelationService.getOne(new QueryWrapper<WxSpreadRelation>().eq("openid", extensionSettleDTO.getOpenid()));
        if (wxSpreadRelation!= null && wxSpreadRelation.getParentOpenid().equals(userInfo.getOpenid())){
            //进行结算
            UserInfo downUserInfo = userInfoService.getOne(new QueryWrapper<UserInfo>().eq("openid", extensionSettleDTO.getOpenid()));
            if (downUserInfo.getAchievement()< extensionSettleDTO.getSettlePoints()){
                throw new CustomException("积分不足");
            }
            //更新积分
            downUserInfo.setAchievement(downUserInfo.getAchievement()-extensionSettleDTO.getSettlePoints());
            downUserInfo.setAchievementSettled(downUserInfo.getAchievementSettled()+extensionSettleDTO.getSettlePoints());
            userInfoService.updateById(downUserInfo);
        }else{
            throw new CustomException("该下级不存在或不是本用户的下级");
        }
        return Response.success();
    }


    /**
     * 查询下级推广积分
     */
    @GetMapping(value = "/extensionPoints")
    @ApiOperation("查询下级推广积分")
    public Response<List<ExtensionIncomeVo>> getDownLevelPoints() {
        UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
        List<ExtensionIncomeVo> list = userInfoService.getDownLevelPoints(userInfo.getOpenid());
        return Response.success(list);
    }



}
