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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xunhong.erp.turbo.api.user.dto.cmd.AdminCreateCmd;
import com.xunhong.erp.turbo.api.user.dto.qry.AdminShowQry;
import com.xunhong.erp.turbo.user.domain.entity.Admin;
import com.xunhong.erp.turbo.user.domain.gateway.AdminGateway;
import com.xunhong.erp.turbo.user.infrastructure.convert.AdminConvert;
import com.xunhong.erp.turbo.user.infrastructure.entity.AdminDO;
import com.xunhong.erp.turbo.user.infrastructure.mapper.AdminMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Slf4j
@Repository
@RequiredArgsConstructor
public class AdminGatewayImpl implements AdminGateway {
	private final AdminMapper adminMapper;
	private final AdminConvert adminConvert;

	@Override
	public Admin show(AdminShowQry adminShowQry) {
		LambdaQueryWrapper<AdminDO> queryWrapper = Wrappers.lambdaQuery(AdminDO.class);
		queryWrapper.eq(Objects.nonNull(adminShowQry.getAdminId()), AdminDO::getAdminId, adminShowQry.getAdminId());
		queryWrapper.eq(Objects.nonNull(adminShowQry.getUserId()), AdminDO::getUserId, adminShowQry.getUserId());
		queryWrapper.eq(Objects.nonNull(adminShowQry.getType()), AdminDO::getType, adminShowQry.getType());

		AdminDO adminDO = adminMapper.selectOne(queryWrapper);
		return adminConvert.toAdmin(adminDO);
	}

	@Override
	public Admin register(AdminCreateCmd adminCreateCmd) {
		AdminDO adminDO = adminConvert.toAdminDO(adminCreateCmd);
		adminMapper.insert(adminDO);
		return adminConvert.toAdmin(adminDO);
	}
}
