package com.fs.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.crm.domain.CrmCustomerLevel;
import com.fs.crm.mapper.CrmCustomerLevelMapper;
import com.fs.crm.param.CrmCustomerLevelParm;
import com.fs.crm.param.CrmCustomerLevelQueryParam;
import com.fs.crm.service.ICrmCustomerLevelService;
import com.fs.crm.vo.CrmCustomerLevelVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CrmCustomerLevelServiceImpl extends ServiceImpl<CrmCustomerLevelMapper, CrmCustomerLevel> implements ICrmCustomerLevelService {

    /**
     * 查询客户级别列表
     * @param param 参数
     * @return  list
     */
    @Override
    public List<CrmCustomerLevelVO> selectCrmCustomerLevelVOList(CrmCustomerLevelQueryParam param) {
        return baseMapper.selectCrmCustomerLevelVOList(param);
    }

    /**
     * 新增客户级别
     * @param param 参数
     * @return 受影响行数
     */
    @Override
    public int saveCustomerLevel(CrmCustomerLevelParm param) {
        CrmCustomerLevel crmCustomerLevel = getCustomerLevelByName(param.getName());
        if (Objects.nonNull(crmCustomerLevel)) {
            throw new CustomException("级别名称已存在");
        }

        crmCustomerLevel = new CrmCustomerLevel();
        BeanUtils.copyProperties(param, crmCustomerLevel, "id");
        return baseMapper.insert(crmCustomerLevel);
    }

    /**
     * 根据名称查询客户级别
     * @param name 名称
     * @return CrmCustomerLevel
     */
    private CrmCustomerLevel getCustomerLevelByName(String name) {
        Wrapper<CrmCustomerLevel> queryWrapper = Wrappers.<CrmCustomerLevel>lambdaQuery()
                .eq(CrmCustomerLevel::getName, name)
                .last("limit 1");
        return getOne(queryWrapper);
    }

    /**
     * 修改客户级别
     * @param param 参数
     * @return 受影响行数
     */
    @Override
    public int editCustomerLevel(CrmCustomerLevelParm param) {
        if (Objects.isNull(param.getId())) {
            throw new CustomException("ID不能为空");
        }

        CrmCustomerLevel customerLevel = getById(param.getId());
        if (Objects.isNull(customerLevel)) {
            throw new CustomException("记录不存在");
        }

        CrmCustomerLevel levelByName = getCustomerLevelByName(param.getName());
        if (Objects.nonNull(levelByName) && !levelByName.getId().equals(customerLevel.getId())) {
            throw new CustomException("级别名称已存在");
        }

        BeanUtils.copyProperties(param, customerLevel, "id");
        return baseMapper.updateById(customerLevel);
    }

    /**
     * 根据ID查询
     * @param id id
     * @return CrmCustomerLevelVO
     */
    @Override
    public CrmCustomerLevelVO selectCrmCustomerLevelVOById(Long id) {
        CrmCustomerLevel customerLevel = getById(id);
        if (Objects.isNull(customerLevel)) {
            throw new CustomException("记录不存在");
        }

        CrmCustomerLevelVO crmCustomerLevelVO = new CrmCustomerLevelVO();
        BeanUtils.copyProperties(customerLevel, crmCustomerLevelVO);
        return crmCustomerLevelVO;
    }
}
