123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace JinDouYun\Cache;
- use Exception;
- use Mall\Framework\Cache\Redis;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- class Stock
- {
-
- private $cache;
- private $onlineEnterpriseId;
- protected $StockKey = 'StockStatistics';
-
- private $serialNumKey = 'SerialNum';
-
- public function __construct($enterpriseId)
- {
- $this->onlineEnterpriseId = $enterpriseId;
- $this->cache = Factory::cache('mapping');
- }
-
- public function createCacheStatistics($value,$score,$key)
- {
- $result = $this->cache->zadd($this->StockKey.$key.'::'.$this->onlineEnterpriseId, $score, $value);
- if ($result == false){
- return false;
- }
- return $result;
- }
-
- public function getCacheStatistics($value,$key)
- {
- $result = $this->cache->ZSCORE($this->StockKey.$key.'::'.$this->onlineEnterpriseId,$value);
- return $result;
- }
-
- public function getCacheStatisticsCount($key)
- {
- $result = $this->cache->ZCARD($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
- return $result;
- }
-
- public function deleteCacheStatistics($key)
- {
- $result = $this->cache->del($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
- return $result;
- }
-
- public function createSerialSn(int $back = 0,string $fix = 'stock'): string
- {
- $key = $fix.$this->serialNumKey.'::'.$this->onlineEnterpriseId;
- $sn = $this->cache->get($key);
- $snDate = substr($sn,0,8);
- $snNo = intval(substr($sn,8));
- $curDate = date('Ymd');
- if($back == 1){
- if($curDate==$snDate){
- $snNo = ($snNo > 1 ) ? ( $snNo-1 ) : 1;
- $sn = $curDate.sprintf("%04d",$snNo);
- }
- }else{
- if(empty($sn)){
- $sn = $curDate.'000001';
- }else{
- $snNo = ($curDate==$snDate) ? ($snNo+1) : 1;
- $sn = $curDate.sprintf("%04d",$snNo);
- }
- }
- $this->cache->set($key,$sn);
- $sn = self::strInsert($sn,8,'-');
- return $sn;
- }
- private static function strInsert($str, $index, $sertstr)
- {
- return substr($str, 0, $index) . $sertstr . substr($str, $index);
- }
- }
|