package com.bonus.canteen.core.menu.service.impl.sys;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.data.dataset.Executors;
import com.bonus.canteen.core.menu.dto.SysMenuMaterialPageDto;
import com.bonus.canteen.core.menu.entity.SysMenuMaterial;
import com.bonus.canteen.core.menu.mapper.SysMenuMaterialMapper;
import com.bonus.canteen.core.menu.vo.SysMenuMaterialVo;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysMenuMaterialServiceImpl extends ServiceImpl<SysMenuMaterialMapper, SysMenuMaterial> {
   private static final Logger log = LoggerFactory.getLogger(SysMenuMaterialServiceImpl.class);
//   @Autowired
//   private SysMenuMaterialMapper sysMenuMaterialMapper;
   @Autowired
   private SysMenuMaterialCategoryServiceImpl sysMenuMaterialCategoryService;
//   @Autowired
//   private SysMenuMaterialRegisterPicService menuMaterialRegisterPicService;
//   @Autowired
//   private SysMenuDishesMaterialService menuDishesMaterialService;

//   public SysMenuMaterial save(SysMenuMaterialAddDto content) {
//      this.checkParam(content.getMaterialName(), content.getCategoryId(), content.getMaterialId());
//      SysMenuMaterial sysMenuMaterial = new SysMenuMaterial();
//      sysMenuMaterial.setMaterialName(content.getMaterialName());
//      sysMenuMaterial.setCategoryId(content.getCategoryId());
//      Executors.doInSystem((s) -> {
//         if (ObjectUtil.isNotEmpty(content.getMaterialId())) {
//            this.sysMenuMaterialMapper.update(sysMenuMaterial, (Wrapper)Wrappers.lambdaUpdate().eq(SysMenuMaterial::getMaterialId, content.getMaterialId()));
//         } else {
//            sysMenuMaterial.setMaterialId(Id.next());
//            this.sysMenuMaterialMapper.insert(sysMenuMaterial);
//         }
//
//      });
//      return sysMenuMaterial;
//   }
//
//   public void checkParam(String materialName, Long categoryId, Long materialId) {
//      boolean exists = (Boolean)Executors.readInSystem(() -> {
//         return this.sysMenuMaterialMapper.exists((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery().ne(ObjectUtil.isNotEmpty(materialId), SysMenuMaterial::getMaterialId, materialId)).eq(SysMenuMaterial::getMaterialName, materialName));
//      });
//      if (exists) {
//         throw new LeException(I18n.getMessage("menu_material_exist", new Object[0]));
//      } else {
//         boolean existsCategory = (Boolean)Executors.readInSystem(() -> {
//            return this.sysMenuMaterialCategoryService.exists((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(SysMenuMaterialCategory.class).eq(SysMenuMaterialCategory::getCategoryId, categoryId)).eq(SysMenuMaterialCategory::getDelFlag, 2));
//         });
//         if (!existsCategory) {
//            throw new LeException("原料类别未查询到");
//         }
//      }
//   }

   public PageVO<SysMenuMaterialVo> queryByPage(SysMenuMaterialPageDto content) {
      PageHelper.startPage(content.getPageNum(), content.getPageSize());
      List<SysMenuMaterial> sysMenuMaterials = Executors.readInSystem(() -> {
         return this.list(Wrappers.lambdaQuery(SysMenuMaterial.class)
                 .in(ObjectUtil.isNotEmpty(content.getCategoryIdList()), SysMenuMaterial::getCategoryId, content.getCategoryIdList())
                 .like(ObjectUtil.isNotEmpty(content.getName()), SysMenuMaterial::getMaterialName, content.getName())
                 .orderByDesc(SysMenuMaterial::getUptime));
      });
      if (ObjectUtil.isEmpty(sysMenuMaterials)) {
         return PageVO.empty();
      } else {
         Set<Long> categoryIds = sysMenuMaterials.stream().map(SysMenuMaterial::getCategoryId).collect(Collectors.toSet());
         Map<Long, String> categoryIdMap = this.sysMenuMaterialCategoryService.getCategoryIdMap(categoryIds);
         List<SysMenuMaterialVo> sysMenuMaterialVos = BeanUtil.copyToList(sysMenuMaterials, SysMenuMaterialVo.class);

          for (SysMenuMaterialVo sysMenuMaterialVo : sysMenuMaterialVos) {
              sysMenuMaterialVo.setCategoryName(categoryIdMap.get(sysMenuMaterialVo.getCategoryId()));
          }

         return PageVO.of(sysMenuMaterials, sysMenuMaterialVos);
      }
   }

//   public Boolean removeByMaterialId(Long materialId) {
//      boolean exists = this.menuDishesMaterialService.exists((Wrapper)Wrappers.lambdaQuery().eq(SysMenuDishesMaterial::getMaterialId, materialId));
//      if (exists) {
//         throw new LeException("原料已被菜品引用，无法删除");
//      } else {
//         this.menuMaterialRegisterPicService.removeByMaterialId(materialId);
//         return (Boolean)Executors.doInSystem((s) -> {
//            return this.remove((Wrapper)Wrappers.lambdaQuery().eq(SysMenuMaterial::getMaterialId, materialId));
//         });
//      }
//   }
//
//   public List<SysMenuMaterialVo> syncSystemMaterial(List<Long> materialIds) {
//      return (List)(ObjectUtil.isEmpty(materialIds) ? Lists.newArrayList() : (List)Executors.readInSystem(() -> {
//         List<SysMenuMaterialVo> sysMenuMaterialVos = this.list((Wrapper)Wrappers.lambdaQuery().in(SysMenuMaterial::getMaterialId, materialIds)).stream().map((x) -> {
//            return (SysMenuMaterialVo)BeanUtil.copyProperties(x, SysMenuMaterialVo.class, new String[0]);
//         }).toList();
//         Map<Long, List<SysMenuMaterialRegisterPic>> byMaterialIdsToMap = this.menuMaterialRegisterPicService.getByMaterialIdsToMap(materialIds);
//         Iterator var4 = sysMenuMaterialVos.iterator();
//
//         while(var4.hasNext()) {
//            SysMenuMaterialVo sysMenuMaterialVo = (SysMenuMaterialVo)var4.next();
//            sysMenuMaterialVo.setPics((List)byMaterialIdsToMap.get(sysMenuMaterialVo.getMaterialId()));
//         }
//
//         return sysMenuMaterialVos;
//      }));
//   }
}
