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

import com.alibaba.cola.exception.BizException;
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.PlatformCreateCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.PlatformDestroyCmd;
import com.xunhong.erp.turbo.api.infra.dto.cmd.PlatformUpdateCmd;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformListQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformPageQry;
import com.xunhong.erp.turbo.api.infra.dto.qry.PlatformShowQry;
import com.xunhong.erp.turbo.infra.domain.entity.Platform;
import com.xunhong.erp.turbo.infra.domain.gateway.PlatformGateway;
import com.xunhong.erp.turbo.infra.infrastructure.convert.PlatformConvert;
import com.xunhong.erp.turbo.infra.infrastructure.entity.PlatformDO;
import com.xunhong.erp.turbo.infra.infrastructure.mapper.PlatformMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author shenyifei
 */
@Repository
@RequiredArgsConstructor
public class PlatformGatewayImpl implements PlatformGateway {
	private final PlatformMapper platformMapper;
	private final PlatformConvert platformConvert;

	@Override
	public Platform save(PlatformCreateCmd platformCreateCmd) {
		PlatformDO platformDO = platformConvert.toPlatformDO(platformCreateCmd);
		platformMapper.insert(platformDO);

		return platformConvert.toPlatform(platformDO);
	}

	@Override
	public IPage<Platform> page(PlatformPageQry platformPageQry) {
		LambdaQueryWrapper<PlatformDO> queryWrapper = Wrappers.lambdaQuery(PlatformDO.class);
		IPage<PlatformDO> page = new Page<>(platformPageQry.getPageIndex(), platformPageQry.getPageSize());
		page = platformMapper.selectPage(page, queryWrapper);

		return page.convert(platformConvert::toPlatform);
	}

	@Override
	public List<Platform> list(PlatformListQry platformListQry) {
		LambdaQueryWrapper<PlatformDO> queryWrapper = Wrappers.lambdaQuery(PlatformDO.class);

		List<PlatformDO> platformDOList = platformMapper.selectList(queryWrapper);
		return platformDOList.stream().map(platformConvert::toPlatform).toList();
	}

	@Override
	public Platform update(PlatformUpdateCmd platformUpdateCmd) {
		PlatformDO platformDO = platformConvert.toPlatformDO(platformUpdateCmd);
		platformMapper.updateById(platformDO);
		return platformConvert.toPlatform(platformDO);
	}

	@Override
	public Platform show(PlatformShowQry platformShowQry) {
		PlatformDO platformDO = platformMapper.selectById(platformShowQry.getPlatformId());
		return platformConvert.toPlatform(platformDO);
	}

	@Override
	public void destroy(PlatformDestroyCmd platformDestroyCmd) {
		// 判断平台是否存在
		PlatformDO platformDO = platformMapper.selectById(platformDestroyCmd.getPlatformId());
		if (Objects.isNull(platformDO)) {
			throw new BizException("400", "平台不存在，不能删除");
		}

		platformMapper.deleteById(platformDestroyCmd.getPlatformId());
	}
}
