package com.hfkj.service.coupon.impl;

import com.hfkj.common.utils.DateUtil;
import com.hfkj.dao.BsOrderCouponMapper;
import com.hfkj.entity.BsOrderCoupon;
import com.hfkj.entity.BsOrderCouponExample;
import com.hfkj.model.order.OrderCouponModel;
import com.hfkj.service.coupon.BsOrderCouponService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsOrderCouponServiceImpl
 * @author: HuRui
 * @date: 2024/5/10
 **/
@Service("orderCouponService")
public class BsOrderCouponServiceImpl implements BsOrderCouponService {

    @Resource
    private BsOrderCouponMapper orderCouponMapper;

    @Override
    public void editData(BsOrderCoupon data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            orderCouponMapper.insert(data);
        } else {
            orderCouponMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public BsOrderCoupon getDetailByChildOrderNo(String orderChildNo) {
        BsOrderCouponExample example = new BsOrderCouponExample();
        example.createCriteria().andChildOrderNoEqualTo(orderChildNo);
        List<BsOrderCoupon> list = orderCouponMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsOrderCoupon getDetail(Long id) {
        return orderCouponMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderCouponModel> getOrderList(Map<String, Object> param) {
        if (MapUtils.getLong(param, "createTimeS") != null) {
            param.put("createTimeS", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeS")), DateUtil.Y_M_D_HMS));
        }
        if (MapUtils.getLong(param, "createTimeE") != null) {
            param.put("createTimeE", DateUtil.date2String(new Date(MapUtils.getLong(param, "createTimeE")), DateUtil.Y_M_D_HMS));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(param, "platformCode"))) {
            param.put("platformCode", MapUtils.getString(param, "platformCode"));
        }
        return orderCouponMapper.getOrderList(param);
    }


}
