package com.waimai.web.admin.service.impl;

import java.util.List;
import com.waimai.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.waimai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.waimai.web.admin.domain.OrderFoods;
import com.waimai.web.admin.mapper.OrdersMapper;
import com.waimai.web.admin.domain.Orders;
import com.waimai.web.admin.service.IOrdersService;

/**
 * 订单记录Service业务层处理
 * 
 * @author callmeyan
 * @date 2023-03-24
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询订单记录
     * 
     * @param id 订单记录主键
     * @return 订单记录
     */
    @Override
    public Orders selectOrdersById(String id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单记录列表
     * 
     * @param orders 订单记录
     * @return 订单记录
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单记录
     * 
     * @param orders 订单记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        int rows = ordersMapper.insertOrders(orders);
        insertOrderFoods(orders);
        return rows;
    }

    /**
     * 修改订单记录
     * 
     * @param orders 订单记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        orders.setUpdateTime(DateUtils.getNowDate());
        ordersMapper.deleteOrderFoodsByOid(orders.getId());
        insertOrderFoods(orders);
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单记录
     * 
     * @param ids 需要删除的订单记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByIds(String[] ids)
    {
        ordersMapper.deleteOrderFoodsByOids(ids);
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单记录信息
     * 
     * @param id 订单记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersById(String id)
    {
        ordersMapper.deleteOrderFoodsByOid(id);
        return ordersMapper.deleteOrdersById(id);
    }

    /**
     * 新增订单商品信息
     * 
     * @param orders 订单记录对象
     */
    public void insertOrderFoods(Orders orders)
    {
        List<OrderFoods> orderFoodsList = orders.getOrderFoodsList();
        String id = orders.getId();
        if (StringUtils.isNotNull(orderFoodsList))
        {
            List<OrderFoods> list = new ArrayList<OrderFoods>();
            for (OrderFoods orderFoods : orderFoodsList)
            {
                orderFoods.setOid(id);
                list.add(orderFoods);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchOrderFoods(list);
            }
        }
    }
}
