package com.erp.tenant.service.goods.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.tenant.entity.goods.Goods;
import com.erp.tenant.repository.goods.GoodsRepository;
import com.erp.tenant.repository.stock.StockRepository;
import com.erp.tenant.service.goods.GoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户端商品服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsServiceImpl implements GoodsService {

    private final GoodsRepository goodsRepository;
    private final StockRepository stockRepository;

    @Override
    public IPage<Goods> getGoodsPage(Long tenantId, String keyword, Long categoryId,
                                     Long brandId, Integer status,
                                     int page, int pageSize) {
        return goodsRepository.findPage(
                new Page<>(page, pageSize),
                tenantId, keyword, categoryId, brandId, status
        );
    }

    @Override
    public Goods getGoodsDetail(Long tenantId, Long goodsId) {
        Goods goods = goodsRepository.findById(goodsId, tenantId);
        if (goods != null) {
            // 补充库存信息
            Integer totalQty = stockRepository.sumQuantityByTenant(tenantId, null);
            goods.setStockQuantity(totalQty != null ? totalQty : 0);
        }
        return goods;
    }

    @Override
    @Transactional
    public boolean putOnSale(Long tenantId, Long goodsId) {
        Goods goods = goodsRepository.findById(goodsId, tenantId);
        if (goods == null) {
            return false;
        }
        int rows = goodsRepository.putOnSale(goodsId, tenantId);
        return rows > 0;
    }

    @Override
    @Transactional
    public boolean putOffSale(Long tenantId, Long goodsId) {
        Goods goods = goodsRepository.findById(goodsId, tenantId);
        if (goods == null) {
            return false;
        }
        int rows = goodsRepository.putOffSale(goodsId, tenantId);
        return rows > 0;
    }

    @Override
    @Transactional
    public int batchPutOnSale(Long tenantId, List<Long> ids) {
        int count = 0;
        for (Long id : ids) {
            if (goodsRepository.putOnSale(id, tenantId) > 0) {
                count++;
            }
        }
        return count;
    }

    @Override
    @Transactional
    public int batchPutOffSale(Long tenantId, List<Long> ids) {
        int count = 0;
        for (Long id : ids) {
            if (goodsRepository.putOffSale(id, tenantId) > 0) {
                count++;
            }
        }
        return count;
    }

    @Override
    public GoodsStats getGoodsStats(Long tenantId) {
        GoodsStats stats = new GoodsStats();
        stats.total = goodsRepository.countByStatus(tenantId, null);
        stats.onSale = goodsRepository.countByStatus(tenantId, Goods.STATUS_ON_SALE);
        stats.offSale = goodsRepository.countByStatus(tenantId, Goods.STATUS_OFF_SALE);
        return stats;
    }
}
