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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.common.page.QueryParseHelper;
import com.bonus.canteen.core.customer.utils.DelFlagEnum;
import com.bonus.canteen.core.menu.dto.MenuDishesEffectAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesEffectEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesEffectPageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishes;
import com.bonus.canteen.core.menu.entity.MenuDishesEffect;
import com.bonus.canteen.core.menu.mapper.MenuDishesEffectMapper;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesEffectService;
import com.bonus.canteen.core.menu.vo.MenuDishesEffectPageVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MenuDishesEffectServiceImpl extends ServiceImpl<MenuDishesEffectMapper, MenuDishesEffect> implements MenuDishesEffectService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesEffectServiceImpl.class);
   @Autowired
   @Lazy
   private MenuDishesMapper menuDishesMapper;

   @Override
   public void addMenuDishesEffect(MenuDishesEffectAddDTO content) {
      List<MenuDishesEffect> effectList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesEffect.class)
              .eq(MenuDishesEffect::getEffectName, content.getEffectName())
              .eq(MenuDishesEffect::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (CollUtil.isNotEmpty(effectList)) {
         throw new ServiceException(I18n.getMessage("menu_effect_name_repeat", new Object[0]));
      } else {
         MenuDishesEffect dishesEffect =  LeBeanUtil.copyCreateProperties(content, MenuDishesEffect.class);
         dishesEffect.setEffectId(Id.next());
         this.baseMapper.insert(dishesEffect);
      }
   }

   @Override
   public Page<MenuDishesEffectPageVO> getMenuDishesEffectPage(String content) {
      Map<String, JSONObject> contentMap = (Map)JSON.parseObject(content, Map.class);
      Page<MenuDishesEffectPageVO> page = QueryParseHelper.getPageFromContent(content, MenuDishesEffectPageVO.class);
      MenuDishesEffectPageDTO dishesEffectPageDTO = (contentMap.get("object")).toJavaObject(MenuDishesEffectPageDTO.class);
      LambdaQueryWrapper<MenuDishesEffect> queryWrapper = new LambdaQueryWrapper();
      queryWrapper.eq(MenuDishesEffect::getDelFlag, DelFlagEnum.DEL_FALSE.key());
      if (StringUtils.isNotBlank(dishesEffectPageDTO.getEffectName())) {
         queryWrapper.like(MenuDishesEffect::getEffectName, dishesEffectPageDTO.getEffectName());
      }

      return this.baseMapper.selectEffectPage(page, queryWrapper);
   }

   @Override
   public void editMenuDishesEffect(MenuDishesEffectEditDTO content) {
      if (StringUtils.isNotBlank(content.getEffectName())) {
         List<MenuDishesEffect> effectList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesEffect.class)
                 .eq(MenuDishesEffect::getEffectName, content.getEffectName())
                 .eq(MenuDishesEffect::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .ne(MenuDishesEffect::getEffectId, content.getEffectId()));
         if (CollUtil.isNotEmpty(effectList)) {
            throw new ServiceException(I18n.getMessage("menu_effect_name_repeat", new Object[0]));
         }
      }

      MenuDishesEffect dishesEffect = LeBeanUtil.copyCreateProperties(content, MenuDishesEffect.class);
      MenuDishesEffect effect = this.baseMapper.selectOne(Wrappers.lambdaQuery(MenuDishesEffect.class)
              .eq(MenuDishesEffect::getEffectId, content.getEffectId())
              .select(MenuDishesEffect::getId));
      if (ObjectUtil.isNotNull(effect)) {
         dishesEffect.setId(effect.getId());
         this.baseMapper.updateById(dishesEffect);
      }

   }

   @Override
   public void removeByEffectId(Long effectId) {
      List<MenuDishes> menuDishesList = this.menuDishesMapper.selectList(Wrappers.lambdaQuery(MenuDishes.class)
              .eq(MenuDishes::getEffectId, effectId)
              .eq(MenuDishes::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (CollUtil.isNotEmpty(menuDishesList)) {
         throw new ServiceException(I18n.getMessage("menu_effect_use_del", new Object[0]));
      } else {
         MenuDishesEffect effect = this.baseMapper.selectOne((Wrappers.lambdaQuery(MenuDishesEffect.class)
                 .eq(MenuDishesEffect::getEffectId, effectId))
                 .select(MenuDishesEffect::getId));
         if (ObjectUtil.isNotNull(effect)) {
            effect.setDelFlag(DelFlagEnum.DEL_TRUE.key());
            this.baseMapper.updateById(effect);
         }

      }
   }
}
