package com.ruoyi.project.business.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.vo.account.ExistContactDto;
import com.ruoyi.project.business.mapper.TcAccountContactHistoryMapper;
import com.ruoyi.project.business.service.ITcAccountService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.mapper.TcAccountContactMapper;
import com.ruoyi.project.business.domain.TcAccountContact;
import com.ruoyi.project.business.service.ITcAccountContactService;

import javax.annotation.Resource;

/**
 * 联系人Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-12
 */
@Service
public class TcAccountContactServiceImpl extends ServiceImpl<TcAccountContactMapper, TcAccountContact> implements ITcAccountContactService 
{
    @Resource
    private TcAccountContactMapper tcAccountContactMapper;

    @Resource
    private ITcAccountService tcAccountService;

    /**
     * 查询联系人
     * 
     * @param contactId 联系人主键
     * @return 联系人
     */
    @Override
    public TcAccountContact selectTcAccountContactByContactId(Long contactId)
    {
        return tcAccountContactMapper.selectTcAccountContactByContactId(contactId);
    }

    /**
     * 查询相关联系人
     * 
     * @param accountId 客户Id
     * @return 联系人
     */
    @Override
    public List<TcAccountContact> selectContactlistByAccountId(Long accountId)
    {
        return tcAccountContactMapper.selectContactlistByAccountId(accountId);
    }

    /**
     * 查询联系人列表
     *
     * @param tcAccountContact 联系人
     * @return 联系人
     */
    @Override
    public List<TcAccountContact> selectTcAccountContactList(TcAccountContact tcAccountContact)
    {
        return tcAccountContactMapper.selectTcAccountContactList(tcAccountContact);
    }

    /**
     * 新增联系人
     * 
     * @param tcAccountContact 联系人
     * @return 结果
     */
    @Override
    public int insertTcAccountContact(TcAccountContact tcAccountContact)
    {
        tcAccountContact.setCreateTime(DateUtils.getNowDate());
        return tcAccountContactMapper.insertTcAccountContact(tcAccountContact);
    }

    /**
     * 修改联系人
     * 
     * @param tcAccountContact 联系人
     * @return 结果
     */
    @Override
    public int updateTcAccountContact(TcAccountContact tcAccountContact)
    {
        tcAccountContact.setUpdateTime(DateUtils.getNowDate());
        return tcAccountContactMapper.updateTcAccountContact(tcAccountContact);
    }

    /**
     * 批量删除联系人
     * 
     * @param contactIds 需要删除的联系人主键
     * @return 结果
     */
    @Override
    public int deleteTcAccountContactByContactIds(Long[] contactIds)
    {
        return tcAccountContactMapper.deleteTcAccountContactByContactIds(contactIds);
    }

    /**
     * 删除联系人信息
     * 
     * @param contactId 联系人主键
     * @return 结果
     */
    @Override
    public int deleteTcAccountContactByContactId(Long contactId)
    {
        return tcAccountContactMapper.deleteTcAccountContactByContactId(contactId);
    }

    /**
     * 查询未关联客户联系人
     *
     * @param tcAccountContact 联系人
     * @return 结果
     */
    @Override
    public List<TcAccountContact> getUnRelatedContact(TcAccountContact tcAccountContact) {
        return tcAccountContactMapper.getUnRelatedContact(tcAccountContact);
    }

    private boolean isContactNotEqual(TcAccountContact oldContact, TcAccountContact contact) {
        if (!Objects.equals(oldContact.getContactName(), contact.getContactName())) {
            return true;
        }
        if (!Objects.equals(oldContact.getPhone(), contact.getPhone())) {
            return true;
        }
        if (!Objects.equals(oldContact.getEmail(), contact.getEmail())) {
            return true;
        }
        if (!Objects.equals(oldContact.getAddress(), contact.getAddress())) {
            return true;
        }
        if (!Objects.equals(oldContact.getJobTitle(), contact.getJobTitle())) {
            return true;
        }
        if (!Objects.equals(oldContact.getBirthday(), contact.getBirthday())) {
            return true;
        }
        if (!Objects.equals(oldContact.getHoroscope(), contact.getHoroscope())) {
            return true;
        }
        if (!Objects.equals(oldContact.getMaritalStatus(), contact.getMaritalStatus())) {
            return true;
        }
        if (!Objects.equals(oldContact.getHobbies(), contact.getHobbies())) {
            return true;
        }
        return false;
    }


    @Override
    public Map<String, Object> getSameContactLib(List<TcAccountContact> tcAccountContacts) {
        if (CollectionUtils.isEmpty(tcAccountContacts)) {
            return Collections.emptyMap();
        }

        Set<String> existPhones = new HashSet<>();
        Set<String> existEmails = new HashSet<>();
        Set<String> existAddresses = new HashSet<>();
        Set<Long> existContactIds = new HashSet<>();


        // TODO 这里需要优化，
        for (TcAccountContact contact : tcAccountContacts) {
            // TODO 并且客户不能是自己？
            TcAccountContact contact1 = getOne(Wrappers.lambdaQuery(TcAccountContact.class)
                    .select(TcAccountContact::getPhone, TcAccountContact::getContactId)
                    .eq(TcAccountContact::getPhone, contact.getPhone())
                    .ne(contact.getContactId() != null, TcAccountContact::getContactId, contact.getContactId()), false);
            if (contact1 != null) {
                existContactIds.add(contact1.getContactId());
                existPhones.add(contact1.getPhone());
            } 
            
            TcAccountContact contact2 = getOne(Wrappers.lambdaQuery(TcAccountContact.class)
                    .select(TcAccountContact::getEmail, TcAccountContact::getContactId)
                    .eq(TcAccountContact::getEmail, contact.getEmail())
                    .ne(contact.getContactId() != null, TcAccountContact::getContactId, contact.getContactId()),false);
            if (contact2 != null) {
                existContactIds.add(contact2.getContactId());
                existEmails.add(contact2.getEmail());
            }
            

            TcAccountContact contact3 = getOne(Wrappers.lambdaQuery(TcAccountContact.class)
                    .select(TcAccountContact::getAddress, TcAccountContact::getContactId)
                    .eq(TcAccountContact::getAddress, contact.getAddress())
                    .ne(contact.getContactId() != null, TcAccountContact::getContactId, contact.getContactId()));
            if (contact3 != null) {
                existContactIds.add(contact3.getContactId());
                existAddresses.add(contact3.getAddress());
            }
        }
        List<ExistContactDto> dtos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(existContactIds)) {
            dtos = baseMapper.selectExistContactAccount(existContactIds, SecurityUtils.getPermissionCharIns());
        }

        boolean exist = !existPhones.isEmpty() || !existEmails.isEmpty() || !existAddresses.isEmpty();

        // tcAccountService.list()
        
        HashMap<String, Object> map = new HashMap<>();
        map.put("exist", exist);
        if (CollectionUtils.isNotEmpty(existPhones)) {
            map.put("samePhone", existPhones);
        }
        if (CollectionUtils.isNotEmpty(existEmails)) {
            map.put("sameEmail", existEmails);
        }
        if (CollectionUtils.isNotEmpty(existAddresses)) {
            map.put("sameAddress", existAddresses);
        }
        if (CollectionUtils.isNotEmpty(dtos)) {
            map.put("sameContact", new HashSet<>(dtos));
        }
        return map;
    }

}
