package com.cyksj.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.OrderDonMapper;
import com.cyksj.mapper.channel.ChannelPopularizeMapper;
import com.cyksj.mapper.order.OrderDonPostMapper;
import com.cyksj.mapper.sys.SysConfigMapper;
import com.cyksj.model.entity.ChannelPopularize;
import com.cyksj.model.entity.OrderDonPost;
import com.cyksj.service.order.OrderDonPostService;
import com.cyksj.service.order.post.AbstractPostDataService;
import com.cyksj.service.order.post.PostDataFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author chan
 * @date 2023/5/16 11:05 AM
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderDonPostServiceImpl extends ServiceImpl<OrderDonPostMapper, OrderDonPost> implements OrderDonPostService {

    private final OrderDonMapper orderDonMapper;

    private final SysConfigMapper sysConfigMapper;

    private final ChannelPopularizeMapper channelPopularizeMapper;

    @Override
    public void postData(Long orderId, Long goodsId, Long popularizeId) throws Exception {
        OrderDonPost orderPost = this.getOne(Wrappers.lambdaQuery(OrderDonPost.class).eq(OrderDonPost::getOrderId, orderId).eq(OrderDonPost::getStatus, Boolean.FALSE));
        if (orderPost == null) {
            log.info("post 数据 不了存在 id:{}", orderId);
            return;
        }
        if (popularizeId != null) {
            ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
            if (channelPopularize != null) {
                String filterGoodsIds = channelPopularize.getFilterGoodsIds();
                if (StrUtil.isNotBlank(filterGoodsIds)) {
                    List<Long> filterGoodsIdList = Jsons.parseList(filterGoodsIds, Long.class);
                    if (CollUtil.isNotEmpty(filterGoodsIdList) && filterGoodsIdList.contains(goodsId)) {
                        log.info("该条:{}投放链接 不回传该商品. 该订单商品id:{}", popularizeId, goodsId);
                        return;
                    }
                }
            }
        }
        post(orderId, orderPost);
    }

    @Override
    public void savePostData(Long orderId, String callback, String callbackType) {
        if (OrderDonPost.Type.verify(callbackType) && StringUtils.isNotBlank(callback)) {
            log.info("订单数据回传添加记录:{},type:{},orderType:{}", orderId, callbackType);
            OrderDonPost.Type byType = OrderDonPost.Type.getByType(callbackType);

            this.save(new OrderDonPost().setCallback(callback).setEvenType(byType.getEvenType()).setOrderId(orderId).setType(byType));
        }

    }


    /**
     * 数据回传
     */
    private void post(Long orderId, OrderDonPost orderPost) throws Exception {
        log.info("回传平台:{}", orderPost.getType().getType());
        AbstractPostDataService postStrategy = PostDataFactory.getPostStrategy(orderPost.getType().getType());

        Assert.notNull(postStrategy, "回传渠道类型不存在。");

        postStrategy.postData(orderId, orderPost.getCallback(), orderPost.getEvenType());
    }


    /**
     * 数据回传
     */
    @Override
    public void post(String type, String callback, String event) throws Exception {
        log.info("回传平台:{}", type);
        AbstractPostDataService postStrategy = PostDataFactory.getPostStrategy(type);

        Assert.notNull(postStrategy, "回传渠道类型不存在。");

        postStrategy.postData(0L, callback, event);
    }
}