package com.ruoyi.web.controller.project;

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.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.service.IInvestmentOpportunityService;
import com.ruoyi.flowable.service.ITerminationService;
import com.ruoyi.invest.domain.*;
import com.ruoyi.invest.domain.dto.TerminationDto;
import com.ruoyi.invest.domain.vo.ProjectTerminationVo;
import com.ruoyi.system.service.ISysDictDataService;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目立项Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@Api(tags = "项目终止")
@RestController
@RequestMapping("/project/termination")
public class TerminationController extends BaseController {
    @Autowired
    private ITerminationService terminationService;
    @Autowired
    private IInvestmentOpportunityService investmentOpportunityService;
    @Autowired
    private ISysDictDataService dictDataService;
    //所有项目终止流程列表
    @ApiOperation("所有项目终止流程列表")
    @GetMapping("/listAll")
    public TableDataInfo listAll(TProjectTermination tProjectTermination)
    {
        startPage();
        List<ProjectTerminationVo> list=terminationService.selectAllTerminationList(tProjectTermination);
        return getDataTable(list);
    }
    /**
     * 导出所有项目终止流程列表
     */
    @ApiOperation("导出所有项目终止流程列表")
    @Log(title = "所有项目终止流程列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectTermination tProjectTermination)
    {
        List<ProjectTerminationVo> list=terminationService.selectAllTerminationList(tProjectTermination)
                .stream().map(n -> {
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(StringUtils.isNotEmpty(n.getProjectStage()))n.setProjectStage(dictDataService.selectDictLabel("PROJECT_STAGE",n.getProjectStage()));
                    if(StringUtils.isNotEmpty(n.getProjectState()))n.setProjectState(dictDataService.selectDictLabel("PROJECT_STATE",n.getProjectState()));
                    if(StringUtils.isNotEmpty(n.getProjectStatus())){
                        if("0".equals(n.getProjectStatus())){
                            n.setProjectStatus("正常");
                        }else if("1".equals(n.getProjectStatus())){
                            n.setProjectStatus("终止");
                        }else if("2".equals(n.getProjectStatus())){
                            n.setProjectStatus("观望");
                        }
                    }
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<ProjectTerminationVo> util = new ExcelUtil<ProjectTerminationVo>(ProjectTerminationVo.class);
        util.exportExcel(response, list, "所有项目终止流程数据");
    }
    /**
     * 查询可申请项目终止列表
     */
    @ApiOperation("查询可申请项目终止列表")
    @GetMapping("/applicableList")
    public TableDataInfo applicableList(TProjectPool tProjectPool)
    {
        startPage();
        //tProjectPool.setProjectStage("5");
        tProjectPool.setDelFlag("0");//
        tProjectPool.setTerminationFlag("0");//未发起终止的
        tProjectPool.setStatus(3);//项目机会流程状态为审批通过
        tProjectPool.setProjectStatus(tProjectPool.getProjectStatus());//观望
        List<TProjectPool> list = investmentOpportunityService.selectApplicableProjectList(tProjectPool);
        return getDataTable(list);
    }
    //我的待办任务
    @ApiOperation("我的待办任务列表")
    @GetMapping("/getMyTaskList")
    public TableDataInfo getMyTaskList(TProjectTermination tProjectTermination)
    {
        startPage();
        //tProjectPool.setProjectStage("1");
        List<ProjectTerminationVo> list=terminationService.getMyTaskList(getUserId().toString(),tProjectTermination);
        return getDataTable(list);
    }
    //我的已办任务
    @ApiOperation("我的已办任务列表")
    @GetMapping("/getMyDoneTaskList")
    public TableDataInfo getMyDoneTaskList(TProjectTermination tProjectTermination)
    {
        startPage();
        //tProjectPool.setProjectStage("1");
        List<ProjectTerminationVo> list=terminationService.getMyDoneTaskList(getUserId().toString(),tProjectTermination);
        return getDataTable(list);
    }
    //我的待办任务
    @ApiOperation("我的单据列表")
    @GetMapping("/getMyList")
    public TableDataInfo getMyList(TProjectTermination tProjectTermination)
    {
        startPage();
        //tProjectPool.setProjectStage("1");
        List<ProjectTerminationVo> list=terminationService.getMyList(getUserId().toString(),tProjectTermination);
        return getDataTable(list);
    }
    /**
     * 导出我的已办任务列表
     */
    @ApiOperation("导出我的待办任务列表")
    @Log(title = "我的待办任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMyTask")
    public void exportMyTask(HttpServletResponse response, TProjectTermination tProjectTermination)
    {
        List<ProjectTerminationVo> list=terminationService.getMyTaskList(getUserId().toString(),tProjectTermination)
                .stream().map(n -> {
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(StringUtils.isNotEmpty(n.getProjectStage()))n.setProjectStage(dictDataService.selectDictLabel("PROJECT_STAGE",n.getProjectStage()));
                    if(StringUtils.isNotEmpty(n.getProjectState()))n.setProjectState(dictDataService.selectDictLabel("PROJECT_STATE",n.getProjectState()));
                    if(StringUtils.isNotEmpty(n.getProjectStatus())){
                        if("0".equals(n.getProjectStatus())){
                            n.setProjectStatus("正常");
                        }else if("1".equals(n.getProjectStatus())){
                            n.setProjectStatus("终止");
                        }else if("2".equals(n.getProjectStatus())){
                            n.setProjectStatus("观望");
                        }
                    }
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<ProjectTerminationVo> util = new ExcelUtil<ProjectTerminationVo>(ProjectTerminationVo.class);
        util.exportExcel(response, list, "我的待办任务数据");
    }

    /**
     * 导出我的已办任务列表
     */
    @ApiOperation("导出我的已办任务列表")
    @Log(title = "我的已办任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMyDoneTask")
    public void exportMyDoneTask(HttpServletResponse response, TProjectTermination tProjectTermination)
    {
        List<ProjectTerminationVo> list=terminationService.getMyDoneTaskList(getUserId().toString(),tProjectTermination)
                .stream().map(n -> {
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(StringUtils.isNotEmpty(n.getProjectStage()))n.setProjectStage(dictDataService.selectDictLabel("PROJECT_STAGE",n.getProjectStage()));
                    if(StringUtils.isNotEmpty(n.getProjectState()))n.setProjectState(dictDataService.selectDictLabel("PROJECT_STATE",n.getProjectState()));
                    if(StringUtils.isNotEmpty(n.getProjectStatus())){
                        if("0".equals(n.getProjectStatus())){
                            n.setProjectStatus("正常");
                        }else if("1".equals(n.getProjectStatus())){
                            n.setProjectStatus("终止");
                        }else if("2".equals(n.getProjectStatus())){
                            n.setProjectStatus("观望");
                        }
                    }
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<ProjectTerminationVo> util = new ExcelUtil<ProjectTerminationVo>(ProjectTerminationVo.class);
        util.exportExcel(response, list, "我的已办任务数据");
    }
    /**
     * 导出我的单据列表
     */
    @ApiOperation("导出我的单据列表")
    @Log(title = "我的单据列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMy")
    public void exportMy(HttpServletResponse response, TProjectTermination tProjectTermination)
    {
        List<ProjectTerminationVo> list=terminationService.getMyList(getUserId().toString(),tProjectTermination)
                .stream().map(n -> {
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(StringUtils.isNotEmpty(n.getProjectStage()))n.setProjectStage(dictDataService.selectDictLabel("PROJECT_STAGE",n.getProjectStage()));
                    if(StringUtils.isNotEmpty(n.getProjectState()))n.setProjectState(dictDataService.selectDictLabel("PROJECT_STATE",n.getProjectState()));
                    if(StringUtils.isNotEmpty(n.getProjectStatus())){
                        if("0".equals(n.getProjectStatus())){
                            n.setProjectStatus("正常");
                        }else if("1".equals(n.getProjectStatus())){
                            n.setProjectStatus("终止");
                        }else if("2".equals(n.getProjectStatus())){
                            n.setProjectStatus("观望");
                        }
                    }
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<ProjectTerminationVo> util = new ExcelUtil<ProjectTerminationVo>(ProjectTerminationVo.class);
        util.exportExcel(response, list, "我的单据数据");
    }
    /**
     * 获取项目池详细信息
     */
    @ApiOperation("获取项目池详细信息")
    @GetMapping(value = "/{pInstId}")
    public AjaxResult getInfo(@PathVariable("pInstId") String pInstId)
    {
        return success(terminationService.selectTProjectTerminationByProcInstId(pInstId));
    }
    //发起流程
    @ApiOperation(value = "投资机会申请发起流程")
    @Log(title = "发起流程", businessType = BusinessType.INSERT)
    @PostMapping("/start")
    public AjaxResult start(@RequestBody TProjectTermination tProjectTermination) {

       //return terminationService.set
        return terminationService.start(tProjectTermination);
    }
    @ApiOperation(value = "审批任务")
    @PutMapping(value = "/complete")
    public AjaxResult complete(@RequestBody TerminationDto dto, HttpServletRequest request) throws Exception  {
        // 从 DTO 中拆分参数
        TProjectTermination tProjectTermination= dto.getProjectTermination();
        FlowBaseInfo flowBaseInfo= dto.getFlowBaseInfo();
        return terminationService.complete(tProjectTermination,flowBaseInfo,request);
    }
    /**
     * 获取通过流程id会议记录详细信息 （会议投决）
     */
    @ApiOperation("获取会议记录详细信息")
    @GetMapping(value = "/getMeetingInfo/{pInstId}")
    public AjaxResult getMeetingInfo(@PathVariable("pInstId") String pInstId) {

        return success(terminationService.selectTProjectMeetingByProcInstId(pInstId));
    }
    /**
     * 根据流程Id查询汇总分数和平均分数 （总裁室）
     */
    @ApiOperation("根据会议ID查询汇总分数和平均分数")
    @GetMapping(value = "/getSumScore/{pInstId}")
    public AjaxResult getSumScore(@PathVariable("pInstId") String pInstId)
    {
        if (StringUtils.isEmpty(pInstId))
        {
            return error("查询打分数据'" + getNickName() + "'失败，请选择会议！");
        }
        return success(terminationService.selectSumScore(pInstId));
    }

    /**
     * 根据项目ID获取最新终止申请
     */
    @ApiOperation("根据项目ID获取最新立项申请")
    @GetMapping(value = "/listProjectPoolIdNew")
    public AjaxResult listProjectPoolIdNew(String projectPoolId)
    {
        return success(terminationService.listProjectPoolIdNew(projectPoolId));
    }
}
