| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace common\services;
- use Yii;
- use common\models\xhOrderDayNum;
- use common\components\configDict;
- class xhOrderDayNumService {
- /**
- * 增加一条订单数
- */
- public static function addOne($time,$merchantId)
- {
- $order = self::getByIds($time, $merchantId);
- if(empty($order)){
- $data = ['time'=>$time,'riseNum'=>1,'totalNum'=>1,'merchantId'=>$merchantId];
- self::add($data);
- }else{
- $newRiseNum = $order['riseNum'] + 1;
- $newTotalNum = $order['totalNum'] + 1;
- $data = ['riseNum'=>$newRiseNum,'totalNum'=>$newTotalNum];
- self::updateByIds($time, $merchantId, $data);
- }
- return $data;
- }
- public static function updateByIds($time,$merchantId,$data)
- {
- xhOrderDayNum::updateByCondition(['time'=>$time,'merchantId'=>$merchantId], $data);
- self::getByIds($time, $merchantId);
- $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
- $params = [$cacheKey];
- if(!empty($data)){
- foreach($data as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- }
- public static function add($data)
- {
- $return = xhOrderDayNum::add($data);
- $merchantId = $return['merchantId'];
- $time = $return['time'];
- $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
- $params = [$cacheKey];
- if(!empty($return)){
- foreach($return as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $return;
- }
- public static function getByIds($time,$merchantId)
- {
- $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
- $order = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($order)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($order as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $orderArr = [];
- foreach($arr as $key => $val){
- $orderArr[$val['key']] = $val['value'];
- }
- unset($orderArr['info_already_get_by_sql']);
- return $orderArr;
- }
- $order = xhOrderDayNum::find()->where(['time'=>$time,'merchantId'=>$merchantId])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($order)){
- foreach($order as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $order;
- }
- }
|