package com.ruoyi.buyerapi.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.buyerapi.domain.BuyerQuickMenu;
import com.ruoyi.buyerapi.mapper.BuyerQuickMenuMapper;
import com.ruoyi.buyerapi.service.IBuyerQuickMenuService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 快捷菜单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-06
 */
@Service
public class BuyerQuickMenuServiceImpl implements IBuyerQuickMenuService {
    @Autowired
    private BuyerQuickMenuMapper buyerQuickMenuMapper;


    /**
     * 查询快捷菜单
     *
     * @param id 快捷菜单主键
     * @return 快捷菜单
     */
    @Override
    public BuyerQuickMenu selectBuyerQuickMenuById(Long id) {
        return buyerQuickMenuMapper.selectBuyerQuickMenuById(id);
    }

    /**
     * 查询快捷菜单列表
     *
     * @param buyerQuickMenu 快捷菜单
     * @return 快捷菜单
     */
    @Override
    public List<BuyerQuickMenu> selectBuyerQuickMenuList(BuyerQuickMenu buyerQuickMenu) {
        buyerQuickMenu.setUserId(SecurityUtils.getUserId());
        return buyerQuickMenuMapper.selectBuyerQuickMenuList(buyerQuickMenu);
    }

    /**
     * 查询快捷菜单信息
     *
     * @return 快捷菜单
     */
    @Override
    public List<BuyerQuickMenu> selectBuyerQuickMenuInfo() {
        BuyerQuickMenu buyerQuickMenu=new BuyerQuickMenu();
        buyerQuickMenu.setUserId(SecurityUtils.getUserId());
        return buyerQuickMenuMapper.selectBuyerQuickMenuList(buyerQuickMenu);
    }

    /**
     * 新增快捷菜单
     *
     * @param buyerQuickMenu 快捷菜单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerQuickMenu(BuyerQuickMenu buyerQuickMenu) {
        // 先删除以前的快捷菜单
        buyerQuickMenuMapper.deleteBuyerQuickMenuByUserId(SecurityUtils.getUserId());
        // 新增快捷菜单
        buyerQuickMenu.setUserId(SecurityUtils.getUserId());
        buyerQuickMenu.setCreateTime(DateUtils.getNowDate());
        return buyerQuickMenuMapper.insertBuyerQuickMenu(buyerQuickMenu);
    }

    /**
     * 批量新增快捷菜单
     *
     * @param buyerQuickMenus 快捷菜单
     * @return 结果
     */
    @Transactional
    @Override
    public int batchInsertBuyerQuickMenu(List<BuyerQuickMenu> buyerQuickMenus) {
        // 先删除以前的快捷菜单
        buyerQuickMenuMapper.deleteBuyerQuickMenuByUserId(SecurityUtils.getUserId());
        if(null != buyerQuickMenus && buyerQuickMenus.size() > 0){
            for (BuyerQuickMenu buyerQuickMenu : buyerQuickMenus) {
                buyerQuickMenu.setUserId(SecurityUtils.getUserId());
                buyerQuickMenu.setCreateTime(DateUtils.getNowDate());
                buyerQuickMenu.setCreateBy(SecurityUtils.getUsername());
            }
            // 批量新增快捷菜单
            return buyerQuickMenuMapper.batchInsertBuyerQuickMenu(buyerQuickMenus);
        }else{
            return 1;
        }
    }

    /**
     * 修改快捷菜单
     *
     * @param buyerQuickMenu 快捷菜单
     * @return 结果
     */
    @Override
    public int updateBuyerQuickMenu(BuyerQuickMenu buyerQuickMenu) {
        buyerQuickMenu.setUpdateTime(DateUtils.getNowDate());
        return buyerQuickMenuMapper.updateBuyerQuickMenu(buyerQuickMenu);
    }

    /**
     * 批量删除快捷菜单
     *
     * @param ids 需要删除的快捷菜单主键
     * @return 结果
     */
    @Override
    public int deleteBuyerQuickMenuByIds(Long[] ids) {
        return buyerQuickMenuMapper.deleteBuyerQuickMenuByIds(ids);
    }

    /**
     * 删除快捷菜单信息
     *
     * @param id 快捷菜单主键
     * @return 结果
     */
    @Override
    public int deleteBuyerQuickMenuById(Long id) {
        return buyerQuickMenuMapper.deleteBuyerQuickMenuById(id);
    }
}
