Stock.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * 缓存库存数据
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/12/10
  7. * Time: 15:56
  8. */
  9. namespace JinDouYun\Cache;
  10. use Exception;
  11. use Mall\Framework\Cache\Redis;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Factory;
  15. class Stock
  16. {
  17. /**
  18. * @var Redis
  19. */
  20. private $cache;
  21. private $onlineEnterpriseId;
  22. protected $StockKey = 'StockStatistics';
  23. /**
  24. * @var string
  25. */
  26. private $serialNumKey = 'SerialNum';
  27. /**
  28. * GoodsBasicCache constructor.
  29. * @param $enterpriseId
  30. * @throws Exception
  31. */
  32. public function __construct($enterpriseId)
  33. {
  34. $this->onlineEnterpriseId = $enterpriseId;
  35. $this->cache = Factory::cache('mapping');
  36. }
  37. /**
  38. * 缓存统计
  39. * @param $value $缓存的字段名 value
  40. * @param $score $缓存的值 score
  41. * @param $key $缓存名字 key
  42. * @return mixed
  43. */
  44. public function createCacheStatistics($value,$score,$key)
  45. {
  46. $result = $this->cache->zadd($this->StockKey.$key.'::'.$this->onlineEnterpriseId, $score, $value);
  47. if ($result == false){
  48. return false;
  49. }
  50. return $result;
  51. }
  52. /**
  53. * 获取统计
  54. * @param $value $字段名 value
  55. * @param $key $表名 key
  56. * @return mixed
  57. */
  58. public function getCacheStatistics($value,$key)
  59. {
  60. $result = $this->cache->ZSCORE($this->StockKey.$key.'::'.$this->onlineEnterpriseId,$value);
  61. return $result;
  62. }
  63. /**
  64. * 获取key的个数
  65. * @param $key //字段名
  66. * @return mixed
  67. */
  68. public function getCacheStatisticsCount($key)
  69. {
  70. $result = $this->cache->ZCARD($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
  71. return $result;
  72. }
  73. /**
  74. * 删除统计
  75. * @param $key $表名
  76. * @return mixed
  77. */
  78. public function deleteCacheStatistics($key)
  79. {
  80. $result = $this->cache->del($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
  81. return $result;
  82. }
  83. /**
  84. * Doc: (des="创建单据当天的序号")
  85. * User: XMing
  86. * Date: 2020/11/6
  87. * Time: 10:24 上午
  88. * -当天从1开始自增
  89. * -订单号模样:20190604000001
  90. * @param int $back 序号回退,如果创建失败,事务回滚可用
  91. * @param string $fix
  92. * @return string
  93. */
  94. public function createSerialSn(int $back = 0,string $fix = 'stock'): string
  95. {
  96. $key = $fix.$this->serialNumKey.'::'.$this->onlineEnterpriseId;
  97. $sn = $this->cache->get($key);
  98. $snDate = substr($sn,0,8);
  99. $snNo = intval(substr($sn,8));
  100. $curDate = date('Ymd');
  101. if($back == 1){//序号回退
  102. if($curDate==$snDate){
  103. $snNo = ($snNo > 1 ) ? ( $snNo-1 ) : 1;
  104. $sn = $curDate.sprintf("%04d",$snNo);
  105. }
  106. }else{//序号增加
  107. if(empty($sn)){
  108. $sn = $curDate.'000001';
  109. }else{
  110. $snNo = ($curDate==$snDate) ? ($snNo+1) : 1;
  111. $sn = $curDate.sprintf("%04d",$snNo);
  112. }
  113. }
  114. $this->cache->set($key,$sn);
  115. $sn = self::strInsert($sn,8,'-');
  116. return $sn;
  117. }
  118. private static function strInsert($str, $index, $sertstr)
  119. {
  120. return substr($str, 0, $index) . $sertstr . substr($str, $index);
  121. }
  122. }