package com.ym.mec.student.controller;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.page.StudentVipGroupQueryInfo;
import com.ym.mec.biz.service.SubjectService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.biz.service.VipGroupCategoryService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 SysUserService sysUserService;
    @Autowired
    private VipGroupCategoryService vipGroupCategoryService;

    @ApiOperation(value = "获取vip课列表筛选条件")
    @GetMapping(value = "/findQueryCondition")
    public HttpResponseResult<List<QueryConditionDto>> 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 HttpResponseResult<Map<String, Object>> queryVipGroups(@RequestBody StudentVipGroupQueryInfo queryInfo){
        SysUser sysUser = sysUserService.getUser();
        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、网管课列表")
    @RequestMapping(value = "/queryVipPracticeGroups")
    public HttpResponseResult<List<StudentVipGroupShowListDto>> queryVipPracticeGroups(@RequestBody StudentVipGroupQueryInfo queryInfo){
        SysUser sysUser = sysUserService.getUser();
        queryInfo.setOrganId(sysUser.getOrganId());
        queryInfo.setUserId(sysUser.getId());
        return succeed(vipGroupService.queryVipPracticeGroups(queryInfo));
    }

    @ApiOperation(value = "获取学生端vip、网管课显示详情")
    @RequestMapping(value = "/getVipGroupShowDetail")
    public HttpResponseResult<StudentVipGroupDetailDto> getVipGroupShowDetail(Long vipGroupId, String groupType){
        return succeed(vipGroupService.getVipGroupShowDetail(vipGroupId,groupType));
    }

    @ApiOperation(value = "检测vip课成能否购买")
    @GetMapping(value = "/enableBuyGroup")
    public HttpResponseResult enableBuyGroup(Long vipGroupId, String groupType){
        vipGroupService.enableBuyVipGroup(vipGroupId,sysUserService.getUserId(),groupType);
        return succeed();
    }

    @ApiOperation(value = "vip课购买")
    @PostMapping("/buyVipGroup")
    @AuditLogAnnotation(operateName = "vip课购买")
    public HttpResponseResult buyVipGroup(VipGroupBuyParamsDto vipGroupBuyParams) throws Exception {
        vipGroupBuyParams.setUserId(sysUserService.getUserId());
        return vipGroupService.buyVipGroup(vipGroupBuyParams);
    }

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

    @ApiOperation(value = "获取vip购买信息")
    @PostMapping("/getVipGroupPayInfo")
	public HttpResponseResult<VipGroupPayInfoDto> getVipGroupPayInfo(Integer vipGroupId){
        return succeed(vipGroupService.getVipGroupPayInfo(vipGroupId,sysUserService.getUserId()));
    }

}
