package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.MemberPayParamDto;
import com.ym.mec.biz.dal.entity.MemberRankSetting;
import com.ym.mec.biz.dal.page.CloudTeacherOrderQueryInfo;
import com.ym.mec.biz.service.CloudTeacherOrderService;
import com.ym.mec.biz.service.MemberFeeSettingService;
import com.ym.mec.biz.service.MemberRankPrivilegesService;
import com.ym.mec.biz.service.MemberRankSettingService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("memberRank")
@Api(tags = "会员收费设置服务")
@RestController
public class MemberRankController extends BaseController {

    @Autowired
    private MemberFeeSettingService memberFeeSettingService;

    @Autowired
    private MemberRankSettingService memberRankSettingService;

    @Autowired
    private MemberRankPrivilegesService memberRankPrivilegesService;

    @Autowired
    private CloudTeacherOrderService cloudTeacherOrderService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "获取所有的收费标准")
    @GetMapping("/queryList")
    public Object getMemberDefaultFee() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        List<MemberRankSetting> result = memberRankSettingService.queryListByIsDefault(false);

        for (MemberRankSetting mrs : result) {
            mrs.setMemberFeeSetting(memberFeeSettingService.findByRankIdAndOrganId(sysUser.getOrganId(), mrs.getId()));
            mrs.setMemberPrivilegesItemList(memberRankPrivilegesService.queryByMemberRankId(mrs.getId()));
        }
        return succeed(result);
    }

    @ApiOperation(value = "购买会员(用于后台创建的团练宝订单支付)")
    @PostMapping("/buy")
    public Object buy(@RequestBody MemberPayParamDto memberPayParamDto) throws Exception {
        return memberRankSettingService.buy(memberPayParamDto);
    }

    @ApiOperation(value = "购买会员（学生端自主购买团练宝会员）")
    @PostMapping("/buy1")
    public Object buy1(@RequestBody MemberPayParamDto memberPayParamDto) throws Exception {
        return memberRankSettingService.buy1(memberPayParamDto);
    }

    /**
     * 齐齐哈尔购买团练宝-默认激活
     * <p> 购买规则
     * <p>活动分部：
     * 齐齐哈尔二组，编号59
     * <p>
     * 活动内容：
     * <p>1. 团练宝按月购买，学员可自行选择购买多少个月，单次购买达到一定数量进行赠送，无需激活，支付后立即生效
     * <p>2. 单月价格：150元/月
     * <p>3. 单次购买2个月赠送1个月
     * <p>4. 单次购买4个月赠送3个月
     * <p>5. 单次购买6个月赠送6个月
     * <p>6. 每笔订单仅按能够满足赠送条件的最高档赠送，不可叠加，
     * <p>例：学员单次购买12个月，则赠送6个月，共18个月；学员单次购买5个月，则赠送3个月，共8个月
     * <p>7. 每笔订单独立计算，多比订单不可叠加，
     * <p>例：学员三笔订单，每笔1个月，不赠送
     * <p>8. 每笔订单达到赠送条件都可赠送，
     * <p>例：学员两笔订单都买的6个月，则两笔订单都送6个月
     * <p>活动时间:
     * <p>1. 2022年2月8日上线预热
     * <p>2. 2022年2月15日开始销售
     * <p>3. 暂无结束时间，长期有效
     *
     * @return
     */
    @ApiOperation(value = "购买会员")
    @PostMapping("/buy2Qqhe")
    public Object buy2Qqhe(@RequestBody MemberPayParamDto memberPayParamDto) throws Exception {
        return memberRankSettingService.buy2Qqhe(memberPayParamDto);
    }

    @ApiOperation(value = "查询是否有待支付的活动订单")
    @GetMapping("/queryBuy2Qqhe")
    public Object queryBuy2Qqhe() {
        return memberRankSettingService.queryBuy2Qqhe();
    }

    @ApiOperation(value = "关闭活动订单")
    @GetMapping("/closeBuy2Qqhe")
    public Object closeBuy2Qqhe(String orderNo) throws Exception {
        return memberRankSettingService.closeBuy2Qqhe(orderNo);
    }

    @ApiOperation(value = "2022小小训练营活动购买会员")
    @PostMapping("/activeBuy")
    public HttpResponseResult ActiveBuy(@RequestBody MemberPayParamDto memberPayParamDto) throws Exception {
		return memberRankSettingService.activeBuy(memberPayParamDto);
    }

    @ApiOperation(value = "2022小小训练营活动购买会员")
    @PostMapping("/checkActiveBuy")
    public HttpResponseResult<Integer> checkActiveBuy(@RequestBody MemberPayParamDto memberPayParamDto) throws Exception {
		return memberRankSettingService.checkActiveBuy(memberPayParamDto);
    }

    @ApiOperation(value = "查询订单列表")
    @GetMapping("/queryOrderPage")
    public Object queryOrderPage(CloudTeacherOrderQueryInfo queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        queryInfo.setStudentId(user.getId());
        return succeed(cloudTeacherOrderService.queryPage(queryInfo));
    }

    @ApiOperation(value = "获取学员购买的云教练活动订单")
    @GetMapping("/queryActiveOrderPage")
    public Object queryActiveOrderPage() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(cloudTeacherOrderService.queryActiveOrderPage(user.getId(), 202203, null));
    }

}
