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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.service.pojo.bo.content.message.InteractRecordMessageBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @Author MUSI
 * @Date 2023/9/4 3:14 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class MessageConvertHelper {


    /**
     * 转换消息
     * @param text
     * @return
     */
    public InteractRecordMessageBO convertMessageBo(String text) {

        JSONObject jsonObject = JSON.parseObject(text);
        if (MapUtils.isEmpty(jsonObject)) {
            return null;
        }
        InteractRecordMessageBO interactRecordMessageBO = new InteractRecordMessageBO();

        String appKey = jsonObject.getString("app_key");
        interactRecordMessageBO.setAppKey(appKey);

        String gp = jsonObject.getString("gp");
        JSONObject gpJson = JSON.parseObject(gp);

        String cusp = jsonObject.getString("cusp");
        JSONObject cuspJson = JSON.parseObject(cusp);

        String sysp = jsonObject.getString("sysp");
        JSONObject syspJson = JSON.parseObject(sysp);

        String enterpriseId = gpJson.getString("merch_id");
        interactRecordMessageBO.setEnterpriseId(enterpriseId);

        String memberId = gpJson.getString("member_id");
        interactRecordMessageBO.setMemberId(memberId);

        String unionId = gpJson.getString("union_id");
        interactRecordMessageBO.setUnionId(unionId);

        String clerkId = cuspJson.getString("share_id");
        interactRecordMessageBO.setClerkId(clerkId);

        String storeId = cuspJson.getString("share_shop_id");
        interactRecordMessageBO.setStoreId(storeId);

        String materialId = cuspJson.getString("material_id");
        interactRecordMessageBO.setMaterialId(materialId);

        String shareMaterialChannel = cuspJson.getString("share_material_channel");
        interactRecordMessageBO.setChannelSource(this.convertChannelSource(shareMaterialChannel));

        String eventCode = jsonObject.getString("event_code");
        interactRecordMessageBO.setEventType(this.convertEventType(eventCode));

        Integer duration = jsonObject.getInteger("duration");
        interactRecordMessageBO.setDurationTime(duration);

        String orderNumber = cuspJson.getString("order_number");
        interactRecordMessageBO.setOrderNumber(orderNumber);

        String goodsId = cuspJson.getString("goods_id");
        interactRecordMessageBO.setGoodsId(goodsId);

        String runningUuid = jsonObject.getString("runningUuid");
        interactRecordMessageBO.setBusinessUUId(runningUuid);

        Long lastAccessTime = jsonObject.getLong("server_timestamp");
        interactRecordMessageBO.setLastAccessTime(lastAccessTime);

        String refUrl = syspJson.getString("ref_url");
        interactRecordMessageBO.setRefUrl(refUrl);

        return interactRecordMessageBO;
    }

    private Integer convertChannelSource(String shareMaterialChannel) {
        if (StringUtils.isBlank(shareMaterialChannel)) {
            return null;
        }
        switch (shareMaterialChannel) {
            case "null" :
            case "NULL":
                return null;
            case "朋友圈":
                return 1;
            case "客户群":
                return 2;
            case "对话框":
                return 3;
            default:
                return 0;
        }
    }

    private Integer convertEventType(String eventCode) {
        if (StringUtils.isBlank(eventCode)) {
            return null;
        }
        switch (eventCode) {
            case "material_page":
            case "material_page_duration":
                return 1;
            case "store_goods_detail":
                return 2;
            case "store_buy_sucess":
                return 3;
            case "store_add_cart":
                return 4;
            default:
                return null;
        }
    }
}
