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.SysConfigDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.DemoGroupApplyDto;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.service.DemoGroupService;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.common.controller.BaseController;
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.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/25
 */
@Api(tags = "试听课")
@RequestMapping("demoGroup")
@RestController
public class DemoGroupController extends BaseController {

    @Autowired
    private DemoGroupService demoGroupService;
    @Autowired
    private SysConfigDao sysConfigDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "试听课申请")
    @PostMapping("/demoGroupApply")
    public Object demoGroupApply(@RequestBody DemoGroupApplyDto demoGroup){
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Teacher teacher = teacherDao.get(user.getId());
        String organId = teacher.getTeacherOrganId().toString();
        if(StringUtils.isNotEmpty(teacher.getFlowOrganRangeId())){
            organId += "," + teacher.getFlowOrganRangeId();
        }
        demoGroup.setUserId(user.getId());
        demoGroup.setOrganIdList(organId);
        demoGroupService.demoGroupApply(demoGroup);
        return succeed();
    }

    @ApiOperation(value = "获取试听课课程时长")
    @GetMapping("/getSingleClassMinutes")
    public Object getSingleClassMinutes(){
        return succeed(sysConfigDao.findConfigValue(SysConfigService.DEMO_GROUP_SINGLE_CLASS_MINUTES_).split(","));
    }

    @ApiOperation(value = "获取老师试听课")
    @GetMapping("/getDemoGroupByTeacherId")
    public Object getDemoGroupByTeacherId() throws Exception {
        return succeed(demoGroupService.getDemoGroupByTeacherId());
    }

    @ApiOperation(value = "获取指定老师试听课")
    @GetMapping("/findByTeacherId")
    public Object findByTeacherId(Date startTime, Date endTime){
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(demoGroupService.findByTeacherId(user.getId(),startTime,endTime));
    }

}
