package com.ym.mec.web.controller.education;

import com.ym.mec.biz.dal.dao.VipGroupDefaultClassesUnitPriceDao;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.VipGroupDefaultClassesUnitPrice;
import com.ym.mec.biz.dal.page.ActivityUserQueryInfo;
import com.ym.mec.biz.service.ActivityUserMapperService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Api(tags = "学员活动排课记录服务")
@RequestMapping("eduActivityUserMapper")
@RestController
public class EduActivityUserMapperController extends BaseController {

    @Autowired
    private ActivityUserMapperService activityUserMapperService;
    @Autowired
    private VipGroupDefaultClassesUnitPriceDao vipGroupDefaultClassesUnitPriceDao;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "获取老师待排课课程类型列表")
    @PostMapping("/queryWaitCourseCategory")
    public HttpResponseResult<List<CourseFormDto>> queryWaitCourseCategory(){
        return succeed(activityUserMapperService.queryWaitCourseCategory(null,organizationService.getEmployeeOrgan(null)));
    }

    @ApiOperation(value = "获取活动排课学员列表")
    @PostMapping("/queryActivityStudentList")
    public HttpResponseResult<List<ActivityUserDto>> queryActivityStudentList(@RequestBody ActivityUserQueryInfo activityUserQueryInfo){
        if(activityUserQueryInfo.getCategoryId() == null){
            throw new BizException("请选择课程类型");
        }
        if(activityUserQueryInfo.getCategoryId().equals(0)){
            activityUserQueryInfo.setCategoryId(null);
        }
        activityUserQueryInfo.setOrganIds(organizationService.getEmployeeOrgan(activityUserQueryInfo.getOrganIds()));
        return succeed(activityUserMapperService.queryActivityStudentList(activityUserQueryInfo));
    }

    @ApiOperation(value = "获取活动排课可排课时长列表")
    @PostMapping("/queryActivityCourseTimeList")
    public HttpResponseResult<List<Integer>> queryActivityCourseTimeList(@RequestBody ActivityUserQueryInfo activityUserQueryInfo){
        if(activityUserQueryInfo.getCategoryId() == null){
            throw new BizException("请选择课程类型");
        }
        if(activityUserQueryInfo.getCategoryId().equals(0)){
            activityUserQueryInfo.setCategoryId(null);
        }
        activityUserQueryInfo.setOrganIds(organizationService.getEmployeeOrgan(activityUserQueryInfo.getOrganIds()));
        return succeed(activityUserMapperService.queryActivityCourseTimeList(activityUserQueryInfo));
    }

    @ApiOperation(value = "管理端VIP活动排课")
    @PostMapping("/createVipGroup")
    public HttpResponseResult createVipGroup(@RequestBody VipGroupApplyDto vipGroupApplyDto){
        VipGroupApplyBaseInfoDto applyBaseInfo = vipGroupApplyDto.getVipGroupApplyBaseInfo();
        if(Objects.isNull(applyBaseInfo.getUserId())){
            throw new BizException("请选择指导老师");
        }
        applyBaseInfo.setEducationalTeacherId(sysUserService.getUserId());
        for (CourseSchedule courseSchedule : vipGroupApplyDto.getCourseSchedules()) {
            courseSchedule.setEndClassTime(DateUtil.addMinutes(courseSchedule.getStartClassTime(), applyBaseInfo.getSingleClassMinutes()));
        }
        VipGroupDefaultClassesUnitPrice vipGroupDefaultClassesUnitPrice = vipGroupDefaultClassesUnitPriceDao.getByVipGroupCategory(applyBaseInfo.getVipGroupCategoryId(), applyBaseInfo.getOrganId());
        if(Objects.nonNull(vipGroupDefaultClassesUnitPrice)){
            applyBaseInfo.setOnlineClassesUnitPrice(vipGroupDefaultClassesUnitPrice.getOnlineClassesUnitPrice());
            applyBaseInfo.setOfflineClassesUnitPrice(vipGroupDefaultClassesUnitPrice.getOfflineClassesUnitPrice());
        }
        return activityUserMapperService.createActivityVipGroup(vipGroupApplyDto);
    }

    @ApiOperation(value = "教师端网管课活动排课")
    @PostMapping("/createPracticeGroup")
    public Object createPracticeGroup(@RequestBody PracticeGroupApplyDto practiceGroupApplyDto){
        PracticeGroupApplyBaseInfoDto applyBaseInfo = practiceGroupApplyDto.getPracticeGroupApplyBaseInfoDto();
        if(Objects.isNull(applyBaseInfo.getUserId())){
            throw new BizException("请选择指导老师");
        }
        for (CourseSchedule courseSchedule : practiceGroupApplyDto.getCourseSchedules()) {
            courseSchedule.setEndClassTime(DateUtil.addMinutes(courseSchedule.getStartClassTime(), applyBaseInfo.getSingleClassMinutes()));
        }
        return activityUserMapperService.createActivityPracticeGroup(practiceGroupApplyDto);
    }
}
