package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.Student;
import com.ym.mec.biz.dal.entity.StudentPreRegistration;
import com.ym.mec.biz.service.StudentService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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 javax.annotation.Resource;
import java.util.Date;

@RequestMapping("student")
@Api(tags = "学生服务")
@RestController
public class StudentController extends BaseController {

    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentService studentService;

    @ApiOperation("注册")
    @PostMapping(value = "/registering")
    public Object registering(@RequestBody StudentPreRegistration studentPreRegistration) {
    	// 判断用户是否存在
        SysUser user = sysUserFeignService.queryUserByMobile(studentPreRegistration.getPhone());
        if(user != null && user.getId() != null){
        	if(user.getUserType().contains("STUDENT")){
        		return succeed("您已注册，请直接下载APP！");
        	}
        }
        return studentService.register(studentPreRegistration) ? succeed() : failed();
    }

	@ApiOperation("更换声部")
	@PostMapping(value = "/updateSubject")
	public Object updateSubject(Integer subjectId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Student student = studentService.get(user.getId());

		if (student != null) {
			student.setSubjectIdList(subjectId + "");
			student.setUpdateTime(new Date());
			studentService.update(student);
		}
		return succeed();
	}
}
