package com.ruoyi.web.controller.study;

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.TStudyInformation;
import com.ruoyi.invest.service.ITStudyInformationService;
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("/study/projectMeetingMinutes")
public class ProjectMeetingMinutesController extends BaseController
{
    @Autowired
    private ITStudyInformationService tStudyInformationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询项目会议纪要列表
     */
    @ApiOperation("查询项目会议纪要列表")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStudyInformation tStudyInformation)
    {
        startPage();
        tStudyInformation.setCreateBy(getNickName());//默认登陆人权限
        tStudyInformation.setContractType("e");
        List<TStudyInformation> list = tStudyInformationService.selectTStudyInformationList(tStudyInformation);
        return getDataTable(list);
    }

    /**
     * 导出项目会议纪要列表
     */
    @ApiOperation("导出项目会议纪要列表")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:export')")
    @Log(title = "项目会议纪要", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TStudyInformation tStudyInformation)
    {
        tStudyInformation.setCreateBy(getNickName());//默认登陆人权限
        tStudyInformation.setContractType("e");
        List<TStudyInformation> list = tStudyInformationService.selectTStudyInformationList(tStudyInformation)
                .stream().map(n -> {
                    n.setContractType(dictDataService.selectDictLabel("file_type",n.getContractType()));
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<TStudyInformation> util = new ExcelUtil<TStudyInformation>(TStudyInformation.class);
        util.exportExcel(response, list, "项目会议纪要数据");
    }

    /**
     * 获取项目会议纪要详细信息
     */
    @ApiOperation("获取项目会议纪要详细信息")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tStudyInformationService.selectTStudyInformationById(id));
    }

    /**
     * 新增项目会议纪要
     */
    @ApiOperation("新增项目会议纪要")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:add')")
    @Log(title = "项目会议纪要", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStudyInformation tStudyInformation)
    {
        tStudyInformation.setId(IdUtils.fastSimpleUUID());
        tStudyInformation.setCreateBy(getNickName());
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tStudyInformation.getListFile(),
                null,
                tStudyInformation.getId(),
                String.valueOf(FileType.GEN.ordinal()),
                getNickName());

        return toAjax(tStudyInformationService.insertTStudyInformation(tStudyInformation));
    }

    /**
     * 修改项目会议纪要
     */
    @ApiOperation("修改项目会议纪要")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:edit')")
    @Log(title = "项目会议纪要", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStudyInformation tStudyInformation)
    {
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tStudyInformation.getListFile(),
                null,
                tStudyInformation.getId(),
                String.valueOf(FileType.GEN.ordinal()),
                getNickName());
        
        return toAjax(tStudyInformationService.updateTStudyInformation(tStudyInformation));
    }

    /**
     * 删除项目会议纪要
     */
    @ApiOperation("删除项目会议纪要")
    @PreAuthorize("@ss.hasPermi('study:projectMeetingMinutes:remove')")
    @Log(title = "项目会议纪要", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tStudyInformationService.updateTStudyInformationByIds(ids));
    }
}
