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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.nutrition.common.dto.HealthChronicDto;
import com.bonus.canteen.core.nutrition.common.mapper.HealthChronicMapper;
import com.bonus.canteen.core.nutrition.common.model.HealthChronic;
import com.bonus.canteen.core.nutrition.common.service.HealthChronicService;
import com.bonus.canteen.core.nutrition.common.vo.HealthChronicVo;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.utils.LeBeanUtil;
import org.apache.commons.collections4.CollectionUtils;
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 java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;

@Service
public class HealthChronicServiceImpl extends ServiceImpl<HealthChronicMapper, HealthChronic> implements HealthChronicService {
//   @Autowired
//   @Lazy
//   private HealthChronicDishesService healthChronicDishesService;

//   public PageVO<HealthChronicWithDetailVo> pageHealthChronic(Page<HealthChronic> page, HealthChronicDto content) {
//      String chronicName = content.getChronicName();
//      LambdaQueryWrapper<HealthChronic> queryWrapper = (LambdaQueryWrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(HealthChronic.class).eq(StrUtil.isNotBlank(chronicName), HealthChronic::getChronicName, chronicName)).eq(HealthChronic::getIfDel, DelFlagEnum.DEL_FALSE.key())).orderByDesc(HealthChronic::getCrtime);
//      page = (Page)((HealthChronicMapper)this.baseMapper).selectPage(page, queryWrapper);
//      List<HealthChronic> records = page.getRecords();
//      List<HealthChronicWithDetailVo> healthChronicWithDetailVos = CollUtil.newArrayList(new HealthChronicWithDetailVo[0]);
//      Iterator var7 = records.iterator();
//
//      while(var7.hasNext()) {
//         HealthChronic record = (HealthChronic)var7.next();
//         Long chronicId = record.getChronicId();
//         List<HealthChronicDishesVo> forbidDishesList = CollUtil.newArrayList(new HealthChronicDishesVo[0]);
//         List<HealthChronicDishesVo> recomDishesList = CollUtil.newArrayList(new HealthChronicDishesVo[0]);
//         List<HealthChronicDishes> dishes = this.healthChronicDishesService.listDishesByChronicId(chronicId);
//         Iterator var13 = dishes.iterator();
//
//         while(var13.hasNext()) {
//            HealthChronicDishes dish = (HealthChronicDishes)var13.next();
//            HealthChronicDishesVo healthChronicDishesVo;
//            if (RecomTypeEnum.RECOM.getKey().equals(dish.getRecomType())) {
//               healthChronicDishesVo = (HealthChronicDishesVo)LeBeanUtil.copyCreateProperties(dish, HealthChronicDishesVo.class);
//               recomDishesList.add(healthChronicDishesVo);
//            } else {
//               healthChronicDishesVo = (HealthChronicDishesVo)LeBeanUtil.copyCreateProperties(dish, HealthChronicDishesVo.class);
//               forbidDishesList.add(healthChronicDishesVo);
//            }
//         }
//
//         HealthChronicWithDetailVo chronicWithDetailVo = HealthChronicWithDetailVo.builder().chronicName(record.getChronicName()).chronicId(record.getChronicId()).forbidDishesList(forbidDishesList).recomDishesList(recomDishesList).build();
//         healthChronicWithDetailVos.add(chronicWithDetailVo);
//      }
//
//      return PageVO.of((Page)page, healthChronicWithDetailVos);
//   }

