package com.xinyilu.web.controller.testItem;

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.TestItemsDTO;
import com.xinyilu.system.domain.testItem.TestItems;
import com.xinyilu.system.domain.vo.TestItemsVO;
import com.xinyilu.system.service.testItem.ITestItemsService;
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 jihan
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/testItems")
public class TestItemsController extends BaseController {
    @Autowired
    private ITestItemsService testItemsService;

    /**
     * 查询测试项目列表
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestItems testItems) {
        startPage();
        List<TestItemsVO> list = testItemsService.selectTestItemsList(testItems);
        return getDataTable(list);
    }

    /**
     * 导出测试项目列表
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:export')")
    @Log(title = "测试项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TestItems testItems) {
        List<TestItemsVO> list = testItemsService.selectTestItemsList(testItems);
        ExcelUtil<TestItemsVO> util = new ExcelUtil<>(TestItemsVO.class);
        util.exportExcel(response, list, "测试项目数据");
    }

    /**
     * 获取测试项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(testItemsService.selectTestItemsById(id));
    }

    /**
     * 新增测试项目
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:add')")
    @Log(title = "测试项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody TestItemsDTO testItems) {
        return testItemsService.insertTestItems(testItems, getUsername());
    }

    /**
     * 修改测试项目
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:edit')")
    @Log(title = "测试项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody TestItemsDTO testItems) {
        return testItemsService.updateTestItems(testItems, getUsername());
    }

    /**
     * 删除测试项目
     */
    @PreAuthorize("@ss.hasPermi('system:testItems:remove')")
    @Log(title = "测试项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return testItemsService.updateTestItemsByIds(ids);
    }

    /**
     * 根据项目id查询项目成绩级别信息
     *
     * @param itemId 项目id
     * @return AjaxResult 测试项目级别信息
     */
    @GetMapping("/getTestItemLevelInfo")
    public AjaxResult getTestItemLevelInfo(Long itemId) {
        return AjaxResult.success(testItemsService.getTestItemLevelInfo(itemId));
    }

    /**
     * 根据项目id查询对应的分类信息
     *
     * @param itemId 测试项目id
     * @return 分类信息
     */
    @GetMapping("/getTestCategoryItemInfo")
    public AjaxResult getTestCategoryItemInfo(Long itemId) {
        if (Objects.isNull(itemId)) {
            return AjaxResult.error("测试项目id不能为空！");
        }
        return testItemsService.getTestCategoryItemInfo(itemId);
    }
}
