package com.gic.haoban.manage.service.pojo.bo.content.context;

import cn.hutool.crypto.digest.MD5;
import com.gic.haoban.manage.api.enums.content.ShareBizType;
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.message.InteractRecordMessageBO;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * @Author MUSI
 * @Date 2023/3/29 3:23 PM
 * @Description
 * @Version
 **/
@Data
public class InteractRecordMessageContext {


    private InteractRecordMessageBO interactRecordMessageBO;
    /**
     * 访问次数
     */
    private Integer times;
    /**
     * 冗余的会员信息
     */
    private MemberInfo memberInfo;

    /**
     * 成交过订单
     */
    private boolean hasDeal;

    public static InteractRecordMessageContext create(InteractRecordMessageBO interactRecordMessageBO) {
        InteractRecordMessageContext context = new InteractRecordMessageContext();
        context.setInteractRecordMessageBO(interactRecordMessageBO);
        return context;
    }

    /**
     * 构建唯一key
     *
     * @return
     */
    public String buildRecordKey() {
        String customerId = StringUtils.isNotBlank(interactRecordMessageBO.getMemberId()) ? interactRecordMessageBO.getMemberId() : interactRecordMessageBO.getUnionId();
        String key = interactRecordMessageBO.getBusinessUUId() + interactRecordMessageBO.getClerkId() + interactRecordMessageBO.getMaterialId() + customerId;
        return MD5.create().digestHex(key);
    }

    /**
     * 获取客户id
     *
     * @return
     */
    public String getCustomerId() {
        if (StringUtils.isNotBlank(this.interactRecordMessageBO.getMemberId())) {
            return this.interactRecordMessageBO.getMemberId();
        }
        return this.interactRecordMessageBO.getUnionId();
    }


    public InteractRecordBO build() {
        if (this.interactRecordMessageBO == null) {
            return null;
        }
        InteractRecordBO temp = new InteractRecordBO();
        temp.setEnterpriseId(this.interactRecordMessageBO.getEnterpriseId());
        temp.setMemberId(this.interactRecordMessageBO.getMemberId());
        temp.setUnionId(this.interactRecordMessageBO.getUnionId());
        temp.setClerkId(this.interactRecordMessageBO.getClerkId());
        temp.setStoreId(this.interactRecordMessageBO.getStoreId());
        temp.setChannelSource(this.interactRecordMessageBO.getChannelSource());
        temp.setEventType(this.interactRecordMessageBO.getEventType());
        temp.setDurationTime(this.interactRecordMessageBO.getDurationTime());
        temp.setBizType(ShareBizType.MATERIAL.getCode());
        temp.setBizId(this.interactRecordMessageBO.getMaterialId() + "");
        temp.setTimes(this.times);
        temp.setTraceId(this.buildRecordKey());
        temp.setDeleteFlag(0);
        temp.setCreateTime(new Date());
        temp.setUpdateTime(new Date());
        temp.setExtendInfo(InteractRecordExtendInfoBO.empty());
        return temp;
    }

    /**
     * 构建销售线索
     */
    public PotentialCustomerBO buildPotentialCustomerBo(InteractRecordBO interactRecordBO){
        PotentialCustomerBO temp = new PotentialCustomerBO();
        BeanUtils.copyProperties(interactRecordBO, temp);
        temp.setDeleteFlag(0);
        temp.setCreateTime(new Date());
        temp.setUpdateTime(new Date());
        temp.setStarFlag(0);
        temp.setDealRecordNum(this.hasDeal ? 1 : 0);
        temp.setLastAccessTime(new Date(this.interactRecordMessageBO.getLastAccessTime()));
        if (this.memberInfo != null) {
            temp.setMemberName(this.memberInfo.memberName);
            temp.setMemberNickName(this.memberInfo.memberNickName);
            temp.setMemberPhone(this.memberInfo.memberPhone);
        }
        return temp;
    }

    @Data
    @AllArgsConstructor
    public static class MemberInfo {
        private String memberName;
        private String memberNickName;
        private String memberPhone;
    }
}
