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

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.common.page.QueryParseHelper;
import com.bonus.canteen.core.customer.utils.DelFlagEnum;
import com.bonus.canteen.core.menu.dto.MenuDishesSuitAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesSuitEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesSuitPageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishesSuit;
import com.bonus.canteen.core.menu.entity.MenuSuitDishes;
import com.bonus.canteen.core.menu.mapper.MenuDishesSuitMapper;
import com.bonus.canteen.core.menu.mapper.MenuSuitDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesSuitService;
import com.bonus.canteen.core.menu.vo.MenuDishesSuitPageVO;
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 MenuDishesSuitServiceImpl extends ServiceImpl<MenuDishesSuitMapper, MenuDishesSuit> implements MenuDishesSuitService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesSuitServiceImpl.class);
   @Autowired
   @Lazy
   private MenuSuitDishesMapper menuSuitDishesMapper;

   @Override
   public void addMenuDishesSuit(MenuDishesSuitAddDTO content) {
      List<MenuDishesSuit> suitList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesSuit.class)
              .eq(MenuDishesSuit::getSuitName, content.getSuitName())
              .eq(MenuDishesSuit::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (CollUtil.isNotEmpty(suitList)) {
         throw new ServiceException(I18n.getMessage("menu_suit_name_repeat", new Object[0]));
      } else {
         MenuDishesSuit dishesSuit = (MenuDishesSuit) LeBeanUtil.copyCreateProperties(content, MenuDishesSuit.class);
         dishesSuit.setSuitId(Id.next());
         this.baseMapper.insert(dishesSuit);
      }
   }

   @Override
   public Page<MenuDishesSuitPageVO> getMenuDishesSuitPage(String content) {
      Map<String, JSONObject> contentMap = (Map)JSON.parseObject(content, Map.class);
      Page<MenuDishesSuitPageVO> page = QueryParseHelper.getPageFromContent(content, MenuDishesSuitPageVO.class);
      MenuDishesSuitPageDTO dishesSuitPageDTO = (contentMap.get("object")).toJavaObject(MenuDishesSuitPageDTO.class);
      LambdaQueryWrapper<MenuDishesSuit> queryWrapper = new LambdaQueryWrapper();
      queryWrapper.eq(MenuDishesSuit::getDelFlag, DelFlagEnum.DEL_FALSE.key());
      if (StringUtils.isNotBlank(dishesSuitPageDTO.getSuitName())) {
         queryWrapper.like(MenuDishesSuit::getSuitName, dishesSuitPageDTO.getSuitName());
      }

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

   @Override
   public void editMenuDishesSuit(MenuDishesSuitEditDTO content) {
      if (StringUtils.isNotBlank(content.getSuitName())) {
         List<MenuDishesSuit> suitList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesSuit.class)
                 .eq(MenuDishesSuit::getSuitName, content.getSuitName())
                 .eq(MenuDishesSuit::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .ne(MenuDishesSuit::getSuitId, content.getSuitId()));
         if (CollUtil.isNotEmpty(suitList)) {
            throw new ServiceException(I18n.getMessage("menu_suit_name_repeat", new Object[0]));
         }
      }

      MenuDishesSuit dishesSuit = LeBeanUtil.copyCreateProperties(content, MenuDishesSuit.class);
      this.baseMapper.update(dishesSuit, Wrappers.lambdaQuery(MenuDishesSuit.class)
              .eq(MenuDishesSuit::getSuitId, content.getSuitId()));
   }

   @Override
   public void removeBySuitId(Long suitId) {
      List<MenuSuitDishes> menuSuitDishesList = this.menuSuitDishesMapper.selectList(Wrappers.lambdaQuery(MenuSuitDishes.class)
              .eq(MenuSuitDishes::getSuitId, suitId)
              .eq(MenuSuitDishes::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (CollUtil.isNotEmpty(menuSuitDishesList)) {
         throw new ServiceException(I18n.getMessage("menu_suit_use_del", new Object[0]));
      } else {
         this.baseMapper.update(null, new LambdaUpdateWrapper<>(MenuDishesSuit.class)
                 .eq(MenuDishesSuit::getSuitId, suitId)
                 .set(MenuDishesSuit::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
      }
   }
}
