package com.fjhx.mail.service.my.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.customer.entity.customer.po.CustomerUser;
import com.fjhx.customer.entity.customer.vo.CustomerVo;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.customer.service.customer.CustomerUserService;
import com.fjhx.mail.entity.my.dto.MyTagMessageAddBathDto;
import com.fjhx.mail.entity.my.dto.MyTagMessageDto;
import com.fjhx.mail.entity.my.dto.MyTagMessageSelectDto;
import com.fjhx.mail.entity.my.po.MyTagMessage;
import com.fjhx.mail.entity.my.vo.MessageVo;
import com.fjhx.mail.mapper.my.MyTagMessageMapper;
import com.fjhx.mail.service.my.MyTagMessageService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 我的标签关联邮件 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-29
 */
@Service
public class MyTagMessageServiceImpl extends ServiceImpl<MyTagMessageMapper, MyTagMessage> implements MyTagMessageService {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerUserService customerUserService;

    @Override
    public Page<MessageVo> getPage(MyTagMessageSelectDto dto) {
        IWrapper<MyTagMessage> wrapper = getWrapper();
        wrapper.eq("mtm", MyTagMessage::getMyTagId, dto.getMyTagId());

        wrapper.and(ObjectUtil.isNotEmpty(dto.getKeyword()), q1 -> q1
                        .like("m.from_email", dto.getKeyword())
                        .or().like("m.subject", dto.getKeyword())
//                .or().like("mc.content", dto.getKeyword())
//                .or().like("c.name", dto.getKeyword())
//                .or().like("cu.name", dto.getKeyword())
                        .or().like("m.from_personal_name", dto.getKeyword())
        );

        wrapper.orderByDesc("mtm", MyTagMessage::getUpdateTime);
        Page<MessageVo> page = this.baseMapper.getPage(dto.getPage(), wrapper, dto.getType(), SecurityUtils.getTenantId());

        List<MessageVo> records = page.getRecords();

        //赋值客户信息
        List<String> eMailList = records.stream().map(MessageVo::getFromEmail).distinct().collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(eMailList)) {
            List<Long> customerIds = customerUserService.listObject(CustomerUser::getCustomerId, q -> q.in(CustomerUser::getEmail, eMailList));
            if (ObjectUtil.isEmpty(customerIds)) {
                return page;
            }
            //赋值客户信息
            List<CustomerVo> customerInfoByIds = customerService.getCustomerInfoByIds(customerIds);
            Map<Long, CustomerVo> customerInfoMap = customerInfoByIds.stream().collect(Collectors.toMap(CustomerVo::getId, Function.identity()));
            for (MessageVo record : records) {
                record.setCustomerInfo(customerInfoMap.get(record.getCustomerId()));
            }
        }

        return page;
    }

    @Override
    public void add(MyTagMessageDto myTagMessageDto) {

        MyTagMessage myTagMessage = getOne(q -> q
                .eq(MyTagMessage::getMessageId, myTagMessageDto.getMessageId())
                .eq(MyTagMessage::getMyTagId, myTagMessageDto.getMyTagId())
        );
        if (myTagMessage != null) {
            throw new ServiceException("标签已被添加");
        }

        this.save(myTagMessageDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public void deleteTag(MyTagMessage myTagMessage) {

        remove(q -> q
                .eq(MyTagMessage::getMyTagId, myTagMessage.getMyTagId())
                .eq(MyTagMessage::getMessageId, myTagMessage.getMessageId())
        );

    }

    @Override
    public void addBath(MyTagMessageAddBathDto dto) {
        List<Long> messageIdList = dto.getMessageIdList();
        Long myTagId = dto.getMyTagId();

        List<Long> existMessageIdList = listObject(MyTagMessage::getMessageId, q -> q
                .eq(MyTagMessage::getMyTagId, myTagId)
                .in(MyTagMessage::getMessageId, messageIdList));


        List<MyTagMessage> myTagMessageList = messageIdList.stream()
                .filter(item -> !existMessageIdList.contains(item))
                .map(item -> {
                    MyTagMessage myTagMessage = new MyTagMessage();
                    myTagMessage.setMessageId(item);
                    myTagMessage.setMyTagId(myTagId);
                    return myTagMessage;
                })
                .collect(Collectors.toList());

        saveBatch(myTagMessageList);

    }

}
