package org.dromara.zhishu.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.zhishu.domain.dto.TShopGoodsPublishedDto;
import org.dromara.zhishu.mapper.TShopGoodsPublishedMapper;
import org.dromara.zhishu.service.TShopGoodsPublishedService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 已发布商品信息Service实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TShopGoodsPublishedServiceImpl implements TShopGoodsPublishedService {

    private final TShopGoodsPublishedMapper tShopGoodsPublishedMapper;

    // ==================== 表管理 ====================

    @DS("slave")
    @Override
    public boolean checkTableExists() {
        try {
            int count = tShopGoodsPublishedMapper.checkTableExists();
            return count > 0;
        } catch (Exception e) {
            log.error("检查表是否存在失败", e);
            return false;
        }
    }

    @DS("slave")
    @Override
    public void createTable() {
        try {
            tShopGoodsPublishedMapper.createTable();
            log.info("创建表成功");
        } catch (Exception e) {
            log.error("创建表失败", e);
            throw new RuntimeException("创建表失败: " + e.getMessage(), e);
        }
    }

    @DS("slave")
    @Override
    public void initTable() {
        if (!checkTableExists()) {
            createTable();
            log.info("初始化表成功");
        } else {
            log.info("表已存在");
        }
    }

    // ==================== 插入操作 ====================

    @DS("slave")
    @Override
    public int insert(TShopGoodsPublishedDto dto) {
        if (dto == null) {
            throw new IllegalArgumentException("已发布商品信息不能为空");
        }
        try {
            return tShopGoodsPublishedMapper.insert(dto);
        } catch (Exception e) {
            log.error("插入已发布商品失败", e);
            throw new RuntimeException("插入已发布商品失败: " + e.getMessage(), e);
        }
    }

    @DS("slave")
    @Override
    public int batchInsert(List<TShopGoodsPublishedDto> list) {
        if (CollectionUtils.isEmpty(list)) {
            log.warn("批量插入已发布商品列表为空");
            return 0;
        }
        try {
            return tShopGoodsPublishedMapper.batchInsert(list);
        } catch (Exception e) {
            log.error("批量插入已发布商品失败, size: {}", list.size(), e);
            throw new RuntimeException("批量插入已发布商品失败: " + e.getMessage(), e);
        }
    }

    // ==================== 删除操作 ====================

    @DS("slave")
    @Override
    public int deleteById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        try {
            return tShopGoodsPublishedMapper.deleteById(id);
        } catch (Exception e) {
            log.error("根据ID删除已发布商品失败, id: {}", id, e);
            throw new RuntimeException("删除已发布商品失败: " + e.getMessage(), e);
        }
    }

    @DS("slave")
    @Override
    public int deleteByCondition(Long erpShopId, Long shopId, Long productId, Long warehouseId) {
        try {
            return tShopGoodsPublishedMapper.deleteByCondition(erpShopId, shopId, productId, warehouseId);
        } catch (Exception e) {
            log.error("根据条件删除已发布商品失败", e);
            throw new RuntimeException("删除已发布商品失败: " + e.getMessage(), e);
        }
    }

    @DS("slave")
    @Override
    public int batchDeleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            log.warn("批量删除已发布商品ID列表为空");
            return 0;
        }
        try {
            return tShopGoodsPublishedMapper.batchDeleteByIds(ids);
        } catch (Exception e) {
            log.error("批量删除已发布商品失败, ids: {}", ids, e);
            throw new RuntimeException("批量删除已发布商品失败: " + e.getMessage(), e);
        }
    }

    @DS("slave")
    @Override
    public int deleteAll() {
        try {
            return tShopGoodsPublishedMapper.deleteAll();
        } catch (Exception e) {
            log.error("删除所有已发布商品失败", e);
            throw new RuntimeException("删除所有已发布商品失败: " + e.getMessage(), e);
        }
    }

    // ==================== 更新操作 ====================

    @DS("slave")
    @Override
    public int updateById(TShopGoodsPublishedDto dto) {
        if (dto == null) {
            throw new IllegalArgumentException("已发布商品信息不能为空");
        }
        if (dto.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        try {
            return tShopGoodsPublishedMapper.updateById(dto);
        } catch (Exception e) {
            log.error("根据ID更新已发布商品失败, id: {}", dto.getId(), e);
            throw new RuntimeException("更新已发布商品失败: " + e.getMessage(), e);
        }
    }

    // ==================== 查询操作 ====================

    @DS("slave")
    @Override
    public TShopGoodsPublishedDto selectById(Long id) {
        if (id == null) {
            return null;
        }
        try {
            return tShopGoodsPublishedMapper.selectById(id);
        } catch (Exception e) {
            log.error("根据ID查询已发布商品失败, id: {}", id, e);
            return null;
        }
    }

    @DS("slave")
    @Override
    public List<TShopGoodsPublishedDto> selectByErpShopId(Long erpShopId) {
        if (erpShopId == null) {
            return List.of();
        }
        try {
            return tShopGoodsPublishedMapper.selectByErpShopId(erpShopId);
        } catch (Exception e) {
            log.error("根据ERP店铺ID查询已发布商品失败, erpShopId: {}", erpShopId, e);
            return List.of();
        }
    }

    @DS("slave")
    @Override
    public List<TShopGoodsPublishedDto> selectByShopId(Long shopId) {
        if (shopId == null) {
            return List.of();
        }
        try {
            return tShopGoodsPublishedMapper.selectByShopId(shopId);
        } catch (Exception e) {
            log.error("根据平台店铺ID查询已发布商品失败, shopId: {}", shopId, e);
            return List.of();
        }
    }

    @DS("slave")
    @Override
    public TShopGoodsPublishedDto selectByProductId(Long productId) {
        if (productId == null) {
            return null;
        }
        try {
            return tShopGoodsPublishedMapper.selectByProductId(productId);
        } catch (Exception e) {
            log.error("根据商品ID查询已发布商品失败, productId: {}", productId, e);
            return null;
        }
    }

    @DS("slave")
    @Override
    public List<TShopGoodsPublishedDto> selectByCondition(TShopGoodsPublishedDto dto) {
        try {
            return tShopGoodsPublishedMapper.selectByCondition(dto);
        } catch (Exception e) {
            log.error("分页条件查询已发布商品失败", e);
            return List.of();
        }
    }

    @DS("slave")
    @Override
    public int selectCountByCondition(TShopGoodsPublishedDto dto) {
        try {
            return tShopGoodsPublishedMapper.selectCountByCondition(dto);
        } catch (Exception e) {
            log.error("条件查询已发布商品总数失败", e);
            return 0;
        }
    }

    @DS("slave")
    @Override
    public List<TShopGoodsPublishedDto> selectAll() {
        try {
            return tShopGoodsPublishedMapper.selectAll();
        } catch (Exception e) {
            log.error("查询所有已发布商品失败", e);
            return List.of();
        }
    }

    @DS("slave")
    @Override
    public int countAll() {
        try {
            return tShopGoodsPublishedMapper.countAll();
        } catch (Exception e) {
            log.error("统计总记录数失败", e);
            return 0;
        }
    }

    @DS("slave")
    @Override
    public TShopGoodsPublishedDto selectByErpShopIdAndProductId(Long erpShopId, Long productId) {
        if (erpShopId == null || productId == null) {
            return null;
        }
        try {
            return tShopGoodsPublishedMapper.selectByErpShopIdAndProductId(erpShopId, productId);
        } catch (Exception e) {
            log.error("根据ERP店铺ID和商品ID查询失败, erpShopId: {}, productId: {}", erpShopId, productId, e);
            return null;
        }
    }
}