RoleService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Role;
  5. import com.jsh.erp.datasource.entities.RoleEx;
  6. import com.jsh.erp.datasource.entities.RoleExample;
  7. import com.jsh.erp.datasource.entities.User;
  8. import com.jsh.erp.datasource.mappers.RoleMapper;
  9. import com.jsh.erp.datasource.mappers.RoleMapperEx;
  10. import com.jsh.erp.exception.JshException;
  11. import com.jsh.erp.utils.PageUtils;
  12. import com.jsh.erp.utils.StringUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import org.springframework.web.context.request.RequestContextHolder;
  18. import org.springframework.web.context.request.ServletRequestAttributes;
  19. import javax.annotation.Resource;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.math.BigDecimal;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. @Service
  26. public class RoleService {
  27. private Logger logger = LoggerFactory.getLogger(RoleService.class);
  28. @Resource
  29. private RoleMapper roleMapper;
  30. @Resource
  31. private RoleMapperEx roleMapperEx;
  32. @Resource
  33. private LogService logService;
  34. @Resource
  35. private UserService userService;
  36. //超管的专用角色
  37. private static Long MANAGE_ROLE_ID = 4L;
  38. public Role getRole(long id)throws Exception {
  39. Role result=null;
  40. try{
  41. result=roleMapper.selectByPrimaryKey(id);
  42. }catch(Exception e){
  43. JshException.readFail(logger, e);
  44. }
  45. return result;
  46. }
  47. public List<Role> getRoleListByIds(String ids)throws Exception {
  48. List<Long> idList = StringUtil.strToLongList(ids);
  49. List<Role> list = new ArrayList<>();
  50. try{
  51. RoleExample example = new RoleExample();
  52. example.createCriteria().andIdIn(idList);
  53. list = roleMapper.selectByExample(example);
  54. }catch(Exception e){
  55. JshException.readFail(logger, e);
  56. }
  57. return list;
  58. }
  59. public List<Role> allList()throws Exception {
  60. RoleExample example = new RoleExample();
  61. example.createCriteria().andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  62. example.setOrderByClause("sort asc, id desc");
  63. List<Role> list=null;
  64. try{
  65. list=roleMapper.selectByExample(example);
  66. }catch(Exception e){
  67. JshException.readFail(logger, e);
  68. }
  69. return list;
  70. }
  71. public List<Role> tenantRoleList() {
  72. List<Role> list=null;
  73. try{
  74. if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
  75. RoleExample example = new RoleExample();
  76. example.createCriteria().andEnabledEqualTo(true).andTenantIdIsNull().andIdNotEqualTo(MANAGE_ROLE_ID)
  77. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  78. example.setOrderByClause("sort asc, id asc");
  79. list=roleMapper.selectByExample(example);
  80. }
  81. }catch(Exception e){
  82. JshException.readFail(logger, e);
  83. }
  84. return list;
  85. }
  86. public List<RoleEx> select(String name, String description)throws Exception {
  87. List<RoleEx> list=null;
  88. try{
  89. PageUtils.startPage();
  90. list=roleMapperEx.selectByConditionRole(name, description);
  91. for(RoleEx roleEx: list) {
  92. String priceLimit = roleEx.getPriceLimit();
  93. if(StringUtil.isNotEmpty(priceLimit)) {
  94. String priceLimitStr = priceLimit
  95. .replace("1", "屏蔽首页采购价")
  96. .replace("2", "屏蔽首页零售价")
  97. .replace("3", "屏蔽首页销售价")
  98. .replace("4", "屏蔽单据采购价")
  99. .replace("5", "屏蔽单据零售价")
  100. .replace("6", "屏蔽单据销售价");
  101. roleEx.setPriceLimitStr(priceLimitStr);
  102. }
  103. }
  104. }catch(Exception e){
  105. JshException.readFail(logger, e);
  106. }
  107. return list;
  108. }
  109. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  110. public int insertRole(JSONObject obj, HttpServletRequest request)throws Exception {
  111. Role role = JSONObject.parseObject(obj.toJSONString(), Role.class);
  112. int result=0;
  113. try{
  114. role.setEnabled(true);
  115. result=roleMapper.insertSelective(role);
  116. logService.insertLog("角色",
  117. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(role.getName()).toString(), request);
  118. }catch(Exception e){
  119. JshException.writeFail(logger, e);
  120. }
  121. return result;
  122. }
  123. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  124. public int updateRole(JSONObject obj, HttpServletRequest request) throws Exception{
  125. Role role = JSONObject.parseObject(obj.toJSONString(), Role.class);
  126. int result=0;
  127. try{
  128. result=roleMapper.updateByPrimaryKeySelective(role);
  129. logService.insertLog("角色",
  130. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(role.getName()).toString(), request);
  131. }catch(Exception e){
  132. JshException.writeFail(logger, e);
  133. }
  134. return result;
  135. }
  136. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  137. public int deleteRole(Long id, HttpServletRequest request)throws Exception {
  138. return batchDeleteRoleByIds(id.toString());
  139. }
  140. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  141. public int batchDeleteRole(String ids, HttpServletRequest request) throws Exception{
  142. return batchDeleteRoleByIds(ids);
  143. }
  144. public int checkIsNameExist(Long id, String name) throws Exception{
  145. RoleExample example = new RoleExample();
  146. example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  147. List<Role> list =null;
  148. try{
  149. list=roleMapper.selectByExample(example);
  150. }catch(Exception e){
  151. JshException.readFail(logger, e);
  152. }
  153. return list==null?0:list.size();
  154. }
  155. public List<Role> findUserRole()throws Exception{
  156. RoleExample example = new RoleExample();
  157. example.setOrderByClause("Id");
  158. example.createCriteria().andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  159. List<Role> list=null;
  160. try{
  161. list=roleMapper.selectByExample(example);
  162. }catch(Exception e){
  163. JshException.readFail(logger, e);
  164. }
  165. return list;
  166. }
  167. /**
  168. * create by: qiankunpingtai
  169. * 逻辑删除角色信息
  170. * create time: 2019/3/28 15:44
  171. * @Param: ids
  172. * @return int
  173. */
  174. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  175. public int batchDeleteRoleByIds(String ids) throws Exception{
  176. StringBuffer sb = new StringBuffer();
  177. sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
  178. List<Role> list = getRoleListByIds(ids);
  179. for(Role role: list){
  180. sb.append("[").append(role.getName()).append("]");
  181. }
  182. logService.insertLog("角色", sb.toString(),
  183. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  184. User userInfo=userService.getCurrentUser();
  185. String [] idArray=ids.split(",");
  186. int result=0;
  187. try{
  188. result=roleMapperEx.batchDeleteRoleByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  189. }catch(Exception e){
  190. JshException.writeFail(logger, e);
  191. }
  192. return result;
  193. }
  194. public Role getRoleWithoutTenant(Long roleId) {
  195. return roleMapperEx.getRoleWithoutTenant(roleId);
  196. }
  197. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  198. public int batchSetStatus(Boolean status, String ids)throws Exception {
  199. logService.insertLog("角色",
  200. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ENABLED).toString(),
  201. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  202. List<Long> roleIds = StringUtil.strToLongList(ids);
  203. Role role = new Role();
  204. role.setEnabled(status);
  205. RoleExample example = new RoleExample();
  206. example.createCriteria().andIdIn(roleIds);
  207. int result=0;
  208. try{
  209. result = roleMapper.updateByExampleSelective(role, example);
  210. }catch(Exception e){
  211. JshException.writeFail(logger, e);
  212. }
  213. return result;
  214. }
  215. /**
  216. * 根据权限进行屏蔽价格-首页
  217. * @param price
  218. * @param type
  219. * @return
  220. */
  221. public Object parseHomePriceByLimit(BigDecimal price, String type, String priceLimit, String emptyInfo, HttpServletRequest request) throws Exception {
  222. if(StringUtil.isNotEmpty(priceLimit)) {
  223. if("buy".equals(type) && priceLimit.contains("1")) {
  224. return emptyInfo;
  225. }
  226. if("retail".equals(type) && priceLimit.contains("2")) {
  227. return emptyInfo;
  228. }
  229. if("sale".equals(type) && priceLimit.contains("3")) {
  230. return emptyInfo;
  231. }
  232. }
  233. return price;
  234. }
  235. /**
  236. * 根据权限进行屏蔽价格-单据
  237. * @param price
  238. * @param billCategory
  239. * @param priceLimit
  240. * @param request
  241. * @return
  242. * @throws Exception
  243. */
  244. public BigDecimal parseBillPriceByLimit(BigDecimal price, String billCategory, String priceLimit, HttpServletRequest request) throws Exception {
  245. if(StringUtil.isNotEmpty(priceLimit)) {
  246. if("buy".equals(billCategory) && priceLimit.contains("4")) {
  247. return BigDecimal.ZERO;
  248. }
  249. if("retail".equals(billCategory) && priceLimit.contains("5")) {
  250. return BigDecimal.ZERO;
  251. }
  252. if("sale".equals(billCategory) && priceLimit.contains("6")) {
  253. return BigDecimal.ZERO;
  254. }
  255. }
  256. return price;
  257. }
  258. /**
  259. * 根据权限进行屏蔽价格-物料
  260. * @param price
  261. * @param type
  262. * @return
  263. */
  264. public Object parseMaterialPriceByLimit(BigDecimal price, String type, String emptyInfo, HttpServletRequest request) throws Exception {
  265. Long userId = userService.getUserId(request);
  266. String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
  267. if(StringUtil.isNotEmpty(priceLimit)) {
  268. if("buy".equals(type) && priceLimit.contains("4")) {
  269. return emptyInfo;
  270. }
  271. if("retail".equals(type) && priceLimit.contains("5")) {
  272. return emptyInfo;
  273. }
  274. if("sale".equals(type) && priceLimit.contains("6")) {
  275. return emptyInfo;
  276. }
  277. }
  278. return price;
  279. }
  280. public String getCurrentPriceLimit(HttpServletRequest request) throws Exception {
  281. Long userId = userService.getUserId(request);
  282. return userService.getRoleTypeByUserId(userId).getPriceLimit();
  283. }
  284. }