package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dao.VipGroupCategoryDao;
import com.ym.mec.biz.dal.dto.StudentVipDouble11Dto;
import com.ym.mec.biz.dal.dto.VipGroupApplyDto;
import com.ym.mec.biz.dal.dto.VipGroupCostCountParamsDto;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.entity.VipGroupCategory;
import com.ym.mec.biz.dal.page.StudentManageQueryInfo;
import com.ym.mec.biz.service.StudentManageService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2019/9/21
 */
@Api(tags = "vip课-教师端")
@RequestMapping("teacherVipGroup")
@RestController
public class TeacherVipGroupController extends BaseController {

	@Autowired
	private VipGroupService vipGroupService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private StudentManageService studentManageService;
	@Autowired
	private VipGroupCategoryDao vipGroupCategoryDao;

	@ApiOperation("vip课申请")
	@PostMapping("/vipGroupApply")
	public Object vipGroupApply(@RequestBody VipGroupApplyDto vipGroupApplyDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Integer userId = user.getId();
		Teacher teacher = teacherDao.get(userId);
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setUserId(userId);
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setTeacherId(Long.valueOf(userId));
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganIdList(teacher.getTeacherOrganId().toString());
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganId(teacher.getTeacherOrganId());
		vipGroupService.createVipGroup(vipGroupApplyDto);
		return succeed();
	}

	@ApiOperation("vip课申请-双11")
	@PostMapping("/vipGroupApplyForDouble11")
	@Transactional(rollbackFor = Exception.class)
	public Object vipGroupApplyForDouble11(@RequestBody VipGroupApplyDto vipGroupApplyDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Integer userId = user.getId();
		Teacher teacher = teacherDao.get(userId);
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setUserId(userId);
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setTeacherId(Long.valueOf(userId));
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganIdList(teacher.getTeacherOrganId().toString());
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganId(teacher.getTeacherOrganId());
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setRegistrationStartTime(DateUtil.addDays(new Date(), -1));
		vipGroupApplyDto.getVipGroupApplyBaseInfo().setPaymentExpireDate(vipGroupApplyDto.getVipGroupApplyBaseInfo().getRegistrationStartTime());
		vipGroupApplyDto.setOnlyProgress(true);

		VipGroupCategory vipGroupCategory = vipGroupCategoryDao.get(vipGroupApplyDto.getVipGroupApplyBaseInfo().getVipGroupCategoryId());
		if(Objects.isNull(vipGroupCategory)){
			return failed("课程形式错误");
		}

		List<Integer> studentIds = Arrays.stream(vipGroupApplyDto.getVipGroupApplyBaseInfo().getStudentIdList().split(",")).map(Integer::valueOf).collect(Collectors.toList());
		List<StudentVipDouble11Dto> studentDouble11Info = studentManageService.getStudentDouble11Info(studentIds);
		Map<Integer, StudentVipDouble11Dto> idStudentInfoMap = new HashMap<>();
		if(!CollectionUtils.isEmpty(studentDouble11Info)){
			idStudentInfoMap = studentDouble11Info.stream().collect(Collectors.toMap(StudentVipDouble11Dto::getUserId, s->s, (s1, s2)->s1));
		}
		for (Integer studentId : studentIds) {
			StudentVipDouble11Dto studentVipDouble11Dto = idStudentInfoMap.get(studentId);
			if(Objects.isNull(studentVipDouble11Dto)){
				return failed("部分学员可排课次不足");
			}
			boolean enableApply = false;
			switch (vipGroupCategory.getStudentNum()){
				case 1:
					enableApply = studentVipDouble11Dto.getCourseOneToOneTime()>=vipGroupApplyDto.getVipGroupApplyBaseInfo().getTotalClassTimes();
					break;
				case 2:
					enableApply = studentVipDouble11Dto.getCourseOneToTwoTime()>=vipGroupApplyDto.getVipGroupApplyBaseInfo().getTotalClassTimes();
					break;
				default:

					break;
			}
			if(!enableApply){
				return failed("部分学员可排课次不足");
			}
		}

		vipGroupService.createVipGroup(vipGroupApplyDto);
		vipGroupService.addVipGroupStudents(vipGroupApplyDto.getVipGroupApplyBaseInfo().getId(), studentIds);
		return succeed();
	}

	@ApiOperation("获取vip课课程和课酬总费用")
	@PostMapping("/getVipGroupCostCount")
	public Object getVipGroupCostCount(VipGroupCostCountParamsDto vipGroupCostCountParamsDto) {
		// return succeed(vipGroupService.vipGroupCostCount(vipGroupCostCountParamsDto));
		return succeed();
	}

	@ApiOperation("获取vip课教学点")
	@PostMapping("/getSchools")
	public Object getSchools() {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(vipGroupService.querySchoolByUserId(user.getId()));
	}

	@ApiOperation(value = "获取教师所在分部学生列表")
	@GetMapping("/queryStudentsWithTeacher")
	public Object queryStudentsWithTeacher(StudentManageQueryInfo queryInfo){
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		queryInfo.setTeacherId(user.getId());
		return succeed(studentManageService.queryStudentsWithTeacher(queryInfo));
	}

	@ApiOperation(value = "获取参与双十一活动学员列表")
	@GetMapping("/queryDouble11Students")
	public HttpResponseResult queryDouble11Students(StudentManageQueryInfo queryInfo){
		SysUser user = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(user)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		queryInfo.setTeacherId(user.getId());
		return succeed(studentManageService.queryDouble11Students(queryInfo));
	}

}
