package com.hfkj.service.coupon.impl;

import com.hfkj.dao.BsOrderCouponNoMapper;
import com.hfkj.entity.BsOrderCouponNo;
import com.hfkj.entity.BsOrderCouponNoExample;
import com.hfkj.service.coupon.BsOrderCouponNoService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsOrderCouponNoServiceImpl
 * @author: HuRui
 * @date: 2024/5/11
 **/
@Service("orderCouponNoService")
public class BsOrderCouponNoServiceImpl implements BsOrderCouponNoService {
    @Resource
    private BsOrderCouponNoMapper orderCouponNoMapper;
    @Override
    public void editData(BsOrderCouponNo data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            orderCouponNoMapper.insert(data);
        } else {
            orderCouponNoMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public BsOrderCouponNo getDetailByCouNo(String couNo) {
        BsOrderCouponNoExample example = new BsOrderCouponNoExample();
        example.createCriteria().andGoodsVpdSourceCouNoEqualTo(couNo);
        List<BsOrderCouponNo> list = orderCouponNoMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsOrderCouponNo> getListByCouponOrderId(Long couponOrderId) {
        BsOrderCouponNoExample example = new BsOrderCouponNoExample();
        example.createCriteria().andCouponOrderIdEqualTo(couponOrderId);
        example.setOrderByClause("create_time desc");
        return orderCouponNoMapper.selectByExample(example);
    }

    @Override
    public BsOrderCouponNo getListByOrderNo(String orderNo) {
        BsOrderCouponNoExample example = new BsOrderCouponNoExample();
        example.createCriteria().andExt1EqualTo(orderNo);
        example.setOrderByClause("create_time desc");
        List<BsOrderCouponNo> list = orderCouponNoMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsOrderCouponNo> getList(Map<String, Object> param) {
        BsOrderCouponNoExample example = new BsOrderCouponNoExample();
        BsOrderCouponNoExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        if (MapUtils.getBoolean(param, "expireStatus") != null) {
            criteria.andExpireStatusEqualTo(MapUtils.getBoolean(param, "expireStatus"));
        }

        if (MapUtils.getBoolean(param, "exceptionStatus") != null) {
            criteria.andExceptionStatusEqualTo(MapUtils.getBoolean(param, "exceptionStatus"));
        }

        example.setOrderByClause("create_time desc");
        return orderCouponNoMapper.selectByExample(example);
    }
}
