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

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.BmProjectInfo;
import com.ruoyi.system.domain.project.BmProjectReferral;
import com.ruoyi.system.mapper.project.BmProjectInfoMapper;
import com.ruoyi.system.mapper.project.BmProjectReferralMapper;
import com.ruoyi.system.service.project.IBmProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 项目-线索信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-04
 */
@Service
public class BmProjectInfoServiceImpl implements IBmProjectInfoService
{
    @Autowired
    private BmProjectInfoMapper bmProjectInfoMapper;

    @Autowired
    private BmProjectReferralMapper bmProjectReferralMapper;

    /**
     * 查询项目-线索信息
     * 
     * @param id 项目-线索信息ID
     * @return 项目-线索信息
     */
    @Override
    public BmProjectInfo selectBmProjectInfoById(Long id)
    {
        BmProjectInfo bmProjectInfo = bmProjectInfoMapper.selectBmProjectInfoById(id);
        return bmProjectInfo;
    }

    /**
     * 查询项目-线索信息列表
     * 
     * @param bmProjectInfo 项目-线索信息
     * @return 项目-线索信息
     */
    @Override
    public List<BmProjectInfo> selectBmProjectInfoList(BmProjectInfo bmProjectInfo)
    {
        return bmProjectInfoMapper.selectBmProjectInfoList(bmProjectInfo);
    }

    /**
     * 新增项目-线索信息
     * 
     * @param bmProjectInfo 项目-线索信息
     * @return 结果
     */
    @Override
    public int insertBmProjectInfo(BmProjectInfo bmProjectInfo)
    {
        bmProjectInfo.setCreateTime(DateUtils.getNowDate());
        bmProjectInfo.setCreateBy(SecurityUtils.getUsername());
        List<BmProjectReferral> referralList = bmProjectInfo.getReferralList();
        if (referralList != null && referralList.size()> 0){
            for (BmProjectReferral bmProjectReferral : referralList) {
                if (StringUtils.isNotBlank(bmProjectReferral.getSysDeptId())){
                    bmProjectReferral.setCreateBy(SecurityUtils.getUsername());
                    bmProjectReferral.setCreateTime(DateUtils.getNowDate());
                    bmProjectReferralMapper.insertBmProjectReferral(bmProjectReferral);
                }
            }
        }
        return bmProjectInfoMapper.insertBmProjectInfo(bmProjectInfo);
    }

    /**
     * 修改项目-线索信息
     * 
     * @param bmProjectInfo 项目-线索信息
     * @return 结果
     */
    @Override
    public int updateBmProjectInfo(BmProjectInfo bmProjectInfo)
    {
        bmProjectInfo.setUpdateTime(DateUtils.getNowDate());
        bmProjectInfo.setUpdateBy(SecurityUtils.getUsername());
        List<BmProjectReferral> referralList = bmProjectInfo.getReferralList();
        if (referralList != null && referralList.size()> 0){
            for (BmProjectReferral bmProjectReferral : referralList) {
                if (bmProjectReferral.getId() != null){
                    bmProjectReferralMapper.updateBmProjectReferral(bmProjectReferral);
                }
            }
        }
        return bmProjectInfoMapper.updateBmProjectInfo(bmProjectInfo);
    }

    /**
     * 批量删除项目-线索信息
     * 
     * @param ids 需要删除的项目-线索信息ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectInfoByIds(Long[] ids)
    {
        return bmProjectInfoMapper.deleteBmProjectInfoByIds(ids);
    }

    /**
     * 删除项目-线索信息信息
     * 
     * @param id 项目-线索信息ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectInfoById(Long id)
    {
        return bmProjectInfoMapper.deleteBmProjectInfoById(id);
    }
}
