package com.bonus.canteen.core.nutrition.common.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.nutrition.common.dto.NutrientReferenceIntakeReviseDto;
import com.bonus.canteen.core.nutrition.common.enums.PregnantStatusEnum;
import com.bonus.canteen.core.nutrition.common.mapper.NutrientReferenceIntakeReviseMapper;
import com.bonus.canteen.core.nutrition.common.model.NutrientReferenceIntakeRevise;
import com.bonus.canteen.core.nutrition.common.service.NutrientReferenceIntakeReviseService;
import com.bonus.canteen.core.nutrition.common.vo.NutrReferenceIntakeVO;
import com.bonus.canteen.core.nutrition.common.vo.NutrientReferenceIntakeReviseVo;
import com.bonus.common.houqin.utils.LeBeanUtil;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

@Service
public class NutrientReferenceIntakeReviseServiceImpl extends ServiceImpl<NutrientReferenceIntakeReviseMapper, NutrientReferenceIntakeRevise> implements NutrientReferenceIntakeReviseService {
   public NutrientReferenceIntakeReviseVo getNutrientReferenceIntakeReference(String nutrition) {
      NutrientReferenceIntakeReviseVo intakeReviseVo = (new NutrientReferenceIntakeReviseVo()).setNutrition(nutrition);
      List<NutrientReferenceIntakeRevise> intakeRevises = this.list();
      Iterator var4 = intakeRevises.iterator();

      while(var4.hasNext()) {
         NutrientReferenceIntakeRevise revise = (NutrientReferenceIntakeRevise)var4.next();
         BigDecimal nutrientValue;
         if (NumberUtil.equals(revise.getPregnantStatus(), PregnantStatusEnum.FIRST_TRIMESTER.key())) {
            nutrientValue = revise.getNutrientValue(nutrition);
            intakeReviseVo.setFirstTrimesterCorrection(nutrientValue);
         } else if (NumberUtil.equals(revise.getPregnantStatus(), PregnantStatusEnum.SECOND_TRIMESTER.key())) {
            nutrientValue = revise.getNutrientValue(nutrition);
            intakeReviseVo.setSecondTrimesterCorrection(nutrientValue);
         } else if (NumberUtil.equals(revise.getPregnantStatus(), PregnantStatusEnum.LATE_PREGNANCY.key())) {
            nutrientValue = revise.getNutrientValue(nutrition);
            intakeReviseVo.setThirdTrimesterCorrection(nutrientValue);
         } else if (NumberUtil.equals(revise.getPregnantStatus(), PregnantStatusEnum.WET_NURSE.key())) {
            nutrientValue = revise.getNutrientValue(nutrition);
            intakeReviseVo.setWetNurseModifier(nutrientValue);
         }
      }

      return intakeReviseVo;
   }

   public void changeNutrientReferenceIntakeReference(NutrientReferenceIntakeReviseDto reviseDto) {
      String nutrition = reviseDto.getNutrition();
      NutrientReferenceIntakeRevise first = (new NutrientReferenceIntakeRevise()).setPregnantStatus(PregnantStatusEnum.FIRST_TRIMESTER.key()).setNutrientValue(nutrition, reviseDto.getFirstTrimesterCorrection());
      this.update(first, Wrappers.lambdaUpdate(NutrientReferenceIntakeRevise.class).eq(NutrientReferenceIntakeRevise::getPregnantStatus, PregnantStatusEnum.FIRST_TRIMESTER.key()));
      NutrientReferenceIntakeRevise second = (new NutrientReferenceIntakeRevise()).setPregnantStatus(PregnantStatusEnum.SECOND_TRIMESTER.key()).setNutrientValue(nutrition, reviseDto.getSecondTrimesterCorrection());
      this.update(second, Wrappers.lambdaUpdate(NutrientReferenceIntakeRevise.class).eq(NutrientReferenceIntakeRevise::getPregnantStatus, PregnantStatusEnum.SECOND_TRIMESTER.key()));
      NutrientReferenceIntakeRevise late = (new NutrientReferenceIntakeRevise()).setPregnantStatus(PregnantStatusEnum.LATE_PREGNANCY.key()).setNutrientValue(nutrition, reviseDto.getThirdTrimesterCorrection());
      this.update(late, Wrappers.lambdaUpdate(NutrientReferenceIntakeRevise.class).eq(NutrientReferenceIntakeRevise::getPregnantStatus, PregnantStatusEnum.LATE_PREGNANCY.key()));
      NutrientReferenceIntakeRevise wetNurse = (new NutrientReferenceIntakeRevise()).setPregnantStatus(PregnantStatusEnum.WET_NURSE.key()).setNutrientValue(nutrition, reviseDto.getWetNurseModifier());
      this.update(wetNurse, Wrappers.lambdaUpdate(NutrientReferenceIntakeRevise.class).eq(NutrientReferenceIntakeRevise::getPregnantStatus, PregnantStatusEnum.WET_NURSE.key()));
   }

   public NutrReferenceIntakeVO getInfoByPregnantStatus(Integer pregnantStatus) {
      NutrientReferenceIntakeRevise referenceIntakeRevise = this.getOne(Wrappers.lambdaQuery(NutrientReferenceIntakeRevise.class).eq(NutrientReferenceIntakeRevise::getPregnantStatus, pregnantStatus));
      return (NutrReferenceIntakeVO) LeBeanUtil.copyCreateProperties(referenceIntakeRevise, NutrReferenceIntakeVO.class);
   }
}
