package com.gic.haoban.manage.service.service.content.message;

import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordExtendInfoBO;
import com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerBO;
import com.gic.haoban.manage.service.pojo.bo.content.context.InteractRecordMessageContext;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.PotentialCustomerService;
import com.gic.haoban.manage.service.service.content.adaptor.InteractRecordMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;

/**
 * @Author MUSI
 * @Date 2023/3/28 2:50 PM
 * @Description
 * @Version 互动记录消息处理器
 **/
@Slf4j
@Component
public class InteractRecordMessageService {

    @Autowired
    InteractRecordService interactRecordService;
    @Autowired
    InteractRecordMessageBuilder interactRecordMessageBuilder;
    @Autowired
    PotentialCustomerService potentialCustomerService;

    /**
     * 处理互动记录
     *
     * @param interactRecordMessageBO
     */
    public void dealRecord(InteractRecordMessageBO interactRecordMessageBO) {

        InteractRecordMessageContext context = InteractRecordMessageContext.create(interactRecordMessageBO);
        // 参数是否完整
        // 根据businessId clerkId memberId materialId 构建唯一标识key
        String recordKey = context.buildRecordKey();
        // 根据key 查询是否存在记录
        InteractRecordBO interactRecordBO = interactRecordService.queryWithInteractInfo(interactRecordMessageBO.getEnterpriseId(), recordKey);
        if (interactRecordBO == null) {
            // 新增
            // 冗余会员信息
            interactRecordMessageBuilder.redundantMemberInfo(context);
            // 查询会员是否有过成交 （是否存在订单）
            interactRecordMessageBuilder.calcHaveOrderNum(context);
            // 计算访问次数
            interactRecordMessageBuilder.calcCustomerVisitTimes(context);
            interactRecordBO = context.build();
        } else {
            // 该key已存在 需要对之前的进行合并
            // 修改访问类型
            interactRecordBO.setUpdateTime(new Date());
            Integer durationTime = Optional.ofNullable(interactRecordBO.getDurationTime()).orElse(0);
            interactRecordBO.setDurationTime(durationTime + Optional.ofNullable(interactRecordMessageBO.getDurationTime()).orElse(0));
            // 事件类型
            if (interactRecordBO.getEventType() < interactRecordBO.getEventType()) {
                interactRecordBO.setEventType(interactRecordMessageBO.getEventType());
            }
        }

        if (null == interactRecordBO.getExtendInfo()) {
            interactRecordBO.setExtendInfo(InteractRecordExtendInfoBO.empty());
        }
        // 组装扩展信息
        if (MaterialInteractRecordEventType.VISIT_PRODUCT.getCode().equals(interactRecordMessageBO.getEventType())) {
            // 浏览商品
            if (StringUtils.isNotBlank(interactRecordMessageBO.getGoodsId())) {
                interactRecordBO.getExtendInfo().getGoodsIds().add(interactRecordMessageBO.getGoodsId());
            }
        }

        if (MaterialInteractRecordEventType.ORDER.getCode().equals(interactRecordMessageBO.getEventType())) {
            // 下单
            // 查询订单信息
            InteractRecordExtendInfoBO.ExtendOrderInfo extendOrderInfo = interactRecordMessageBuilder.buildMaterialOrderItemInfo(context);
            if (extendOrderInfo != null) {
                interactRecordBO.getExtendInfo().getExtendOrderInfos().add(extendOrderInfo);
            }

        }

        interactRecordService.saveOrUpdateInteractRecord(interactRecordBO);

        // 更新销售线索
        if (StringUtils.isNotBlank(interactRecordMessageBO.getMemberId())) {

            // 查询销售线索
            PotentialCustomerBO potentialCustomerBO = potentialCustomerService.queryPotentialCustomerInfo(interactRecordMessageBO.getClerkId(), interactRecordMessageBO.getMemberId());
            if (potentialCustomerBO == null) {
                potentialCustomerBO = context.buildPotentialCustomerBo(interactRecordBO);
            }
            // 最近访问时间
            if (potentialCustomerBO.getLastAccessTime().before(new Date(interactRecordMessageBO.getLastAccessTime()))) {
                potentialCustomerBO.setLastAccessTime(new Date(interactRecordMessageBO.getLastAccessTime()));
            }
            // 标记为未读
            potentialCustomerBO.setSeeFlag(0);
            //
            potentialCustomerBO.setStoreId(interactRecordBO.getStoreId());
            potentialCustomerBO.setBizId(interactRecordBO.getBizId());
            potentialCustomerBO.setEventType(interactRecordBO.getEventType());
            potentialCustomerBO.setChannelSource(interactRecordBO.getChannelSource());
            potentialCustomerBO.setEventType(interactRecordBO.getEventType());
            potentialCustomerBO.setDurationTime(interactRecordBO.getDurationTime());
            potentialCustomerBO.setTimes(interactRecordBO.getTimes());
            potentialCustomerService.saveOrUpdatePotentialCustomer(potentialCustomerBO);
        }
    }

}
