package com.ruoyi.web.controller.invest;

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

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.invest.domain.TProjectCirculation;
import com.ruoyi.invest.service.ITProjectCirculationService;
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.invest.domain.TProjectScoring;
import com.ruoyi.invest.service.ITProjectScoringService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 打分Controller
 * 
 * @author ruoyi
 * @date 2024-02-23
 */
@Api(tags = "打分表接口")
@RestController
@RequestMapping("/invest/scoring")
public class TProjectScoringController extends BaseController
{
    @Autowired
    private ITProjectScoringService tProjectScoringService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    /**
     * 查询打分列表
     */
    @ApiOperation("查询打分列表")
    @GetMapping("/list")
    public TableDataInfo list(TProjectScoring tProjectScoring)
    {
        startPage();
        List<TProjectScoring> list = tProjectScoringService.selectTProjectScoringList(tProjectScoring);
        return getDataTable(list);
    }

    /**
     * 导出打分列表
     */
    @ApiOperation("导出打分列表")
    @PreAuthorize("@ss.hasPermi('invest:scoring:export')")
    @Log(title = "打分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectScoring tProjectScoring)
    {
        List<TProjectScoring> list = tProjectScoringService.selectTProjectScoringList(tProjectScoring);
        ExcelUtil<TProjectScoring> util = new ExcelUtil<TProjectScoring>(TProjectScoring.class);
        util.exportExcel(response, list, "打分数据");
    }

    /**
     * 获取打分详细信息
     */
    @ApiOperation("获取打分详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectScoringService.selectTProjectScoringById(id));
    }

    /**
     * 新增打分
     */
    @ApiOperation("新增打分")
    @Log(title = "打分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectScoring tProjectScoring)
    {
        tProjectScoring.setCreateBy(getNickName());

        // todo 增加项目创建记录
        tProjectCirculationService.insertTProjectCirculation(tProjectScoring.getProjectPoolId(),"上传打分表",getNickName());

        return toAjax(tProjectScoringService.insertTProjectScoring(tProjectScoring));
    }

    /**
     * 修改打分
     */
    @ApiOperation("修改打分")
    @PreAuthorize("@ss.hasPermi('invest:scoring:edit')")
    @Log(title = "打分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectScoring tProjectScoring)
    {
        return toAjax(tProjectScoringService.updateTProjectScoring(tProjectScoring));
    }

    /**
     * 删除打分
     */
    @ApiOperation("删除打分")
    @PreAuthorize("@ss.hasPermi('invest:scoring:remove')")
    @Log(title = "打分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tProjectScoringService.updateTProjectScoringByIds(ids));
    }

    /**
     * 根据会议ID查询汇总分数和平均分数
     */
    @ApiOperation("根据会议ID查询汇总分数和平均分数")
    @GetMapping(value = "/selectMeetingId")
    public AjaxResult selectMeetingId(String meetingId)
    {
        if (StringUtils.isEmpty(meetingId))
        {
            return error("查询打分数据'" + getNickName() + "'失败，请选择会议！");
        }
        return success(tProjectScoringService.selectMeetingId(meetingId));
    }

    /**
     * 根据会议ID查询汇总分数和平均分数
     */
    @ApiOperation("根据会议ID查询是否打分和打分数据")
    @GetMapping(value = "/getCanScoringMeetingId")
    public AjaxResult getCanScoringMeetingId(String meetingId)
    {
        if (StringUtils.isEmpty(meetingId))
        {
            return error("查询打分数据'" + getNickName() + "'失败，请选择会议！");
        }
        TProjectScoring tProjectScoring = tProjectScoringService.getCanScoringMeetingId(meetingId,getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("tProjectScoring", tProjectScoring);
        return ajax;
    }

}
