package com.ym.mec.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.MemberRankSetting;
import com.ym.mec.biz.dal.enums.PeriodEnum;
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;

@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(Integer memberRankId, PeriodEnum periodEnum, BigDecimal amount, boolean isUseBalance) throws Exception {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		return succeed(memberRankSettingService.buy(sysUser.getId(), memberRankId, periodEnum, amount, isUseBalance));
	}
	
    @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));
    }

}
