UserBusinessService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.jsh.erp.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jsh.erp.constants.BusinessConstants;
  4. import com.jsh.erp.datasource.entities.User;
  5. import com.jsh.erp.datasource.entities.UserBusiness;
  6. import com.jsh.erp.datasource.entities.UserBusinessExample;
  7. import com.jsh.erp.datasource.mappers.UserBusinessMapper;
  8. import com.jsh.erp.datasource.mappers.UserBusinessMapperEx;
  9. import com.jsh.erp.exception.JshException;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import javax.annotation.Resource;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.util.Date;
  19. import java.util.List;
  20. @Service
  21. public class UserBusinessService {
  22. private Logger logger = LoggerFactory.getLogger(UserBusinessService.class);
  23. @Resource
  24. private UserBusinessMapper userBusinessMapper;
  25. @Resource
  26. private UserBusinessMapperEx userBusinessMapperEx;
  27. @Resource
  28. private LogService logService;
  29. @Resource
  30. private UserService userService;
  31. public UserBusiness getUserBusiness(long id)throws Exception {
  32. UserBusiness result=null;
  33. try{
  34. result=userBusinessMapper.selectByPrimaryKey(id);
  35. }catch(Exception e){
  36. JshException.readFail(logger, e);
  37. }
  38. return result;
  39. }
  40. public List<UserBusiness> getUserBusiness()throws Exception {
  41. UserBusinessExample example = new UserBusinessExample();
  42. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  43. List<UserBusiness> list=null;
  44. try{
  45. list=userBusinessMapper.selectByExample(example);
  46. }catch(Exception e){
  47. JshException.readFail(logger, e);
  48. }
  49. return list;
  50. }
  51. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  52. public int insertUserBusiness(JSONObject obj, HttpServletRequest request) throws Exception {
  53. UserBusiness userBusiness = JSONObject.parseObject(obj.toJSONString(), UserBusiness.class);
  54. int result=0;
  55. try{
  56. String value = userBusiness.getValue();
  57. String newValue = value.replaceAll(",","\\]\\[");
  58. newValue = newValue.replaceAll("\\[0\\]","").replaceAll("\\[\\]","");
  59. userBusiness.setValue(newValue);
  60. result=userBusinessMapper.insertSelective(userBusiness);
  61. logService.insertLog("关联关系", BusinessConstants.LOG_OPERATION_TYPE_ADD, request);
  62. }catch(Exception e){
  63. JshException.writeFail(logger, e);
  64. }
  65. return result;
  66. }
  67. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  68. public int updateUserBusiness(JSONObject obj, HttpServletRequest request) throws Exception {
  69. UserBusiness userBusiness = JSONObject.parseObject(obj.toJSONString(), UserBusiness.class);
  70. int result=0;
  71. try{
  72. String value = userBusiness.getValue();
  73. String newValue = value.replaceAll(",","\\]\\[");
  74. newValue = newValue.replaceAll("\\[0\\]","").replaceAll("\\[\\]","");
  75. newValue = newValue.replace("\"", "");
  76. userBusiness.setValue(newValue);
  77. result=userBusinessMapper.updateByPrimaryKeySelective(userBusiness);
  78. logService.insertLog("关联关系", BusinessConstants.LOG_OPERATION_TYPE_EDIT, request);
  79. }catch(Exception e){
  80. JshException.writeFail(logger, e);
  81. }
  82. return result;
  83. }
  84. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  85. public int deleteUserBusiness(Long id, HttpServletRequest request)throws Exception {
  86. return batchDeleteUserBusinessByIds(id.toString());
  87. }
  88. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  89. public int batchDeleteUserBusiness(String ids, HttpServletRequest request)throws Exception {
  90. return batchDeleteUserBusinessByIds(ids);
  91. }
  92. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  93. public int batchDeleteUserBusinessByIds(String ids) throws Exception{
  94. logService.insertLog("关联关系",
  95. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(ids).toString(),
  96. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  97. User userInfo=userService.getCurrentUser();
  98. String [] idArray=ids.split(",");
  99. int result=0;
  100. try{
  101. result= userBusinessMapperEx.batchDeleteUserBusinessByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  102. }catch(Exception e){
  103. JshException.writeFail(logger, e);
  104. }
  105. return result;
  106. }
  107. public int checkIsNameExist(Long id, String name)throws Exception {
  108. return 1;
  109. }
  110. public List<UserBusiness> getBasicData(String keyId, String type){
  111. List<UserBusiness> list=null;
  112. try{
  113. list= userBusinessMapperEx.getBasicDataByKeyIdAndType(keyId, type);
  114. }catch(Exception e){
  115. JshException.readFail(logger, e);
  116. }
  117. return list;
  118. }
  119. public String getUBValueByTypeAndKeyId(String type, String keyId) throws Exception {
  120. String ubValue = "";
  121. List<UserBusiness> ubList = getBasicData(keyId, type);
  122. if(ubList!=null && ubList.size()>0) {
  123. ubValue = ubList.get(0).getValue();
  124. }
  125. return ubValue;
  126. }
  127. public String[] getUBValuByTypeAndKeyIdToArray(String type, String keyId) throws Exception {
  128. String ubValue = getUBValueByTypeAndKeyId(type, keyId);
  129. if (ubValue == null || ubValue.length() <= 2) { // "[x]"最小长度3
  130. return new String[0];
  131. }
  132. return ubValue.substring(1, ubValue.length()-1).split("\\]\\[");
  133. }
  134. public Long checkIsValueExist(String type, String keyId)throws Exception {
  135. UserBusinessExample example = new UserBusinessExample();
  136. example.createCriteria().andTypeEqualTo(type).andKeyIdEqualTo(keyId)
  137. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  138. List<UserBusiness> list=null;
  139. try{
  140. list= userBusinessMapper.selectByExample(example);
  141. }catch(Exception e){
  142. JshException.readFail(logger, e);
  143. }
  144. Long id = null;
  145. if(list!=null&&list.size() > 0) {
  146. id = list.get(0).getId();
  147. }
  148. return id;
  149. }
  150. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  151. public int updateBtnStr(String keyId, String type, String btnStr) throws Exception{
  152. logService.insertLog("关联关系",
  153. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append("角色的按钮权限").toString(),
  154. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  155. UserBusiness userBusiness = new UserBusiness();
  156. userBusiness.setBtnStr(btnStr);
  157. UserBusinessExample example = new UserBusinessExample();
  158. example.createCriteria().andKeyIdEqualTo(keyId).andTypeEqualTo(type);
  159. int result=0;
  160. try{
  161. result= userBusinessMapper.updateByExampleSelective(userBusiness, example);
  162. }catch(Exception e){
  163. JshException.writeFail(logger, e);
  164. }
  165. return result;
  166. }
  167. }