package com.czg.service.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.service.ShopInfoService;
import com.czg.constants.SystemConstants;
import com.czg.order.dto.ShopMerchantDTO;
import com.czg.order.entity.ShopDirectMerchant;
import com.czg.order.entity.ShopMerchant;
import com.czg.order.service.ShopMerchantService;
import com.czg.pay.AlipayAuthInfoDto;
import com.czg.pay.NativeMerchantDTO;
import com.czg.pay.PolyMerchantDTO;
import com.czg.service.order.mapper.ShopMerchantMapper;
import com.czg.service.order.service.ShopDirectMerchantService;
import com.czg.utils.AssertUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 第三方商户进件 服务层实现。
 *
 * @author Administrator
 * @since 2025-02-11
 */
@CacheConfig(cacheNames = "shop:merchant")
@Service
public class ShopMerchantServiceImpl extends ServiceImpl<ShopMerchantMapper, ShopMerchant> implements ShopMerchantService {

    @Resource
    private ShopDirectMerchantService shopDirectMerchantService;
    @DubboReference
    private ShopInfoService shopInfoService;

    @Override
    public ShopMerchantDTO detail(Long shopId) {
        ShopMerchantDTO shopMerchantVO = new ShopMerchantDTO();
        ShopMerchant one = getOne(query().eq(ShopMerchant::getShopId, shopId));
        if (one == null) {
            return null;
        }
        shopMerchantVO.setShopId(shopId);
        shopMerchantVO.setChannel(one.getChannel());
        shopMerchantVO.setRelatedId(one.getRelatedId());
        if (StrUtil.isNotBlank(one.getNativePayJson())) {
            shopMerchantVO.setNativeMerchantDTO(JSONObject.parseObject(one.getNativePayJson(), NativeMerchantDTO.class));
        }
        if (StrUtil.isNotBlank(one.getPolyPayJson())) {
            shopMerchantVO.setPolyMerchantDTO(JSONObject.parseObject(one.getPolyPayJson(), PolyMerchantDTO.class));
        }
        if (one.getRelatedId() != null) {
            shopMerchantVO.setShopDirectMerchant(shopDirectMerchantService.getOne(query().eq(ShopDirectMerchant::getShopId, one.getRelatedId())));
        }
        return shopMerchantVO;
    }

    /**
     * * shopId           修改的店铺Id
     * * relatedLicenceNo 关联值 原生native必填 对应 tb_shop_direct_merchant 的 licence_no营业执照
     * * channel          {@link com.czg.constant.PayChannelCst}
     * * jsonObject       支付参数 详情
     * * isUp             是否要修改 传否时 如果存在支付数据 且与当前渠道不一致 则不修改
     */
    @Override
    @CacheEvict(key = "#shopMerchantParam.shopId")
    public Boolean editEntry(ShopMerchantDTO shopMerchantParam, boolean isUp) {
        ShopMerchant shopMerchant = getOne(query().eq(ShopMerchant::getShopId, shopMerchantParam.getShopId()));
        if (shopMerchant == null) {
            shopMerchant = new ShopMerchant();
            shopMerchant.setAlipayAppId(SystemConstants.PayAppId.ALIPAY_APP_ID);
            shopMerchant.setWechatAppId(SystemConstants.PayAppId.WECHAT_APP_ID);
            shopMerchant.setRelatedId(shopMerchantParam.getShopId());
        }
        shopMerchant.setShopId(shopMerchantParam.getShopId());
        if (isUp) {
            if (StrUtil.isNotBlank(shopMerchantParam.getChannel())) {
                shopMerchant.setChannel(shopMerchantParam.getChannel());
            }
            shopMerchant.setRelatedId(shopMerchantParam.getRelatedId());
            if (shopMerchant.getRelatedId() != null && !shopMerchantParam.getRelatedId().equals(shopMerchant.getRelatedId())) {
                ShopDirectMerchant shopDirectMerchant = shopDirectMerchantService.getById(shopMerchant.getRelatedId());
                if (shopDirectMerchant != null) {
                    NativeMerchantDTO nativeMerchantDTO = new NativeMerchantDTO();
                    nativeMerchantDTO.setWechatMerchantId(shopDirectMerchant.getWechatMerchantId());
                    nativeMerchantDTO.setAlipayMerchantId(shopDirectMerchant.getAlipayMerchantId());
                    if (StrUtil.isNotBlank(shopDirectMerchant.getAlipayAuthInfo())) {
                        AlipayAuthInfoDto alipayAuthInfoDto = JSONObject.parseObject(shopDirectMerchant.getAlipayAuthInfo(), AlipayAuthInfoDto.class);
                        nativeMerchantDTO.setAlipayAuthInfo(alipayAuthInfoDto);
                    }
                    shopMerchant.setNativePayJson(JSONObject.toJSONString(nativeMerchantDTO));
                } else {
                    shopMerchant.setNativePayJson(null);
                    shopMerchant.setWechatAppId(null);
                    shopMerchant.setAlipayAppId(null);
                }
            }
            if (shopMerchantParam.getPolyMerchantDTO() != null) {
                shopMerchant.setPolyPayJson(JSONObject.toJSONString(shopMerchantParam.getPolyMerchantDTO()));
            }
        } else {
            //只有进件会进入这里
            if (shopMerchant.getRelatedId() == null) {
                shopMerchant.setRelatedId(shopMerchantParam.getShopId());
            }
            if (StrUtil.isBlank(shopMerchant.getNativePayJson()) && shopMerchantParam.getNativeMerchantDTO() != null) {
                shopMerchant.setNativePayJson(JSONObject.toJSONString(shopMerchantParam.getNativeMerchantDTO()));
            }
        }
        if (shopMerchant.getId() == null) {
            return save(shopMerchant);
        } else {
            return updateById(shopMerchant, false);
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    public void upMerchant(@NotBlank Long relatedId, @NotNull NativeMerchantDTO nativeMerchantDTO) {
        ShopMerchant upShopMerchant = new ShopMerchant();
        upShopMerchant.setAlipayAppId(nativeMerchantDTO.getAlipayMerchantId());
        upShopMerchant.setWechatAppId(nativeMerchantDTO.getWechatMerchantId());
        upShopMerchant.setNativePayJson(JSONObject.toJSONString(nativeMerchantDTO));
        update(upShopMerchant, query().eq(ShopMerchant::getRelatedId, relatedId));
    }

    @Cacheable(key = "#shopId")
    @Override
    public ShopMerchant getByShopId(Long shopId) {
        ShopMerchant shopMerchant = getOne(query().eq(ShopMerchant::getShopId, shopId));
        AssertUtil.isNull(shopMerchant, "暂未开通支付.");
        return shopMerchant;
    }

    @Override
    public ShopDirectMerchant getMainMerchant(Long shopId) {
        Long mainIdByShopId = shopInfoService.getMainIdByShopId(shopId);
        if (mainIdByShopId != null) {
            return shopDirectMerchantService.getOne(query().eq(ShopDirectMerchant::getShopId, mainIdByShopId));
        }
        return null;
    }
}
