package cn.reghao.tnb.file.app.zoss.db.repository;

import cn.reghao.tnb.file.app.zoss.db.mapper.UploadChannelMapper;
import cn.reghao.tnb.file.app.zoss.model.po.UploadChannel;
import cn.reghao.tnb.file.app.zoss.rpc.StoreServiceWrapper;
import cn.reghao.tnb.file.app.service.StoreConfigService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author reghao
 * @date 2025-10-19 22:07:47
 */
@Repository
public class ChannelRepository {
    private final UploadChannelMapper uploadChannelMapper;
    private final StoreConfigService storeConfigService;
    private final StoreServiceWrapper storeServiceWrapper;

    public ChannelRepository(UploadChannelMapper uploadChannelMapper, StoreConfigService storeConfigService,
                             StoreServiceWrapper storeServiceWrapper) {
        this.uploadChannelMapper = uploadChannelMapper;
        this.storeConfigService = storeConfigService;
        this.storeServiceWrapper = storeServiceWrapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void createUploadChannel(UploadChannel uploadChannel) throws Exception {
        uploadChannelMapper.save(uploadChannel);

        int userNodeId = uploadChannel.getUserNodeId();
        String prefix = uploadChannel.getPrefix();
        int scope = uploadChannel.getScope();
        int ossUser = storeConfigService.getLocalOssUser();
        storeServiceWrapper.createChannel(userNodeId, prefix, scope, ossUser);
    }
}
