package com.hai.service.impl;

import com.hai.dao.OutRechargeChildOrderMapper;
import com.hai.dao.OutRechargeOrderRecordMapper;
import com.hai.entity.OutRechargeChildOrder;
import com.hai.entity.OutRechargeChildOrderExample;
import com.hai.entity.OutRechargeOrderRecord;
import com.hai.entity.OutRechargeOrderRecordExample;
import com.hai.service.OutRechargeChildOrderService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("outRechargeChildOrderService")
public class OutRechargeChildOrderServiceImpl implements OutRechargeChildOrderService {

    @Resource
    private OutRechargeChildOrderMapper rechargeChildOrderMapper;

    @Override
    public List<OutRechargeChildOrder> getListRechargeChildOrder(Map<String, Object> map) {

        OutRechargeChildOrderExample example = new OutRechargeChildOrderExample();
        OutRechargeChildOrderExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map , "parent_order_id") != null) {
            criteria.andParentOrderIdEqualTo(MapUtils.getLong(map , "parent_order_id"));
        }
        if (MapUtils.getInteger(map , "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map , "status"));
        }

        example.setOrderByClause("create_time desc");

        return rechargeChildOrderMapper.selectByExample(example);
    }

    @Override
    public OutRechargeChildOrder findById(Long id) {
        return rechargeChildOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public OutRechargeChildOrder findByOrderNo(String orderNo) {
        OutRechargeChildOrderExample example = new OutRechargeChildOrderExample();
        OutRechargeChildOrderExample.Criteria criteria = example.createCriteria();

        criteria.andOrderNoEqualTo(orderNo);

        List<OutRechargeChildOrder> list = rechargeChildOrderMapper.selectByExample(example);

        if (list.size() > 0 ) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public OutRechargeChildOrder findByOrderId(Long orderId) {
        return null;
    }

    @Override
    public void insertOrder(OutRechargeChildOrder rechargeChildOrder) {
        rechargeChildOrderMapper.insert(rechargeChildOrder);
    }

    @Override
    public void updateOrder(OutRechargeChildOrder rechargeChildOrder) {
        rechargeChildOrderMapper.updateByPrimaryKey(rechargeChildOrder);
    }
}
