package com.ghy.customer.service.impl;
import java.util.List;
import com.ghy.common.utils.DateUtils;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.service.CustomerService;
import com.ghy.system.domain.SysArea;
import com.ghy.system.service.ISysAreaService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ghy.customer.mapper.CustomerPlaceMapper;
import com.ghy.customer.domain.CustomerPlace;
import com.ghy.customer.service.ICustomerPlaceService;
import com.ghy.common.core.text.Convert;

/**
 * 分销申请Service业务层处理
 *
 * @author clunt
 * @date 2022-08-11
 */
@Service
public class CustomerPlaceServiceImpl implements ICustomerPlaceService
{
    @Autowired
    private CustomerPlaceMapper customerPlaceMapper;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ISysAreaService sysAreaService;
    /**
     * 查询分销申请
     *
     * @param id 分销申请主键
     * @return 分销申请
     */
    @Override
    public CustomerPlace selectCustomerPlaceById(String id)
    {
        return customerPlaceMapper.selectCustomerPlaceById(id);
    }

    @Override
    public List<CustomerPlace> getByCustomerIds(List<Long> ids) {
        List<CustomerPlace> byCustomerIds = customerPlaceMapper.getByCustomerIds(ids);
        byCustomerIds.forEach(customerPlace -> {
            Customer customer = customerService.selectByCustomerId(customerPlace.getCustomerId());
            customerPlace.setCustomerLogoUrl(customer.getCustomerLogoUrl());
        });
        return byCustomerIds;
    }

    /**
     * 查询分销申请列表
     *
     * @param customerPlace 分销申请
     * @return 分销申请
     */
    @Override
    public List<CustomerPlace> selectCustomerPlaceList(CustomerPlace customerPlace)
    {
        List<CustomerPlace> places = customerPlaceMapper.selectCustomerPlaceList(customerPlace);
        places.forEach(model->{
            Customer param = new Customer();
            param.setCustomerId(model.getCustomerId());
            List<Customer> customerList = customerService.getCustomerList(param);
            if(CollectionUtils.isNotEmpty(customerList)){
                model.setCustomerPlaceName(customerList.get(0).getCustomerPlaceName());
                model.setCustomerPlacePhone(customerList.get(0).getCustomerPlacePhone());
                model.setParentCustomerPlaceName(customerList.get(0).getParentCustomerPlaceName());
                model.setParentCustomerPlacePhone(customerList.get(0).getParentCustomerPlacePhone());
            }
            // 城市
            if(model.getCity() != null){
                SysArea area = sysAreaService.selectById(model.getCity());
                if(area != null){
                    model.setCityName(area.getAreaName());
                }
            }
        });
        return places;
    }

    /**
     * 查询分销申请列表
     *
     * @param customerPlace 分销申请
     * @return 分销申请
     */
    @Override
    public List<CustomerPlace> selectCustomerPlaceSimpleList(CustomerPlace customerPlace)
    {
        return customerPlaceMapper.selectCustomerPlaceList(customerPlace);
    }

    /**
     * 新增分销申请
     *
     * @param customerPlace 分销申请
     * @return 结果
     */
    @Override
    public int insertCustomerPlace(CustomerPlace customerPlace)
    {
        customerPlace.setCreateTime(DateUtils.getNowDate());
        return customerPlaceMapper.insertCustomerPlace(customerPlace);
    }

    /**
     * 修改分销申请
     *
     * @param customerPlace 分销申请
     * @return 结果
     */
    @Override
    public int updateCustomerPlace(CustomerPlace customerPlace)
    {
        customerPlace.setUpdateTime(DateUtils.getNowDate());
        return customerPlaceMapper.updateCustomerPlace(customerPlace);
    }

    /**
     * 批量删除分销申请
     *
     * @param ids 需要删除的分销申请主键
     * @return 结果
     */
    @Override
    public int deleteCustomerPlaceByIds(String ids)
    {
        return customerPlaceMapper.deleteCustomerPlaceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除分销申请信息
     *
     * @param id 分销申请主键
     * @return 结果
     */
    @Override
    public int deleteCustomerPlaceById(String id)
    {
        return customerPlaceMapper.deleteCustomerPlaceById(id);
    }

    @Override
    public int changeStatus(String ids, Integer status) {
        return customerPlaceMapper.changeStatus(ids, status);
    }
}