package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.thmz.Menu;
import cn.hnthyy.thmz.mapper.thmz.MenuMapper;
import cn.hnthyy.thmz.mapper.thmz.UserMenuRelationMapper;
import cn.hnthyy.thmz.service.thmz.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class MenuServiceImpl  implements MenuService {
    @SuppressWarnings("all")
    @Autowired
    private MenuMapper menuMapper;
    @SuppressWarnings("all")
    @Autowired
    private UserMenuRelationMapper userMenuRelationMapper;

    @Override
    public List<Menu> queryAllParentMenu() {
        return menuMapper.selectAllParentMenu();
    }

    @Override
    public List<Menu> queryAllMenu(String name) {
        if (StringUtils.isBlank(name)) {
            name=null;
        }else {
            name="%"+name+"%";
        }
        return menuMapper.selectAllMenu(name);
    }

    @Override
    public Menu queryById(Long id) {
        return menuMapper.selectById(id);
    }

    @Override
    public List<Menu> queryByParentId(Long parentId) {
        return menuMapper.selectByParentId(parentId);
    }

    @Override
    public List<Menu> queryByIds(List<Long> ids) {
        return menuMapper.selectByIds(ids);
    }

    @Override
    public int saveMenu(Menu menu) {
        if(menu.getId()==null){
            menu.setCreateTime(new Date());
            return menuMapper.insertMenu(menu);
        }
        return menuMapper.updateMenu(menu);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int removeById(Long id) {
        userMenuRelationMapper.deleteByMenuId(id);
        return menuMapper.deleteById(id);
    }

    @Override
    public int modifyMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }
}
