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.domain.CustomerPlace;
import com.ghy.customer.mapper.CustomerMapper;
import com.ghy.customer.service.CustomerService;
import com.ghy.customer.service.ICustomerPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    @Resource
    private CustomerMapper customerMapper;

    @Autowired
    private ICustomerPlaceService customerPlaceService;

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

    @Override
    public List<Customer> getCustomerList(Customer customer) {
        List<Customer> customerList = customerMapper.getCustomerList(customer);
        customerList.forEach(model->{
            if(model.getCustomerPlace() != null){
                Customer place = selectByCustomerId(model.getCustomerPlace());
                if(place != null){
                    model.setCustomerPlaceName(place.getName());
                    model.setCustomerPlacePhone(place.getPhone());
                }
            }
            if(model.getParentCustomerPlace() != null){
                Customer parent = selectByCustomerId(model.getParentCustomerPlace());
                if(parent != null){
                    model.setParentCustomerPlaceName(parent.getName());
                    model.setParentCustomerPlacePhone(parent.getPhone());
                }
            }
        });
        return customerList;
    }

    @Override
    public List<Customer> getPlaceCustomerList() {
        Customer customer = new Customer();
        customer.setPlaceStatus(2);
        List<Customer> customerList = customerMapper.getCustomerList(customer);
        customerList.forEach(model->{
            CustomerPlace param = new CustomerPlace();
            param.setCustomerId(model.getCustomerId());
            List<CustomerPlace> place = customerPlaceService.selectCustomerPlaceSimpleList(param);
            if(!CollectionUtils.isEmpty(place)){
                model.setPlaceName(place.get(0).getName());
            }
        });
        return customerList;
    }

    @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.selectByIds(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;
    }

    @Override
    public List<Customer> selectByIds(Set<Long> customerIds) {
        if (CollectionUtils.isEmpty(customerIds)) {
            return new ArrayList<>();
        }
        return customerMapper.selectByIds(customerIds);
    }

    @Override
    public Customer selectByAccount(String account) {
        return customerMapper.selectByAccount(account);
    }
}
