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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.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.MenuDishesCookAddDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesCookEditDTO;
import com.bonus.canteen.core.menu.dto.MenuDishesCookPageDTO;
import com.bonus.canteen.core.menu.entity.MenuDishes;
import com.bonus.canteen.core.menu.entity.MenuDishesCook;
import com.bonus.canteen.core.menu.mapper.MenuDishesCookMapper;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.menu.service.MenuDishesCookService;
import com.bonus.canteen.core.menu.vo.MenuDishesCookPageVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
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 MenuDishesCookServiceImpl extends ServiceImpl<MenuDishesCookMapper, MenuDishesCook> implements MenuDishesCookService {
    private static final Logger log = LoggerFactory.getLogger(MenuDishesCookServiceImpl.class);
    @Autowired
    @Lazy
    private MenuDishesMapper menuDishesMapper;

    @Override
    public void addMenuDishesCook(MenuDishesCookAddDTO content) {
        List<MenuDishesCook> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesCook.class)
                .eq(MenuDishesCook::getCookName, content.getCookName())
                .eq(MenuDishesCook::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
        if (CollUtil.isNotEmpty(cookList)) {
            throw new ServiceException(I18n.getMessage("menu_cook_name_repeat", new Object[0]));
        } else {
            String username = SecurityUtils.getUsername();
            MenuDishesCook dishesCook = new MenuDishesCook();
            BeanUtil.copyProperties(content, dishesCook, new String[0]);
            dishesCook.setCrby(username);
            dishesCook.setCookId(Id.next());
            this.baseMapper.insert(dishesCook);
        }
    }

    @Override
    public Page<MenuDishesCookPageVO> getMenuDishesCookPage(String content) {
        Map<String, JSONObject> contentMap = JSON.parseObject(content, Map.class);
        Page<MenuDishesCook> page = QueryParseHelper.getPageFromContent(content, MenuDishesCook.class);
        MenuDishesCookPageDTO dishesCookPageDTO = (contentMap.get("object")).toJavaObject(MenuDishesCookPageDTO.class);
        return this.baseMapper.selectAllList(page, dishesCookPageDTO, DelFlagEnum.DEL_FALSE.key());
    }

    @Override
    public void editMenuDishesCook(MenuDishesCookEditDTO content) {
        if (StringUtils.isNotBlank(content.getCookName())) {
            List<MenuDishesCook> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesCook.class)
                    .eq(MenuDishesCook::getCookName, content.getCookName())
                    .eq(MenuDishesCook::getDelFlag, DelFlagEnum.DEL_FALSE.key())
                    .ne(MenuDishesCook::getCookId, content.getCookId()));
            if (!CollUtil.isEmpty(cookList)) {
                throw new ServiceException(I18n.getMessage("menu_cook_name_repeat", new Object[0]));
            }
        }

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

    @Override
    public void removeByMaterialId(Long cookId) {
        List<MenuDishes> menuDishesList = this.menuDishesMapper.selectList(Wrappers.lambdaQuery(MenuDishes.class)
                .eq(MenuDishes::getCookId, cookId)
                .eq(MenuDishes::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
        if (menuDishesList != null && !menuDishesList.isEmpty()) {
            throw new ServiceException(I18n.getMessage("menu_cook_use_del", new Object[0]));
        } else {
            this.baseMapper.update( null,(new LambdaUpdateWrapper<>(MenuDishesCook.class)
                    .eq(MenuDishesCook::getCookId, cookId)
                    .set(MenuDishesCook::getDelFlag, DelFlagEnum.DEL_TRUE.key())));
        }
    }


}
