PersonService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.AccountHead;
  6. import com.jsh.erp.datasource.entities.DepotHead;
  7. import com.jsh.erp.datasource.entities.Person;
  8. import com.jsh.erp.datasource.entities.PersonExample;
  9. import com.jsh.erp.datasource.mappers.AccountHeadMapperEx;
  10. import com.jsh.erp.datasource.mappers.DepotHeadMapperEx;
  11. import com.jsh.erp.datasource.mappers.PersonMapper;
  12. import com.jsh.erp.datasource.mappers.PersonMapperEx;
  13. import com.jsh.erp.exception.BusinessRunTimeException;
  14. import com.jsh.erp.exception.JshException;
  15. import com.jsh.erp.utils.PageUtils;
  16. import com.jsh.erp.utils.StringUtil;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.web.context.request.RequestContextHolder;
  22. import org.springframework.web.context.request.ServletRequestAttributes;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. @Service
  30. public class PersonService {
  31. private Logger logger = LoggerFactory.getLogger(PersonService.class);
  32. @Resource
  33. private PersonMapper personMapper;
  34. @Resource
  35. private PersonMapperEx personMapperEx;
  36. @Resource
  37. private UserService userService;
  38. @Resource
  39. private LogService logService;
  40. @Resource
  41. private AccountHeadMapperEx accountHeadMapperEx;
  42. @Resource
  43. private DepotHeadMapperEx depotHeadMapperEx;
  44. public Person getPerson(long id)throws Exception {
  45. Person result=null;
  46. try{
  47. result=personMapper.selectByPrimaryKey(id);
  48. }catch(Exception e){
  49. JshException.readFail(logger, e);
  50. }
  51. return result;
  52. }
  53. public List<Person> getPersonListByIds(String ids)throws Exception {
  54. List<Long> idList = StringUtil.strToLongList(ids);
  55. List<Person> list = new ArrayList<>();
  56. try{
  57. PersonExample example = new PersonExample();
  58. example.createCriteria().andIdIn(idList);
  59. list = personMapper.selectByExample(example);
  60. }catch(Exception e){
  61. JshException.readFail(logger, e);
  62. }
  63. return list;
  64. }
  65. public List<Person> getPerson()throws Exception {
  66. PersonExample example = new PersonExample();
  67. example.createCriteria().andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  68. List<Person> list=null;
  69. try{
  70. list=personMapper.selectByExample(example);
  71. }catch(Exception e){
  72. JshException.readFail(logger, e);
  73. }
  74. return list;
  75. }
  76. public List<Person> select(String name, String type)throws Exception {
  77. List<Person> list=null;
  78. try{
  79. PageUtils.startPage();
  80. list=personMapperEx.selectByConditionPerson(name, type);
  81. }catch(Exception e){
  82. JshException.readFail(logger, e);
  83. }
  84. return list;
  85. }
  86. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  87. public int insertPerson(JSONObject obj, HttpServletRequest request)throws Exception {
  88. Person person = JSONObject.parseObject(obj.toJSONString(), Person.class);
  89. int result=0;
  90. try{
  91. person.setEnabled(true);
  92. result=personMapper.insertSelective(person);
  93. logService.insertLog("经手人",
  94. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(person.getName()).toString(), request);
  95. }catch(Exception e){
  96. JshException.writeFail(logger, e);
  97. }
  98. return result;
  99. }
  100. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  101. public int updatePerson(JSONObject obj, HttpServletRequest request)throws Exception {
  102. Person person = JSONObject.parseObject(obj.toJSONString(), Person.class);
  103. int result=0;
  104. try{
  105. result=personMapper.updateByPrimaryKeySelective(person);
  106. logService.insertLog("经手人",
  107. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(person.getName()).toString(), request);
  108. }catch(Exception e){
  109. JshException.writeFail(logger, e);
  110. }
  111. return result;
  112. }
  113. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  114. public int deletePerson(Long id, HttpServletRequest request)throws Exception {
  115. return batchDeletePersonByIds(id.toString());
  116. }
  117. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  118. public int batchDeletePerson(String ids, HttpServletRequest request) throws Exception{
  119. return batchDeletePersonByIds(ids);
  120. }
  121. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  122. public int batchDeletePersonByIds(String ids)throws Exception {
  123. int result =0;
  124. String [] idArray=ids.split(",");
  125. //校验财务主表 jsh_accounthead
  126. List<AccountHead> accountHeadList =null;
  127. try{
  128. accountHeadList=accountHeadMapperEx.getAccountHeadListByHandsPersonIds(idArray);
  129. }catch(Exception e){
  130. JshException.readFail(logger, e);
  131. }
  132. if(accountHeadList!=null&&accountHeadList.size()>0){
  133. logger.error("异常码[{}],异常提示[{}],参数,HandsPersonIds[{}]",
  134. ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
  135. throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
  136. ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
  137. }
  138. //校验单据主表 jsh_depot_head
  139. List<DepotHead> depotHeadList =null;
  140. try{
  141. depotHeadList=depotHeadMapperEx.getDepotHeadListByCreator(idArray);
  142. }catch(Exception e){
  143. JshException.readFail(logger, e);
  144. }
  145. if(depotHeadList!=null&&depotHeadList.size()>0){
  146. logger.error("异常码[{}],异常提示[{}],参数,HandsPersonIds[{}]",
  147. ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
  148. throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
  149. ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
  150. }
  151. //记录日志
  152. StringBuffer sb = new StringBuffer();
  153. sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
  154. List<Person> list = getPersonListByIds(ids);
  155. for(Person person: list){
  156. sb.append("[").append(person.getName()).append("]");
  157. }
  158. logService.insertLog("经手人", sb.toString(),
  159. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  160. //删除经手人
  161. try{
  162. result=personMapperEx.batchDeletePersonByIds(idArray);
  163. }catch(Exception e){
  164. JshException.writeFail(logger, e);
  165. }
  166. return result;
  167. }
  168. public int checkIsNameExist(Long id, String name) throws Exception{
  169. PersonExample example = new PersonExample();
  170. example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  171. List<Person> list =null;
  172. try{
  173. list=personMapper.selectByExample(example);
  174. }catch(Exception e){
  175. JshException.readFail(logger, e);
  176. }
  177. return list==null?0:list.size();
  178. }
  179. public Map<Long,String> getPersonMap() throws Exception {
  180. List<Person> personList = getPerson();
  181. Map<Long,String> personMap = new HashMap<>();
  182. for(Person person : personList){
  183. personMap.put(person.getId(), person.getName());
  184. }
  185. return personMap;
  186. }
  187. public String getPersonByMapAndIds(Map<Long,String> personMap, String personIds)throws Exception {
  188. List<Long> ids = StringUtil.strToLongList(personIds);
  189. StringBuffer sb = new StringBuffer();
  190. for(Long id: ids){
  191. sb.append(personMap.get(id) + " ");
  192. }
  193. return sb.toString();
  194. }
  195. public List<Person> getPersonByType(String type)throws Exception {
  196. PersonExample example = new PersonExample();
  197. example.createCriteria().andTypeEqualTo(type).andEnabledEqualTo(true)
  198. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  199. example.setOrderByClause("sort asc, id desc");
  200. List<Person> list =null;
  201. try{
  202. list=personMapper.selectByExample(example);
  203. }catch(Exception e){
  204. JshException.readFail(logger, e);
  205. }
  206. return list;
  207. }
  208. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  209. public int batchSetStatus(Boolean status, String ids)throws Exception {
  210. logService.insertLog("经手人",
  211. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ENABLED).toString(),
  212. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  213. List<Long> personIds = StringUtil.strToLongList(ids);
  214. Person person = new Person();
  215. person.setEnabled(status);
  216. PersonExample example = new PersonExample();
  217. example.createCriteria().andIdIn(personIds);
  218. int result=0;
  219. try{
  220. result = personMapper.updateByExampleSelective(person, example);
  221. }catch(Exception e){
  222. JshException.writeFail(logger, e);
  223. }
  224. return result;
  225. }
  226. }