package com.hfkj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.SecMenuMapper;
import com.hfkj.dao.SecPermissionMapper;
import com.hfkj.dao.SecRolePermissionRelMapper;
import com.hfkj.entity.*;
import com.hfkj.service.SecMenuService;
import com.hfkj.service.SecPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("secMenuService")
public class SecMenuServiceImpl implements SecMenuService {

	@Resource
	private SecMenuMapper secMenuMapper;

	@Resource
	private SecPermissionMapper secPermissionMapper;

	@Resource
	private SecPermissionService secPermissionService;

	@Resource
	private SecRolePermissionRelMapper secRolePermissionRelMapper;


	@Override
	public PageInfo<SecMenu> findPage(String menuName, Integer pageNum, Integer pageSize) {
		SecMenuExample example = new SecMenuExample();
		SecMenuExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(menuName)) {
			criteria.andMenuNameEqualTo(menuName);
		}
		example.setOrderByClause("menu_p_sid,menu_type,menu_sort");
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<SecMenu>(secMenuMapper.selectByExample(example));
	}

	@Override
	@Transactional
	public void addMenu(SecMenu secMenu, SecPermission permission) {
		secMenuMapper.insert(secMenu);
		permission.setMenuId(secMenu.getId());
		secPermissionMapper.insert(permission);
	}

	@Override
	public SecMenu findById(Long menuId) {
		return secMenuMapper.selectByPrimaryKey(menuId);
	}

	@Override
	@Transactional
	public void editMenu(SecMenu secMenu, SecPermission permission) {
		secMenuMapper.updateByPrimaryKey(secMenu);
		secPermissionMapper.updateByPrimaryKey(permission);
	}

	@Override
	public List<SecMenu> findAll() {
		SecMenuExample example = new SecMenuExample();
		SecMenuExample.Criteria criteria = example.createCriteria();
		criteria.andIdIsNotNull();
		criteria.andMenuTypeEqualTo(0);
		example.setOrderByClause("menu_p_sid,menu_type,menu_sort");
		return secMenuMapper.selectByExample(example);
	}

	@Override
	@Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
	public void deleteMenuList(String menuIds) {
		String[] menuIdArray = menuIds.split(",");

		for (String menuId : menuIdArray){
			List<SecMenu> menuList = this.findSonMenu(Long.parseLong(menuId));
			if (menuList != null && menuList.size() != 0) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_HAS_SON_ERROR, "");
			}
			this.deleteMenu(Long.parseLong(menuId));
		}
	}

	@Override
	public List<SecMenu> findSonMenu(Long menuId) {
		SecMenuExample example = new SecMenuExample();
		SecMenuExample.Criteria criteria = example.createCriteria();
		criteria.andMenuPSidEqualTo(menuId);
		List<SecMenu> secMenuList = secMenuMapper.selectByExample(example);
		return secMenuList;
	}

	@Override
	@Transactional
	public void deleteMenu(Long menuId) {
		//删除菜单
		secMenuMapper.deleteByPrimaryKey(menuId);

		// 查询菜单对应权限
		SecPermission permission = secPermissionService.findByMenuId(menuId);
		secPermissionMapper.deleteByPrimaryKey(permission.getId());

		// 查找到角色权限关系
		SecRolePermissionRelExample relExample = new SecRolePermissionRelExample();
		relExample.createCriteria().andPermissionIdEqualTo(permission.getId());
		List<SecRolePermissionRel> rels = secRolePermissionRelMapper.selectByExample(relExample);
		if (rels != null && rels.size() != 0) {
			for (SecRolePermissionRel rel : rels) {
				secRolePermissionRelMapper.deleteByPrimaryKey(rel.getId());
			}
		}
	}


}
