package com.ruoyi.web.controller.file;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
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.service.ITUnifyFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目技术报告Controller
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Api(tags = "研究资料管理-项目技术报告")
@RestController
@RequestMapping("/file/projectTechnology")
public class ProjectTechnologyController extends BaseController
{
    @Autowired
    private ITProjectInformationService tProjectInformationService;
    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询项目技术报告列表
     */
    @ApiOperation("查询项目技术报告列表")
    @GetMapping("/list")
    public TableDataInfo list(TProjectInformation tProjectInformation)
    {
        startPage();
        tProjectInformation.setFileType("n");
        List<TProjectInformation> list = tProjectInformationService.selectTProjectInformationList(tProjectInformation);
        return getDataTable(list);
    }

    /**
     * 导出项目技术报告列表
     */
    @ApiOperation("导出项目技术报告列表")
    @Log(title = "项目技术报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectInformation tProjectInformation)
    {
        tProjectInformation.setFileType("n");
        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("获取项目技术报告详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectInformationService.selectTProjectInformationById(id));
    }

    /**
     * 新增项目技术报告
     */
    @ApiOperation("新增项目技术报告")
    @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("修改项目技术报告")
    @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("删除项目技术报告")
    @Log(title = "项目技术报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //删除附件
        tUnifyFileService.updateTUnifyFileBusinessIds(ids);
        return toAjax(tProjectInformationService.updateTProjectInformationByIds(ids));
    }

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