package com.hfkj.service.meal.impl;

import com.hfkj.dao.BsMealTableOrderProductMapper;
import com.hfkj.entity.BsMealTableOrderChild;
import com.hfkj.entity.BsMealTableOrderProduct;
import com.hfkj.entity.BsMealTableOrderProductExample;
import com.hfkj.service.meal.BsMealTableOrderProductService;
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: BsMealTableOrderProductServiceImpl
 * @author: HuRui
 * @date: 2023/12/6
 **/
@Service("mealTableOrderProductService")
public class BsMealTableOrderProductServiceImpl implements BsMealTableOrderProductService {

    @Resource
    private BsMealTableOrderProductMapper mealTableOrderProductMapper;

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

    @Override
    public List<BsMealTableOrderProduct> getOrderProductList(Map<String, Object> param) {
        BsMealTableOrderProductExample example = new BsMealTableOrderProductExample();
        BsMealTableOrderProductExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "orderNo"))) {
            criteria.andOrderNoEqualTo(MapUtils.getString(param, "orderNo"));
        }

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

    @Override
    public BsMealTableOrderProduct getOrderProduct(String orderNo, Long productId) {
        BsMealTableOrderProductExample example = new BsMealTableOrderProductExample();
        example.createCriteria().andOrderNoEqualTo(orderNo).andProductIdEqualTo(productId).andStatusNotEqualTo(0);
        List<BsMealTableOrderProduct> list = mealTableOrderProductMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
}
