package com.bonus.canteen.core.menu.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bonus.common.houqin.utils.Arith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;

public class NutritionEntityUtil {
   private static final Logger log = LoggerFactory.getLogger(NutritionEntityUtil.class);

   private NutritionEntityUtil() {
   }

   public static NutritionEntity countNutrition(List<NutritionEntity> nutritionEntityList) {
      if (CollectionUtils.isEmpty(nutritionEntityList)) {
         return new NutritionEntity();
      } else {
         NutritionEntity nutritionEntity = new NutritionEntity();
         Iterator var2 = nutritionEntityList.iterator();

         while(var2.hasNext()) {
            NutritionEntity tempNutrition = (NutritionEntity)var2.next();
            double proportion = Arith.div(tempNutrition.getWeight(), tempNutrition.getBaseWeight(), 4);
            Field[] var6 = tempNutrition.getClass().getDeclaredFields();
            int var7 = var6.length;

            for(int var8 = 0; var8 < var7; ++var8) {
               Field field = var6[var8];
               ReflectionUtils.makeAccessible(field);

               try {
                  String fieldName = field.getName();
                  Object temp = field.get(tempNutrition);
                  double param = 0.0;
                  if (temp != null && (Double)temp != -1.0) {
                     param = (Double)temp;
                  }

                  Field declaredField = nutritionEntity.getClass().getDeclaredField(fieldName);
                  ReflectionUtils.makeAccessible(declaredField);
                  Object tempObject = declaredField.get(nutritionEntity);
                  double lastValue = 0.0;
                  if (tempObject != null) {
                     lastValue = (Double)tempObject;
                  }

                  double newValue = Arith.add(Arith.mul(param, proportion), lastValue);
                  declaredField.set(nutritionEntity, newValue);
               } catch (NoSuchFieldException | IllegalAccessException var21) {
                  log.info("获取属性值异常 : {}", var21.getMessage());
               }
            }
         }

         Field[] var22 = nutritionEntity.getClass().getDeclaredFields();
         int var23 = var22.length;

         for(int var24 = 0; var24 < var23; ++var24) {
            Field field = var22[var24];
            ReflectionUtils.makeAccessible(field);

            try {
               Object tempObject = field.get(nutritionEntity);
               if (tempObject != null && (Double)tempObject != -1.0) {
                  field.set(nutritionEntity, Arith.round((Double)tempObject, 2));
               }
            } catch (Exception var20) {
               log.info("最终获取属性值异常 : {}", var20.getMessage());
            }
         }

         return nutritionEntity;
      }
   }
}
