package com.yanzhu.jh.project.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yanzhu.jh.project.domain.vo.SurProjectBuildNodeDataExport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yanzhu.jh.project.domain.SurProjectBuildNodeData;
import com.yanzhu.jh.project.service.ISurProjectBuildNodeDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目建设节点数据Controller
 * 
 * @author ruoyi
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/project/build_node_data")
public class SurProjectBuildNodeDataController extends BaseController
{
    @Autowired
    private ISurProjectBuildNodeDataService surProjectBuildNodeDataService;



    /**
     * 查询项目建设节点数据列表
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectBuildNodeData surProjectBuildNodeData)
    {
        startPage();
        List<SurProjectBuildNodeData> list = surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        return getDataTable(list);
    }

    /**
     * 导出项目建设节点数据列表
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:export')")
    @Log(title = "项目建设节点数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectBuildNodeData surProjectBuildNodeData)
    {
        List<SurProjectBuildNodeData> list = surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        List<SurProjectBuildNodeDataExport> exportList=new ArrayList<>();
        for(SurProjectBuildNodeData it :list){
            exportList.add(SurProjectBuildNodeDataExport.convert(it));
        }
        ExcelUtil<SurProjectBuildNodeDataExport> util = new ExcelUtil<SurProjectBuildNodeDataExport>(SurProjectBuildNodeDataExport.class);
        util.exportExcel(response, exportList, "项目计划节点");
    }

    @Log(title = "项目建设节点数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('project:build_node_data:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SurProjectBuildNodeDataExport> util = new ExcelUtil<SurProjectBuildNodeDataExport>(SurProjectBuildNodeDataExport.class);
        InputStream is=file.getInputStream();
        List<SurProjectBuildNodeDataExport> list = util.importExcel(is);
        is.close();
        int n=surProjectBuildNodeDataService.importData(list);
        return success("成功导入"+n+"条数据!");
    }

    /**
     * 获取项目建设节点数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectBuildNodeDataService.selectSurProjectBuildNodeDataById(id));
    }

    /**
     * 新增项目建设节点数据
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:add')")
    @Log(title = "项目建设节点数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectBuildNodeData surProjectBuildNodeData)
    {
        return toAjax(surProjectBuildNodeDataService.insertSurProjectBuildNodeData(surProjectBuildNodeData));
    }

    /**
     * 修改项目建设节点数据
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:edit')")
    @Log(title = "项目建设节点数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectBuildNodeData surProjectBuildNodeData)
    {
        return toAjax(surProjectBuildNodeDataService.updateSurProjectBuildNodeData(surProjectBuildNodeData));
    }

    /**
     * 删除项目建设节点数据
     */
    @PreAuthorize("@ss.hasPermi('project:build_node_data:remove')")
    @Log(title = "项目建设节点数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectBuildNodeDataService.deleteSurProjectBuildNodeDataByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('project:build_node_data:list')")
    @GetMapping("/listByProject")
    public AjaxResult getListByProject(long projectId){
        SurProjectBuildNodeData surProjectBuildNodeData=new SurProjectBuildNodeData();
        surProjectBuildNodeData.setProjectId(projectId);
        List list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        if(list.size()>0){
            return AjaxResult.success(list);
        }
        surProjectBuildNodeDataService.addProjectNodes(projectId);
        list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        return AjaxResult.success(list);
    }
}
