package com.test.test.controller;

import com.test.common.annotation.Log;
import com.test.common.core.controller.BaseController;
import com.test.common.core.domain.AjaxResult;
import com.test.common.core.page.TableDataInfo;
import com.test.common.enums.BusinessType;
import com.test.common.enums.EnumDefectStatus;
import com.test.common.enums.EnumSeverityLevel;
import com.test.common.utils.poi.ExcelUtil;
import com.test.test.domain.TestDefect;
import com.test.test.domain.qo.IDQO;
import com.test.test.domain.qo.TestDefectListQO;
import com.test.test.service.ITestDefectService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 测试缺陷Controller
 *
 * @author test
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/test/defect")
public class TestDefectController extends BaseController
{
    @Autowired
    private ITestDefectService testDefectService;

    /**
     * 查询测试缺陷列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TestDefectListQO qo)
    {
        startPage();
        List<TestDefect> list = testDefectService.selectTestDefectList(qo);
        return getDataTable(list);
    }

    /**
     * 导出测试缺陷列表
     */
    @Log(title = "测试缺陷", businessType = BusinessType.EXPORT)
    @PostMapping("/exportBug")
    public void export(HttpServletResponse response,
                              @RequestBody TestDefectListQO qo) {
        List<TestDefect> list = testDefectService.selectTestDefectList(qo);
        for (TestDefect testDefect : list) {
            testDefect.setStatus(EnumDefectStatus.getDescriptionByCode(testDefect.getStatus()));
            testDefect.setLevel(EnumSeverityLevel.getDescriptionByCode(testDefect.getLevel()));
        }
        ExcelUtil<TestDefect> util = new ExcelUtil<TestDefect>(TestDefect.class);
        util.exportExcel(response, list, "缺陷数据");
    }

    /**
     * 批量导出测试缺陷列表
     */
    @Log(title = "测试缺陷", businessType = BusinessType.EXPORT)
    @PostMapping("/batchExportBug")
    public void batchExport(HttpServletResponse response,
                                   @RequestBody List<TestDefect> list) {
        ExcelUtil<TestDefect> util = new ExcelUtil<TestDefect>(TestDefect.class);
        for (TestDefect testDefect : list) {
            testDefect.setStatus(EnumDefectStatus.getDescriptionByCode(testDefect.getStatus()));
            testDefect.setLevel(EnumSeverityLevel.getDescriptionByCode(testDefect.getLevel()));
        }
        util.exportExcel(response, list, "缺陷数据");
    }

    /**
     * 获取测试缺陷详细信息
     */
    @PostMapping("/bugDetail")
    public AjaxResult getInfo(@RequestBody IDQO qo)
    {
        return success(testDefectService.selectTestDefectById(qo.getId()));
    }

    /**
     * 新增测试缺陷
     */
    @Log(title = "测试缺陷", businessType = BusinessType.INSERT)
    @PostMapping("/addBug")
    public AjaxResult add(@RequestBody TestDefect testDefect)
    {
        return toAjax(testDefectService.insertTestDefect(testDefect));
    }

    /**
     * 修改测试缺陷
     */
    @Log(title = "测试缺陷", businessType = BusinessType.UPDATE)
    @PostMapping("/editBug")
    public AjaxResult edit(@RequestBody TestDefect testDefect)
    {
        return toAjax(testDefectService.updateTestDefect(testDefect));
    }

    /**
     * 删除测试缺陷
     */
    @Log(title = "测试缺陷", businessType = BusinessType.DELETE)
    @PostMapping("/delBug")
    public AjaxResult remove(@RequestBody IDQO qo)
    {
        TestDefect testDefect = testDefectService.selectTestDefectById(qo.getId());
        testDefect.setUpdateBy(getUsername());
        testDefect.setUpdateTime(new Date());
        testDefect.setDelFlag("1");
        return toAjax(testDefectService.updateTestDefect(testDefect));
    }
}
