package com.community.huiju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.BillInvoiceMapper;
import com.community.huiju.dao.TpBillOrderMapper;
import com.community.huiju.model.BillInvoice;
import com.community.huiju.model.TpBillOrder;
import com.community.huiju.service.IBillOrderService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 缴费订单表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-04-18
 */
@Service
@Slf4j
public class BillOrderServiceImpl extends ServiceImpl<TpBillOrderMapper, TpBillOrder> implements IBillOrderService {

    @Autowired
    private TpBillOrderMapper tpBillOrderMapper;

    @Autowired
    private BillInvoiceMapper billInvoiceMapper;

    @Override
    public ResponseBean getByOrderNumberBillInvoice(UserElement userElement, String orderNumber) {
        ResponseBean responseBean = new ResponseBean();

        // 1.根据订单查询出所有的缴费组
        // 2.根据缴费组查询查询出缴费单

        // 当前订单所有的收费组
        List<TpBillOrder> billOrderList = tpBillOrderMapper.getByOrderNumberBillInvoice(orderNumber);

        billOrderList.forEach(e->{
            QueryWrapper<TpBillOrder> billInvoiceOrderQueryWrapper = new QueryWrapper<>();
            billInvoiceOrderQueryWrapper.eq("tp_bill_id", e.getTpBillId());
            billInvoiceOrderQueryWrapper.eq("order_bumber", orderNumber);
            // 当前收费组下的所有的缴费单
            List<TpBillOrder> billInvoiceOrderList = tpBillOrderMapper.selectList(billInvoiceOrderQueryWrapper);
            List<BillInvoice> billInvoiceList = billInvoiceOrderList.stream().map(billInvoice -> {
                                                    return billInvoiceMapper.selectById(billInvoice.getTpBillInvoiceId());
                                                }).collect(Collectors.toList());
            e.setBillInvoiceList(billInvoiceList);
        });

        Map<String, Object> result = Maps.newHashMap();
        result.put("orderNumber", orderNumber);
        result.put("billOrderList", billOrderList);

        responseBean.addSuccess(result);
        return responseBean;
    }
}
