package com.duanledexianxianxian.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.duanledexianxianxian.demo.common.util.BeanUtils;
import com.duanledexianxianxian.demo.domain.dto.MenuDTO;
import com.duanledexianxianxian.demo.domain.entity.Menu;
import com.duanledexianxianxian.demo.dao.MenuDao;
import com.duanledexianxianxian.demo.domain.model.query.MenuForm;
import com.duanledexianxianxian.demo.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单服务实现类
 *
 * @author duanledexianxianxian
 * @since 2020 -03-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements IMenuService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addMenu(MenuForm form) {
        // 做插入前检查
        Menu menu = BeanUtils.copyObject(form, Menu.class);
        this.baseMapper.insert(menu);
        return menu.getMenuId();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean editMenu(MenuForm form) {
        // 做编辑前检查
        Menu menu = BeanUtils.copyObject(form, Menu.class);
        int cnt = this.baseMapper.updateById(menu);
        return SqlHelper.retBool(cnt);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteMenu(Long menuId) {
        // 做删除前检查
        return SqlHelper.retBool(this.baseMapper.deleteById(menuId));
    }

    @Override
    public List<MenuDTO> getUserMenuList(Long userId) {
        return BeanUtils.copyList(this.baseMapper.selectList(new LambdaQueryWrapper<Menu>()
                .eq(Menu::getUserId, userId)), MenuDTO.class);
    }
}
