| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use linslin\yii2\curl;
- use common\components\wxUtil;
- use common\models\xhSetMeal;
- class xhSetMealService {
- /**
- * 随机取一个套餐
- */
- public static function getRandOne()
- {
- $set = xhSetMeal::find()->asArray()->one();
- return $set;
- }
- public static function getById($id)
- {
- $cacheKey = dict::getCacheKey('xhSet').$id;
- $set = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($set)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($set as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $setArr = [];
- foreach($arr as $key => $val){
- $setArr[$val['key']] = $val['value'];
- }
- unset($setArr['info_already_get_by_sql']);
- return $setArr;
- }
- $set = xhSetMeal::find()->where(['id' => $id])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($set)){
- foreach($set as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $set;
- }
- public static function add($data)
- {
- $return = xhSetMeal::add($data);
- $id = $return['id'];
- $cacheKey = dict::getCacheKey('xhSet').$id;
- $params = [$cacheKey];
- if(!empty($return)){
- foreach($return as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $return;
- }
- }
|