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.service.ITProjectCirculationService;
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.TProjectInvestigatePerson;
import com.ruoyi.invest.service.ITProjectInvestigatePersonService;
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/person")
public class TProjectInvestigatePersonController extends BaseController
{
    @Autowired
    private ITProjectInvestigatePersonService tProjectInvestigatePersonService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    /**
     * 查询尽调人员关联表列表
     */
    @ApiOperation("查询尽调人员关联表列表")
    @GetMapping("/list")
    public TableDataInfo list(TProjectInvestigatePerson tProjectInvestigatePerson)
    {
        startPage();
        List<TProjectInvestigatePerson> list = tProjectInvestigatePersonService.selectTProjectInvestigatePersonList(tProjectInvestigatePerson);
        return getDataTable(list);
    }

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

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

    /**
     * 新增尽调人员关联表
     */
    @ApiOperation("新增尽调人员关联表")
    @Log(title = "尽调人员关联表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectInvestigatePerson tProjectInvestigatePerson)
    {
        if(StringUtils.isNull(tProjectInvestigatePerson.getId())){
            tProjectInvestigatePerson.setId(IdUtils.fastSimpleUUID());
        }
        tProjectInvestigatePerson.setInvestigatePerson(getNickName());
        tProjectInvestigatePerson.setInvestigatePersonId(getUserId().toString());
        tProjectInvestigatePerson.setDeptId(getDeptId());
        // todo 保存附件信息
        List<TUnifyFile> tUnifyFileList = tProjectInvestigatePerson.getListFile();
        if(null != tUnifyFileList && !tUnifyFileList.isEmpty()){
            for (TUnifyFile tUnifyFile:
                    tUnifyFileList) {
                if(tUnifyFile.getId() == null){
                    tUnifyFile.setFileId(tProjectInvestigatePerson.getProjectPoolId());
                    tUnifyFile.setFileBusinessId(tProjectInvestigatePerson.getId());//尽调ID
                    tUnifyFile.setUploadType(String.valueOf(FileType.INVESTIGATE.ordinal()));//文件类型:尽调
                    tUnifyFile.setCreateBy(getNickName());
                    tUnifyFileService.insertTUnifyFile(tUnifyFile);
                }
            }
        }
        // todo 增加项目创建记录
        TProjectCirculation tProjectCirculation = new TProjectCirculation();
        tProjectCirculation.setProjectPoolId(tProjectInvestigatePerson.getProjectPoolId());
        tProjectCirculation.setStageName("上传尽调报告");
        tProjectCirculation.setCreateBy(getNickName());
        tProjectCirculationService.insertTProjectCirculation(tProjectCirculation);

        return toAjax(tProjectInvestigatePersonService.insertTProjectInvestigatePerson(tProjectInvestigatePerson));
    }

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

    /**
     * 删除尽调人员关联表
     */
    @ApiOperation("删除尽调人员关联表")
    @Log(title = "尽调人员关联表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //删除附件
        tUnifyFileService.updateTUnifyFileBusinessIds(ids);
        return toAjax(tProjectInvestigatePersonService.deleteTProjectInvestigatePersonByIds(ids));
    }

    /**
     * 根据尽调申请ID获取尽调人员关联表详细信息
     */
    @ApiOperation("获取尽调人员关联表详细信息")
    @GetMapping(value = "/getProjectInvestigateId")
    public AjaxResult getProjectInvestigateId(String projectInvestigateId)
    {
        return success(tProjectInvestigatePersonService.selectByProjectInvestigateId(projectInvestigateId,getUserId()));
    }

    /**
     * 获取尽调人员关联表详细信息集合
     */
    @ApiOperation("获取尽调人员关联表详细信息集合")
    @GetMapping(value = "/selectProjectInvestigateIdList")
    public AjaxResult selectProjectInvestigateIdList(String projectInvestigateId)
    {
        return success(tProjectInvestigatePersonService.selectProjectInvestigateIdList(projectInvestigateId));
    }



}