package com.hai.service.impl;

import com.hai.dao.HighDiscountPackageActualMapper;
import com.hai.dao.HighDiscountPackageDiscountActualMapper;
import com.hai.entity.HighDiscountPackageActual;
import com.hai.entity.HighDiscountPackageActualExample;
import com.hai.entity.HighDiscountPackageDiscountActual;
import com.hai.entity.HighDiscountPackageDiscountActualExample;
import com.hai.service.HighDiscountPackageActualService;
import org.springframework.stereotype.Service;

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

/**
 * @serviceName HighDiscountPackageActualServiceImpl.java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description // 优惠券包实际库存
 * @createTime 3:06 下午 2021/12/8
 **/
@Service("highDiscountPackageActualService")
public class HighDiscountPackageActualServiceImpl implements HighDiscountPackageActualService {

    @Resource
    private HighDiscountPackageActualMapper discountPackageActualMapper;

    @Resource
    private HighDiscountPackageDiscountActualMapper discountPackageDiscountActualMapper;

    @Override
    public void insertHighDiscountPackageActual(HighDiscountPackageActual highDiscountPackageActual) {
        discountPackageActualMapper.insert(highDiscountPackageActual);
    }

    @Override
    public void updateHighDiscountPackageActual(HighDiscountPackageActual highDiscountPackageActual) {
        discountPackageActualMapper.updateByPrimaryKey(highDiscountPackageActual);
    }

    @Override
    public List<HighDiscountPackageActual> getHighDiscountPackageActualList(Integer discountPackageId) {

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

        criteria.andDiscountPackageIdEqualTo(discountPackageId).andStatusEqualTo(1);

        return discountPackageActualMapper.selectByExample(example);
    }


    @Override
    public long getStockByPackageId(Integer packageId) {
        HighDiscountPackageDiscountActualExample example = new HighDiscountPackageDiscountActualExample();
        example.createCriteria().andDiscountPackageIdEqualTo(packageId).andStatusEqualTo(1);
        return discountPackageDiscountActualMapper.countByExample(example);
    }

    @Override
    public HighDiscountPackageActual getDetailByChildOrderId(Long childOrderId) {
        HighDiscountPackageActualExample example = new HighDiscountPackageActualExample();
        example.createCriteria().andChildOrderIdEqualTo(childOrderId);
        List<HighDiscountPackageActual> list = discountPackageActualMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighDiscountPackageActual> getListByPackageId(Integer packageId, Integer status) {
        HighDiscountPackageActualExample example = new HighDiscountPackageActualExample();
        HighDiscountPackageActualExample.Criteria criteria = example.createCriteria().andDiscountPackageIdEqualTo(packageId);
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause("created_time");
        return discountPackageActualMapper.selectByExample(example);
    }

    @Override
    public Long getDiscountPackageNum(Integer discountPackageId) {
        HighDiscountPackageActualExample example = new HighDiscountPackageActualExample();
        HighDiscountPackageActualExample.Criteria criteria = example.createCriteria();
        criteria.andDiscountPackageIdEqualTo(discountPackageId).andStatusEqualTo(1);
        return discountPackageActualMapper.countByExample(example);
    }

    @Override
    public Long userPurchaseByUserId(Integer userId, Integer discountPackage) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(2);
        statusList.add(3);
        HighDiscountPackageActualExample example = new HighDiscountPackageActualExample();
        example.createCriteria().andDiscountPackageIdEqualTo(discountPackage).andUserIdEqualTo(userId).andStatusIn(statusList);
        return discountPackageActualMapper.countByExample(example);
    }
}
