package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.annotation.DataScope;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.domain.customer.CustomerFollowRecord;
import com.dsk.system.domain.customer.dto.CustomerFollowRecordSearchDto;
import com.dsk.system.mapper.CustomerFollowRecordMapper;
import com.dsk.system.service.ICustomerFollowRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户跟进记录(CustomerFollowRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-05-18 15:07:59
 */
@Slf4j
@Service
public class CustomerFollowRecordServiceImpl implements ICustomerFollowRecordService {

    @Resource
    private CustomerFollowRecordMapper baseMapper;

    @Override
//    @DataScope(deptAlias = "d",userAlias = "u")
    public List<CustomerFollowRecord> selectList(CustomerFollowRecordSearchDto dto) {
        LambdaQueryWrapper<CustomerFollowRecord> wrapper = Wrappers.lambdaQuery();
        if (!ObjectUtils.isEmpty(dto.getCustomerId())) {
            wrapper.eq(CustomerFollowRecord::getCustomerId, dto.getCustomerId());
        }
        wrapper.eq(CustomerFollowRecord::getUserId, SecurityUtils.getUserId())
                .orderByDesc(CustomerFollowRecord::getCreateTime);
        return baseMapper.selectList(wrapper);

//        dto.setUserId(SecurityUtils.getUserId());
//        return baseMapper.selectList(dto);
    }

    @Override
    public boolean add(CustomerFollowRecord followRecord) {
        if (ObjectUtils.isEmpty(followRecord.getContent())) throw new BeanException("跟进内容不能为空");
        if (ObjectUtils.isEmpty(followRecord.getCustomerId())) throw new BeanException("跟进客户不能为空");
        followRecord.setUserId(SecurityUtils.getUserId());
        return baseMapper.insert(followRecord) != 0;
    }
}
