package com.yonge.cooleshow.tenant.controller.open;

import com.yonge.cooleshow.biz.dal.dto.req.TeacherSubmitReq;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
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.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.validation.Valid;

/**
 * @author 袁亮
 * @apiNote 二维码方式注册老师账号
 */
@RestController
@RequestMapping("${app-config.url.tenant:}/open/teacher")
@Api(value = "教师表", tags = "教师表")
public class OpenTeacherController extends BaseController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TenantInfoService tenantInfoService;

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperation(value = "新增", notes = "传入teacher")
    public HttpResponseResult<Boolean> submit(@Valid @RequestBody TeacherSubmitReq teacherSubmitReq) {

        // 验证码不能为空
        if (StringUtils.isBlank(teacherSubmitReq.getCode())) {
            throw new BizException("验证码不能为空");
        }

        Long tenantId = teacherSubmitReq.getTenantId();
        if (tenantId == null) {
            throw new BizException("未选择机构");
        }
        TenantInfo tenantInfo = tenantInfoService.getById(tenantId);
        if (tenantInfo == null || Boolean.FALSE.equals(tenantInfo.getEnableFlag())) {
            throw new BizException("机构不可用");
        }
        teacherSubmitReq.setUserId(null);
        return teacherService.submit(teacherSubmitReq);
    }
}
