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

import com.gic.haoban.manage.service.pojo.bo.content.context.InteractRecordMessageContext;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.member.api.dto.MemberDTO;
import com.gic.member.api.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author MUSI
 * @Date 2023/3/29 3:48 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class InteractRecordMessageBuilder {

    @Autowired
    private InteractRecordService interactRecordService;
    @Autowired
    private MemberService memberService;

    /**
     * 计算互动记录的访问次数
     * // 通过clerkId、memberId、materialId 查询之前存在的记录总数，本次是第几次访问 在之前的记录总数上加1
     *
     * @param context
     */
    public void calcCustomerVisitTimes(InteractRecordMessageContext context) {
        InteractRecordQO searchQo = new InteractRecordQO();
        searchQo.setClerkId(context.getInteractRecordMessageBO().getClerkId());
        searchQo.setMemberId(context.getInteractRecordMessageBO().getMemberId());
        if (StringUtils.isBlank(searchQo.getMemberId())) {
            searchQo.setUnionId(context.getInteractRecordMessageBO().getUnionId());
        }
        searchQo.setPageSize(1);
        Integer count = interactRecordService.countInteractRecord(searchQo);
        context.setTimes(count + 1);
    }

    /**
     * 冗余会员信息
     * @param context
     */
    public void redundantMemberInfo(InteractRecordMessageContext context) {
        String memberId = context.getInteractRecordMessageBO().getMemberId();
        if (StringUtils.isBlank(memberId)) {
            log.info("互动记录冗余会员信息 会员不存在");
            return;
        }
        MemberDTO memberDTO = memberService.queryMember(memberId);
        if (memberDTO == null) {
            log.info("查询会员信息为空 {}", memberId);
            return;
        }
        InteractRecordMessageContext.MemberInfo memberInfo = new InteractRecordMessageContext
                .MemberInfo(memberDTO.getMemberName(), memberDTO.getThirdNickname(), memberDTO.getPhoneNumber());
        context.setMemberInfo(memberInfo);
    }

    /**
     * 计算会员是否有过订单
     * @param context
     */
    public void calcHaveOrderNum(InteractRecordMessageContext context) {
        String memberId = context.getInteractRecordMessageBO().getMemberId();
        if (StringUtils.isBlank(memberId)) {
            log.info("互动记录冗余会员信息 会员id不存在");
            return;
        }
        // 查询会员全渠道订单
    }



}
