package com.xunhong.erp.turbo.infra.infrastructure.gateway;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.ChannelUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.ChannelPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.ChannelShowQry;
import com.xunhong.erp.turbo.infra.domain.entity.Channel;
import com.xunhong.erp.turbo.infra.domain.gateway.ChannelGateway;
import com.xunhong.erp.turbo.infra.infrastructure.convert.ChannelConvert;
import com.xunhong.erp.turbo.infra.infrastructure.entity.ChannelDO;
import com.xunhong.erp.turbo.infra.infrastructure.mapper.ChannelMapper;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Repository
@RequiredArgsConstructor
public class ChannelGatewayImpl implements ChannelGateway {

	private final ChannelMapper channelMapper;
	private final ChannelConvert channelConvert;
	private final RedissonClient redissonClient;

	@Override
	public IPage<Channel> page(ChannelPageQry channelPageQry) {
		LambdaQueryWrapper<ChannelDO> queryWrapper = Wrappers.lambdaQuery(ChannelDO.class);
		Page<ChannelDO> page = new Page<>(channelPageQry.getPageIndex(), channelPageQry.getPageSize());
		channelMapper.selectPage(page, queryWrapper);

		return page.convert(channelConvert::toChannel);
	}

	@Override
	public Channel show(ChannelShowQry channelShowQry) {
		LambdaQueryWrapper<ChannelDO> queryWrapper = Wrappers.lambdaQuery(ChannelDO.class);
		queryWrapper.eq(Objects.nonNull(channelShowQry.getChannelId()), ChannelDO::getChannelId, channelShowQry.getChannelId());
		queryWrapper.eq(StrUtil.isNotEmpty(channelShowQry.getDomain()), ChannelDO::getDomain, channelShowQry.getDomain());
		ChannelDO channelDO = channelMapper.selectOne(queryWrapper);

		return channelConvert.toChannel(channelDO);
	}

	@Override
	public Channel save(ChannelCreateCmd channelCreateCmd) {
		ChannelDO channelDO = channelConvert.toChannelDO(channelCreateCmd);
		channelMapper.insert(channelDO);
		return channelConvert.toChannel(channelDO);
	}

	@Override
	public void destroy(ChannelDestroyCmd channelDestroyCmd) {
		ChannelDO channelDO = channelMapper.selectById(channelDestroyCmd.getChannelId());

		channelMapper.deleteById(channelDestroyCmd.getChannelId());
		redissonClient.getBucket("channel:" + channelDO.getChannelId()).deleteAsync();
	}

	@Override
	public Channel update(ChannelUpdateCmd channelUpdateCmd) {
		ChannelDO channelDO = channelConvert.toChannelDO(channelUpdateCmd);
		channelMapper.updateById(channelDO);

		redissonClient.getBucket("channel:" + channelDO.getChannelId()).deleteAsync();
		return channelConvert.toChannel(channelDO);
	}
}
