package com.bonus.canteen.core.order.api;

import com.bonus.canteen.core.order.common.dto.OrderSearchParamDTO;
import com.bonus.canteen.core.order.common.service.OrderInfoService;
import com.bonus.canteen.core.order.common.vo.OrderDetailVO;
import com.bonus.canteen.core.order.common.vo.OrderInfoVO;
import com.bonus.canteen.core.order.web.business.OrderWebBusiness;
import com.bonus.canteen.core.order.web.dto.OrderUserPermissionDTO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderNutritionApi {
   @Autowired
   @Lazy
   private OrderInfoService orderInfoService;
   @Autowired
   @Lazy
   private OrderWebBusiness orderWebBusiness;

   public List<OrderInfoVO> listOrderInfoVO(OrderSearchParamDTO param, boolean filterWithPermission) {
      OrderUserPermissionDTO permissionDTO = filterWithPermission ? this.orderWebBusiness.currentUserPermissions() : null;
      return this.orderInfoService.listVoByParam(param, permissionDTO, true);
   }

   public List<OrderDetailVO> listPersonalDietRecord(Long custId, LocalDate startDate, LocalDate endDate) {
      if (custId != null && startDate != null && endDate != null) {
         OrderSearchParamDTO param = new OrderSearchParamDTO();
         param.setCustId(custId);
         param.setStartDate(startDate);
         param.setEndDate(endDate);
         return this.orderInfoService.listVoByParam(param, null, true).stream().flatMap((s) -> {
            return s.getOrderDetailList().stream();
         }).collect(Collectors.toList());
      } else {
         throw new ServiceException(I18n.getMessage("order.param-illegal"));
      }
   }
}
