package com.ruoyi.web.controller.invest;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.invest.domain.TProjectCirculation;
import com.ruoyi.invest.domain.TProjectInformation;
import com.ruoyi.invest.service.ITProjectCirculationService;
import com.ruoyi.invest.service.ITProjectInformationService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.tool.domain.TUnifyFile;
import com.ruoyi.tool.service.ITUnifyFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件资料Controller
 * 
 * @author ruoyi
 * @date 2024-02-22
 */
@Api(tags = "文件资料")
@RestController
@RequestMapping("/invest/information")
public class TProjectInformationController extends BaseController
{
    @Autowired
    private ITProjectInformationService tProjectInformationService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询文件资料列表
     */
    @ApiOperation("查询文件资料列表")
    @PreAuthorize("@ss.hasPermi('invest:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(TProjectInformation tProjectInformation)
    {
        startPage();
        List<TProjectInformation> list = tProjectInformationService.selectTProjectInformationList(tProjectInformation);
        return getDataTable(list);
    }

    /**
     * 导出文件资料列表
     */
    @ApiOperation("导出文件资料列表")
    @PreAuthorize("@ss.hasPermi('invest:information:export')")
    @Log(title = "文件资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectInformation tProjectInformation)
    {
        List<TProjectInformation> list = tProjectInformationService.selectTProjectInformationList(tProjectInformation)
                .stream().map(n -> {
                    n.setProjectPoolId(n.gettProjectPool().getProjectName());
                    n.setFileType(dictDataService.selectDictLabel("file_type",n.getFileType()));
                    n.setProjectStage(dictDataService.selectDictLabel("project_stage",n.getProjectStage()));
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<TProjectInformation> util = new ExcelUtil<TProjectInformation>(TProjectInformation.class);
        util.exportExcel(response, list, "文件资料数据");
    }

    /**
     * 获取文件资料详细信息
     */
    @ApiOperation("获取文件资料详细信息")
    @PreAuthorize("@ss.hasPermi('invest:information:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectInformationService.selectTProjectInformationById(id));
    }

    /**
     * 新增文件资料
     */
    @ApiOperation("新增文件资料")
    @PreAuthorize("@ss.hasPermi('invest:information:add')")
    @Log(title = "文件资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectInformation tProjectInformation)
    {
        tProjectInformation.setId(IdUtils.fastSimpleUUID());
        tProjectInformation.setCreateBy(getNickName());
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectInformation.getListFile(),
                tProjectInformation.getProjectPoolId(),
                tProjectInformation.getId(),//文件资料ID
                String.valueOf(FileType.INFORMATION.ordinal()),//文件类型:文件资料
                getNickName());

        // todo 增加文件创建记录
        tProjectCirculationService.insertTProjectCirculation(tProjectInformation.getProjectPoolId(),tProjectInformation.getFileName()+"(文件资料)",getNickName());

        return toAjax(tProjectInformationService.insertTProjectInformation(tProjectInformation));
    }

    /**
     * 修改文件资料
     */
    @ApiOperation("修改文件资料")
    @PreAuthorize("@ss.hasPermi('invest:information:edit')")
    @Log(title = "文件资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectInformation tProjectInformation)
    {
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectInformation.getListFile(),
                tProjectInformation.getProjectPoolId(),
                tProjectInformation.getId(),//文件资料ID
                String.valueOf(FileType.INFORMATION.ordinal()),//文件类型:文件资料
                getNickName());

        return toAjax(tProjectInformationService.updateTProjectInformation(tProjectInformation));
    }

    /**
     * 删除文件资料
     */
    @ApiOperation("删除文件资料")
    @PreAuthorize("@ss.hasPermi('invest:information:remove')")
    @Log(title = "文件资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //删除附件
        tUnifyFileService.updateTUnifyFileBusinessIds(ids);
        return toAjax(tProjectInformationService.updateTProjectInformationByIds(ids));
    }

    /**
     * 根据项目ID获取会议记录
     */
    @ApiOperation("根据项目ID获取会议记录")
    @PreAuthorize("@ss.hasPermi('invest:information:query')")
    @GetMapping(value = "/listProjectPoolId")
    public AjaxResult listProjectPoolId(String projectPoolId)
    {
        return success(tProjectInformationService.listProjectPoolId(projectPoolId));
    }

}
