package com.xinyilu.web.controller.testItemCountryScoreinfo;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.utils.poi.ExcelUtil;
import com.xinyilu.system.domain.testItemCountryScoreinfo.TestItemCountryScore;
import com.xinyilu.system.domain.vo.TestItemCountryScoreVO;
import com.xinyilu.system.service.testItemCountryScoreinfo.ITestItemCountryScoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 全国平均成绩Controller
 *
 * @author xinyilu
 * @date 2022-08-03
 */
@RestController
@RequestMapping("/system/testItemCountryScore")
public class TestItemCountryScoreController extends BaseController {
    @Autowired
    private ITestItemCountryScoreService testItemCountryScoreService;

    /**
     * 查询全国平均成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestItemCountryScoreVO testItemCountryScore) {
        startPage();
        List<TestItemCountryScoreVO> list = testItemCountryScoreService.selectTestItemCountryScoreList(testItemCountryScore);
        return getDataTable(list);
    }

    /**
     * 导出全国平均成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:export')")
    @Log(title = "全国平均成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TestItemCountryScoreVO testItemCountryScore) {
        List<TestItemCountryScoreVO> list = testItemCountryScoreService.selectTestItemCountryScoreList(testItemCountryScore);
        ExcelUtil<TestItemCountryScoreVO> util = new ExcelUtil<>(TestItemCountryScoreVO.class);
        util.exportExcel(response, list, "全国平均成绩数据");
    }

    /**
     * 获取全国平均成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(testItemCountryScoreService.selectTestItemCountryScoreById(id));
    }

    /**
     * 新增全国平均成绩
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:add')")
    @Log(title = "全国平均成绩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody TestItemCountryScore testItemCountryScore) {
        testItemCountryScore.setCreateBy(getUsername());
        return toAjax(testItemCountryScoreService.insertTestItemCountryScore(testItemCountryScore));
    }

    /**
     * 修改全国平均成绩
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:edit')")
    @Log(title = "全国平均成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody TestItemCountryScore testItemCountryScore) {
        testItemCountryScore.setUpdateBy(getUsername());
        return toAjax(testItemCountryScoreService.updateTestItemCountryScore(testItemCountryScore));
    }

    /**
     * 删除全国平均成绩
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCountryScore:remove')")
    @Log(title = "全国平均成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(testItemCountryScoreService.updateTestItemCountryScoreByIds(ids));
    }

    /**
     * 根据项目id和学生性别查询对应得平均分
     *
     * @param itemId 项目id
     * @param sex    学生性别
     * @return 全国平均成绩信息
     */
    @GetMapping("/getItemCountryScoreInfo")
    public AjaxResult getTestItemCountryScoreInfo(Long itemId, String sex) {
        if (Objects.isNull(itemId)) {
            return AjaxResult.error("所属项目不能为空");
        }
        if (StringUtils.isBlank(sex)) {
            return AjaxResult.error("性别不能为空");
        }
        return testItemCountryScoreService.getTestItemCountryScoreInfo(itemId, sex);
    }
}
