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.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.MenuDishesTasteAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesTasteEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesTastePageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishesTaste;
import com.bonus.canteen.core.menu.entity.MenuTasteDishes;
import com.bonus.canteen.core.menu.mapper.MenuDishesTasteMapper;
import com.bonus.canteen.core.menu.mapper.MenuTasteDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesTasteService;
import com.bonus.canteen.core.menu.vo.MenuDishesTastePageVO;
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 MenuDishesTasteServiceImpl extends ServiceImpl<MenuDishesTasteMapper, MenuDishesTaste> implements MenuDishesTasteService {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesTasteServiceImpl.class);
   @Autowired
   @Lazy
   private MenuTasteDishesMapper menuTasteDishesMapper;

   @Override
   public void addMenuDishesTaste(MenuDishesTasteAddDTO content) {
      List<MenuDishesTaste> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesTaste.class)
              .eq(MenuDishesTaste::getTasteName, content.getTasteName())
              .eq(MenuDishesTaste::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      if (!CollUtil.isEmpty(cookList)) {
         throw new ServiceException(I18n.getMessage("menu_taste_name_repeat", new Object[0]));
      } else {
         String username = SecurityUtils.getUsername();
         MenuDishesTaste dishesTaste = new MenuDishesTaste();
         BeanUtil.copyProperties(content, dishesTaste, new String[0]);
         dishesTaste.setCrby(username);
         dishesTaste.setTasteId(Id.next());
         this.baseMapper.insert(dishesTaste);
      }
   }

   @Override
   public Page<MenuDishesTastePageVO> getMenuDishesTastePage(String content) {
      Map<String, JSONObject> contentMap = (Map) JSON.parseObject(content, Map.class);
      Page<MenuDishesTaste> page = QueryParseHelper.getPageFromContent(content, MenuDishesTaste.class);
      MenuDishesTastePageDTO dishesTastePageDTO = (contentMap.get("object")).toJavaObject(MenuDishesTastePageDTO.class);
      return this.baseMapper.selectAllList(page, dishesTastePageDTO, DelFlagEnum.DEL_FALSE.key());
   }

   @Override
   public void editMenuDishesTaste(MenuDishesTasteEditDTO content) {
      if (StringUtils.isNotBlank(content.getTasteName())) {
         List<MenuDishesTaste> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesTaste.class)
                 .eq(MenuDishesTaste::getTasteName, content.getTasteName())
                 .eq(MenuDishesTaste::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                 .ne(MenuDishesTaste::getTasteId, content.getTasteId()));
         if (!CollUtil.isEmpty(cookList)) {
            throw new ServiceException(I18n.getMessage("menu_taste_name_repeat", new Object[0]));
         }
      }

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

   @Override
   public void removeByTasteId(Long tasteId) {
      List<MenuTasteDishes> tasteDishesList = this.menuTasteDishesMapper.selectList(Wrappers.lambdaQuery(MenuTasteDishes.class)
              .eq(MenuTasteDishes::getTasteId, tasteId)
              .eq(MenuTasteDishes::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
      boolean dishesUse = tasteDishesList != null && !tasteDishesList.isEmpty();
      if (dishesUse) {
         throw new ServiceException(I18n.getMessage("menu_taste_use_del", new Object[0]));
      } else {
         this.baseMapper.update(null, (new LambdaUpdateWrapper<>(MenuDishesTaste.class)
                 .eq(MenuDishesTaste::getTasteId, tasteId))
                 .set(MenuDishesTaste::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
      }
   }
}