package com.storlead.sales.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.mybatis.entity.SysBaseField;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.pojo.entity.customer.CustomerCompanyEntity;
import com.storlead.sales.pojo.entity.customer.LiaisonEntity;
import com.storlead.sales.mapper.customer.CustomerCompanyMapper;
import com.storlead.sales.service.customer.CustomerCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户企业信息表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-04-12
 */
@Service
public class CustomerCompanyServiceImpl extends MyBaseServiceImpl<CustomerCompanyMapper, CustomerCompanyEntity> implements CustomerCompanyService {

    @Override
    public CustomerCompanyEntity getByCustomerId(Long id) {
        LambdaQueryWrapper<CustomerCompanyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerCompanyEntity::getCustomerId,id);
        wrapper.eq(CustomerCompanyEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.last("limit 1");
        CustomerCompanyEntity customerCompanyEntity = getOne(wrapper);

        return customerCompanyEntity;
    }

    @Override
    public Map<Long, CustomerCompanyEntity> customerIdListToCompanyMap(List<Long> customerIdList) {
        List<CustomerCompanyEntity> customerEntities = baseMapper.selectList(new LambdaQueryWrapper<CustomerCompanyEntity>().in(CustomerCompanyEntity::getCustomerId, customerIdList));
//        List<Long> liaisonIds = liaisonEntities.stream().map(LiaisonEntity::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(customerEntities)){
            SysBaseField.dictFileds(customerEntities);
            return customerEntities.stream().collect(Collectors.toMap(CustomerCompanyEntity::getCustomerId, Function.identity(),(existing, replacement) -> existing));
        }
        return null;
    }
}
