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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.MenuDishesLabelAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesLabelEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesLabelPageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishesLabel;
import com.bonus.canteen.core.menu.entity.MenuLabelDishes;
import com.bonus.canteen.core.menu.mapper.MenuDishesLabelMapper;
import com.bonus.canteen.core.menu.mapper.MenuLabelDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesLabelService;
import com.bonus.canteen.core.menu.vo.MenuDishesLabelPageVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
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 MenuDishesLabelServiceImpl extends ServiceImpl<MenuDishesLabelMapper, MenuDishesLabel> implements MenuDishesLabelService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesLabelServiceImpl.class);
   @Autowired
   @Lazy
   private MenuLabelDishesMapper menuLabelDishesMapper;

   @Override
   public void addMenuDishesLabel(MenuDishesLabelAddDTO content) {
      List<MenuDishesLabel> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesLabel.class)
              .eq(MenuDishesLabel::getLabelName, content.getLabelName())
              .eq(MenuDishesLabel::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (!CollUtil.isEmpty(cookList)) {
         throw new ServiceException(I18n.getMessage("menu_label_name_repeat", new Object[0]));
      } else {
         String username = SecurityUtils.getUsername();
         MenuDishesLabel dishesLabel = new MenuDishesLabel();
         BeanUtil.copyProperties(content, dishesLabel, new String[0]);
         dishesLabel.setCrby(username);
         dishesLabel.setLabelId(Id.next());
         this.baseMapper.insert(dishesLabel);
      }
   }

   @Override
   public Page<MenuDishesLabelPageVO> getMenuMenuDishesLabelPage(String content) {
      Map<String, JSONObject> contentMap = JSON.parseObject(content, Map.class);
      Page<MenuDishesLabel> page = QueryParseHelper.getPageFromContent(content, MenuDishesLabel.class);
      MenuDishesLabelPageDTO dishesLabelPageDTO = (contentMap.get("object")).toJavaObject(MenuDishesLabelPageDTO.class);
      return this.baseMapper.selectAllList(page, dishesLabelPageDTO, DelFlagEnum.DEL_FALSE.key());
   }

   @Override
   public void editMenuDishesLabel(MenuDishesLabelEditDTO content) {
      if (StringUtils.isNotBlank(content.getLabelName())) {
         List<MenuDishesLabel> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesLabel.class)
                 .eq(MenuDishesLabel::getLabelName, content.getLabelName())
                 .eq(MenuDishesLabel::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .ne(MenuDishesLabel::getLabelId, content.getLabelId()));
         if (CollUtil.isNotEmpty(cookList)) {
            throw new ServiceException(I18n.getMessage("menu_label_name_repeat", new Object[0]));
         }
      }

      String username = SecurityUtils.getUsername();
      MenuDishesLabel dishesLabel = new MenuDishesLabel();
      BeanUtil.copyProperties(content, dishesLabel, new String[0]);
      dishesLabel.setUpby(username);
      this.baseMapper.update(dishesLabel, Wrappers.lambdaQuery(MenuDishesLabel.class)
              .eq(MenuDishesLabel::getLabelId, content.getLabelId()));
   }

   @Override
   public void removeByLabelId(Long labelId) {
      List<MenuLabelDishes> labelDishesList = this.menuLabelDishesMapper.selectList(Wrappers.lambdaQuery(MenuLabelDishes.class)
              .eq(MenuLabelDishes::getLabelId, labelId)
              .eq(MenuLabelDishes::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      boolean dishesUse = labelDishesList != null && !labelDishesList.isEmpty();
      if (dishesUse) {
         throw new ServiceException(I18n.getMessage("menu_label_use_del", new Object[0]));
      } else {
        this.baseMapper.update(null, Wrappers.lambdaUpdate(MenuDishesLabel.class)
                .eq(MenuDishesLabel::getLabelId, labelId)
                .set(MenuDishesLabel::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
      }
   }
}
