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.utils.poi.ExcelUtil;
import com.test.test.domain.TestProject;
import com.test.test.domain.qo.IDQO;
import com.test.test.domain.qo.TestProjectListQO;
import com.test.test.service.ITestProjectService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 需求controller
 */
@Slf4j
@RestController
@RequestMapping("/test/project")
public class TestProjectController extends BaseController {

    @Resource
    private ITestProjectService testProjectService;

    /**
     * 查询需求列表
     * @param qo
     * @return
     */
    @GetMapping("/projectList")
    public TableDataInfo projectList(@Validated TestProjectListQO qo) {
        startPage();
        List<TestProject> list = testProjectService.selectTestProjectList(qo);
        return getDataTable(list);
    }

    /**
     * 查询需求详情
     * @param qo
     * @return
     */
    @PostMapping("/projectDetail")
    public AjaxResult projectDetail(@RequestBody IDQO qo) {
        return success(testProjectService.selectTestProjectById(qo.getId()));
    }

    /**
     * 新增需求
     */
    @Log(title = "需求", businessType = BusinessType.INSERT)
    @PostMapping("/addProject")
    public AjaxResult addProject(@RequestBody TestProject testProject) {
        return toAjax(testProjectService.insertTestProject(testProject));
    }

    /**
     * 删除需求
     */
    @Log(title = "需求", businessType = BusinessType.DELETE)
    @PostMapping("/delProject")
    public AjaxResult delProject(@RequestBody IDQO qo) {
        TestProject testProject = testProjectService.selectTestProjectById(qo.getId());
        testProject.setDelFlag("1");
        return toAjax(testProjectService.updateTestProject(testProject));
    }

    /**
     * 修改需求
     */
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PostMapping("/editProject")
    public AjaxResult editProject(@RequestBody TestProject testProject) {
        return toAjax(testProjectService.updateTestProject(testProject));
    }

    /**
     * 导出需求列表
     */
    @Log(title = "需求", businessType = BusinessType.EXPORT)
    @PostMapping("/exportProject")
    public void exportProject(HttpServletResponse response,
        @RequestBody TestProjectListQO qo) {
        List<TestProject> list = testProjectService.selectTestProjectList(qo);
        ExcelUtil<TestProject> util = new ExcelUtil<TestProject>(TestProject.class);
        util.exportExcel(response, list, "需求数据");
    }

    /**
     * 批量导出需求列表
     */
    @Log(title = "需求", businessType = BusinessType.EXPORT)
    @PostMapping("/batchExportProject")
    public void batchExportProject(HttpServletResponse response,
        @RequestBody List<TestProject> list) {
        ExcelUtil<TestProject> util = new ExcelUtil<TestProject>(TestProject.class);
        util.exportExcel(response, list, "需求数据");
    }
}
