package com.jwipc.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.order.entity.AppointmentRecord;
import com.jwipc.order.mapper.AppointmentRecordMapper;
import com.jwipc.order.model.po.*;
import com.jwipc.order.model.query.AppointmentRecordQuery;
import com.jwipc.order.model.query.AppointmentShowTimeQuery;
import com.jwipc.order.service.IAppointmentRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 送货单预约记录表 服务实现类
 * </p>
 *
 * @author WT
 * @since 2024-03-05
 */
@Service
public class AppointmentRecordServiceImpl extends ServiceImpl<AppointmentRecordMapper, AppointmentRecord> implements IAppointmentRecordService {

    @Override
    public List<AppointmentRecordPO> findPageByQuery(Page<AppointmentRecord> dbPage, AppointmentRecordQuery query) {
        if(dbPage == null){
            return baseMapper.findPageByQuery(query);
        }
        return baseMapper.findPageByQuery(dbPage,query);
    }

    @Override
    public AppointmentShowMainPO appointmentShow(AppointmentShowTimeQuery query) {
        List<AppointmentShowPO> showDtos = baseMapper.appointmentShow(query);
        List<AppointmentShowPO> collect = showDtos.stream().filter(t -> t.getSelectOrder() == 1).collect(Collectors.toList());
        //都没有时间匹配的默认选中第一个
        if (collect.size() == 0 && CollectionUtil.isNotEmpty(showDtos)) {
            showDtos.get(0).setSelectOrder(1);
        }
        HashSet<String> supplierCodes = new HashSet<>();
        showDtos.forEach(t -> {
            List<AppointmentShowPO.Item> items = t.getItems();
            if (CollectionUtil.isNotEmpty(items)) {
                BigDecimal reduce = items.stream()
                        .peek(t2 -> {
                            String supplierCode = t2.getSupplierCode();
                            if (StringUtils.isNotBlank(supplierCode)) {
                                supplierCodes.add(supplierCode);
                            }
                        })
                        .filter(t2 -> ObjectUtil.isNotNull(t2.getDeLiveryTimeTotal()))
                        .map(t2 -> new BigDecimal(t2.getDeLiveryTimeTotal()))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                t.setAllUseCount(reduce);
                t.setCanUse(t.getMaxNum() - t.getAllUseCount().intValue());
            }else{
                t.setAllUseCount(BigDecimal.ZERO);
                t.setCanUse(t.getMaxNum());

            }
        });
        long appointCount = this.count(new LambdaQueryWrapper<AppointmentRecord>()
                .eq(AppointmentRecord::getAppointmentDate, query.getAppointmentDate()));

        AppointmentShowMainPO appointmentShowMainPo = new AppointmentShowMainPO();
        appointmentShowMainPo.setDtos(showDtos);
        appointmentShowMainPo.setAppointmentCount((int) appointCount);
        appointmentShowMainPo.setSupplierCount(supplierCodes.size());
        return appointmentShowMainPo;
    }

    @Override
    public List<DeliveryDetailRecordPO> deliveryDetailsRecord(Page<AppointmentRecord> dbPage, AppointmentRecordQuery query) {
        if (ObjectUtil.isNull(dbPage)) {
            return baseMapper.getDeleiveryDetail(query);
        } else {
            return baseMapper.getDeleiveryDetail(dbPage, query);
        }
    }
}
