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.ConditionDto;
import com.ym.mec.biz.dal.dto.QueryConditionDto;
import com.ym.mec.biz.dal.dto.VipGroupBuyParamsDto;
import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.enums.OrderTypeEnum;
import com.ym.mec.biz.dal.page.StudentVipGroupQueryInfo;
import com.ym.mec.biz.service.StudentPaymentOrderService;
import com.ym.mec.biz.service.SubjectService;
import com.ym.mec.biz.service.VipGroupCategoryService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Joburgess
 * @Date 2019/9/23
 */

@Api(tags = "vip课-学生端")
@RequestMapping("studentVipGroup")
@RestController
public class StudentVipGroupController extends BaseController {

    @Autowired
    private VipGroupService vipGroupService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VipGroupCategoryService vipGroupCategoryService;
    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;

    @ApiOperation(value = "获取vip课列表筛选条件")
    @GetMapping(value = "/findQueryCondition")
    public Object findQueryCondition(){
        List<QueryConditionDto> conditions=new ArrayList<>();
        conditions.add(new QueryConditionDto("乐器选择","subjectId",subjectService.findSubjectConditions()));
        conditions.add(new QueryConditionDto("课程类型","categoryId",vipGroupCategoryService.findConditionCategorys()));
        List<ConditionDto> conditionDtos=new ArrayList<>();
        conditionDtos.add(new ConditionDto(0L,"线下"));
        conditionDtos.add(new ConditionDto(1L,"线上"));
        conditions.add(new QueryConditionDto("类别选择","classType",conditionDtos));
        return succeed(conditions);
    }

    @ApiOperation(value = "获取vip课列表")
    @PostMapping(value = "/queryVipGroups")
    public Object queryVipGroups(@RequestBody StudentVipGroupQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (null == sysUser) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        queryInfo.setOrganId(sysUser.getOrganId());
        queryInfo.setUserId(sysUser.getId());
        Map<String,Object> result=new HashMap<>();
        result.put("recommendVipGroups",new ArrayList<>());
        result.put("pageInfo",vipGroupService.findStudentVipGroupList(queryInfo));
        return succeed(result);
    }

    @ApiOperation(value = "获取学生端vip课显示详情")
    @GetMapping(value = "/getVipGroupShowDetail")
    public Object getVipGroupShowDetail(Long vipGroupId){
        return succeed(vipGroupService.getVipGroupShowDetail(vipGroupId));
    }

    @ApiOperation(value = "检测vip课成能否购买")
    @GetMapping(value = "/enableBuyGroup")
    public Object enableBuyGroup(Integer vipGroupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (null == sysUser) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        vipGroupService.enableBuyVipGroup(vipGroupId,sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "vip课购买")
    @PostMapping("/buyVipGroup")
    public Object buyVipGroup(VipGroupBuyParamsDto vipGroupBuyParams) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (null == sysUser) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(!vipGroupBuyParams.isRepeatPay()){
            // 判断是否存在支付中的记录
            List<StudentPaymentOrder> list = studentPaymentOrderService
                    .queryByCondition(GroupType.VIP,
                            vipGroupBuyParams.getVipGroupId().toString(),
                            sysUser.getId(),
                            DealStatusEnum.ING,
                            OrderTypeEnum.SMALL_CLASS_TO_BUY);
            if (list.size() > 0) {
                return failed(HttpStatus.CONTINUE, "您有待支付的订单");
            }
        }
        return succeed(vipGroupService.buyVipGroup(vipGroupBuyParams));
    }

    @ApiOperation(value = "vip课退课")
    @PostMapping("/applyRefund")
	public Object applyRefund(Long vipGroupId) {
		vipGroupService.applyRefund(vipGroupId);
		return succeed();
	}

    @ApiOperation(value = "获取vip购买信息")
    @PostMapping("/getVipGroupPayInfo")
	public Object getVipGroupPayInfo(Integer vipGroupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (null == sysUser) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(vipGroupService.getVipGroupPayInfo(vipGroupId,sysUser.getId()));
    }

}
