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.ExamOrganizationRelationExtraDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExamOrganizationRelation;
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.List;
import java.util.Map;
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<ExamOrganizationRelationExtraDto>> 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("获取子合作单位")
    @GetMapping(value = "/getChildOrgans")
    public HttpResponseResult<List<ExamOrganizationRelationExtraDto>> getChildOrgans(Long examId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        Integer organId=null;
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.nonNull(employee)){
            organId=employee.getOrganId();
        }

        return succeed(examOrganizationRelationService.getChildOrgans(examId,organId));
    }

    @ApiOperation("添加合作单位")
    @PostMapping(value = "/addExamOrganizations")
    public HttpResponseResult addExamOrganizations(@RequestBody List<ExamOrganizationRelation> organizationRelations){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户异常");
        }
        examOrganizationRelationService.addExamOrganizations(organizationRelations,employee.getOrganId());
        return succeed();
    }

    @ApiOperation("更新考级项目与合作单位关联信息")
    @PostMapping(value = "/updateExamOrganizationRelation")
    public HttpResponseResult updateExamOrganizationRelation(@RequestBody ExamOrganizationRelation examOrganizationRelation){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户异常");
        }
        examOrganizationRelationService.updateExamOrganizationRelation(examOrganizationRelation,employee.getOrganId());
        return succeed();
    }

    @ApiOperation("发送考级报名链接")
    @PostMapping(value = "/sendUrl")
    public HttpResponseResult sendUrl(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        Employee employee = employeeService.get(sysUser.getId());
        if(Objects.isNull(employee)){
            return failed("用户异常");
        }
        examOrganizationRelationService.sendUrl(examId,employee.getOrganId());
        return succeed();
    }

    @ApiOperation("删除")
    @PostMapping(value = "/del")
    public HttpResponseResult del(Long id){
        examOrganizationRelationService.deleteExamOrgan(id);
        return succeed();
    }

    @ApiOperation("获取本单位项目信息")
    @GetMapping(value = "/getExamOrganStatistics")
    public HttpResponseResult getExamOrganStatistics(Long examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("请重新登录");
        }
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.nonNull(employee)){
                organId = employee.getOrganId();
            }
        }
        return succeed(examOrganizationRelationService.getExamOrganStatistics(organId, examId));
    }

    @ApiOperation("获取未关联到考级项目的合作单位")
    @GetMapping(value = "/queryUnRelatedOrgans")
    public HttpResponseResult<Map<String, Object>> queryUnRelatedOrgans(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.queryUnRelatedOrgans(queryInfo));
    }

}
