package com.gic.haoban.manage.service.service.out.impl.content;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.dto.qdto.content.InteractRecordQDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordExtendInfoDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordInfoDTO;
import com.gic.haoban.manage.api.service.content.InteractRecordApiService;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.bo.content.MemberAttrChangeBO;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.PotentialCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/29 1:12 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component("interactRecordApiService")
public class InteractRecordApiServiceImpl implements InteractRecordApiService {

    @Autowired
    private InteractRecordService interactRecordService;
    @Autowired
    PotentialCustomerService potentialCustomerService;

    /**
     * 查询互动记录
     *
     * @param interactRecordQDTO
     * @return
     */
    @Override
    public ServiceResponse<Page<InteractRecordInfoDTO>> queryInteractRecordList(InteractRecordQDTO interactRecordQDTO) {
        Page<InteractRecordInfoDTO> result = new Page<>();
        result.setTotalCount(0);
        result.setResult(Collections.emptyList());
        InteractRecordQO searchQo = new InteractRecordQO();
        BeanUtils.copyProperties(interactRecordQDTO, searchQo);
        Page<InteractRecordBO> pageResult = interactRecordService.queryInteractRecordPage(searchQo);
        if (pageResult == null || CollectionUtils.isEmpty(pageResult.getResult())) {
            log.info("查询结果为空 {}", JSON.toJSONString(interactRecordQDTO));
            return ServiceResponse.success(result);
        }

        // 拼接描述
        // 组装扩展信息
        List<InteractRecordInfoDTO> recordInfos = pageResult.getResult()
                .stream()
                .map(item -> {
                    InteractRecordInfoDTO temp = new InteractRecordInfoDTO();
                    BeanUtils.copyProperties(item, temp);
                    if (item.getLastAccessTime() != null) {
                        temp.setCreateTime(item.getLastAccessTime());
                    }
                    temp.setInteractRecordId(item.getId());
                    if (item.getExtendInfo() != null) {
                        InteractRecordExtendInfoDTO extendInfo = new InteractRecordExtendInfoDTO();
                        BeanUtils.copyProperties(item.getExtendInfo(), extendInfo);
                        temp.setExtendInfo(extendInfo);
                    }
                    return temp;
                })
                .collect(Collectors.toList());
        result.setTotalCount(pageResult.getTotalCount());
        result.setResult(recordInfos);
        result.setCurrentPage(interactRecordQDTO.getPageNum());
        result.setPageSize(interactRecordQDTO.getPageSize());
        return ServiceResponse.success(result);
    }

    /**
     * 处理member信息变化的MQ
     *
     * @param params
     * @return
     */
    @Override
    public ServiceResponse<Void> memberChangeMQ(String params) {

        log.info("会员合并事件 > {}", params);
        if (StringUtils.isBlank(params)) {
            log.info("会员id发生变化 {}", params);
            return ServiceResponse.success();
        }
        MemberAttrChangeBO memberAttrChangeBO = JSON.parseObject(params, MemberAttrChangeBO.class);
        if (memberAttrChangeBO == null) {
            return ServiceResponse.success();
        }
        if (MemberAttrChangeBO.MemberChangeOpt.MINI_APP_AUTHORIZED.getCode().equals(memberAttrChangeBO.getOpt())) {
            // 会员授权事件
            // 根据unionId 刷新 memberId 并写入销售线索

            interactRecordService.memberAuthorized(memberAttrChangeBO.getMemberId(), memberAttrChangeBO.getUnionId());

        }
        if (MemberAttrChangeBO.MemberChangeOpt.MEMBER_MERGE.getCode().equals(memberAttrChangeBO.getOpt())) {
            //会员合并
            // 把合并的memberId 修改成 合并后的memberId
            interactRecordService.mergeMember(memberAttrChangeBO.getMergeMemberId(), memberAttrChangeBO.getMemberId());
            potentialCustomerService.mergeMember(memberAttrChangeBO.getMergeMemberId(), memberAttrChangeBO.getMemberId());
        }
        return ServiceResponse.success();
    }
}
