package com.fjhx.customer.service.customer.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.customer.entity.customer.dto.CustomerUserDto;
import com.fjhx.customer.entity.customer.dto.CustomerUserSelectDto;
import com.fjhx.customer.entity.customer.po.CustomerUser;
import com.fjhx.customer.entity.customer.vo.CustomerUserVo;
import com.fjhx.customer.mapper.customer.CustomerUserMapper;
import com.fjhx.customer.service.customer.CustomerUserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户用户表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-04
 */
@Service
public class CustomerUserServiceImpl extends ServiceImpl<CustomerUserMapper, CustomerUser> implements CustomerUserService {

    @Override
    public List<CustomerUserVo> getList(CustomerUserSelectDto dto) {
        IWrapper<CustomerUser> wrapper = getWrapper();
        wrapper.orderByDesc("cu", CustomerUser::getId);
        List<CustomerUserVo> list = this.baseMapper.getList(wrapper);
        return list;
    }

    @Override
    public Page<CustomerUserVo> getPage(CustomerUserSelectDto dto) {
        IWrapper<CustomerUser> wrapper = getWrapper();
        wrapper.orderByDesc("cu", CustomerUser::getId);
        Page<CustomerUserVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public CustomerUserVo detail(Long id) {
        CustomerUser CustomerUser = this.getById(id);
        CustomerUserVo result = BeanUtil.toBean(CustomerUser, CustomerUserVo.class);
        return result;
    }

    @Override
    public void add(CustomerUserDto customerUserDto) {
        if(ObjectUtil.isEmpty(customerUserDto.getCustomerId())){
            throw new ServiceException("客户id不能为空");
        }
        List<CustomerUser> list = this.list(q -> q.eq(CustomerUser::getCustomerId, customerUserDto.getCustomerId()).eq(CustomerUser::getEmail, customerUserDto.getEmail()));
        if(ObjectUtil.isNotEmpty(list)){
            throw new ServiceException("邮箱不能重复");
        }
        this.save(customerUserDto);
    }

    @Override
    public void edit(CustomerUserDto customerUserDto) {
        this.updateById(customerUserDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
