package com.keao.edu.user.controller;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.user.dao.EmployeeDao;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExamOrganizationRelation;
import com.keao.edu.user.entity.Organization;
import com.keao.edu.user.page.ExamOrganizationRelationQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamOrganizationRelationService;
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.Objects;

/**
 * @Author Joburgess
 * @Date 2020.06.18
 */
@RestController
@RequestMapping("examOrganizationRelation")
@Api(tags = "考级项目合作单位服务")
public class ExamOrganizationRelationController extends BaseController {

    @Autowired
    private ExamOrganizationRelationService examOrganizationRelationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("分页查询")
    @GetMapping(value = "/list")
    public HttpResponseResult<PageInfo<ExamOrganizationRelation>> getList(ExamOrganizationRelationQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        if(!sysUser.getIsSuperAdmin()&&Objects.isNull(queryInfo.getOrganId())){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.nonNull(employee)){
                queryInfo.setOrganId(employee.getOrganId());
            }
        }

        return succeed(examOrganizationRelationService.queryExamOrgans(queryInfo));
    }

    @ApiOperation("更新考级项目与合作单位关联信息")
    @PostMapping(value = "/updateExamOrganizationRelation")
    public HttpResponseResult updateExamOrganizationRelation(@RequestBody ExamOrganizationRelation examOrganizationRelation){
        examOrganizationRelationService.updateExamOrganizationRelation(examOrganizationRelation);
        return succeed();
    }

    @ApiOperation("发送考级报名链接")
    @PostMapping(value = "/sendUrl")
    public HttpResponseResult sendUrl(Integer examId, String organIds){
        examOrganizationRelationService.sendUrl(examId, organIds);
        return succeed();
    }

    @ApiOperation("获取未关联到考级项目的合作单位")
    @GetMapping(value = "/queryUnRelatedOrgans")
    public HttpResponseResult<PageInfo<Organization>> queryUnRelatedOrgans(ExamOrganizationRelationQueryInfo queryInfo){
        return succeed(examOrganizationRelationService.queryUnRelatedOrgans(queryInfo));
    }

}
