package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.StudentCompetitionRankingDto;
import com.ym.mec.biz.dal.entity.StudentCompetition;
import com.ym.mec.biz.service.ContractService;
import com.ym.mec.biz.service.StudentCompetitionService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
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.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020.11.10
 */
@RestController
@RequestMapping("studentCompetition")
@Api(tags = "长三角比赛服务")
public class StudentCompetitionController extends BaseController {

    @Autowired
    private StudentCompetitionService studentCompetitionService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "新增参赛作品")
    @PostMapping("/add")
    public HttpResponseResult add(@RequestBody StudentCompetition studentCompetition){
        if(StringUtils.isBlank(studentCompetition.getUsername())||StringUtils.isBlank(studentCompetition.getIdCardNo())){
            return failed("参赛选手身份信息不完整");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("请登录");
        }
        studentCompetition.setUserId(sysUser.getId());
        studentCompetitionService.addStudentCompetition(studentCompetition);
        return succeed();
    }

    @ApiOperation(value = "检测学生是否已有上传作品")
    @GetMapping("/checkIsUpload")
    public HttpResponseResult<StudentCompetition> checkIsUpload(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("请登录");
        }
        return succeed(studentCompetitionService.checkIsUpload(sysUser.getId()));
    }

    @ApiOperation(value = "获取获奖名单")
    @GetMapping("/getWinnerList")
    public HttpResponseResult<List<StudentCompetitionRankingDto>> getWinnerList(){
        return succeed(studentCompetitionService.getWinnerList());
    }

    @ApiOperation(value = "获取指定参赛作品详情")
    @GetMapping("/get")
    public HttpResponseResult<StudentCompetition> get(Long competitionId){
        StudentCompetition studentCompetition = studentCompetitionService.get(competitionId);
        if(Objects.isNull(studentCompetition)||!studentCompetition.isShow()){
            return succeed();
        }
        return succeed(studentCompetition);
    }

}
