package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanException;
import com.dsk.common.core.domain.model.LoginUser;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.domain.customer.ContactInfo;
import com.dsk.system.domain.customer.dto.ContactInfoSearchDto;
import com.dsk.system.domain.customer.vo.ContactInfoListVo;
import com.dsk.system.mapper.ContactInfoMapper;
import com.dsk.system.service.IContactInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * 用户联系人(CustomerContact)表服务实现类
 *
 * @author makejava
 * @since 2023-07-25 17:18:04
 */
@Slf4j
@Service
public class ContactInfoServiceImpl implements IContactInfoService {

    @Resource
    private ContactInfoMapper baseMapper;

    @Override
    public List<ContactInfoListVo> selectList(ContactInfoSearchDto dto) {
        return baseMapper.selectPageList(dto);
    }

    @Override
    public ContactInfo selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean insert(ContactInfo bean) {
        verifyParameter(bean);
        return baseMapper.insert(bean) > 0;
    }

    @Override
    public boolean update(ContactInfo bean) {
        if (ObjectUtils.isEmpty(bean.getId())) throw new BeanException("id不能为空！");
        verifyParameter(bean);
        return baseMapper.updateById(bean) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 参数验证
     */
    private void verifyParameter(ContactInfo bean) {
//        if (ObjectUtils.isEmpty(bean.getCustomerId())) throw new BeanException("客户id不能为空！");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bean.setCreateBy(loginUser.getUser().getNickName());
        bean.setUpdateId(loginUser.getUserId());
        bean.setUpdateBy(loginUser.getUser().getNickName());
    }
}
