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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.common.utils.SysUtil;
import com.bonus.canteen.core.menu.entity.MenuMaterialRegister;
import com.bonus.canteen.core.menu.entity.MenuMaterialRegisterPic;
import com.bonus.canteen.core.menu.mapper.MenuMaterialRegisterMapper;
import com.bonus.canteen.core.menu.mapper.MenuMaterialRegisterPicMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;

@Service
public class MenuMaterialRegisterPicService extends ServiceImpl<MenuMaterialRegisterPicMapper, MenuMaterialRegisterPic> {
   @Resource
   private MenuMaterialRegisterMapper menuMaterialRegisterMapper;

   public void saveImageByMaterialId(Long materialId, List<String> imageUrls) {
      if (!ObjectUtil.isEmpty(imageUrls) && !ObjectUtil.isEmpty(materialId)) {
         List<MenuMaterialRegisterPic> pics = imageUrls.stream()
                 .map(url -> new MenuMaterialRegisterPic()
                         .setMaterialId(materialId)
                         .setImageUrl(SysUtil.getCutPath(url)))
                 .collect(toList());

         this.saveBatch(pics);
      }
   }

   public void removeByMaterialId(List<Long> materialIds) {
      if (!ObjectUtil.isEmpty(materialIds)) {
         this.remove(new LambdaQueryWrapper<>(MenuMaterialRegisterPic.class).in(MenuMaterialRegisterPic::getMaterialId,
                 materialIds));
         this.menuMaterialRegisterMapper.update(null,Wrappers.lambdaUpdate(MenuMaterialRegister.class)
                 .in(MenuMaterialRegister::getMaterialId, materialIds)
                 .set(MenuMaterialRegister::getIfSuccess, 0));
      }
   }

   public List<MenuMaterialRegisterPic> getByMaterialId(Long materialId) {
      return this.getByMaterialIdsToMap(Collections.singletonList(materialId)).get(materialId);
   }

   public List<MenuMaterialRegisterPic> getByMaterialIds(List<Long> materialIds) {
      return ObjectUtil.isEmpty(materialIds) ? null :
              this.list(new LambdaQueryWrapper<>(MenuMaterialRegisterPic.class).in(MenuMaterialRegisterPic::getMaterialId,
                      materialIds));
   }

   public Map<Long, List<MenuMaterialRegisterPic>> getByMaterialIdsToMap(List<Long> materialIds) {
      return this.getByMaterialIds(materialIds).stream().collect(Collectors.groupingBy(MenuMaterialRegisterPic::getMaterialId));
   }

   public void removePicById(Long id) {
      MenuMaterialRegisterPic menuMaterialRegisterPic = this.getById(id);
      if (!ObjectUtil.isEmpty(menuMaterialRegisterPic)) {
         this.removeById(id);
         this.menuMaterialRegisterMapper.updateMaterialRegisterStatus(menuMaterialRegisterPic.getMaterialId(), 0);
      }
   }
}
