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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.enums.content.MaterialInteractRecordEventType;
import com.gic.haoban.manage.api.enums.content.TriggerCustomerChannelType;
import com.gic.haoban.manage.service.entity.content.TabHaobanInteractRecord;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordExtendInfoBO;
import com.gic.store.goods.dto.goods.GoodsInfoSimpleDTO;
import com.gic.store.goods.dto.qdto.ErpGoodsInfoQDTO;
import com.gic.store.goods.enums.GoodsStatusEnum;
import com.gic.store.goods.service.GoodsInfoOutApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/29 2:49 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class InteractRecordBuilder {

    @Autowired
    private GoodsInfoOutApiService goodsInfoOutApiService;

    /**
     * 互动记录描述
     *
     * @param interactRecord
     * @return
     */
    public String buildDesc(TabHaobanInteractRecord interactRecord) {
        StringBuilder stringBuilder = new StringBuilder();
        if (TriggerCustomerChannelType.showDescChannel().contains(interactRecord.getChannelSource())) {
            TriggerCustomerChannelType channelType = TriggerCustomerChannelType.getInstance(interactRecord.getChannelSource());
            if (channelType != null) {
                stringBuilder.append(String.format("通过[%s]", channelType.getDesc()));
            }
        }
        MaterialInteractRecordEventType instance = MaterialInteractRecordEventType.getInstance(interactRecord.getEventType());
        if (instance != null) {
            stringBuilder.append(instance.getDesc());
        }
        return stringBuilder.toString();
    }

    /**
     * 互动记录转换扩展信息
     *
     * @param interactRecord
     * @return
     */
    public InteractRecordExtendInfoBO convertExtendInfo(TabHaobanInteractRecord interactRecord) {
        if (interactRecord == null) {
            log.info("互动记录为空");
            return null;
        }
        if (StringUtils.isBlank(interactRecord.getExtendInfo())) {
            log.info("互动记录扩展信息为空 {}", interactRecord.getId());
            return null;
        }
        if (MaterialInteractRecordEventType.VISIT_MATERIAL.getCode().equals(interactRecord.getEventType())) {
            // 浏览素材事件无扩展信息
            log.info("浏览素材事件无扩展信息 {}", interactRecord.getId());
            return null;
        }
        InteractRecordExtendInfoBO extendInfo = JSON.parseObject(interactRecord.getExtendInfo(), new TypeReference<InteractRecordExtendInfoBO>() {
        });
        if (MaterialInteractRecordEventType.VISIT_PRODUCT.getCode().equals(interactRecord.getEventType())) {
            // 浏览商品事件
            List<String> visitGoodsIds = extendInfo.getGoodsIds();
            if (CollectionUtils.isEmpty(visitGoodsIds)) {
                return null;
            }
            // 拼接商品信息
            List<InteractRecordExtendInfoBO.ExtendGoodsInfo> extendGoodsInfos = this.convertGoodsInfo(visitGoodsIds, interactRecord.getEnterpriseId());
            extendInfo.setExtendGoodsInfos(extendGoodsInfos);
            extendInfo.setInvalidGoodsNum(visitGoodsIds.size() - extendGoodsInfos.size());
            return extendInfo;
        }
        return extendInfo;
    }

    private List<InteractRecordExtendInfoBO.ExtendGoodsInfo> convertGoodsInfo(List<String> visitGoodsIds, String enterpriseId) {
        ErpGoodsInfoQDTO search = new ErpGoodsInfoQDTO();
        search.setEnterpriseId(enterpriseId);
        search.setGoodsIds(visitGoodsIds);
        search.setExcludeStatus(Arrays.asList(GoodsStatusEnum.DELETED.getCode(), GoodsStatusEnum.BACK.getCode()));
        ServiceResponse<List<GoodsInfoSimpleDTO>> serviceResponse =
                goodsInfoOutApiService.queryErpGoodsInfoWithIds(search);
        if (!serviceResponse.isSuccess()) {
            return Collections.emptyList();
        }
        return serviceResponse.getResult()
                .stream()
                .map(item -> {
                    InteractRecordExtendInfoBO.ExtendGoodsInfo extendGoodsInfo = new InteractRecordExtendInfoBO.ExtendGoodsInfo();
                    extendGoodsInfo.setGoodsName(item.getGoodsName());
                    extendGoodsInfo.setGoodsCode(item.getGoodsCode());
                    extendGoodsInfo.setGoodsImageUrl(item.getGoodsImageUrl());
                    // todo 商品价格 待订单迭代上线后再调整
                    // extendGoodsInfo.setSalePrice(item.getStatus());
                    return extendGoodsInfo;
                })
                .collect(Collectors.toList());

    }
}
