package com.xinyilu.web.controller.itemCategory;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
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.TestItemCategoryDTO;
import com.xinyilu.system.domain.itemCategory.TestItemCategory;
import com.xinyilu.system.service.itemCategory.ITestItemCategoryService;
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;

/**
 * 测试项目分类Controller
 *
 * @author jihan
 * @date 2022-08-01
 */
@RestController
@RequestMapping("/system/itemCategory")
public class TestItemCategoryController extends BaseController {
    @Autowired
    private ITestItemCategoryService testItemCategoryService;

    /**
     * 查询测试项目分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:itemCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(TestItemCategory testItemCategory) {
        List<TestItemCategory> list = testItemCategoryService.selectTestItemCategoryList(testItemCategory);
        return AjaxResult.success(list);
    }

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

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

    /**
     * 新增测试项目分类
     */
    @PreAuthorize("@ss.hasPermi('system:itemCategory:add')")
    @Log(title = "测试项目分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody TestItemCategoryDTO testItemCategory) {
        //创建人
        testItemCategory.setCreateBy(getUsername());
        return testItemCategoryService.insertTestItemCategory(testItemCategory);
    }

    /**
     * 修改测试项目分类
     */
    @PreAuthorize("@ss.hasPermi('system:itemCategory:edit')")
    @Log(title = "测试项目分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody TestItemCategoryDTO testItemCategory) {
        //修改人
        testItemCategory.setUpdateBy(getUsername());
        return testItemCategoryService.updateTestItemCategory(testItemCategory);
    }

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