package com.xinyilu.web.controller.testItemCityScoreinfo;

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.dto.TestItemCityScoreinfoDTO;
import com.xinyilu.system.domain.vo.TestItemCityScoreinfoVo;
import com.xinyilu.system.service.testItemCityScoreinfo.ITestItemCityScoreService;
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/testItemCityScore")
public class TestItemCityScoreController extends BaseController {
    @Autowired
    private ITestItemCityScoreService testItemCityScoreService;

    /**
     * 查询全市平均成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:testItemCityScore:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestItemCityScoreinfoVo testItemCityScore) {
        startPage();
        List<TestItemCityScoreinfoVo> list = testItemCityScoreService.selectTestItemCityScoreList(testItemCityScore);
        return getDataTable(list);
    }

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

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

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

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

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

    /**
     * @param areaId 区域id
     * @param itemId 项目id
     * @param sex    性别
     * @return 全市平均成绩信息
     */
    @GetMapping("/getItemCityScoreInfo")
    public AjaxResult getItemCityScoreInfo(Long areaId, Long itemId, String sex) {
        if (Objects.isNull(areaId)) {
            return AjaxResult.error("所属区域信息不能为空");
        }
        if (Objects.isNull(itemId)) {
            return AjaxResult.error("测试项目信息不能为空");
        }
        if (StringUtils.isBlank(sex)) {
            return AjaxResult.error("性别不能为空");
        }
        return testItemCityScoreService.getItemCityScoreInfo(areaId, itemId, sex);
    }
}
