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.MenuDishesStyleAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesStyleEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesStylePageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishes;
import com.bonus.canteen.core.menu.entity.MenuDishesStyle;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.menu.mapper.MenuDishesStyleMapper;
import com.bonus.canteen.core.menu.service.MenuDishesStyleService;
import com.bonus.canteen.core.menu.vo.MenuDishesStylePageVO;
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 MenuDishesStyleServiceImpl extends ServiceImpl<MenuDishesStyleMapper, MenuDishesStyle> implements MenuDishesStyleService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesStyleServiceImpl.class);
   @Autowired
   @Lazy
   private MenuDishesMapper menuDishesMapper;

   @Override
   public void addMenuDishesStyle(MenuDishesStyleAddDTO content) {
      List<MenuDishesStyle> styleList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesStyle.class)
              .eq(MenuDishesStyle::getStyleName, content.getStyleName())
              .eq(MenuDishesStyle::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (CollUtil.isNotEmpty(styleList)) {
         throw new ServiceException(I18n.getMessage("menu_style_name_repeat", new Object[0]));
      } else {
         MenuDishesStyle dishesStyle = LeBeanUtil.copyCreateProperties(content, MenuDishesStyle.class);
         dishesStyle.setStyleId(Id.next());
         this.baseMapper.insert(dishesStyle);
      }
   }

   @Override
   public Page<MenuDishesStylePageVO> getMenuDishesStylePage(String content) {
      Map<String, JSONObject> contentMap = JSON.parseObject(content, Map.class);
      Page<MenuDishesStylePageVO> page = QueryParseHelper.getPageFromContent(content, MenuDishesStylePageVO.class);
      MenuDishesStylePageDTO dishesStylePageDTO = (contentMap.get("object")).toJavaObject(MenuDishesStylePageDTO.class);
      LambdaQueryWrapper<MenuDishesStyle> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(MenuDishesStyle::getDelFlag, DelFlagEnum.DEL_FALSE.key());
      if (StringUtils.isNotBlank(dishesStylePageDTO.getStyleName())) {
         queryWrapper.like(MenuDishesStyle::getStyleName, dishesStylePageDTO.getStyleName());
      }

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

   @Override
   public void editMenuDishesStyle(MenuDishesStyleEditDTO content) {
      if (StringUtils.isNotBlank(content.getStyleName())) {
         List<MenuDishesStyle> styleList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesStyle.class)
                 .eq(MenuDishesStyle::getStyleName, content.getStyleName())
                 .eq(MenuDishesStyle::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .ne(MenuDishesStyle::getStyleId, content.getStyleId()));
         if (CollUtil.isNotEmpty(styleList)) {
            throw new ServiceException(I18n.getMessage("menu_style_name_repeat", new Object[0]));
         }
      }

      MenuDishesStyle dishesStyle = LeBeanUtil.copyCreateProperties(content, MenuDishesStyle.class);
      MenuDishesStyle style = this.baseMapper.selectOne(Wrappers.lambdaQuery(MenuDishesStyle.class)
              .eq(MenuDishesStyle::getStyleId, content.getStyleId())
              .select(MenuDishesStyle::getId));
      if (ObjectUtil.isNotNull(style)) {
         dishesStyle.setId(style.getId());
         this.baseMapper.updateById(dishesStyle);
      }

   }

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

      }
   }
}
