package com.qmth.distributed.print.api;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.SsoService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.bean.params.OpenParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 * 单点登录 前端控制器
 * </p>
 *
 * @author xf
 */
@Api(tags = "单点登录Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX  + SystemConstant.PREFIX_URL_EXAM + "/sso")
public class SsoController {

    @Autowired
    SsoService ssoService;
    @Resource
    SysOrgService sysOrgService;

    @ApiOperation(value = "题库单点登录")
    @ApiResponses({@ApiResponse(code = 200, message = "题库单点登录", response = Result.class)})
    @RequestMapping(value = "/question_library_login", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SSO)
    public Result questionLibraryLogin(@ApiParam(value = "登录名", required = true) @RequestParam String loginName,
                                       @ApiParam(value = "真实姓名", required = true) @RequestParam String realName,
                                       @ApiParam(value = "角色", required = true) @RequestParam RoleTypeEnum role,
                                       @ApiParam(value = "返回url") @RequestParam(required = false) String returnUrl,
                                       @ApiParam(value = "其它参数") @RequestParam(required = false) String params) throws IOException {
        if (Objects.isNull(loginName) || Objects.equals(loginName, "")) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("登录名不能为空");
        }
        if (Objects.isNull(role)) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("角色不能为空");
        }
        if (role != RoleTypeEnum.SUBJECT_TEACHER && role != RoleTypeEnum.ASSIGN_TEACHER) {
            throw ExceptionResultEnum.ERROR.exception("角色类型超出限制");
        }
        OpenParams openParams = new OpenParams();
        openParams.setName(realName);
        openParams.setRoleName(role.name());
        return ResultUtil.ok(ssoService.questionLibraryLogin(loginName,realName, role, returnUrl, JSON.toJSONString(openParams)));
    }

    @ApiOperation(value = "教研分析单点登录")
    @ApiResponses({@ApiResponse(code = 200, message = "教研分析单点登录", response = Result.class)})
    @RequestMapping(value = "/analysis_login", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SSO)
    public Result analysisLogin(@ApiParam(value = "登录名", required = true) @RequestParam String loginName,
                                @ApiParam(value = "角色", required = true) @RequestParam RoleTypeEnum role,
                                @ApiParam(value = "学院名称", required = true) @RequestParam String orgName,
                                @ApiParam(value = "真实名") @RequestParam(required = false) String realName,
                                @ApiParam(value = "手机号码") @RequestParam(required = false) String mobileNumber,
                                @ApiParam(value = "是否启用") @RequestParam(required = false) Boolean enable,
                                @ApiParam(value = "返回url") @RequestParam(required = false) String returnUrl) throws IOException {
        if (Objects.isNull(loginName) || Objects.equals(loginName, "")) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("登录名不能为空");
        }
        if (Objects.isNull(role)) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("角色不能为空");
        }
        Long schoolId = SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId()));

        SysOrg sysOrg = sysOrgService.getOne(new QueryWrapper<SysOrg>()
                .lambda()
                .eq(SysOrg::getSchoolId,schoolId)
                .eq(SysOrg::getName,orgName));
        if (Objects.isNull(sysOrg)){
            throw ExceptionResultEnum.ERROR.exception("机构不存在");
        }

        // 不是教务处老师查询开课学院
        if (!RoleTypeEnum.OFFICE_TEACHER.equals(role)){
            sysOrg = sysOrgService.findCollegeLevelOrgByOrgId(sysOrgService.getOne(new QueryWrapper<SysOrg>().lambda()
                    .eq(SysOrg::getSchoolId,schoolId)
                    .eq(SysOrg::getName,orgName)).getId());
        }

        if (Objects.isNull(orgName) || Objects.equals(orgName, "")) {
            throw ExceptionResultEnum.PARAMS_ERROR.exception("学院名称不能为空");
        }
        if (role != RoleTypeEnum.OFFICE_TEACHER && role != RoleTypeEnum.PRESIDENT && role != RoleTypeEnum.TEACHER) {
            throw ExceptionResultEnum.ERROR.exception("角色类型超出限制");
        }
        return ResultUtil.ok(ssoService.analysisLogin(loginName, role, sysOrg.getName(), realName, mobileNumber, enable, returnUrl));
    }
}

