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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.nutrition.common.enums.YesOrNoEnum;
import com.bonus.canteen.core.nutrition.common.mapper.HealthBodyRecordMapper;
import com.bonus.canteen.core.nutrition.common.model.HealthBodyRecord;
import com.bonus.canteen.core.nutrition.common.service.HealthBodyRecordService;
import com.bonus.canteen.core.nutrition.utils.NuConvertUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class HealthBodyRecordServiceImpl extends ServiceImpl<HealthBodyRecordMapper, HealthBodyRecord> implements HealthBodyRecordService {
   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void insertLatestRecord(HealthBodyRecord healthBodyRecord) {
      if (this.checkDate(healthBodyRecord)) {
         BigDecimal bmi = NuConvertUtil.getBMI(healthBodyRecord.getWeight(), healthBodyRecord.getHeight());
         healthBodyRecord.setBmi(bmi);
         LambdaUpdateWrapper<HealthBodyRecord> updateWrapper = Wrappers.lambdaUpdate(HealthBodyRecord.class)
                         .set(HealthBodyRecord::getIfLatest, YesOrNoEnum.NO.key())
                          .eq(HealthBodyRecord::getCustId, healthBodyRecord.getCustId())
                          .eq(HealthBodyRecord::getIfLatest, YesOrNoEnum.YES.key());
         this.update(updateWrapper);
         healthBodyRecord.setIfLatest(YesOrNoEnum.YES.key());
         this.save(healthBodyRecord);
      }
   }

//   public HealthBodyRecord getLatestRecordByCustId(Long custId) {
//      LambdaQueryWrapper<HealthBodyRecord> queryWrapper = (LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(HealthBodyRecord.class).eq(HealthBodyRecord::getCustId, custId)).eq(HealthBodyRecord::getIfLatest, YesOrNoEnum.YES.key());
//      return (HealthBodyRecord)this.getOne(queryWrapper);
//   }
//
//   public PageVO<HealthBodyRecordPageVO> pageHealthMonitoring(HealthMonitoringPageDTO param) {
//      PageHelper.startPage(param);
//      List<HealthBodyRecordPageVO> monitoringPageVOS = ((HealthBodyRecordMapper)this.baseMapper).listHealthMonitoring(param);
//      return PageVO.of(monitoringPageVOS);
//   }
//
//   public List<HealthBodyRecordVo> historyHealthMonitoring(Long custId) {
//      LambdaQueryWrapper<HealthBodyRecord> queryWrapper = (LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(HealthBodyRecord.class).eq(HealthBodyRecord::getCustId, custId)).orderByDesc(HealthBodyRecord::getCrtime);
//      List<HealthBodyRecord> healthBodyRecords = ((HealthBodyRecordMapper)this.baseMapper).selectList(queryWrapper);
//      return LeBeanUtil.copyCreateList(healthBodyRecords, HealthBodyRecordVo.class);
//   }
//
//   public List<HealthBodyRecordPageVO> listHealthMonitoring(HealthMonitoringPageDTO param) {
//      return ((HealthBodyRecordMapper)this.baseMapper).listHealthMonitoring(param);
//   }
//
   public Boolean checkDate(HealthBodyRecord healthBodyRecord) {
      return !ObjectUtil.isNull(healthBodyRecord.getHeight()) && !ObjectUtil.isNull(healthBodyRecord.getWeight());
   }

}
