|  | @ -0,0 +1,218 @@
 | 
	
		
			
				|  |  | /*******************************************************************************
 | 
	
		
			
				|  |  |  * Copyright (c) 2005, 2014 springside.github.io
 | 
	
		
			
				|  |  |  *
 | 
	
		
			
				|  |  |  * Licensed under the Apache License, Version 2.0 (the "License");
 | 
	
		
			
				|  |  |  *******************************************************************************/
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.role;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.role.DoctorRoleDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.role.DoctorRoleRealmDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.service.doctor.CareDoctorService;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.role.DoctorRole;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.role.DoctorRoleRealm;
 | 
	
		
			
				|  |  | import com.yihu.mysql.query.BaseJpaService;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | import org.springside.modules.utils.Clock;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 医生角色管理类.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service("doctorRoleService")
 | 
	
		
			
				|  |  | @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | public class RoleService extends BaseJpaService<DoctorRole, DoctorRoleDao> {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DoctorRoleDao doctorRoleDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DoctorRoleRealmDao doctorRoleRealmDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private Clock clock = Clock.DEFAULT;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao doctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private CareDoctorService doctorInfoService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取用户角色列表
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<DoctorRole> getUserRole(String user) {
 | 
	
		
			
				|  |  |         return doctorRoleDao.findUserRole(user);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 区域去末尾零
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private String getShortArea(String area) {
 | 
	
		
			
				|  |  |         char strs[] = area.toCharArray();// 将字符串转化成字符数组
 | 
	
		
			
				|  |  |         String last = "";
 | 
	
		
			
				|  |  |         for (int i = area.length() - 1; i >= 0; i--) {
 | 
	
		
			
				|  |  |             if ('0' == strs[i]) {
 | 
	
		
			
				|  |  |                 last += "0";
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (last.length() > 0) {
 | 
	
		
			
				|  |  |             return area.substring(0, area.lastIndexOf(last));
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             return area;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取角色区域
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<Map<String, String>> getUserRoleAndArea(String user) {
 | 
	
		
			
				|  |  |         List<Map<String, String>> re = new ArrayList<>();
 | 
	
		
			
				|  |  |         List<DoctorRole> roleList = doctorRoleDao.findUserRole(user);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         int resultLevel = 5;
 | 
	
		
			
				|  |  |         for (DoctorRole one : roleList) {
 | 
	
		
			
				|  |  |             if(resultLevel>roleConverse(one.getCode())){
 | 
	
		
			
				|  |  |                 resultLevel=roleConverse(one.getCode());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (roleList != null && roleList.size() > 0) {
 | 
	
		
			
				|  |  |             //遍历角色
 | 
	
		
			
				|  |  |             for (DoctorRole role : roleList) {
 | 
	
		
			
				|  |  |                 if(role.getCode().length()!=6&&role.getCode().length()!=10){
 | 
	
		
			
				|  |  |                     continue;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 Map<String, String> map = new HashMap<>();
 | 
	
		
			
				|  |  |                 map.put("code", role.getCode());
 | 
	
		
			
				|  |  |                 map.put("name", role.getName());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //获取区域权限
 | 
	
		
			
				|  |  |                 String areaString = "";
 | 
	
		
			
				|  |  |                 List<DoctorRoleRealm> areaList = doctorRoleRealmDao.findByRoleCode(role.getCode());
 | 
	
		
			
				|  |  |                 if (areaList != null && areaList.size() > 0) {
 | 
	
		
			
				|  |  |                     //获取最大权限区域
 | 
	
		
			
				|  |  |                     List<String> areas = new ArrayList<>();
 | 
	
		
			
				|  |  |                     List<String> orgs = new ArrayList<>();
 | 
	
		
			
				|  |  |                     for (DoctorRoleRealm areaItem : areaList) {
 | 
	
		
			
				|  |  |                         String currentArea = areaItem.getAreaCode();
 | 
	
		
			
				|  |  |                         int currentLength = currentArea.length();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         if (currentLength == 6) //区域
 | 
	
		
			
				|  |  |                         {
 | 
	
		
			
				|  |  |                             areas.add(currentArea);
 | 
	
		
			
				|  |  |                         } else {               //机构
 | 
	
		
			
				|  |  |                             orgs.add(currentArea);
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     //区域权限重复校验
 | 
	
		
			
				|  |  |                     for (int i = areas.size() - 1; i >= 0; i--) {
 | 
	
		
			
				|  |  |                         String currentArea = areas.get(i);
 | 
	
		
			
				|  |  |                         for (int j = 0; j < areas.size() - i - 1; j++) {
 | 
	
		
			
				|  |  |                             String shortArea = getShortArea(areas.get(j));
 | 
	
		
			
				|  |  |                             if (currentArea.startsWith(shortArea))   //包含则移除
 | 
	
		
			
				|  |  |                             {
 | 
	
		
			
				|  |  |                                 areas.remove(i);
 | 
	
		
			
				|  |  |                             }
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     //机构权限重复校验
 | 
	
		
			
				|  |  |                     for (int m = orgs.size() - 1; m >= 0; m--) {
 | 
	
		
			
				|  |  |                         String currentOrg = orgs.get(m);
 | 
	
		
			
				|  |  |                         for (int n = 0; n < areas.size() - m - 1; n++) {
 | 
	
		
			
				|  |  |                             String shortArea = getShortArea(areas.get(n));
 | 
	
		
			
				|  |  |                             if (currentOrg.startsWith(shortArea))   //包含则移除
 | 
	
		
			
				|  |  |                             {
 | 
	
		
			
				|  |  |                                 orgs.remove(m);
 | 
	
		
			
				|  |  |                             }
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     for (String item : areas) {
 | 
	
		
			
				|  |  |                         areaString += item + ",";
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     for (String item : orgs) {
 | 
	
		
			
				|  |  |                         areaString += item + ",";
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     areaString = areaString.substring(0, areaString.length() - 1);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 map.put("areas", areaString);
 | 
	
		
			
				|  |  |                 map.put("level",roleConverse(role.getCode())+"");
 | 
	
		
			
				|  |  |                 map.put("isManage", "1");
 | 
	
		
			
				|  |  |                 if(resultLevel==roleConverse(role.getCode())){
 | 
	
		
			
				|  |  |                     map.put("high", "1");
 | 
	
		
			
				|  |  |                 }else{
 | 
	
		
			
				|  |  |                     map.put("high", "0");
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 re.add(map);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return re;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 角色code转成角色级别
 | 
	
		
			
				|  |  |      * @param roleCode 角色code
 | 
	
		
			
				|  |  |      * @return 1、省级,2、市级,3、区级,4、机构
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Integer roleConverse(String roleCode){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(roleCode.length()==6){
 | 
	
		
			
				|  |  |             if("0".equals(roleCode.substring(3,4))){
 | 
	
		
			
				|  |  |                 return 1;
 | 
	
		
			
				|  |  |             }else if("00".equals(roleCode.substring(4))){
 | 
	
		
			
				|  |  |                 return 2;
 | 
	
		
			
				|  |  |             }else{
 | 
	
		
			
				|  |  |                 return 3;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }else{
 | 
	
		
			
				|  |  |             return 4;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Map<String,Object> findManageUserRole(String doctor){
 | 
	
		
			
				|  |  |         String sql ="SELECT " +
 | 
	
		
			
				|  |  |                 " r.id, " +
 | 
	
		
			
				|  |  |                 " r.`code`, " +
 | 
	
		
			
				|  |  |                 " r.`name`, " +
 | 
	
		
			
				|  |  |                 " r.description " +
 | 
	
		
			
				|  |  |                 " FROM " +
 | 
	
		
			
				|  |  |                 " manage_role_user m " +
 | 
	
		
			
				|  |  |                 " JOIN manage_roles r ON m.role_id = r.id " +
 | 
	
		
			
				|  |  |                 " WHERE " +
 | 
	
		
			
				|  |  |                 " m.user_code = '"+doctor+"' and r.`code` ='dispatcher' ";
 | 
	
		
			
				|  |  |         List<Map<String,Object>> rs = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         if(rs!=null&&rs.size()>0){
 | 
	
		
			
				|  |  |             return rs.get(0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<Map<String,Object>> queryDispatcherInfoByHospital(String hospital){
 | 
	
		
			
				|  |  |         String sql ="SELECT " +
 | 
	
		
			
				|  |  |                 " d.`code`, " +
 | 
	
		
			
				|  |  |                 " d.`name`, " +
 | 
	
		
			
				|  |  |                 " d.`hospital_name` as hospitalName " +
 | 
	
		
			
				|  |  |                 " FROM " +
 | 
	
		
			
				|  |  |                 " manage_role_user m " +
 | 
	
		
			
				|  |  |                 " JOIN manage_roles r ON m.role_id = r.id " +
 | 
	
		
			
				|  |  |                 " JOIN (select code,name,hospital_name from wlyy_doctor where hospital = '"+ hospital +"') as d ON m.user_code = d.code" +
 | 
	
		
			
				|  |  |                 " WHERE " +
 | 
	
		
			
				|  |  |                 " r.code = 'dispatcher'";
 | 
	
		
			
				|  |  |         return jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |