package com.jeesharp.business.modules.marketing.tenantQrcode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.marketing.tenantQrcode.entity.MarketingTenantQrcode;
import com.jeesharp.business.modules.marketing.tenantQrcode.mapper.MarketingTenantQrcodeMapper;
import com.jeesharp.business.modules.marketing.tenantQrcode.service.MarketingTenantQrcodeService;
import com.jeesharp.business.modules.marketing.tenantQrcode.vo.MarketingTenantQrcodeQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 店铺推广二维码领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Service
public class MarketingTenantQrcodeServiceImpl extends CrudService<MarketingTenantQrcodeMapper, MarketingTenantQrcode, MarketingTenantQrcodeQueryVO>
        implements MarketingTenantQrcodeService {

    private static final int STATUS_ENABLED = 1;

    @Override
    @Transactional(readOnly = false)
    public MarketingTenantQrcode createMarketingTenantQrcode(MarketingTenantQrcode entity) {
        if (StrUtil.isBlank(entity.getId())) {
            entity.preInsert();
        }
        if (entity.getStatus() == null) {
            entity.setStatus(STATUS_ENABLED);
        }
        if (entity.getScanCount() == null) {
            entity.setScanCount(0L);
        }
        getBaseMapper().insert(entity);
        return entity;
    }

    @Override
    public MarketingTenantQrcode getByWxScene(String wxScene) {
        if (StrUtil.isBlank(wxScene)) {
            return null;
        }
        return getBaseMapper().getByWxScene(wxScene.trim());
    }

    @Override
    public MarketingTenantQrcode getByTenantAndChannel(String tenantId, String channelCode) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(channelCode)) {
            return null;
        }
        return getBaseMapper().getByTenantAndChannel(tenantId, channelCode.trim());
    }

    @Override
    public MarketingTenantQrcode getByTenantAndChannelIncludingDeleted(String tenantId, String channelCode) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(channelCode)) {
            return null;
        }
        return getBaseMapper().getByTenantAndChannelIncludingDeleted(tenantId, channelCode.trim());
    }

    @Override
    @Transactional(readOnly = false)
    public MarketingTenantQrcode restoreDeletedMarketingTenantQrcode(MarketingTenantQrcode entity) {
        if (entity.getStatus() == null) {
            entity.setStatus(STATUS_ENABLED);
        }
        if (entity.getScanCount() == null) {
            entity.setScanCount(0L);
        }
        entity.preUpdate();
        getBaseMapper().restoreDeleted(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void incrementScanCount(String id) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        getBaseMapper().incrementScanCount(id);
    }

    /**
     * 逻辑删除走 Mapper XML，勿用父类 Db.remove（会与 MP delete(Wrapper) 参数冲突）。
     */
    @Override
    @Transactional(readOnly = false)
    public boolean delete(String id) {
        if (StrUtil.isBlank(id)) {
            return false;
        }
        return getBaseMapper().delete(id.trim()) > 0;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean batchDelete(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        for (String id : ids) {
            if (StrUtil.isNotBlank(id)) {
                getBaseMapper().delete(id.trim());
            }
        }
        return true;
    }
}
