package com.ruoyi.web.controller.invest;

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

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.FileType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.invest.domain.TProjectCirculation;
import com.ruoyi.invest.domain.TProjectRecord;
import com.ruoyi.invest.service.ITProjectCirculationService;
import com.ruoyi.invest.service.ITProjectRecordService;
import com.ruoyi.system.service.ISysDictDataService;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 跟进记录Controller
 * 
 * @author ruoyi
 * @date 2024-02-21
 */
@Api(tags = "跟进记录")
@RestController
@RequestMapping("/invest/record")
public class TProjectRecordController extends BaseController
{
    @Autowired
    private ITProjectRecordService tProjectRecordService;

    @Autowired
    private ITProjectCirculationService tProjectCirculationService;

    @Autowired
    private ITUnifyFileService tUnifyFileService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询跟进记录列表
     */
    @ApiOperation("查询跟进记录列表")
    @PreAuthorize("@ss.hasPermi('invest:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TProjectRecord tProjectRecord)
    {
        startPage();
        List<TProjectRecord> list = tProjectRecordService.selectTProjectRecordList(tProjectRecord);
        return getDataTable(list);
    }

    /**
     * 导出跟进记录列表
     */
    @ApiOperation("导出跟进记录列表")
    @PreAuthorize("@ss.hasPermi('invest:record:export')")
    @Log(title = "跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProjectRecord tProjectRecord)
    {
        List<TProjectRecord> list = tProjectRecordService.selectTProjectRecordList(tProjectRecord)
                .stream().map(n -> {
                    n.setProjectId(n.gettProjectPool().getProjectName());
                    n.setUpWay(dictDataService.selectDictLabel("UP_WAY",n.getUpWay()));
                    return n;
                })
                .collect(Collectors.toList());
        ExcelUtil<TProjectRecord> util = new ExcelUtil<TProjectRecord>(TProjectRecord.class);
        util.exportExcel(response, list, "跟进记录数据");
    }

    /**
     * 获取跟进记录详细信息
     */
    @ApiOperation("获取跟进记录详细信息")
    @PreAuthorize("@ss.hasPermi('invest:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tProjectRecordService.selectTProjectRecordById(id));
    }

    /**
     * 新增跟进记录
     */
    @ApiOperation("新增跟进记录")
    @PreAuthorize("@ss.hasPermi('invest:record:add')")
    @Log(title = "跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProjectRecord tProjectRecord)
    {
        tProjectRecord.setId(IdUtils.fastSimpleUUID());
        tProjectRecord.setCreateBy(getNickName());
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectRecord.getListFile(),
                tProjectRecord.getProjectId(),
                tProjectRecord.getId(),
                String.valueOf(FileType.RECORD.ordinal()),
                getNickName());

        // todo 增加跟进记录创建记录
        tProjectCirculationService.insertTProjectCirculation(tProjectRecord.getProjectId(),tProjectRecord.getUpPerson()+"(跟进记录)",getNickName());

        return toAjax(tProjectRecordService.insertTProjectRecord(tProjectRecord));
    }

    /**
     * 修改跟进记录
     */
    @ApiOperation("修改跟进记录")
    @PreAuthorize("@ss.hasPermi('invest:record:edit')")
    @Log(title = "跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProjectRecord tProjectRecord)
    {
        // todo 保存附件信息
        tUnifyFileService.insertTUnifyFileList(tProjectRecord.getListFile(),
                tProjectRecord.getProjectId(),
                tProjectRecord.getId(),
                String.valueOf(FileType.RECORD.ordinal()),
                getNickName());

        return toAjax(tProjectRecordService.updateTProjectRecord(tProjectRecord));
    }

    /**
     * 删除跟进记录
     */
    @ApiOperation("删除跟进记录")
    @PreAuthorize("@ss.hasPermi('invest:record:remove')")
    @Log(title = "跟进记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //删除附件
        tUnifyFileService.updateTUnifyFileBusinessIds(ids);
        return toAjax(tProjectRecordService.updateTProjectRecordByIds(ids));
    }

    /**
     * 根据项目ID获取跟进记录
     */
    @ApiOperation("根据项目ID获取跟进记录")
    @PreAuthorize("@ss.hasPermi('invest:meeting:query')")
    @GetMapping(value = "/listPorjectId")
    public AjaxResult listPorjectId(String projectId)
    {
        return success(tProjectRecordService.listProjectId(projectId));
    }
}
