package com.cyksj.service.advertisement.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.mapper.advertisement.AdvertisementOrderPostMapper;
import com.cyksj.model.entity.AdvertisementOrderPost;
import com.cyksj.model.entity.OrderDonPost;
import com.cyksj.service.advertisement.AdvertisementOrderPostService;
import com.cyksj.service.order.post.AbstractPostDataService;
import com.cyksj.service.order.post.PostDataFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author chan
 * @date 2023/5/16 11:05 AM
 */
@Service
@Slf4j
public class AdvertisementOrderPostServiceImpl  extends ServiceImpl<AdvertisementOrderPostMapper, AdvertisementOrderPost> implements AdvertisementOrderPostService {

    @Override
    public void postData(Long orderId) throws Exception {
        AdvertisementOrderPost orderPost = this.getOne(Wrappers.lambdaQuery(AdvertisementOrderPost.class).eq(AdvertisementOrderPost::getOrderId, orderId).eq(AdvertisementOrderPost::getStatus, Boolean.FALSE));
        if (orderPost == null) {
            log.info("post 数据 不了存在 id:{}", orderId);
            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);
            AdvertisementOrderPost.Type byType = AdvertisementOrderPost.Type.getByType(callbackType);

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

    }


    /**
     * 数据回传
     */
    private void post(Long orderId, AdvertisementOrderPost orderPost) throws Exception {
        AbstractPostDataService postStrategy = PostDataFactory.getPostStrategy(orderPost.getType().getType());

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

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

}