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.dto.ExaminationBasicDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExaminationBasic;
import com.keao.edu.user.enums.ExamStatusEnum;
import com.keao.edu.user.page.ExaminationQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExaminationBasicService;
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("examinationBasic")
@Api(tags = "考级项目服务")
public class ExaminationBasicController extends BaseController {

    @Autowired
    private ExaminationBasicService examinationBasicService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("查询指定考级项目")
    @GetMapping(value = "/getExaminationBasic")
    public HttpResponseResult<ExaminationBasicDto> getExaminationBasic(Integer examId) {
        return succeed(examinationBasicService.getExaminationBasic(examId));
    }

    @ApiOperation("分页查询")
    @GetMapping(value = "/list")
    public HttpResponseResult<PageInfo<ExaminationBasicDto>> getList(ExaminationQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            queryInfo.setOrganId(employee.getOrganId());
        }
        return succeed(examinationBasicService.findExaminationBasics(queryInfo));
    }

    @ApiOperation("创建考级项目")
    @PostMapping(value = "/addExaminationBasic")
    public HttpResponseResult<ExaminationBasic> addExaminationBasic(@RequestBody ExaminationBasic examinationBasic){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户信息异常");
        }
        examinationBasic.setCreatorId(sysUser.getId());
        examinationBasic.setOrganId(employee.getOrganId());
        return succeed(examinationBasicService.addExaminationBasic(examinationBasic));
    }

    @ApiOperation("更新考级项目")
    @PostMapping(value = "/update")
    public HttpResponseResult update(@RequestBody ExaminationBasic examinationBasic){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examinationBasicService.updateExam(examinationBasic,sysUser.getId());
        return succeed();
    }

    @ApiOperation("更新考级项目状态")
    @PostMapping(value = "/updateExamBasicStatus")
    public HttpResponseResult updateExamBasicStatus(Long examId, ExamStatusEnum statusEnum){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examinationBasicService.updateExamBasicStatus(examId, statusEnum, sysUser.getId());
        return succeed();
    }

    @ApiOperation("关闭考级项目")
    @PostMapping(value = "/closeExam")
    public HttpResponseResult closeExam(Integer examId, String reason){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer organId = null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId = employee.getOrganId();
        }
        examinationBasicService.closeExam(organId, examId, reason);
        return succeed();
    }

    @ApiOperation("获取考级项目信息")
    @PostMapping(value = "/getInfo")
    public HttpResponseResult<ExaminationBasic> getInfo(Integer examId){
        return succeed(examinationBasicService.getInfo(examId));
    }
}
