package com.zcloud.service.system.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Role;
import com.zcloud.mapper.dsno2.system.RoleMapper;
import com.zcloud.service.system.RoleService;

/**
 * 说明：角色服务接口实现类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	/**通过角色ID获取数据
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData findById(PageData pd) throws Exception {
		return roleMapper.findById(pd);
	}

	/**通过id查找(返回角色对象)
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public Role getRoleById(String ROLE_ID) throws Exception{
		return roleMapper.getRoleById(ROLE_ID);
	}

	/**通过角色编码查找
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData getRoleByRnumber(PageData pd) throws Exception{
		return roleMapper.getRoleByRnumber(pd);
	}

	/**通过组ID获取组下级角色列表
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public List<Role> listAllRolesByPId(PageData pd) throws Exception {
		return roleMapper.listAllRolesByPId(pd);
	}
	public List<Role> listAllRolesById(PageData pd) throws Exception {
		return roleMapper.listAllRolesById(pd);
	}
	public PageData findByName(String name, String PARENT_ID) throws Exception {
		PageData pageData = new PageData();
		pageData.put("NAME", name);
		pageData.put("PARENT_ID", PARENT_ID);
		return roleMapper.getPdByParIdAndName(pageData);
	}
	/**添加
	 * @param pd
	 * @throws Exception
	 */
	public void add(PageData pd) throws Exception{
		roleMapper.add( pd);
	}

	/**保存修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd) throws Exception{
		roleMapper.edit( pd);
	}

	/**删除角色
	 * @param ROLE_ID
	 * @throws Exception
	 */
	public void deleteRoleById(String ROLE_ID) throws Exception{
		roleMapper.deleteRoleById(ROLE_ID);
	}

	/**给当前角色附加菜单权限
	 * @param role
	 * @throws Exception
	 */
	public void updateRoleRights(Role role) throws Exception{
		roleMapper.updateRoleRights(role);
	}

	/**给全部子角色加菜单权限
	 * @param pd
	 * @throws Exception
	 */
	public void setAllRights(PageData pd) throws Exception{
		roleMapper.setAllRights(pd);
	}

	/**权限(增删改查)
	 * @param msg 区分增删改查
	 * @param pd
	 * @throws Exception
	 */
	public void saveB4Button(String msg,PageData pd) throws Exception{
		if("add_qx".equals(msg)){
			roleMapper.addQx(pd);
		}else if("del_qx".equals(msg)){
			roleMapper.delQx(pd);
		}else if("edit_qx".equals(msg)){
			roleMapper.editQx(pd);
		}else if("cha_qx".equals(msg)){
			roleMapper.chaQx(pd);
		}
	}

	/**通过角色ID数组获取角色列表
	 * @param arryROLE_ID
	 * @throws Exception
	 */
	public List<Role> getRoleByArryROLE_ID(String[] arryROLE_ID)throws Exception{
		return roleMapper.listAllRolesByArryROLE_ID(arryROLE_ID);
	}

	/**角色列表(弹窗选择用)
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public List<PageData> roleListWindow(Page page)throws Exception{
		return roleMapper.roleWindowlistPage(page);
	}

	/**
	 * 查询角色分组list
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getGroupList(PageData pd)throws Exception{
		return roleMapper.getGroupList(pd);
	}

	/**查询分组内的角色
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> getRoleListByPid(PageData pd)throws Exception{
		return roleMapper.getRoleListByPid(pd);
	}

	/**通过上级ID和是否主角色查找
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData getRoleByPidAndMain(PageData pd) throws Exception{
		return roleMapper.getRoleByPidAndMain(pd);
	}

	/**
	 * 代理机构管理-查询角色组
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> getGroupListForAgency(PageData pd) throws Exception {
		List<PageData> list = roleMapper.getGroupListForAgency(pd);
		for (PageData pageData : list) {
			List<PageData> MemberGearData = new ArrayList<>();
			PageData data = new PageData();
			PageData firstGear = new PageData();
			firstGear.put("GEAR_BUY_COUNT",0);
			firstGear.put("GEAR_REMAIN_COUNT",0);
			firstGear.put("GEAR_NUMBER",50);
			firstGear.put("GEAR_LEVEL","1");
			data.put("firstGear", firstGear);
			PageData secondGear = new PageData();
			secondGear.put("GEAR_BUY_COUNT",0);
			secondGear.put("GEAR_REMAIN_COUNT",0);
			secondGear.put("GEAR_NUMBER",150);
			secondGear.put("GEAR_LEVEL","2");
			data.put("secondGear", secondGear);
			PageData thirdGear = new PageData();
			thirdGear.put("GEAR_BUY_COUNT",0);
			thirdGear.put("GEAR_REMAIN_COUNT",0);
			thirdGear.put("GEAR_NUMBER",300);
			thirdGear.put("GEAR_LEVEL","3");
			data.put("thirdGear", thirdGear);
			PageData fourthGear = new PageData();
			fourthGear.put("GEAR_BUY_COUNT",0);
			fourthGear.put("GEAR_REMAIN_COUNT",0);
			fourthGear.put("GEAR_NUMBER",800);
			fourthGear.put("GEAR_LEVEL","4");
			data.put("fourthGear", fourthGear);
			PageData fifthGear = new PageData();
			fifthGear.put("GEAR_BUY_COUNT",0);
			fifthGear.put("GEAR_REMAIN_COUNT",0);
			fifthGear.put("GEAR_NUMBER",2000);
			fifthGear.put("GEAR_LEVEL","5");
			data.put("fifthGear", fifthGear);
			MemberGearData.add(data);
			pageData.put("MemberGearData",MemberGearData);
		}
		return list;
	}

	/**
	 * 获取市场人员分组角色
	 * @param pd
	 * @return
	 */
	@Override
	public List<PageData> getMarketingGroupList(PageData pd) {
		return roleMapper.getMarketingGroupList(pd);
	}

	@Override
	public void updateRoleNavs(PageData pd)throws Exception{
		roleMapper.updateRoleNavs(pd);
	}
}
