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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.menu.entity.MenuSequence;
import com.bonus.canteen.core.menu.mapper.MenuSequenceMapper;
import com.bonus.canteen.core.menu.service.MenuSequenceService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class MenuSequenceServiceImpl extends ServiceImpl<MenuSequenceMapper, MenuSequence> implements MenuSequenceService {
   @Override
   public synchronized String getSequenceNo(int type) {
      LocalDateTime now = LocalDateTime.now();
      StringBuilder str = new StringBuilder();
      int monthValue = now.getMonthValue();
      int dayOfMonth = now.getDayOfMonth();
      int hour = now.getHour();
      int minute = now.getMinute();
      int second = now.getSecond();
      str.append(String.format("%02d", monthValue)).append(String.format("%02d", dayOfMonth)).append(String.format("%02d", hour)).append(String.format("%02d", minute)).append(String.format("%02d", second));
      MenuSequence menuSequence = this.getOne(Wrappers.lambdaQuery(MenuSequence.class)
              .eq(MenuSequence::getType, type)
              .eq(MenuSequence::getTimeIndex, str.toString()));
      if (menuSequence == null) {
         menuSequence = new MenuSequence();
         menuSequence.setSequence(1);
         menuSequence.setTimeIndex(str.toString());
         menuSequence.setType(type);
         this.save(menuSequence);
      } else {
         Integer sqe = menuSequence.getSequence() + 1;
         menuSequence.setSequence(sqe);
         this.updateById(menuSequence);
      }

      String var10000 = menuSequence.getTimeIndex();
      return var10000 + String.format("%02d", menuSequence.getSequence());
   }

}
