AmbServiceImpl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.goafanti.ambSystem.service.Impl;
  2. import com.goafanti.ambSystem.bo.AmbAll;
  3. import com.goafanti.ambSystem.bo.InputAmb;
  4. import com.goafanti.ambSystem.bo.OutAmb;
  5. import com.goafanti.ambSystem.service.AmbService;
  6. import com.goafanti.common.dao.*;
  7. import com.goafanti.common.error.BusinessException;
  8. import com.goafanti.common.model.Admin;
  9. import com.goafanti.common.model.AmbSystem;
  10. import com.goafanti.common.model.UserRole;
  11. import com.goafanti.common.utils.StringUtils;
  12. import com.goafanti.core.mybatis.BaseMybatisDao;
  13. import com.goafanti.core.mybatis.page.Pagination;
  14. import com.goafanti.core.shiro.token.TokenManager;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import java.util.*;
  19. @Service
  20. public class AmbServiceImpl extends BaseMybatisDao<AmbSystemMapper> implements AmbService {
  21. @Value(value = "${amb.maxLvl}")
  22. private Integer maxLvl=null;
  23. @Autowired
  24. private AmbSystemMapper ambSystemMapper;
  25. @Autowired
  26. private AdminMapper adminMapper;
  27. @Autowired
  28. private UserRoleMapper userRoleMapper;
  29. @Autowired
  30. private RoleMapper roleMapper;
  31. @Override
  32. public int addAmb(InputAmb in) {
  33. getParentParam(in);
  34. in.setStatus("0");
  35. in.setCreateBy(TokenManager.getAdminId());
  36. in.setCreateTime(new Date());
  37. ambSystemMapper.insertSelective(in);
  38. updateAmbRole(in.getLeader());
  39. return 1;
  40. }
  41. public void getParentParam(InputAmb in){
  42. AmbSystem parentAmb = ambSystemMapper.selectByPrimaryKey(in.getParentId());
  43. in.setLvl(parentAmb.getLvl()+1);
  44. if (in.getLvl()>maxLvl)throw new BusinessException(String.format("层级最多添加%s层,如需添加更多请联系管理员。",maxLvl));
  45. in.setAncestors(parentAmb.getAncestors()+","+parentAmb.getId());
  46. in.setAncestorsNames(getAncestorsNames(in.getAncestors()));
  47. }
  48. @Override
  49. public int updateAmb(InputAmb in) {
  50. AmbSystem use = ambSystemMapper.selectByPrimaryKey(in.getId());
  51. int i=0;
  52. //0为执行计算巴名称,1为已执行,无需再执行
  53. if (in.getParentId()!=null&&in.getParentId()!=use.getParentId()){
  54. getParentParam(in);
  55. i=updateAncestorsNames(in,use.getName(),i);
  56. }
  57. if (in.getName()!=null&&!in.getName().equals(use.getName())){
  58. i=updateAncestorsNames(in,use.getName(),i);
  59. }
  60. in.setUpdateBy(TokenManager.getAdminId());
  61. in.setUpdateTime(new Date());
  62. ambSystemMapper.updateByPrimaryKeySelective(in);
  63. if (!in.getLeader().equals(use.getLeader()))pushAdminAmbRole(in,use);
  64. return 1;
  65. }
  66. /**
  67. * 设置巴主巴角色
  68. * @param in
  69. */
  70. private void pushAdminAmbRole(InputAmb in,AmbSystem use) {
  71. //如果是修改,处理原负责人
  72. if (in.getId()!=null){
  73. if (use !=null &&use.getLeader()!=null){
  74. updateAmbRole(use.getLeader());
  75. }
  76. }
  77. updateAmbRole(in.getLeader());
  78. }
  79. /**
  80. *
  81. * @param leader
  82. */
  83. private void updateAmbRole(String leader) {
  84. StringBuffer str=new StringBuffer();
  85. List<AmbSystem> list=selectByLeader(leader);
  86. Admin a =new Admin();
  87. if (!list.isEmpty()){
  88. //不为空就是巴主,如果不是巴主则设置成巴主
  89. Integer i = userRoleMapper.selectByUidAndRoleName(leader, "巴主");
  90. if (i<1){
  91. String roleId = roleMapper.selectIdByName("巴主");
  92. UserRole userRole=new UserRole();
  93. userRole.setUid(leader);
  94. userRole.setRid(roleId);
  95. userRoleMapper.insert(userRole);
  96. }
  97. int ambManage=0;
  98. for (AmbSystem ambSystem : list) {
  99. str.append(ambSystem.getLvl()).append(",");
  100. if(ambSystem.getLvl()==5||ambSystem.getLvl()==6){
  101. ambManage=1;
  102. }
  103. }
  104. a.setAmbManage(ambManage);
  105. }
  106. a.setId(leader);
  107. if (StringUtils.isNotBlank(str)) a.setAmbRole(str.substring(0,str.length()-1));
  108. if (a.getAmbRole()!=null){
  109. adminMapper.updateByPrimaryKeySelective(a);
  110. }
  111. }
  112. private List<AmbSystem> selectByLeader(String leader) {
  113. AmbSystem a =new AmbSystem();
  114. a.setLeader(leader);
  115. return ambSystemMapper.selectByParameter(a);
  116. }
  117. private List<AmbSystem> selectByParentId(Long parentId) {
  118. AmbSystem a =new AmbSystem();
  119. a.setParentId(parentId);
  120. return ambSystemMapper.selectByParameter(a);
  121. }
  122. private int updateAncestorsNames(InputAmb in,String useName,Integer i) {
  123. if (i>0)return i;
  124. return ambSystemMapper.updateAncestorsNames(in.getId(),in.getName(),useName);
  125. }
  126. @Override
  127. public Pagination<OutAmb> selectAmb(InputAmb in) {
  128. HashMap<String, Object> param = new HashMap<>();
  129. param.put("name",in.getName());
  130. param.put("lvl",in.getLvl());
  131. param.put("leader",in.getLeader());
  132. param.put("ancestors",in.getAncestors());
  133. return (Pagination<OutAmb>) findPage("selectAmbSystemList","selectAmbSystemCount",param,in.getPageNo(),in.getPageSize());
  134. }
  135. @Override
  136. public List<?> selectAll() {
  137. List<AmbAll> list = ambSystemMapper.getAncestorsList(null);
  138. List<AmbAll> all=new ArrayList<>();
  139. for (AmbAll a : list) {
  140. if (a.getLvl()==0){
  141. pushLvl(a,list);
  142. all.add(a);
  143. }
  144. }
  145. return all;
  146. }
  147. @Override
  148. public int deleteAmb(InputAmb in) {
  149. return ambSystemMapper.deleteByPrimaryKey(in.getId());
  150. }
  151. @Override
  152. public int checkAmb(InputAmb in,Integer type) {
  153. if (type==0){
  154. int i = ambSystemMapper.selectByName(in.getName());
  155. if (i>0)return -1;
  156. }else if (type==2){
  157. List<AmbSystem> ambSystems = selectByParentId(in.getId());
  158. if (!ambSystems.isEmpty())return -1;
  159. }
  160. return 0;
  161. }
  162. @Override
  163. public Object detailsAmb(InputAmb in) {
  164. return ambSystemMapper.selectDtailsAmb(in.getId());
  165. }
  166. private void pushLvl(AmbAll a , List<AmbAll> list) {
  167. if (a.getLvl()<7){
  168. List<AmbAll> all=new ArrayList<>();
  169. for (AmbAll as : list) {
  170. if (as.getParentId().equals(a.getId())){
  171. pushLvl(as,list);
  172. all.add(as);
  173. }
  174. }
  175. a.setList(all);
  176. }
  177. }
  178. private String getAncestorsNames(String ancestors) {
  179. String[] split = ancestors.split(",");
  180. List<String> list = Arrays.asList(split);
  181. List<AmbAll> ancestorsList = ambSystemMapper.getAncestorsList(list);
  182. StringBuffer sb=new StringBuffer();
  183. for (AmbAll ambSystem : ancestorsList) {
  184. for (String s : list) {
  185. //第一层无需装配
  186. if (!ambSystem.getId().toString().equals("1")){
  187. if (ambSystem.getId().toString().equals(s)){
  188. sb=sb.append(ambSystem.getName()).append(",");
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. String str="";
  195. if (sb.length()>1)str=sb.substring(0,sb.length()-1);
  196. return str;
  197. }
  198. @Override
  199. public Object getMyambDtails() {
  200. List<AmbSystem> ambSystems = ambSystemMapper.selectByParameter(new AmbSystem(TokenManager.getAdminId()));
  201. return ambSystems;
  202. }
  203. }