package com.czg.service.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.merchant.ShopMerchantEditDTO;
import com.czg.account.entity.ShopMerchant;
import com.czg.account.service.ShopMerchantService;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.ShopMerchantMapper;
import com.czg.utils.AssertUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;

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

    @Override
    public ShopMerchant detail(Integer shopId) {
        ShopMerchant one = queryChain().eq(ShopMerchant::getShopId, shopId).one();
        return one == null ? new ShopMerchant() : one;
    }

    @CacheEvict(key = "#shopMerchantEditDTO.shopId")
    @Override
    public Boolean edit(ShopMerchantEditDTO shopMerchantEditDTO) {
        ShopMerchant shopMerchant = queryChain().eq(ShopMerchant::getShopId, shopMerchantEditDTO.getShopId()).one();
        if (shopMerchant == null) {
            shopMerchant = new ShopMerchant();
            shopMerchant.setShopId(shopMerchantEditDTO.getShopId());
            BeanUtil.copyProperties(shopMerchantEditDTO, shopMerchant);
            return save(shopMerchant);
        }
        BeanUtil.copyProperties(shopMerchantEditDTO, shopMerchant);
        return updateById(shopMerchant);
    }

    @Cacheable(key = "#id")
    @Override
    public ShopMerchant getById(Serializable id) {
        ShopMerchant shopMerchant = getMapper().selectOneById(id);
        AssertUtil.isNull(shopMerchant, "暂未开通支付");
        return shopMerchant;
    }
}
