package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.PracticeGroupSellPriceDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.PracticeGroupApplyBaseInfoDto;
import com.ym.mec.biz.dal.dto.PracticeGroupApplyDto;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.service.PracticeGroupService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
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.*;

import java.util.Map;
import java.util.Objects;

import static com.ym.mec.biz.dal.enums.PracticeGroupType.CHARGE;

@Api(tags = "网管课-教师端")
@RequestMapping("teacherPracticeGroup")
@RestController
public class TeacherPracticeGroupController extends BaseController {

	@Autowired
	private PracticeGroupService practiceGroupService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private PracticeGroupSellPriceDao practiceGroupSellPriceDao;

	@ApiOperation("获取网管课分部价格")
	@RequestMapping("/queryPracticeGroupSellPrice")
	public Object queryPracticeGroupSellPrice(){
		Teacher teacher = teacherDao.get(sysUserService.getUserId());
		if(Objects.isNull(teacher)){
			return failed("请指定指导老师!");
		}
		return succeed(practiceGroupSellPriceDao.get(teacher.getTeacherOrganId()));
	}

	@ApiOperation("课酬总费用")
	@GetMapping("/getPracticeGroupCostCount")
	public Object getPracticeGroupCostCount(){
		Integer userId = sysUserService.getUserId();
		Teacher teacher = teacherDao.get(userId);
		PracticeGroupApplyBaseInfoDto practice = new PracticeGroupApplyBaseInfoDto();
		practice.setUserId(userId);
		practice.setOrganId(teacher.getOrganId());
		Map results = practiceGroupService.countPracticeGroupPredictFee(practice,practice.getUserId());
		return succeed(results);
	}


	@ApiOperation(value = "网管课申请")
	@PostMapping("/practiceGroupApply")
	public Object practiceGroupApply(@RequestBody PracticeGroupApplyDto practiceGroupApplyDto){
		PracticeGroupApplyBaseInfoDto applyBaseInfoDto = practiceGroupApplyDto.getPracticeGroupApplyBaseInfoDto();
		if(Objects.isNull(applyBaseInfoDto.getOrganId())){
			SysUser student = teacherDao.getUser(applyBaseInfoDto.getStudentId());
			if(Objects.isNull(student)||Objects.isNull(student.getOrganId())){
				return failed("学员信息异常");
			}
			applyBaseInfoDto.setOrganId(student.getOrganId());
		}
		applyBaseInfoDto.setType(CHARGE);
		applyBaseInfoDto.setMemo("指导老师老师端申请网管课");
		applyBaseInfoDto.setUserId(sysUserService.getUserId());
		return practiceGroupService.createPracticeGroup(practiceGroupApplyDto);
	}

}
