package com.ruoyi.project.business.service.impl;

import java.util.List;

import com.ruoyi.common.enums.FileBuzType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.TbFile;
import com.ruoyi.project.business.domain.TcAccountContactHistory;
import com.ruoyi.project.business.domain.TcAccountHistory;
import com.ruoyi.project.business.mapper.TbFileMapper;
import com.ruoyi.project.business.mapper.TcAccountContactHistoryMapper;
import com.ruoyi.project.business.mapper.TcAccountHistoryMapper;
import com.ruoyi.project.business.service.ITcAccountHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 客户（组织）履历Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-23
 */
@Service
public class TcAccountHistoryServiceImpl implements ITcAccountHistoryService
{
    @Autowired
    private TcAccountHistoryMapper tcAccountHistoryMapper;

    @Autowired
    private TcAccountContactHistoryMapper tcAccountContactHistoryMapper;

    @Resource
    private TbFileMapper tbFileMapper;

    /**
     * 查询客户（组织）履历
     * 
     * @param id 客户（组织）履历主键
     * @return 客户（组织）履历
     */
    @Override
    public TcAccountHistory selectTcAccountHistoryById(Long id)
    {
        TcAccountHistory tcAccountHistory = tcAccountHistoryMapper.selectTcAccountHistoryById(id);
        // 再查询客户联系人履历
        TcAccountContactHistory tcAccountContactHistory = new TcAccountContactHistory();
        tcAccountContactHistory.setAccountId(tcAccountHistory.getAccountId());
        tcAccountContactHistory.setVersion(tcAccountHistory.getVersion());
        List<TcAccountContactHistory> contactList = tcAccountContactHistoryMapper.selectTcAccountContactHistoryList(tcAccountContactHistory);
        tcAccountHistory.setContactList(contactList);
        // 再查询附件
        TbFile tbFile = new TbFile();
        tbFile.setBusinessId(id);
        tbFile.setBusinessType(FileBuzType.ACCOUNT_HISTORY.getType());
        List<TbFile> fileList = tbFileMapper.selectTbFileList(tbFile);
        tcAccountHistory.setFileList(fileList);
        return tcAccountHistory;
    }

    /**
     * 查询客户（组织）履历列表
     * 
     * @param tcAccountHistory 客户（组织）履历
     * @return 客户（组织）履历
     */
    @Override
    public List<TcAccountHistory> selectTcAccountHistoryList(TcAccountHistory tcAccountHistory)
    {
        return tcAccountHistoryMapper.selectTcAccountHistoryList(tcAccountHistory);
    }

    /**
     * 新增客户（组织）履历
     * 
     * @param tcAccountHistory 客户（组织）履历
     * @return 结果
     */
    @Override
    public int insertTcAccountHistory(TcAccountHistory tcAccountHistory)
    {
        tcAccountHistory.setCreateTime(DateUtils.getNowDate());
        tcAccountHistory.setCreateBy(SecurityUtils.getUserId().toString());
        return tcAccountHistoryMapper.insertTcAccountHistory(tcAccountHistory);
    }

    /**
     * 修改客户（组织）履历
     * 
     * @param tcAccountHistory 客户（组织）履历
     * @return 结果
     */
    @Override
    public int updateTcAccountHistory(TcAccountHistory tcAccountHistory)
    {
        tcAccountHistory.setUpdateTime(DateUtils.getNowDate());
        return tcAccountHistoryMapper.updateTcAccountHistory(tcAccountHistory);
    }

    /**
     * 批量删除客户（组织）履历
     * 
     * @param ids 需要删除的客户（组织）履历主键
     * @return 结果
     */
    @Override
    public int deleteTcAccountHistoryByIds(Long[] ids)
    {
        return tcAccountHistoryMapper.deleteTcAccountHistoryByIds(ids);
    }

    /**
     * 删除客户（组织）履历信息
     * 
     * @param id 客户（组织）履历主键
     * @return 结果
     */
    @Override
    public int deleteTcAccountHistoryById(Long id)
    {
        return tcAccountHistoryMapper.deleteTcAccountHistoryById(id);
    }
}
