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

import cn.hutool.core.collection.CollUtil;
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.qry.OrderRebatePageQry;
import com.xunhong.erp.turbo.biz.domain.entity.OrderRebate;
import com.xunhong.erp.turbo.biz.domain.gateway.OrderRebateGateway;
import com.xunhong.erp.turbo.biz.infrastructure.convert.OrderRebateConvert;
import com.xunhong.erp.turbo.biz.infrastructure.entity.OrderCompanyDO;
import com.xunhong.erp.turbo.biz.infrastructure.entity.OrderRebateDO;
import com.xunhong.erp.turbo.biz.infrastructure.entity.PurchaseOrderDO;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.OrderCompanyMapper;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.OrderRebateMapper;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.PurchaseOrderMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author shenyifei
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class OrderRebateGatewayImpl implements OrderRebateGateway {
	private final OrderRebateMapper orderRebateMapper;
	private final OrderRebateConvert orderRebateConvert;

	private final OrderCompanyMapper orderCompanyMapper;
	private final PurchaseOrderMapper purchaseOrderMapper;


	@Override
	public IPage<OrderRebate> page(OrderRebatePageQry orderRebatePageQry) {
		LambdaQueryWrapper<OrderRebateDO> queryWrapper = Wrappers.lambdaQuery(OrderRebateDO.class);
		queryWrapper.eq(Objects.nonNull(orderRebatePageQry.getIsPaid()), OrderRebateDO::getIsPaid, orderRebatePageQry.getIsPaid());

		queryWrapper.orderByDesc(OrderRebateDO::getCreatedAt);

		IPage<OrderRebateDO> page = new Page<>(orderRebatePageQry.getPageIndex(), orderRebatePageQry.getPageSize());
		page = orderRebateMapper.selectPage(page, queryWrapper, orderRebatePageQry);

		List<Long> orderIdList = page.getRecords().stream().map(OrderRebateDO::getOrderId).toList();
		if (CollUtil.isNotEmpty(orderIdList)) {
			LambdaQueryWrapper<OrderCompanyDO> queryWrapper2 = Wrappers.lambdaQuery(OrderCompanyDO.class);
			queryWrapper2.in(OrderCompanyDO::getOrderId, orderIdList);
			queryWrapper2.select(OrderCompanyDO::getOrderId, OrderCompanyDO::getShortName, OrderCompanyDO::getFullName, OrderCompanyDO::getCompanyId);
			List<OrderCompanyDO> orderCompanyDOList = orderCompanyMapper.selectList(queryWrapper2);
			Map<Long, OrderCompanyDO> orderCompanyDOMap = orderCompanyDOList.stream().collect(Collectors.toMap(OrderCompanyDO::getOrderId, Function.identity()));
			page.getRecords().forEach(orderCostDO -> {
				OrderCompanyDO orderCompanyDO = orderCompanyDOMap.get(orderCostDO.getOrderId());
				if (Objects.nonNull(orderCompanyDO)) {
					orderCostDO.setOrderCompanyDO(orderCompanyDO);
				}
			});

			List<PurchaseOrderDO> purchaseOrderDOList = purchaseOrderMapper.selectByOrderIdList(orderIdList);
			Map<Long, PurchaseOrderDO> purchaseOrderDOMap = purchaseOrderDOList.stream().collect(Collectors.toMap(PurchaseOrderDO::getOrderId, Function.identity()));
			page.getRecords().forEach(orderCostDO -> {
				PurchaseOrderDO purchaseOrderDO = purchaseOrderDOMap.get(orderCostDO.getOrderId());
				if (Objects.nonNull(purchaseOrderDO)) {
					orderCostDO.setPurchaseOrderDO(purchaseOrderDO);
				}
			});
		}

		return page.convert(orderRebateConvert::toOrderRebate);
	}
}

