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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.thmz.RoleMenuRelation;
import cn.hnthyy.thmz.mapper.thmz.RoleMenuRelationMapper;
import cn.hnthyy.thmz.service.thmz.RoleMenuRelationService;
import cn.hnthyy.thmz.vo.RoleMenuRelationVo;
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.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class RoleMenuRelationServiceImpl implements RoleMenuRelationService {
    @SuppressWarnings("all")
    @Autowired
    private RoleMenuRelationMapper roleMenuRelationMapper;

    @Override
    public RoleMenuRelation queryById(Long id) {
        return roleMenuRelationMapper.selectById(id);
    }

    @Override
    public List<RoleMenuRelation> queryByRoleIds(List<Long> roleIds) {
        return roleMenuRelationMapper.selectByRoleIds(roleIds);
    }

    @Override
    public List<RoleMenuRelation> queryByMenuId(Long menuId) {
        return roleMenuRelationMapper.selectByMenuId(menuId);
    }

    @Override
    public List<RoleMenuRelation> queryByRoleId(Long roleId) {
        return roleMenuRelationMapper.selectByRoleId(roleId);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int saveMenuRelation(RoleMenuRelationVo roleMenuRelationVo, Long createId) throws MzException {
        if (null == roleMenuRelationVo.getRole().getId()) {
            throw new MzException("保存角色与菜单关系失败,角色ID参数为空");
        }
        List<String> menuIds = null;
        if (roleMenuRelationVo.getMenuIds() != null) {
            menuIds = Arrays.asList(roleMenuRelationVo.getMenuIds().split(","));
        }
        Date now = new Date();
        roleMenuRelationMapper.deleteByRoleId(roleMenuRelationVo.getRole().getId());
        for (String menuId : menuIds) {
            if (StringUtils.isBlank(menuId)) {
                continue;
            }
            Long menuIdL = Long.valueOf(menuId);
            RoleMenuRelation newRoleMenuRelation = new RoleMenuRelation();
            newRoleMenuRelation.setRoleId(roleMenuRelationVo.getRole().getId());
            newRoleMenuRelation.setMenuId(menuIdL);
            newRoleMenuRelation.setCreateTime(now);
            newRoleMenuRelation.setCreateId(createId);
            roleMenuRelationMapper.insertRoleMenuRelation(newRoleMenuRelation);
        }
        return 1;
    }

    @Override
    public int removeById(Long id) {
        return roleMenuRelationMapper.deleteById(id);
    }

    @Override
    public int removeByRoleId(Long roleId) {
        return roleMenuRelationMapper.deleteByRoleId(roleId);
    }

    @Override
    public int removeByMenuId(Long menuId) {
        return roleMenuRelationMapper.deleteByMenuId(menuId);
    }
}
