MsgService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.jsh.erp.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jsh.erp.constants.BusinessConstants;
  4. import com.jsh.erp.constants.ExceptionConstants;
  5. import com.jsh.erp.datasource.entities.Msg;
  6. import com.jsh.erp.datasource.entities.MsgEx;
  7. import com.jsh.erp.datasource.entities.MsgExample;
  8. import com.jsh.erp.datasource.entities.User;
  9. import com.jsh.erp.datasource.mappers.MsgMapper;
  10. import com.jsh.erp.datasource.mappers.MsgMapperEx;
  11. import com.jsh.erp.exception.BusinessRunTimeException;
  12. import com.jsh.erp.utils.PageUtils;
  13. import com.jsh.erp.utils.StringUtil;
  14. import com.jsh.erp.utils.Tools;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import org.springframework.web.context.request.RequestContextHolder;
  20. import org.springframework.web.context.request.ServletRequestAttributes;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletRequest;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import static com.jsh.erp.utils.Tools.getCenternTime;
  27. @Service
  28. public class MsgService {
  29. private Logger logger = LoggerFactory.getLogger(MsgService.class);
  30. @Resource
  31. private MsgMapper msgMapper;
  32. @Resource
  33. private MsgMapperEx msgMapperEx;
  34. @Resource
  35. private DepotHeadService depotHeadService;
  36. @Resource
  37. private UserService userService;
  38. @Resource
  39. private LogService logService;
  40. public Msg getMsg(long id)throws Exception {
  41. Msg result=null;
  42. try{
  43. result=msgMapper.selectByPrimaryKey(id);
  44. }catch(Exception e){
  45. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  46. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  47. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  48. ExceptionConstants.DATA_READ_FAIL_MSG);
  49. }
  50. return result;
  51. }
  52. public List<Msg> getMsg()throws Exception {
  53. MsgExample example = new MsgExample();
  54. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  55. List<Msg> list=null;
  56. try{
  57. list=msgMapper.selectByExample(example);
  58. }catch(Exception e){
  59. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  60. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  61. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  62. ExceptionConstants.DATA_READ_FAIL_MSG);
  63. }
  64. return list;
  65. }
  66. public List<MsgEx> select(String name)throws Exception {
  67. List<MsgEx> list=null;
  68. try{
  69. User userInfo = userService.getCurrentUser();
  70. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  71. PageUtils.startPage();
  72. list = msgMapperEx.selectByConditionMsg(userInfo.getId(), name);
  73. if (null != list) {
  74. for (MsgEx msgEx : list) {
  75. if (msgEx.getCreateTime() != null) {
  76. msgEx.setCreateTimeStr(getCenternTime(msgEx.getCreateTime()));
  77. }
  78. }
  79. }
  80. }
  81. }catch(Exception e){
  82. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  83. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  84. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  85. ExceptionConstants.DATA_READ_FAIL_MSG);
  86. }
  87. return list;
  88. }
  89. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  90. public int insertMsg(JSONObject obj, HttpServletRequest request)throws Exception {
  91. Msg msg = JSONObject.parseObject(obj.toJSONString(), Msg.class);
  92. int result=0;
  93. try{
  94. User userInfo = userService.getCurrentUser();
  95. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  96. msg.setCreateTime(new Date());
  97. msg.setStatus("1");
  98. result=msgMapper.insertSelective(msg);
  99. logService.insertLog("消息",
  100. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(msg.getMsgTitle()).toString(), request);
  101. }
  102. }catch(Exception e){
  103. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  104. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  105. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  106. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  107. }
  108. return result;
  109. }
  110. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  111. public int updateMsg(JSONObject obj, HttpServletRequest request) throws Exception{
  112. Msg msg = JSONObject.parseObject(obj.toJSONString(), Msg.class);
  113. int result=0;
  114. try{
  115. result=msgMapper.updateByPrimaryKeySelective(msg);
  116. logService.insertLog("消息",
  117. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(msg.getMsgTitle()).toString(), request);
  118. }catch(Exception e){
  119. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  120. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  121. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  122. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  123. }
  124. return result;
  125. }
  126. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  127. public int deleteMsg(Long id, HttpServletRequest request)throws Exception {
  128. int result=0;
  129. try{
  130. result=msgMapper.deleteByPrimaryKey(id);
  131. logService.insertLog("消息",
  132. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(id).toString(), request);
  133. }catch(Exception e){
  134. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  135. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  136. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  137. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  138. }
  139. return result;
  140. }
  141. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  142. public int batchDeleteMsg(String ids, HttpServletRequest request) throws Exception{
  143. List<Long> idList = StringUtil.strToLongList(ids);
  144. MsgExample example = new MsgExample();
  145. example.createCriteria().andIdIn(idList);
  146. int result=0;
  147. try{
  148. result=msgMapper.deleteByExample(example);
  149. logService.insertLog("消息", "批量删除,id集:" + ids, request);
  150. }catch(Exception e){
  151. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  152. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  153. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  154. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  155. }
  156. return result;
  157. }
  158. public int checkIsNameExist(Long id, String name)throws Exception {
  159. MsgExample example = new MsgExample();
  160. example.createCriteria().andIdNotEqualTo(id).andMsgTitleEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  161. List<Msg> list=null;
  162. try{
  163. list= msgMapper.selectByExample(example);
  164. }catch(Exception e){
  165. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  166. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  167. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  168. ExceptionConstants.DATA_READ_FAIL_MSG);
  169. }
  170. return list==null?0:list.size();
  171. }
  172. /**
  173. * create by: qiankunpingtai
  174. * 逻辑删除角色信息
  175. * create time: 2019/3/28 15:44
  176. * @Param: ids
  177. * @return int
  178. */
  179. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  180. public int batchDeleteMsgByIds(String ids) throws Exception{
  181. logService.insertLog("序列号",
  182. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(ids).toString(),
  183. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  184. String [] idArray=ids.split(",");
  185. int result=0;
  186. try{
  187. result=msgMapperEx.batchDeleteMsgByIds(idArray);
  188. }catch(Exception e){
  189. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  190. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  191. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  192. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  193. }
  194. return result;
  195. }
  196. public List<MsgEx> getMsgByStatus(String status)throws Exception {
  197. List<MsgEx> resList=new ArrayList<>();
  198. try{
  199. User userInfo = userService.getCurrentUser();
  200. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  201. MsgExample example = new MsgExample();
  202. example.createCriteria().andStatusEqualTo(status).andUserIdEqualTo(userInfo.getId())
  203. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  204. example.setOrderByClause("id desc");
  205. List<Msg> list = msgMapper.selectByExample(example);
  206. if (null != list) {
  207. for (Msg msg : list) {
  208. if (msg.getCreateTime() != null) {
  209. MsgEx msgEx = new MsgEx();
  210. msgEx.setId(msg.getId());
  211. msgEx.setMsgTitle(msg.getMsgTitle());
  212. msgEx.setMsgContent(msg.getMsgContent());
  213. msgEx.setStatus(msg.getStatus());
  214. msgEx.setType(msg.getType());
  215. msgEx.setCreateTimeStr(Tools.getCenternTime(msg.getCreateTime()));
  216. resList.add(msgEx);
  217. }
  218. }
  219. }
  220. }
  221. }catch(Exception e){
  222. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  223. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  224. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  225. ExceptionConstants.DATA_READ_FAIL_MSG);
  226. }
  227. return resList;
  228. }
  229. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  230. public void batchUpdateStatus(String ids, String status) throws Exception{
  231. List<Long> idList = StringUtil.strToLongList(ids);
  232. Msg msg = new Msg();
  233. msg.setStatus(status);
  234. MsgExample example = new MsgExample();
  235. example.createCriteria().andIdIn(idList);
  236. try{
  237. msgMapper.updateByExampleSelective(msg, example);
  238. }catch(Exception e){
  239. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  240. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  241. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  242. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  243. }
  244. }
  245. public Long getMsgCountByStatus(String status)throws Exception {
  246. Long result=null;
  247. try{
  248. User userInfo=userService.getCurrentUser();
  249. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  250. result = msgMapperEx.getMsgCountByStatus(status, userInfo.getId());
  251. }
  252. }catch(Exception e){
  253. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  254. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  255. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  256. ExceptionConstants.DATA_READ_FAIL_MSG);
  257. }
  258. return result;
  259. }
  260. public Integer getMsgCountByType(String type)throws Exception {
  261. int msgCount = 0;
  262. try{
  263. User userInfo = userService.getCurrentUser();
  264. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  265. MsgExample example = new MsgExample();
  266. example.createCriteria().andTypeEqualTo(type).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  267. List<Msg> list = msgMapper.selectByExample(example);
  268. msgCount = list.size();
  269. }
  270. }catch(Exception e){
  271. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  272. ExceptionConstants.DATA_READ_FAIL_CODE, ExceptionConstants.DATA_READ_FAIL_MSG,e);
  273. throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
  274. ExceptionConstants.DATA_READ_FAIL_MSG);
  275. }
  276. return msgCount;
  277. }
  278. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  279. public void readAllMsg() throws Exception{
  280. try{
  281. User userInfo = userService.getCurrentUser();
  282. if(!BusinessConstants.DEFAULT_MANAGER.equals(userInfo.getLoginName())) {
  283. Msg msg = new Msg();
  284. msg.setStatus("2");
  285. MsgExample example = new MsgExample();
  286. example.createCriteria();
  287. msgMapper.updateByExampleSelective(msg, example);
  288. }
  289. }catch(Exception e){
  290. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  291. ExceptionConstants.DATA_WRITE_FAIL_CODE, ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  292. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  293. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  294. }
  295. }
  296. }