package com.wb.permission.domain;

import com.wb.permission.pojo.WbMenuRes;
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.Map;

/**
* Description:
* Author: wangll
* Version: 1.0
* Create Date: 2020-03-16 13:53:36
*/
@Service
public class WbMenuResBO {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
    * Description: 新增和修改
    * @param wbMenuRes
    * @return
    * Note: Nothing much.
    */
    public void save(WbMenuRes wbMenuRes){
        if (StringUtils.isEmpty(wbMenuRes.getMenuResId())) {
            wbMenuRes.setMenuResId(SysUtil.getId());
            sqlSessionTemplate.insert("resources/permission.WbMenuResMapper.insertSelective",wbMenuRes);
        }else{
            sqlSessionTemplate.update("resources/permission.WbMenuResMapper.updateByPrimaryKeySelective",wbMenuRes);
        }
    }

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

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

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

    /**
     * Description: 删除通过菜单id
     * @param tenancyId
     * @param parentMenuId
     * @return
     * Note: Nothing much.
     */
    public void deleteByMenuId(String tenancyId,String parentMenuId){
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        if (!StringUtils.isEmpty(parentMenuId)){
            dataMap.put("parentMenuId",parentMenuId);
        }
        sqlSessionTemplate.delete("resources/permission.WbMenuResMapper.deleteByMenuId",dataMap);
    }
}