package com.ywt.mg.services;

import com.ywt.mg.core.SqlHelper;
import com.ywt.mg.core.utils.Checker;

import com.ywt.mg.domain.mealEntities.UserMealOrderView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderDishService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private SqlHelper sqlHelper;

    private final Logger logger = LoggerFactory.getLogger(OrderDishService.class);



    /**
     * 根据订单IDs得到所有的成本之和
     *
     * @param orderIds eg：1,3,5
     * @return 和
     */
    public int getAllDishCostPriceSumByOrderIds(String orderIds) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select sum(dish_cost_price * count) from nutrimeal.order_dish where order_id in (")
                .append(orderIds)
                .append(") and deleted = 0 and dish_cost_price is not null;");
        Integer dishCostPriceSum = jdbcTemplate.queryForObject(sqlBuilder.toString(), Integer.class);
        return Checker.getIntegerValue(dishCostPriceSum);
    }


    /**
     * 根据 list 列表得到所有的成本之和
     *
     * @param list {@link List <UserMealOrderView>}
     * @return 和
     */
    public int getAllDishCostPriceSumByUserMealOrderViewList(List<UserMealOrderView> list) {
        String orderIds = getOrderIdsByUserMealOrderViewList(list);
        return getAllDishCostPriceSumByOrderIds(orderIds);
    }


    /**
     * 根据 list 列表得到oderIds
     * @param list {@link List<UserMealOrderView>}
     * @return orderIds eg：1，3，5,-1
     */
    public String getOrderIdsByUserMealOrderViewList(List<UserMealOrderView> list) {
        StringBuilder sqlBuilder = new StringBuilder();
        for (UserMealOrderView view : list) {
            sqlBuilder.append(view.getOrderId()).append(",");
        }
        sqlBuilder.append("-1");
        return sqlBuilder.toString();
    }
}
