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;
import java.util.Optional;

/**
 * @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);
    }

    /**
     * 构建唯一key
     *
     * @return
     */
    public String buildRecordKeyWithUnionId() {
        String customerId = 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());
        Integer durationTime = Optional.ofNullable(this.interactRecordMessageBO.getDurationTime()).orElse(0);
        temp.setDurationTime(durationTime / 1000);
        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.setLastAccessTime(new Date(this.interactRecordMessageBO.getLastAccessTime()));
        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);
        if (this.hasDeal != null) {
            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);
            temp.setMemberImageUrl(this.memberInfo.memberImgUrl);
        }
        return temp;
    }

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

        private String memberImgUrl;
    }
}
