package com.bonus.material.project.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.project.domain.Project;
import com.bonus.material.project.domain.ProjectType;
import com.bonus.material.project.service.ProjectService;
import com.bonus.material.supplier.domain.MaSupplier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 30791
 * @version 1.0
 * Create by 2025/11/13 11:23
 */

@RestController
@RequestMapping("/project")
@Api(value = "工程管理", tags = "工程管理")
public class ProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    /**
     * 查询工程列表
     */
    @ApiOperation(value = "查询工程列表")
    @GetMapping("/list")
    public AjaxResult list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 查询工程详细
     */
    @ApiOperation(value = "查询工程详细")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        Project project=projectService.selectProjectById(projectId);
        return AjaxResult.success(project);
    }

    /**
     * 新增工程
     */
    @ApiOperation(value = "新增工程")
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        projectService.insertProject(project);
        return AjaxResult.success();
    }

    /**
     * 修改工程
     */
    @ApiOperation(value = "修改工程")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Project project)
    {
        projectService.updateProject(project);
        return AjaxResult.success();
    }

    /**
     * 删除工程
     */
    @ApiOperation(value = "删除工程")
    @PostMapping("/delete/{projectId}")
    public AjaxResult remove(@PathVariable("projectId") Long projectId)
    {
        projectService.deleteProjectById(projectId);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除工程")
    @PostMapping("/batch/delete")
    public AjaxResult delProjectBatch(@RequestBody List<Long> ids) {
        projectService.delProjectBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 获取工程类型列表
     */
    @ApiOperation(value = "获取工程类型列表")
    @GetMapping("/list/project_type")
    public AjaxResult getProjectTypeList()
    {

        // 从字典表中查询
        List<ProjectType> projectTypes = projectService.selectProjectTypeList();
        return AjaxResult.success(projectTypes);
    }

    /**
     * 导出
     * @param response
     * @param project
     */
    @PostMapping("/export")
    @SysLog(title = "项目管理", businessType = OperaType.EXPORT, logType = 0, module = "系统管理->项目管理", details = "导出项目信息")
    public void export(HttpServletResponse response, Project project) {
        try {
            List<Project> list = projectService.selectProjectList(project);
            ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
            util.exportExcel(response,list,"项目数据");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }


}
