package org.hzhub.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hzhub.common.core.constant.SystemConstants;
import org.hzhub.common.core.utils.MapstructUtils;
import org.hzhub.common.satoken.utils.LoginHelper;
import org.hzhub.crm.domain.CrmLeadFollow;
import org.hzhub.crm.domain.bo.CrmLeadFollowBo;
import org.hzhub.crm.domain.vo.CrmLeadFollowVo;
import org.hzhub.crm.mapper.CrmLeadFollowMapper;
import org.hzhub.crm.service.ICrmLeadFollowService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CRM线索跟进记录 Service 实现
 *
 * @author hzhub
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CrmLeadFollowServiceImpl implements ICrmLeadFollowService {

    private final CrmLeadFollowMapper followMapper;

    @Override
    public List<CrmLeadFollowVo> selectFollowRecordsByLeadId(Long leadId) {
        LambdaQueryWrapper<CrmLeadFollow> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CrmLeadFollow::getLeadId, leadId)
            .eq(CrmLeadFollow::getDelFlag, SystemConstants.NORMAL)
            .orderByDesc(CrmLeadFollow::getCreateTime);
        return followMapper.selectVoList(wrapper);
    }

    @Override
    public int insertFollowRecord(CrmLeadFollowBo follow) {
        // 设置跟进人为当前用户
        if (ObjectUtil.isNull(follow.getFollowUserId())) {
            follow.setFollowUserId(LoginHelper.getUserId());
        }

        CrmLeadFollow crmFollow = MapstructUtils.convert(follow, CrmLeadFollow.class);
        return followMapper.insert(crmFollow);
    }
}