package com.wansenai.service.shop.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wansenai.entities.shop.BasShop;
import com.wansenai.mappers.shop.BasShopMapper;
import com.wansenai.service.shop.BasShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BasShopServiceImpl extends ServiceImpl<BasShopMapper, BasShop> implements BasShopService {

    @Override
    public BasShop getShopByCode(String code) {
        return baseMapper.getShopByCode(code);
    }

    @Override
    public List<BasShop> getShopListByName(String name) {
        return baseMapper.getShopListByName(name);
    }

    @Override
    public List<BasShop> getShopListByChannel(String channel) {
        return baseMapper.getShopListByChannel(channel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddShop(List<BasShop> shopList) {
        if (CollectionUtils.isEmpty(shopList)) {
            return false;
        }
        
        // 设置创建时间
        Date now = new Date();
        shopList.forEach(shop -> {
            shop.setCreateTime(now);
            // 这里可以设置创建人，通常从当前登录用户中获取
            // shop.setCreateBy(getCurrentUsername());
        });
        
        return baseMapper.batchInsert(shopList) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addShop(BasShop shop) {
        // 设置创建时间
        shop.setCreateTime(new Date());
        // 这里可以设置创建人，通常从当前登录用户中获取
        // shop.setCreateBy(getCurrentUsername());
        
        return save(shop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateShop(BasShop shop) {
        // 设置更新时间
        shop.setUpdateTime(new Date());
        // 这里可以设置更新人，通常从当前登录用户中获取
        // shop.setUpdateBy(getCurrentUsername());
        
        return updateById(shop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteShop(Long id) {
        return removeById(id);
    }
}
