package com.ghy.customer.service.impl;

import com.ghy.common.core.text.Convert;
import com.ghy.common.exception.ServiceException;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.mapper.CustomerMapper;
import com.ghy.customer.service.CustomerService;
import org.springframework.stereotype.Service;

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

/**
 * @author clunt
 * 消费者Impl层
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Override
    public Customer selectByCustomerId(Long customerId) {
        return customerMapper.selectByCustomerId(customerId);
    }

    @Override
    public List<Customer> getCustomerList(Customer customer) {
        return customerMapper.getCustomerList(customer);
    }

    @Override
    public int deleteByIds(String ids) {
        Long [] customerIds = Convert.toLongArray(ids);
        for (Long customerId : customerIds)
        {
            Customer customer = selectByCustomerId(customerId);
            if (countUserCustomer(customer) > 0)
            {
                throw new ServiceException(String.format("%1$s正在使用,不能删除", customer.getName()));
            }
        }
        return customerMapper.deleteByIds(customerIds);
    }

    @Override
    public int deleteByCustomerId(Long customerId) {
        Customer customer = selectByCustomerId(customerId);
        if (countUserCustomer(customer) > 0)
        {
            throw new ServiceException(String.format("%1$s正在使用,不能删除", customer.getName()));
        }
        return customerMapper.deleteByCustomerId(customerId);
    }

    @Override
    public int insertCustomer(Customer customer) {
        return customerMapper.insertCustomer(customer);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateCustomer(customer);
    }

    public int countUserCustomer(Customer customer) {
        //TODO 需要校验用户是否在被使用。
        return 0;
    }
}
