package com.wb.permission.domain;

import com.wb.permission.pojo.WbMenu;
import com.wb.util.SysUtil;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Description:
* Author: wangll
* Version: 1.0
* Create Date: 2020-03-19 13:44:46
*/
@Service
public class WbMenuBO {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
    * Description: 新增和修改
    * @param wbMenu
    * @return
    * Note: Nothing much.
    */
    public void save(WbMenu wbMenu){
        if (StringUtils.isEmpty(wbMenu.getId())) {
            wbMenu.setId(SysUtil.getId());
            wbMenu.setMenuId(wbMenu.getId());
            sqlSessionTemplate.insert("resources/permission.WbMenuMapper.insertSelective",wbMenu);
        }else{
            sqlSessionTemplate.update("resources/permission.WbMenuMapper.updateByPrimaryKeySelective",wbMenu);
        }
    }

    /**
    * Description: 新增
    * @param wbMenu
    * @return
    * Note: Nothing much.
    */
    public void insert(WbMenu wbMenu){
       sqlSessionTemplate.insert("resources/permission.WbMenuMapper.insertSelective",wbMenu);
    }

    /**
    * Description: 修改
    * @param wbMenu
    * @return
    * Note: Nothing much.
    */
    public void update(WbMenu wbMenu){
       sqlSessionTemplate.update("resources/permission.WbMenuMapper.updateByPrimaryKeySelective",wbMenu);
    }

    /**
    * Description: 删除
    * @param id
    * @return
    * Note: Nothing much.
    */
    public void delete(String id){
       sqlSessionTemplate.delete("resources/permission.WbMenuMapper.deleteByPrimaryKey",id);
    }

    /**
     * Description: 查询授权菜单
     * @param tenancyId
     * @param menuId
     * @return
     * Note: Nothing much.
     */
    public List<WbMenu> selectGrantMenu(String tenancyId, String menuId){
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        if (!StringUtils.isEmpty("menuId")){
            dataMap.put("menuId",menuId);
        }
        return sqlSessionTemplate.selectList("resources/permission.WbMenuMapper.selectGrantMenu",dataMap);
    }

    /**
     * Description: 保存菜单
     * @param wbMenu
     * @return
     * Note: Nothing much.
     */
    public void insertMenu(WbMenu wbMenu){
        sqlSessionTemplate.insert("resources/permission.WbMenuMapper.insertMenu",wbMenu);
    }

    public List<WbMenu> listWbMenu(String tenancyId,String userId,String menuType,String parentMenu){
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        if (!StringUtils.isEmpty(userId)){
            dataMap.put("userId",userId);
        }
        if (!StringUtils.isEmpty(menuType)){
            dataMap.put("menuType",menuType);
        }
        if (!StringUtils.isEmpty(parentMenu)){
            dataMap.put("parentMenu",parentMenu);
        }
        List data = sqlSessionTemplate.selectList("resources/permission.WbMenuMapper.selectWbMenu",dataMap);
        return data;
    }
}