package com.ruoyi.web.controller.invest;

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

import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.invest.domain.TProjectCirculation;
import com.ruoyi.invest.domain.TProjectScoring;
import com.ruoyi.invest.service.ITProjectCirculationService;
import com.ruoyi.invest.service.ITProjectPoolService;
import com.ruoyi.tool.domain.TUnifyFile;
import com.ruoyi.tool.service.ITUnifyFileService;
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.TProjectInvestigate;
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 ITProjectCirculationService tProjectCirculationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ITProjectPoolService tProjectPoolService;

    /**
     * 查询尽调关联列表
     */
    @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)
    {
        tProjectInvestigate.setId(IdUtils.fastSimpleUUID());
        // todo 保存附件信息
        List<TUnifyFile> tUnifyFileList = tProjectInvestigate.getListFile();
        if(null != tUnifyFileList && !tUnifyFileList.isEmpty()){
            for (TUnifyFile tUnifyFile:
                    tUnifyFileList) {
                tUnifyFile.setFileId(tProjectInvestigate.getProjectPoolId());
                tUnifyFile.setFileBusinessId(tProjectInvestigate.getId());//尽调ID
                tUnifyFile.setUploadType(String.valueOf(FileType.INVESTIGATE.ordinal()));//文件类型:尽调
                tUnifyFile.setCreateBy(getNickName());
                tUnifyFileService.insertTUnifyFile(tUnifyFile);
            }
        }

        // todo 增加尽调创建记录
        TProjectCirculation tProjectCirculation = new TProjectCirculation();
        tProjectCirculation.setProjectPoolId(tProjectInvestigate.getProjectPoolId());
        tProjectCirculation.setStageName(tProjectInvestigate.getInvestigateName()+"(尽职背调)");
        tProjectCirculation.setCreateBy(getNickName());
        tProjectCirculationService.insertTProjectCirculation(tProjectCirculation);

        // todo 更改尽调状态
        tProjectPoolService.updateTProjectPoolByInvestigateFlag(tProjectInvestigate.getProjectPoolId(),"1");

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

    /**
     * 修改尽调关联
     */
    @ApiOperation("修改尽调关联")
    @Log(title = "尽调关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectInvestigate tProjectInvestigate)
    {
        // todo 保存附件信息
        List<TUnifyFile> tUnifyFileList = tProjectInvestigate.getListFile();
        if(null != tUnifyFileList && !tUnifyFileList.isEmpty()){
            for (TUnifyFile tUnifyFile:
                    tUnifyFileList) {
                if(tUnifyFile.getId() == null){
                    tUnifyFile.setFileId(tProjectInvestigate.getProjectPoolId());
                    tUnifyFile.setFileBusinessId(tProjectInvestigate.getId());//尽调ID
                    tUnifyFile.setUploadType(String.valueOf(FileType.INVESTIGATE.ordinal()));//文件类型:尽调
                    tUnifyFile.setCreateBy(getNickName());
                    tUnifyFileService.insertTUnifyFile(tUnifyFile);
                }
            }
        }
        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));
    }


}
