SequenceServiceImpl.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.jsh.erp.service.impl;
  2. import com.jsh.erp.constants.BusinessConstants;
  3. import com.jsh.erp.datasource.mappers.*;
  4. import com.jsh.erp.exception.JshException;
  5. import com.jsh.erp.service.SequenceService;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import javax.annotation.Resource;
  11. /**
  12. * Description
  13. *
  14. * @Author: longyong
  15. * @Date: 2021/3/16 16:33
  16. */
  17. @Service
  18. public class SequenceServiceImpl implements SequenceService {
  19. private Logger logger = LoggerFactory.getLogger(SequenceServiceImpl.class);
  20. @Resource
  21. private SequenceMapperEx sequenceMapperEx;
  22. /**
  23. * 创建一个唯一的序列号
  24. * */
  25. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  26. @Override
  27. public String buildOnlyNumber()throws Exception{
  28. Long buildOnlyNumber=null;
  29. synchronized (this){
  30. try{
  31. sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.DEPOT_NUMBER_SEQ); //编号+1
  32. buildOnlyNumber= sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.DEPOT_NUMBER_SEQ);
  33. }catch(Exception e){
  34. JshException.writeFail(logger, e);
  35. }
  36. }
  37. if(buildOnlyNumber<BusinessConstants.SEQ_TO_STRING_MIN_LENGTH){
  38. StringBuffer sb=new StringBuffer(buildOnlyNumber.toString());
  39. int len=BusinessConstants.SEQ_TO_STRING_MIN_LENGTH.toString().length()-sb.length();
  40. for(int i=0;i<len;i++){
  41. sb.insert(0,BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
  42. }
  43. return sb.toString();
  44. }else{
  45. return buildOnlyNumber.toString();
  46. }
  47. }
  48. /**
  49. * 构建供应商编号
  50. */
  51. @Override
  52. public String buildSupplierNumber() {
  53. Long buildOnlyNumber = null;
  54. synchronized (this){
  55. sequenceMapperEx.updateBuildOnlyNumber(BusinessConstants.SUPPLIER_NUMBER_SEQ); //编号+1
  56. buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(BusinessConstants.SUPPLIER_NUMBER_SEQ);
  57. }
  58. if(buildOnlyNumber < BusinessConstants.SUPPLIER_SEQ_TO_STRING_MIN_LENGTH){
  59. StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
  60. int len = BusinessConstants.SUPPLIER_SEQ_TO_STRING_MIN_LENGTH.toString().length() - sb.length();
  61. for(int i=0; i<len; i++){
  62. sb.insert(0,BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
  63. }
  64. return sb.toString();
  65. }else{
  66. return buildOnlyNumber.toString();
  67. }
  68. }
  69. }