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 com.wansenai.service.user.ISysUserService;
import com.wansenai.utils.SnowflakeIdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BasShopServiceImpl extends ServiceImpl<BasShopMapper, BasShop> implements BasShopService {

    private final ISysUserService sysUserService;

    @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.setId(SnowflakeIdUtil.nextId());
        shop.setCreateTime(new Date());
        // 这里可以设置创建人，通常从当前登录用户中获取
         shop.setCreateBy(sysUserService.getCurrentUserName());
        
        return save(shop);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteShop(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return removeByIds(ids);
    }
}
