package com.ghy.customer.service.impl;

import com.ghy.common.core.text.Convert;
import com.ghy.common.exception.ServiceException;
import com.ghy.common.utils.StringUtils;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @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 Long countCustomer(Customer customer) {
        return customerMapper.countCustomer(customer);
    }

    @Override
    public Customer selectByOpenId(Customer customer) {
        List<Customer> list = customerMapper.getCustomerList(customer);
        if(StringUtils.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }

    @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);
    }

    @Override
    public Map<Long, Customer> byCustomerUseridInMap(List<Long> customerUserIdList) {
        Map<Long, Customer> longCustomerMap = new HashMap<>();
        if(customerUserIdList != null && customerUserIdList.size() > 0){
            List<Customer> customerList = customerMapper.getByCustomerIdList(customerUserIdList);
            if(customerList != null && customerList.size() > 0){
                for(Customer customer : customerList){
                    longCustomerMap.put(customer.getCustomerId(), customer);
                }
            }
        }
        return longCustomerMap;
    }

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