package com.willalp.canteen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.canteen.domain.HsGiftOrderDetail;
import com.willalp.canteen.domain.resp.OrderResp;
import com.willalp.canteen.mapper.HsGiftOrderDetailMapper;
import com.willalp.canteen.service.IHsGiftOrderDetailService;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 食堂订单明细Service业务层处理
 *
 * @author songyu
 * @date 2021-12-03
 */
@Service
public class HsGiftOrderDetailServiceImpl extends ServiceImpl<HsGiftOrderDetailMapper, HsGiftOrderDetail> implements IHsGiftOrderDetailService {
    @Autowired
    private HsGiftOrderDetailMapper hsGiftOrderDetailMapper;

    /**
     * 查询食堂订单明细列表
     *
     * @param hsGiftOrderDetail 食堂订单明细
     * @return 食堂订单明细
     */
    @Override
    public List<OrderResp> selectHsCanteenOrderDetailList(HsGiftOrderDetail hsGiftOrderDetail) {
        return hsGiftOrderDetailMapper.selectHsCanteenOrderDetailList(hsGiftOrderDetail);
    }

    @Override
    public List<HsGiftOrderDetail> shopOrderStatList(String eventId) {
        if (StringUtils.isBlank(eventId)) {
            throw new BaseException("请选择一个活动");
        }
        List<HsGiftOrderDetail> orderDetails = getBaseMapper().selectList(new QueryWrapper<HsGiftOrderDetail>()
                .eq("event_id", eventId));
        List<HsGiftOrderDetail> list = null;
        if (orderDetails.size() != 0) {
            Map<String, List<HsGiftOrderDetail>> mapStat = new HashMap<>();
            orderDetails.forEach(detail -> mapStat.computeIfAbsent(detail.getGiftNumber(), k -> new ArrayList<>()));
            if (0 != mapStat.size()) {
                mapStat.forEach((key, details) -> details.addAll(orderDetails.stream().filter(detail -> StringUtils.isNotBlank(detail.getGiftNumber()) && detail.getGiftNumber().equals(key)).collect(Collectors.toList())));
            }
            list = new ArrayList<>();
            List<HsGiftOrderDetail> finalList = list;
            mapStat.forEach((key, details) -> {
                HsGiftOrderDetail hsGiftOrderDetail = new HsGiftOrderDetail();
                hsGiftOrderDetail.setGiftNumber(key);
                hsGiftOrderDetail.setGiftName(details.get(0).getGiftName());
                hsGiftOrderDetail.setPrice(details.get(0).getTotal());
                hsGiftOrderDetail.setAmount(details.size());
                BigDecimal size = new BigDecimal(details.size());
                hsGiftOrderDetail.setTotal(details.get(0).getTotal().multiply(size));
                finalList.add(hsGiftOrderDetail);
            });
        }
        return list;
    }

}
