package com.xunhong.erp.turbo.biz.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.biz.dto.cmd.CompanyCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.CompanyDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.CompanyUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.CompanyListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.CompanyPageQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.CompanyShowQry;
import com.xunhong.erp.turbo.biz.domain.entity.Company;
import com.xunhong.erp.turbo.biz.domain.gateway.CompanyGateway;
import com.xunhong.erp.turbo.biz.infrastructure.convert.CompanyConvert;
import com.xunhong.erp.turbo.biz.infrastructure.entity.BoxProductDO;
import com.xunhong.erp.turbo.biz.infrastructure.entity.CompanyDO;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.CompanyMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

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

/**
 * @author shenyifei
 */
@Repository
@RequiredArgsConstructor
public class CompanyGatewayImpl implements CompanyGateway {
	private final CompanyMapper companyMapper;
	private final CompanyConvert companyConvert;

	@Override
	public Company save(CompanyCreateCmd companyCreateCmd) {
		CompanyDO companyDO = companyConvert.toCompanyDO(companyCreateCmd);
		companyMapper.insert(companyDO);

		return companyConvert.toCompany(companyDO);
	}

	@Override
	public IPage<Company> page(CompanyPageQry companyPageQry) {
		LambdaQueryWrapper<CompanyDO> queryWrapper = Wrappers.lambdaQuery(CompanyDO.class);
		queryWrapper.like(StrUtil.isNotBlank(companyPageQry.getShortName()), CompanyDO::getShortName, companyPageQry.getShortName());
		queryWrapper.like(StrUtil.isNotBlank(companyPageQry.getFullName()), CompanyDO::getFullName, companyPageQry.getFullName());
		queryWrapper.like(StrUtil.isNotBlank(companyPageQry.getTaxNumber()), CompanyDO::getTaxNumber, companyPageQry.getTaxNumber());
		queryWrapper.like(StrUtil.isNotBlank(companyPageQry.getRemark()), CompanyDO::getRemark, companyPageQry.getRemark());
		queryWrapper.eq(Objects.nonNull(companyPageQry.getStatus()), CompanyDO::getStatus, companyPageQry.getStatus());

		queryWrapper.orderByDesc(CompanyDO::getCreatedAt);

		IPage<CompanyDO> page = new Page<>(companyPageQry.getPageIndex(), companyPageQry.getPageSize());
		page = companyMapper.selectPage(page, queryWrapper);

		return page.convert(companyConvert::toCompany);
	}

	@Override
	public List<Company> list(CompanyListQry companyListQry) {
		LambdaQueryWrapper<CompanyDO> queryWrapper = Wrappers.lambdaQuery(CompanyDO.class);
		List<CompanyDO> companyDOList = companyMapper.selectList(queryWrapper);
		return companyDOList.stream().map(companyConvert::toCompany).toList();
	}

	@Override
	public Company update(CompanyUpdateCmd companyUpdateCmd) {
		LambdaQueryWrapper<CompanyDO> queryWrapper = Wrappers.lambdaQuery(CompanyDO.class);
		queryWrapper.eq(CompanyDO::getCompanyId, companyUpdateCmd.getCompanyId());
		queryWrapper.last("limit 1");

		CompanyDO companyDO = companyMapper.selectOne(queryWrapper);

		companyConvert.toCompanyDO(companyDO, companyUpdateCmd);
		companyMapper.updateById(companyDO);

		return companyConvert.toCompany(companyDO);
	}

	@Override
	public Company show(CompanyShowQry companyShowQry) {
		LambdaQueryWrapper<CompanyDO> queryWrapper = Wrappers.lambdaQuery(CompanyDO.class);
		queryWrapper.eq(CompanyDO::getCompanyId, companyShowQry.getCompanyId());
		queryWrapper.last("limit 1");

		CompanyDO companyDO = companyMapper.selectOne(queryWrapper);
		return companyConvert.toCompany(companyDO);
	}

	@Override
	public void destroy(CompanyDestroyCmd companyDestroyCmd) {
		LambdaQueryWrapper<CompanyDO> queryWrapper = Wrappers.lambdaQuery(CompanyDO.class);
		queryWrapper.eq(CompanyDO::getCompanyId, companyDestroyCmd.getCompanyId());
		queryWrapper.last("limit 1");

		CompanyDO companyDO = companyMapper.selectOne(queryWrapper);
		companyDO.deleteById();
	}
}

