package com.czg.service.market.service.impl;

import cn.hutool.core.util.StrUtil;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.dto.ShopCouponDTO;
import com.czg.market.entity.MkShopCouponRecord;
import com.czg.market.service.MkDistributionUserService;
import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.service.ShopCouponService;
import com.czg.utils.FunUtils;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkShareBase;
import com.czg.market.service.MkShareBaseService;
import com.czg.service.market.mapper.MkShareBaseMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 分享奖励基础配置 服务层实现。
 *
 * @author ww
 * @since 2026-01-27
 */
@Service
public class MkShareBaseServiceImpl extends ServiceImpl<MkShareBaseMapper, MkShareBase> implements MkShareBaseService {

    @Resource
    private ShopCouponService shopCouponService;
    @Resource
    private MkDistributionUserService distributionUserService;
    @Resource
    private MkShopCouponRecordService mkShopCouponRecordService;

    @Override
    public MkShareBase getShareBase(Long shopId) {
        MkShareBase shareBase = getById(shopId);
        if (shareBase != null) {
            if (shareBase.getSharerCouponId() != null) {
                ShopCouponDTO sharerCoupon = shopCouponService.getCouponById(shareBase.getSharerCouponId());
                if (sharerCoupon != null) {
                    shareBase.setSharerCouponName(sharerCoupon.getTitle());
                    shareBase.setSharerCoupon(sharerCoupon);
                }
            }
            if (shareBase.getSharedUserCouponId() != null) {
                ShopCouponDTO shareUserCoupon = shopCouponService.getCouponById(shareBase.getSharedUserCouponId());
                if (shareUserCoupon != null) {
                    shareBase.setSharedUserCouponName(shareUserCoupon.getTitle());
                }
            }
        }
        return shareBase;
    }

    @Override
    public void shareClaim(String tagType, Long shopId, Long fromUserId, Long toUserId) {
        //获取邀请码
        String inviteCode = null;
        try {
            inviteCode = distributionUserService.getInviteCode(shopId, fromUserId);
        } catch (Exception _) {
        }
        //绑定上下级
        if (StrUtil.isNotBlank(inviteCode)) {
            FunUtils.safeRunVoid(() -> {
                distributionUserService.bindInviteUser(fromUserId, toUserId, shopId);
            }, "shareClaim绑定上下级");
        }
        MkShareBase shareBase = getById(shopId);
        if (shareBase == null || !shareBase.getIsEnabled().equals(1) || StrUtil.isBlank(shareBase.getRewardSharePages())) {
            return;
        }
        if (!shareBase.getRewardSharePages().contains(tagType)) {
            return;
        }
        FunUtils.safeRun(() -> {
            //发放分享人优惠券
            if (shareBase.getSharerCouponId() != null) {
                boolean grant = true;
                if (shareBase.getRewardTimesType().equals(1)) {
                    boolean exists = mkShopCouponRecordService.exists(query()
                            .eq(MkShopCouponRecord::getShopId, shopId)
                            .eq(MkShopCouponRecord::getShopUserId, fromUserId)
                            .eq(MkShopCouponRecord::getSource, "邀请获得"));
                    grant = !exists;
                }
                if (grant) {
                    grant = !mkShopCouponRecordService.exists(query()
                            .eq(MkShopCouponRecord::getShopId, shopId)
                            .eq(MkShopCouponRecord::getShopUserId, fromUserId)
                            .eq(MkShopCouponRecord::getSourceFlowId, toUserId)
                            .eq(MkShopCouponRecord::getSource, "邀请获得"));
                }
                if (grant) {
                    MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO();
                    giftDTO.setShopId(shopId)
                            .setSourceId(shareBase.getShopId())
                            .setSourceFlowId(toUserId)
                            .setShopUserId(fromUserId)
                            .setCouponId(shareBase.getSharerCouponId())
                            .setSource("邀请获得");
                    int receiveNum = 1;
                    if (shareBase.getSharerCouponNum() != null && shareBase.getSharerCouponNum() > 0) {
                        receiveNum = shareBase.getSharerCouponNum();
                    }
                    mkShopCouponRecordService.receiveCoupon(giftDTO, receiveNum, false);
                }
            }
            return null;
        }, "邀请人获取优惠券");
        FunUtils.safeRun(() -> {
            //发放被分享人优惠券
            if (shareBase.getSharedUserCouponId() != null) {
                boolean exists = mkShopCouponRecordService.exists(query()
                        .eq(MkShopCouponRecord::getShopId, shopId)
                        .eq(MkShopCouponRecord::getShopUserId, toUserId)
                        .eq(MkShopCouponRecord::getSourceFlowId, fromUserId)
                        .eq(MkShopCouponRecord::getSource, "参与分享获得"));
                if (!exists) {
                    MkShopCouponGiftDTO giftDTO = new MkShopCouponGiftDTO();
                    giftDTO.setShopId(shopId)
                            .setSourceId(shareBase.getShopId())
                            .setShopUserId(toUserId)
                            .setSourceFlowId(fromUserId)
                            .setCouponId(shareBase.getSharedUserCouponId())
                            .setSource("参与分享获得");
                    int receiveNum = 1;
                    if (shareBase.getSharedUserCouponNum() != null && shareBase.getSharedUserCouponNum() > 0) {
                        receiveNum = shareBase.getSharedUserCouponNum();
                    }
                    mkShopCouponRecordService.receiveCoupon(giftDTO, receiveNum, false);
                }
            }
            return null;
        }, "被分享人获取优惠券");
    }
}
