package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.StorePaymentConfig;
import shop.alien.mapper.StorePaymentConfigMapper;
import shop.alien.store.service.StorePaymentConfigService;

/**
 * 支付配置表 服务实现类
 *
 * @author system
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class StorePaymentConfigServiceImpl extends ServiceImpl<StorePaymentConfigMapper, StorePaymentConfig> implements StorePaymentConfigService {

    @Override
    public StorePaymentConfig getByStoreId(Integer storeId) {
        if (storeId == null) {
            return null;
        }
        LambdaQueryWrapper<StorePaymentConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StorePaymentConfig::getStoreId, storeId);
        return this.getOne(wrapper);
    }

    @Override
    public StorePaymentConfig getByStoreUserId(Integer storeUserId) {
        if (storeUserId == null) {
            return null;
        }
        LambdaQueryWrapper<StorePaymentConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StorePaymentConfig::getStoreUserId, storeUserId);
        return this.getOne(wrapper);
    }

    @Override
    public StorePaymentConfig getByAppId(String appId) {
        if (!StringUtils.hasText(appId)) {
            return null;
        }
        LambdaQueryWrapper<StorePaymentConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StorePaymentConfig::getAppId, appId);
        wrapper.last("LIMIT 1");
        return this.getOne(wrapper);
    }

    @Override
    public IPage<StorePaymentConfig> pageList(Page<StorePaymentConfig> page, Integer storeId, String appId) {
        LambdaQueryWrapper<StorePaymentConfig> wrapper = new LambdaQueryWrapper<>();
        if (storeId != null) {
            wrapper.eq(StorePaymentConfig::getStoreId, storeId);
        }
        if (StringUtils.hasText(appId)) {
            wrapper.like(StorePaymentConfig::getAppId, appId);
        }
        wrapper.orderByDesc(StorePaymentConfig::getUpdatedTime);
        return this.page(page, wrapper);
    }
}
