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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.customer.constants.CustAgeEnum;
import com.bonus.canteen.core.nutrition.common.dto.HealthInfoFullDto;
import com.bonus.canteen.core.nutrition.common.enums.HealthInfoSourceEnum;
import com.bonus.canteen.core.nutrition.common.mapper.HealthInfoMapper;
import com.bonus.canteen.core.nutrition.common.model.HealthInfo;
import com.bonus.canteen.core.nutrition.common.model.HealthBodyRecord;
import com.bonus.canteen.core.nutrition.common.model.HealthInfoChronic;
import com.bonus.canteen.core.nutrition.common.service.HealthBodyRecordService;
import com.bonus.canteen.core.nutrition.common.service.HealthInfoChronicService;
import com.bonus.canteen.core.nutrition.common.service.HealthInfoService;
import com.bonus.canteen.core.nutrition.common.vo.HealthInfoFullVo;
import com.bonus.canteen.core.nutrition.common.vo.HealthInfoSimpleVo;
import com.bonus.canteen.core.nutrition.utils.NuConvertUtil;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class HealthInfoServiceImpl extends ServiceImpl<HealthInfoMapper, HealthInfo> implements HealthInfoService {
   @Resource
   @Lazy
   private HealthBodyRecordService healthBodyRecordService;
   @Resource
   @Lazy
   private HealthInfoChronicService healthInfoChronicService;
   @Autowired
   private HealthInfoMapper healthInfoMapper;

   @Override
   public HealthInfoSimpleVo simplifyHealthInfo(Long custId) {
      return healthInfoMapper.getSimpleInfo(custId);
   }

   public HealthInfoFullVo detailHealthInfo(Long custId) {
      HealthInfoFullVo healthInfoFullVo = ((HealthInfoMapper)this.baseMapper).fullHealthInfo(custId);
      healthInfoFullVo.setAge(CustAgeEnum.getAge(healthInfoFullVo.getBirthday()));
      return healthInfoFullVo;
   }

   @Transactional(
           rollbackFor = {Exception.class}
   )
   public void editHealthInfo(HealthInfoFullDto content) {
      Long custId = content.getCustId();
      BigDecimal bmi = NuConvertUtil.getBMI(content.getWeight(), content.getHeight());
      content.setBmi(bmi);
      this.remove((Wrapper) Wrappers.lambdaQuery(HealthInfo.class).eq(HealthInfo::getCustId, custId));
      this.healthInfoChronicService.remove((Wrapper)Wrappers.lambdaQuery(HealthInfoChronic.class).eq(HealthInfoChronic::getCustId, custId));
      HealthInfo healthInfo = (HealthInfo)LeBeanUtil.copyCreateProperties(content, HealthInfo.class);
      healthInfo.setId(Id.next());
      this.save(healthInfo);
      List<Long> chronicIds = StrUtil.isNotBlank(content.getChronicIds()) ? Arrays.stream(content.getChronicIds().split(",")).map((s) -> {
         return Long.parseLong(s);
      }).collect(Collectors.toList()) : CollUtil.newArrayList(new Long[0]);
      if (CollUtil.isNotEmpty((Collection)chronicIds)) {
         this.healthInfoChronicService.insertAssociateBatch(custId, (List)chronicIds);
      }

      HealthBodyRecord healthBodyRecord = (HealthBodyRecord)LeBeanUtil.copyCreateProperties(content, HealthBodyRecord.class);
      healthBodyRecord.setId(Id.next());
      healthBodyRecord.setHealthInfoSource(HealthInfoSourceEnum.WEB.key());
      this.healthBodyRecordService.insertLatestRecord(healthBodyRecord);
   }

}
