package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.domain.customer.CustomerDecisionChain;
import com.dsk.system.domain.customer.dto.CustomerDecisionChainSearchDto;
import com.dsk.system.mapper.CustomerDecisionChainMapper;
import com.dsk.system.service.ICustomerDecisionChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * 客户决策链条(CustomerDecisionChain)表服务实现类
 *
 * @author makejava
 * @since 2023-05-16 15:33:45
 */
@Slf4j
@Service
public class CustomerDecisionChainServiceImpl implements ICustomerDecisionChainService {
    @Resource
    private CustomerDecisionChainMapper baseMapper;

    @Override
    public List<CustomerDecisionChain> selectList(CustomerDecisionChainSearchDto dto) {
        return baseMapper.selectList(Wrappers.<CustomerDecisionChain>lambdaQuery()
                .eq(CustomerDecisionChain::getCustomerId, dto.getCustomerId())
                .orderByDesc(CustomerDecisionChain::getCreateTime));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CustomerDecisionChain selectById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 新增数据
     *
     * @param customerDecisionChain 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(CustomerDecisionChain customerDecisionChain) {
        verifyParameter(customerDecisionChain);
        return baseMapper.insert(customerDecisionChain) > 0;
    }

    /**
     * 修改数据
     *
     * @param customerDecisionChain 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(CustomerDecisionChain customerDecisionChain) {
        if (ObjectUtils.isEmpty(customerDecisionChain.getId())) throw new BeanException("id不能为空！");
        verifyParameter(customerDecisionChain);
        return baseMapper.updateById(customerDecisionChain) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 参数验证
     *
     * @param customerDecisionChain
     */
    private void verifyParameter(CustomerDecisionChain customerDecisionChain) {
        if (ObjectUtils.isEmpty(customerDecisionChain.getCustomerId())) throw new BeanException("客户id不能为空！");
        customerDecisionChain.setUpdateId(SecurityUtils.getUserId());
        customerDecisionChain.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
    }
}
