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

/**
 * 项目池Controller
 * 
 * @author ruoyi
 * @date 2024-02-21
 */
@Api(tags = "投资机会")
@RestController
@RequestMapping("/project/investmentOpportunity")
public class InvestmentOpportunityController extends BaseController
{
    @Autowired
    private IInvestmentOpportunityService investmentOpportunityService;

    @Autowired
    private ITProjectCompanyService tProjectCompanyService;

    @Autowired
    private ITProjectContactsService tProjectContactsService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ITProjectPoolService tProjectPoolService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询所有投资机会流程
     */
    @ApiOperation("查询所有投资机会流程")
    @GetMapping("/listAll")
    public TableDataInfo listAll(TProjectPool tProjectPool)
    {
        startPage();
        List<TProjectPoolVo> list=investmentOpportunityService.selectAllInvestmentOpportunityList(tProjectPool);
        return getDataTable(list);
    }
    /**
     * 导出所有投资机会流程列表
     */
    @ApiOperation("导出所有投资机会流程列表")
    @Log(title = "所有投资机会流程列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectPool tProjectPool)
    {
        List<TProjectPoolVo> list=investmentOpportunityService.selectAllInvestmentOpportunityList(tProjectPool)
                .stream().map(n -> {
                    if(null != n.gettProjectChannel()){
                        n.setChannel(n.gettProjectChannel().getChannelName());
                    }
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(null != n.gettProjectCompany()){
                        n.setCompany(n.gettProjectCompany().getCompanyName());
                    }
                    if(null != n.gettProjectContacts()){
                        n.setProjectContacts(n.gettProjectContacts().getName());
                    }
                    if(StringUtils.isNotEmpty(n.getInvestType()))n.setInvestType(dictDataService.selectDictLabel("INVEST_TYPE",n.getInvestType()));
                    if(StringUtils.isNotEmpty(n.getInvestPloy()))n.setInvestPloy(dictDataService.selectDictLabel("INVEST_PLOY",n.getInvestPloy()));
                    if(StringUtils.isNotEmpty(n.getInvestWorth()))n.setInvestWorth(dictDataService.selectDictLabel("INVEST_WORTH",n.getInvestWorth()));
                    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()));
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<TProjectPoolVo> util = new ExcelUtil<TProjectPoolVo>(TProjectPoolVo.class);
        util.exportExcel(response, list, "所有投资机会流程数据");
    }
    //我的待办任务
    @ApiOperation("我的待办任务列表")
    @GetMapping("/getMyTaskList")
    public TableDataInfo getMyTaskList(TProjectPool tProjectPool)
    {
        startPage();
        List<TProjectPoolVo> list=investmentOpportunityService.getMyTaskList(getUserId().toString(),tProjectPool);
        return getDataTable(list);
    }
    //我的已办任务
    @ApiOperation("我的已办任务列表")
    @GetMapping("/getMyDoneTaskList")
    public TableDataInfo getMyDoneTaskList(TProjectPool tProjectPool)
    {
        startPage();
        List<TProjectPoolVo> list=investmentOpportunityService.getMyDoneTaskList(getUserId().toString(),tProjectPool);
        return getDataTable(list);
    }
    //我的待办任务
    @ApiOperation("我的单据列表")
    @GetMapping("/getMyList")
    public TableDataInfo getMyList(TProjectPool tProjectPool)
    {
        startPage();
        List<TProjectPoolVo> list=investmentOpportunityService.getMyList(getUserId().toString(),tProjectPool);
        return getDataTable(list);
    }
    /**
     * 导出我的已办任务列表
     */
    @ApiOperation("导出我的待办任务列表")
    @Log(title = "我的待办任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMyTask")
    public void exportMyTask(HttpServletResponse response, TProjectPool tProjectPool)
    {
        List<TProjectPoolVo> list=investmentOpportunityService.getMyTaskList(getUserId().toString(),tProjectPool)
                .stream().map(n -> {
                    if(null != n.gettProjectChannel()){
                        n.setChannel(n.gettProjectChannel().getChannelName());
                    }
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(null != n.gettProjectCompany()){
                        n.setCompany(n.gettProjectCompany().getCompanyName());
                    }
                    if(null != n.gettProjectContacts()){
                        n.setProjectContacts(n.gettProjectContacts().getName());
                    }
                    if(StringUtils.isNotEmpty(n.getInvestType()))n.setInvestType(dictDataService.selectDictLabel("INVEST_TYPE",n.getInvestType()));
                    if(StringUtils.isNotEmpty(n.getInvestPloy()))n.setInvestPloy(dictDataService.selectDictLabel("INVEST_PLOY",n.getInvestPloy()));
                    if(StringUtils.isNotEmpty(n.getInvestWorth()))n.setInvestWorth(dictDataService.selectDictLabel("INVEST_WORTH",n.getInvestWorth()));
                    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<TProjectPoolVo> util = new ExcelUtil<TProjectPoolVo>(TProjectPoolVo.class);
        util.exportExcel(response, list, "我的待办任务数据");
    }

    /**
     * 导出我的已办任务列表
     */
    @ApiOperation("导出我的已办任务列表")
    @Log(title = "我的已办任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMyDoneTask")
    public void exportMyDoneTask(HttpServletResponse response, TProjectPool tProjectPool)
    {
        List<TProjectPoolVo> list=investmentOpportunityService.getMyDoneTaskList(getUserId().toString(),tProjectPool)
                .stream().map(n -> {
                    if(null != n.gettProjectChannel()){
                        n.setChannel(n.gettProjectChannel().getChannelName());
                    }
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(null != n.gettProjectCompany()){
                        n.setCompany(n.gettProjectCompany().getCompanyName());
                    }
                    if(null != n.gettProjectContacts()){
                        n.setProjectContacts(n.gettProjectContacts().getName());
                    }
                    if(StringUtils.isNotEmpty(n.getInvestType()))n.setInvestType(dictDataService.selectDictLabel("INVEST_TYPE",n.getInvestType()));
                    if(StringUtils.isNotEmpty(n.getInvestPloy()))n.setInvestPloy(dictDataService.selectDictLabel("INVEST_PLOY",n.getInvestPloy()));
                    if(StringUtils.isNotEmpty(n.getInvestWorth()))n.setInvestWorth(dictDataService.selectDictLabel("INVEST_WORTH",n.getInvestWorth()));
                    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<TProjectPoolVo> util = new ExcelUtil<TProjectPoolVo>(TProjectPoolVo.class);
        util.exportExcel(response, list, "我的已办任务数据");
    }
    /**
     * 导出我的单据列表
     */
    @ApiOperation("导出我的单据列表")
    @Log(title = "我的单据列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMy")
    public void exportMy(HttpServletResponse response, TProjectPool tProjectPool)
    {
        List<TProjectPoolVo> list=investmentOpportunityService.getMyList(getUserId().toString(),tProjectPool)
                .stream().map(n -> {
                    if(null != n.gettProjectChannel()){
                        n.setChannel(n.gettProjectChannel().getChannelName());
                    }
                    if(StringUtils.isNotEmpty(n.getIndustry()))n.setIndustry(dictDataService.selectDictLabel("CUSTOMER_TRADE",n.getIndustry()));
                    if(null != n.gettProjectCompany()){
                        n.setCompany(n.gettProjectCompany().getCompanyName());
                    }
                    if(null != n.gettProjectContacts()){
                        n.setProjectContacts(n.gettProjectContacts().getName());
                    }
                    if(StringUtils.isNotEmpty(n.getInvestType()))n.setInvestType(dictDataService.selectDictLabel("INVEST_TYPE",n.getInvestType()));
                    if(StringUtils.isNotEmpty(n.getInvestPloy()))n.setInvestPloy(dictDataService.selectDictLabel("INVEST_PLOY",n.getInvestPloy()));
                    if(StringUtils.isNotEmpty(n.getInvestWorth()))n.setInvestWorth(dictDataService.selectDictLabel("INVEST_WORTH",n.getInvestWorth()));
                    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<TProjectPoolVo> util = new ExcelUtil<TProjectPoolVo>(TProjectPoolVo.class);
        util.exportExcel(response, list, "我的单据数据");
    }
    /**
     * 获取项目池详细信息
     */
    @ApiOperation("获取项目池详细信息")
    @GetMapping(value = "/{pInstId}")
    public AjaxResult getInfo(@PathVariable("pInstId") String pInstId)
    {
        return success(investmentOpportunityService.selectTProjectPoolByProcInstId(pInstId));
    }
    //发起流程
    @ApiOperation(value = "投资机会申请发起流程")
    @Log(title = "发起流程", businessType = BusinessType.INSERT)
    @PostMapping("/start")
    public AjaxResult start(
                            @RequestBody TProjectPool tProjectPool
                            ) {

        return investmentOpportunityService.start(tProjectPool);
    }

    @ApiOperation(value = "审批任务")
    @PutMapping(value = "/complete")
    public AjaxResult complete(@RequestBody InvestDto dto, HttpServletRequest request) throws Exception  {
        // 从 DTO 中拆分参数
        TProjectPool tProjectPool = dto.getProjectPool();
        TProjectReview tProjectReview = dto.getProjectReview();
        FlowBaseInfo flowBaseInfo= dto.getFlowBaseInfo();
        return investmentOpportunityService.complete(tProjectPool,tProjectReview,flowBaseInfo,request);
    }
    /**
     * 编辑项目机会
     */
    @ApiOperation("编辑项目机会")
    @Log(title = "项目池", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody TProjectPool tProjectPool)
    {
        tProjectPool.setUpdateBy(getNickName());
        TProjectCompany tProjectCompany = tProjectPool.gettProjectCompany();
        tProjectCompany.setUpdateBy(getNickName());
        TProjectContacts tProjectContacts = tProjectPool.gettProjectContacts();
        tProjectContacts.setUpdateBy(getNickName());
        tProjectContacts.setId(tProjectPool.getProjectContacts());
        // 上传并返回新文件名称
        // todo 保存公司附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectPool.getListFile(),
                tProjectPool.getId(),
                tProjectCompany.getCompanyId(),
                String.valueOf(FileType.COMPANY.ordinal()),
                getNickName());

        // todo 保存BP附件信息
        tUnifyFileService.insertTUnifyFileLists(tProjectPool.getBpFile(),
                tProjectPool.getId(),
                tProjectPool.getId(),
                String.valueOf(FileType.BP.ordinal()),
                getNickName(),"a","1");

        // todo 保存其他附件信息
        tUnifyFileService.insertTUnifyFileLists(tProjectPool.getOtherFile(),
                tProjectPool.getId(),
                tProjectPool.getId(),
                String.valueOf(FileType.OTHER.ordinal()),
                getNickName(),"k","1");

        tProjectCompanyService.updateTProjectCompany(tProjectCompany);
        tProjectContactsService.updateTProjectContacts(tProjectContacts);
        return toAjax(tProjectPoolService.updateTProjectPool(tProjectPool));
    }







}
