123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * 缓存库存数据
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/12/10
- * Time: 15:56
- */
- 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
- {
- /**
- * @var Redis
- */
- private $cache;
- private $onlineEnterpriseId;
- protected $StockKey = 'StockStatistics';
- /**
- * @var string
- */
- private $serialNumKey = 'SerialNum';
- /**
- * GoodsBasicCache constructor.
- * @param $enterpriseId
- * @throws Exception
- */
- public function __construct($enterpriseId)
- {
- $this->onlineEnterpriseId = $enterpriseId;
- $this->cache = Factory::cache('mapping');
- }
- /**
- * 缓存统计
- * @param $value $缓存的字段名 value
- * @param $score $缓存的值 score
- * @param $key $缓存名字 key
- * @return mixed
- */
- public function createCacheStatistics($value,$score,$key)
- {
- $result = $this->cache->zadd($this->StockKey.$key.'::'.$this->onlineEnterpriseId, $score, $value);
- if ($result == false){
- return false;
- }
- return $result;
- }
- /**
- * 获取统计
- * @param $value $字段名 value
- * @param $key $表名 key
- * @return mixed
- */
- public function getCacheStatistics($value,$key)
- {
- $result = $this->cache->ZSCORE($this->StockKey.$key.'::'.$this->onlineEnterpriseId,$value);
- return $result;
- }
- /**
- * 获取key的个数
- * @param $key //字段名
- * @return mixed
- */
- public function getCacheStatisticsCount($key)
- {
- $result = $this->cache->ZCARD($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
- return $result;
- }
- /**
- * 删除统计
- * @param $key $表名
- * @return mixed
- */
- public function deleteCacheStatistics($key)
- {
- $result = $this->cache->del($this->StockKey.$key.'::'.$this->onlineEnterpriseId);
- return $result;
- }
- /**
- * Doc: (des="创建单据当天的序号")
- * User: XMing
- * Date: 2020/11/6
- * Time: 10:24 上午
- * -当天从1开始自增
- * -订单号模样:20190604000001
- * @param int $back 序号回退,如果创建失败,事务回滚可用
- * @param string $fix
- * @return string
- */
- 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);
- }
- }
|