   public List<HealthChronicVo> dictHealthChronic(HealthChronicDto content) {
      LambdaQueryWrapper<HealthChronic> queryWrapper = Wrappers.lambdaQuery(HealthChronic.class)
              .eq(StrUtil.isNotBlank(content.getChronicName()), HealthChronic::getChronicName, content.getChronicName())
              .eq(HealthChronic::getIfDel, DelFlagEnum.DEL_FALSE.key())
              .orderByDesc(HealthChronic::getCrtime);
      List<HealthChronic> list = this.list(queryWrapper);
      return LeBeanUtil.copyCreateList(list, HealthChronicVo.class);
   }

//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void addHealthChronic(HealthChronicWithDishesDto chronicWithDishesDto) {
//      String chronicName = chronicWithDishesDto.getChronicName();
//      this.checkChronicName(chronicName, (Long)null);
//      HealthChronic healthChronic = (HealthChronic)LeBeanUtil.copyCreateProperties(chronicWithDishesDto, HealthChronic.class);
//      long chronicId = Id.next();
//      healthChronic.setId(Id.next());
//      healthChronic.setChronicId(chronicId);
//      healthChronic.setIfDel(DelFlagEnum.DEL_FALSE.key());
//      this.save(healthChronic);
//      this.addChronicDishesList(chronicWithDishesDto, chronicId);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void updateHealthChronic(HealthChronicWithDishesDto chronicWithDishesDto) {
//      String chronicName = chronicWithDishesDto.getChronicName();
//      Long chronicId = chronicWithDishesDto.getChronicId();
//      this.checkChronicName(chronicName, chronicId);
//      HealthChronic healthChronic = (HealthChronic)LeBeanUtil.copyCreateProperties(chronicWithDishesDto, HealthChronic.class);
//      this.update(healthChronic, (Wrapper)Wrappers.lambdaQuery(HealthChronic.class).eq(HealthChronic::getChronicId, chronicId));
//      this.healthChronicDishesService.remove((Wrapper)Wrappers.lambdaQuery(HealthChronicDishes.class).eq(HealthChronicDishes::getChronicId, chronicId));
//      this.addChronicDishesList(chronicWithDishesDto, chronicId);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void deleteHealthChronic(Long chronicId) {
//      LambdaUpdateWrapper<HealthChronic> updateWrapper = (LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(HealthChronic.class).set(HealthChronic::getIfDel, DelFlagEnum.DEL_TRUE.key())).eq(HealthChronic::getChronicId, chronicId);
//      this.update(updateWrapper);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void addChronicDishesList(HealthChronicWithDishesDto chronicWithDishesDto, long chronicId) {
//      List<HealthChronicDishes> forbidDishesList = chronicWithDishesDto.getForbidDishesList();
//      if (CollectionUtils.isNotEmpty(forbidDishesList)) {
//         Iterator var5 = forbidDishesList.iterator();
//
//         while(var5.hasNext()) {
//            HealthChronicDishes healthChronicDishes = (HealthChronicDishes)var5.next();
//            healthChronicDishes.setId(Id.next());
//            healthChronicDishes.setChronicId(chronicId);
//            healthChronicDishes.setRecomType(RecomTypeEnum.FORBID.getKey());
//         }
//
//         this.healthChronicDishesService.saveBatch(forbidDishesList);
//      }
//
//      List<HealthChronicDishes> recomDishesList = chronicWithDishesDto.getRecomDishesList();
//      if (CollectionUtils.isNotEmpty(recomDishesList)) {
//         Iterator var9 = recomDishesList.iterator();
//
//         while(var9.hasNext()) {
//            HealthChronicDishes healthChronicDishes = (HealthChronicDishes)var9.next();
//            healthChronicDishes.setId(Id.next());
//            healthChronicDishes.setChronicId(chronicId);
//            healthChronicDishes.setRecomType(RecomTypeEnum.RECOM.getKey());
//         }
//
//         this.healthChronicDishesService.saveBatch(recomDishesList);
//      }
//
//   }
//
//   private void checkChronicName(String chronicName, Long chronicId) {
//      LambdaQueryWrapper<HealthChronic> wrapper = (LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(HealthChronic.class).eq(HealthChronic::getChronicName, chronicName)).eq(HealthChronic::getIfDel, DelFlagEnum.DEL_FALSE.key());
//      if (ObjectUtil.isNotNull(chronicId)) {
//         wrapper.ne(HealthChronic::getChronicId, chronicId);
//      }
//
//      Long count = this.count(wrapper);
//      if (count > 0L) {
//         throw new LeException(I18n.getMessage("nutrition.chronic.chronicName.exists", new Object[0]));
//      }
//   }

   public String selectNameById(Long chronicId) {
      return ((HealthChronicMapper)this.baseMapper).selectNameById(chronicId);
   }

//   public List<HealthChronic> getAllChronic() {
//      LambdaQueryWrapper<HealthChronic> queryWrapper = (LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(HealthChronic.class).eq(HealthChronic::getIfDel, LeConstants.COMMON_NO)).orderByDesc(HealthChronic::getId);
//      return ((HealthChronicMapper)this.baseMapper).selectList(queryWrapper);
//   }

}
