package com.ruoyi.web.controller.invest;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.invest.domain.*;
import com.ruoyi.invest.domain.dto.InvestDto;
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.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.service.ITProjectInvestigateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 尽调关联Controller
 * 
 * @author zjc
 * @date 2024-03-13
 */
@Api(tags = "尽职背调")
@RestController
@RequestMapping("/invest/investigate")
public class TProjectInvestigateController extends BaseController
{
    @Autowired
    private ITProjectInvestigateService tProjectInvestigateService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;


    /**
     * 查询尽调关联列表
     */
    @ApiOperation("查询尽调关联列表")
    @GetMapping("/list")
    public TableDataInfo list(TProjectInvestigate tProjectInvestigate)
    {
        startPage();
        List<TProjectInvestigate> list = tProjectInvestigateService.selectTProjectInvestigateList(tProjectInvestigate);
        return getDataTable(list);
    }

    /**
     * 导出尽调关联列表
     */
    @ApiOperation("导出尽调关联列表")
    @Log(title = "尽调关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectInvestigate tProjectInvestigate)
    {
        List<TProjectInvestigate> list = tProjectInvestigateService.selectTProjectInvestigateList(tProjectInvestigate);
        ExcelUtil<TProjectInvestigate> util = new ExcelUtil<TProjectInvestigate>(TProjectInvestigate.class);
        util.exportExcel(response, list, "尽调关联数据");
    }

    /**
     * 获取尽调关联详细信息
     */
    @ApiOperation("获取尽调关联详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectInvestigateService.selectTProjectInvestigateById(id));
    }

    /**
     * 新增尽调关联
     */
    @ApiOperation("新增尽调关联")
    @Log(title = "尽调关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectInvestigate tProjectInvestigate)
    {
        return tProjectInvestigateService.start(tProjectInvestigate);
    }

    /**
     * 修改尽调关联
     */
    @ApiOperation("修改尽调关联")
    @Log(title = "尽调关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectInvestigate tProjectInvestigate)
    {
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectInvestigate.getListFile(),
                tProjectInvestigate.getProjectPoolId(),
                tProjectInvestigate.getId(),//尽调ID
                String.valueOf(FileType.INVESTIGATE.ordinal()),//文件类型:尽调
                getNickName());

        return toAjax(tProjectInvestigateService.updateTProjectInvestigate(tProjectInvestigate));
    }

    /**
     * 删除尽调关联
     */
    @ApiOperation("删除尽调关联")
    @Log(title = "尽调关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tProjectInvestigateService.updateTProjectInvestigateByIds(ids));
    }

    /**
     * 根据项目ID获取尽调
     */
    @ApiOperation("根据项目ID获取尽调数据")
    @GetMapping(value = "/listProjectPoolId")
    public AjaxResult listProjectPoolId(String projectPoolId)
    {
        if (StringUtils.isEmpty(projectPoolId))
        {
            return error("查询尽调数据失败，请提供项目ID！");
        }
        List<TProjectInvestigate> tProjectInvestigate = tProjectInvestigateService.listProjectPoolId(projectPoolId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("tProjectInvestigate", tProjectInvestigate);
        return ajax;
    }

    /**
     * 修改尽调完成
     */
    @ApiOperation("修改尽调完成")
    @Log(title = "修改尽调完成", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody String id)
    {
        return toAjax(tProjectInvestigateService.editStatus(id));
    }

    /**
     * 根据项目ID获取最新尽调完成
     */
    @ApiOperation("根据项目ID获取最新尽调")
    @GetMapping(value = "/listProjectPoolIdNew")
    public AjaxResult listProjectPoolIdNew(String projectPoolId)
    {
        return success(tProjectInvestigateService.listProjectPoolIdNew(projectPoolId));
    }

    //我的待办任务
    @ApiOperation("我的待办任务列表")
    @GetMapping("/getMyTaskList")
    public TableDataInfo getMyTaskList(TProjectInvestigateVo tProjectInvestigate)
    {
        startPage();
        List<TProjectInvestigateVo> list=tProjectInvestigateService.getMyTaskList(getUserId().toString(),tProjectInvestigate);
        return getDataTable(list);
    }
    //我的已办任务
    @ApiOperation("我的已办任务列表")
    @GetMapping("/getMyDoneTaskList")
    public TableDataInfo getMyDoneTaskList(TProjectInvestigateVo tProjectInvestigate)
    {
        startPage();
        List<TProjectInvestigateVo> list=tProjectInvestigateService.getMyDoneTaskList(getUserId().toString(),tProjectInvestigate);
        return getDataTable(list);
    }
    //我的待办任务
    @ApiOperation("我的单据列表")
    @GetMapping("/getMyList")
    public TableDataInfo getMyList(TProjectInvestigateVo tProjectInvestigate)
    {
         startPage();
        List<TProjectInvestigateVo> list=tProjectInvestigateService.getMyList(getUserId().toString(),tProjectInvestigate);
        return getDataTable(list);
    }

    @ApiOperation(value = "审批任务")
    @PutMapping(value = "/complete")
    public AjaxResult complete(@RequestBody InvestDto dto, HttpServletRequest request) {
        // 从 DTO 中拆分参数
        TProjectInvestigate tProjectInvestigate = dto.getTProjectInvestigate();
        TProjectReview tProjectReview = dto.getProjectReview();
        FlowBaseInfo flowBaseInfo= dto.getFlowBaseInfo();
        return tProjectInvestigateService.complete(tProjectInvestigate,tProjectReview,flowBaseInfo,request);
    }
}
