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 com.gic.haoban.manage.service.service.content.adaptor.MessageConvertHelper;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

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

    /**
     * 业务类型
     * @return
     */
    public Integer getBizType() {
        // 素材id不为空 且 素材id是数字类型
        if (StringUtils.isNotBlank(interactRecordMessageBO.getMaterialId())
                && NumberUtils.isDigits(interactRecordMessageBO.getMaterialId())) {
            return ShareBizType.MATERIAL.getCode();
        } else if (StringUtils.isNotBlank(interactRecordMessageBO.getWxaLinkId()) &&
                interactRecordMessageBO.getGoodsCount() > 1) {
            return ShareBizType.MULTIPLE_GOODS.getCode();
        }
        return ShareBizType.GOODS.getCode();
    }

    public String getBizId() {
        Integer bizType = this.getBizType();
        if (ShareBizType.MATERIAL.getCode().equals(bizType)) {
            return interactRecordMessageBO.getMaterialId();
        }
        if (ShareBizType.GOODS.getCode().equals(bizType)) {
            return interactRecordMessageBO.getGoodsId();
        }
        if (ShareBizType.MULTIPLE_GOODS.getCode().equals(bizType)) {
            return interactRecordMessageBO.getWxaLinkId();
        }
        return StringUtils.EMPTY;
    }

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

    /**
     * 构建唯一key
     *
     * @return
     */
    public String buildRecordKey() {
        String key = interactRecordMessageBO.getBusinessUUId() + interactRecordMessageBO.getClerkId()
                + this.getBizId() + this.getCustomerId();
        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.setShareSource(this.interactRecordMessageBO.getShareSource());
        temp.setEventType(this.interactRecordMessageBO.getEventType());
        Integer durationTime = Optional.ofNullable(this.interactRecordMessageBO.getDurationTime()).orElse(0);
        temp.setDurationTime(durationTime / 1000);
        temp.setBizType(this.getBizType());
        temp.setBizId(this.getBizId());
        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;
    }
}
