date("Y"),'month'=>date("m"),'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($year,$month,$merchantId, $data); } return $data; } public static function updateByIds($year,$month,$merchantId,$data) { xhOrderMonthNum::updateByCondition(['year'=>$year,'month'=>$month,'merchantId'=>$merchantId], $data); self::getByIds($year,$month,$merchantId); $cacheKey = configDict::getCacheKey('xhOrderMonthNum').'_'.$year.'_'.$month.'_'.$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 = xhOrderMonthNum::add($data); $merchantId = $return['merchantId']; $year = $return['year']; $month = $return['month']; $cacheKey = configDict::getCacheKey('xhOrderMonthNum').'_'.$year.'_'.$month.'_'.$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($year,$month,$merchantId) { $cacheKey = configDict::getCacheKey('xhOrderMonthNum').'_'.$year.'_'.$month.'_'.$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 = xhOrderMonthNum::find()->where(['year'=>$year,'month'=>$month,'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; } }