package com.hchyun.test.controller;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;


import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.controller.HcyBaseController;
import com.hchyun.common.utils.ServerResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hchyun.common.annotation.Log;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.enums.BusinessType;
import com.hchyun.test.entity.Results;
import com.hchyun.test.service.ResultsService;
import com.hchyun.common.utils.poi.ExcelUtil;

/**
 * 成绩Controller
 * 
 * @author hchyun
 * @date 2021-01-24
 */
@Api(value = "成绩管理",tags = "成绩管理")
@RestController
@RequestMapping("/test/results")
public class ResultsController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(ResultsController.class);

    @Autowired
    private ResultsService resultsService;

    /**
     * 查询成绩列表
     */
    @ApiOperation("查询成绩列表")
    @PreAuthorize("@ss.hasPermi('test:results:list')")
    @GetMapping("/list")
    public Serializable list(Results results) {
        try {
            startPage();
            ServerResult<List<Results>> serverResult = resultsService.selectResultsList(results);
            if (serverResult.isStart()) {
                return getDataTable(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出成绩列表
     */
    @ApiOperation("导出成绩列表")
    @PreAuthorize("@ss.hasPermi('test:results:export')")
    @Log(title = "成绩", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Results results) {
        try {
            ServerResult<List<Results>> serverResult = resultsService.selectResultsList(results);
            ExcelUtil<Results> util = new ExcelUtil<Results>(Results. class);
            if (serverResult.isStart()) {
                return util.exportExcel(serverResult.getData(), "results");
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取成绩详细信息
     */
    @ApiOperation("获取成绩详细信息")
    @ApiImplicitParam(name = "id" , value = "成绩id" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('test:results:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<Results> serverResult = resultsService.selectResultsById(id);
            if (serverResult.isStart()) {
                return AjaxResult.success(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增成绩
     */
    @ApiOperation("新增成绩")
    @ApiImplicitParam(name = "results" , value = "新增成绩信息" , dataType = "Results")
    @PreAuthorize("@ss.hasPermi('test:results:add')")
    @Log(title = "成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Results results) {
        if (results.getJava() == null || results.getJava()<0) {
            return AjaxResult.error("java成绩不能为空!");
        }
        if (results.getImages() == null || results.getImages().equals("")) {
            return AjaxResult.error("图片路径不能为空!");
        }
        if (results.getFile() == null || results.getFile().equals("")) {
            return AjaxResult.error("文件路径不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = resultsService.insertResults(results);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改成绩
     */
    @ApiOperation("修改成绩")
    @ApiImplicitParam(name = "results" , value = "修改成绩信息" , dataType = "Results")
    @PreAuthorize("@ss.hasPermi('test:results:edit')")
    @Log(title = "成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Results results) {
//    public AjaxResult edit(@RequestBody Map<String,Object> map) {
        try {

            if (results.getJava() == null || results.getJava()<0) {
                return AjaxResult.error("java成绩不能为空!");
            }
            if (results.getImages() == null || results.getImages().equals("")) {
                return AjaxResult.error("图片路径不能为空!");
            }
            if (results.getFile() == null || results.getFile().equals("")) {
                return AjaxResult.error("文件路径不能为空!");
            }
            ServerResult<Integer> serverResult = resultsService.updateResults(results);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除成绩
     */
    @ApiOperation("删除成绩")
    @ApiImplicitParam(name = "ids" , value = "成绩ids" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('test:results:remove')")
    @Log(title = "成绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            if (ids.length<0){
                return AjaxResult.error("id不能为空!");
            }
            ServerResult<Integer> serverResult = resultsService.deleteResultsByIds(ids);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
