package org.dromara.zhishu.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.dromara.zhishu.domain.bo.ShopWarehouseBo;
import org.dromara.zhishu.domain.vo.ShopWarehouseVo;
import org.dromara.zhishu.domain.ShopWarehouse;
import org.dromara.zhishu.mapper.ShopWarehouseMapper;
import org.dromara.zhishu.service.IShopWarehouseService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 店铺与仓库自动发布关联Service业务层处理
 *
 * @author yxy
 * @date 2025-12-28
 */
@RequiredArgsConstructor
@Service
public class ShopWarehouseServiceImpl implements IShopWarehouseService {

    private final ShopWarehouseMapper baseMapper;

    /**
     * 查询店铺与仓库自动发布关联
     *
     * @param id 主键
     * @return 店铺与仓库自动发布关联
     */
    @Override
    public ShopWarehouseVo queryById(Long id) {
        ShopWarehouse shopWarehouse = baseMapper.selectById(id);
        return MapstructUtils.convert(shopWarehouse, ShopWarehouseVo.class);
    }

    /**
     * 根据店铺ID查询关联列表
     *
     * @param shopId 店铺ID
     * @return 关联列表
     */
    @Override
    public List<ShopWarehouseVo> queryListByShopId(Long shopId) {
        List<ShopWarehouse> list = baseMapper.selectByShopId(shopId);
        return list.stream()
                .map(item -> MapstructUtils.convert(item, ShopWarehouseVo.class))
                .collect(Collectors.toList());
    }

    /**
     * 新增店铺与仓库自动发布关联
     *
     * @param bo 店铺与仓库自动发布关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ShopWarehouseBo bo) {
        ShopWarehouse add = MapstructUtils.convert(bo, ShopWarehouse.class);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改店铺与仓库自动发布关联
     *
     * @param bo 店铺与仓库自动发布关联
     * @return 是否修改成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ShopWarehouseBo bo) {
        ShopWarehouse update = MapstructUtils.convert(bo, ShopWarehouse.class);
        return baseMapper.update(update) > 0;
    }

    /**
     * 根据店铺删除关联信息
     * @param shopId 店铺ID
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByShopId(Long shopId) {
        return baseMapper.deleteByShopId(shopId) > 0;
    }

}