xhUserUniteService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace common\services;
  3. use common\components\util;
  4. use Yii;
  5. use common\components\stringUtil;
  6. use common\components\dict;
  7. use common\components\wxUtil;
  8. use common\models\xhUserUnite;
  9. class xhUserUniteService {
  10. public static function getByIds($alipayId,$sjId)
  11. {
  12. $cacheKey = dict::getCacheKey('xhUserUnite').'_'.$sjId.'_'.$alipayId;
  13. $user = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  14. if(!empty($user)){
  15. $arr = [];
  16. $i = 0;
  17. $x = 0;
  18. foreach($user as $key => $val){
  19. $i++;
  20. if($i%2 == 0){
  21. $arr[$x]['value'] = $val;
  22. $x++;
  23. }else{
  24. $arr[$x]['key'] = $val;
  25. }
  26. }
  27. $userArr = [];
  28. foreach($arr as $key => $val){
  29. $userArr[$val['key']] = $val['value'];
  30. }
  31. unset($userArr['info_already_get_by_sql']);
  32. return $userArr;
  33. }
  34. $user = xhUserUnite::find()->where(['alipayId' => $alipayId,'sjId'=>$sjId])->asArray()->one();
  35. $params = [$cacheKey];
  36. $params[] = 'info_already_get_by_sql';
  37. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  38. if(!empty($user)){
  39. foreach($user as $key => $val){
  40. $params[] = $key;
  41. $params[] = $val;
  42. }
  43. }
  44. Yii::$app->redis->executeCommand('HMSET',$params);
  45. return $user;
  46. }
  47. public static function add($data)
  48. {
  49. $return = xhUserUnite::add($data);
  50. $sjId = $return['sjId'];
  51. $alipayId = $return['alipayId'];
  52. $cacheKey = dict::getCacheKey('xhUserUnite').'_'.$sjId.'_'.$alipayId;
  53. $params = [$cacheKey];
  54. if(!empty($return)){
  55. foreach($return as $key => $val){
  56. $params[] = $key;
  57. $params[] = $val;
  58. }
  59. }
  60. Yii::$app->redis->executeCommand('HMSET',$params);
  61. return $return;
  62. }
  63. public static function updateByIds($alipayId,$sjId,$data)
  64. {
  65. xhUserUnite::updateByCondition(['alipayId'=>$alipayId,'sjId'=>$sjId], $data);
  66. self::getByIds($alipayId, $sjId);
  67. $cacheKey = dict::getCacheKey('xhUserUnite').'_'.$sjId.'_'.$alipayId;
  68. $params = [$cacheKey];
  69. if(!empty($data)){
  70. foreach($data as $key => $val){
  71. $params[] = $key;
  72. $params[] = $val;
  73. }
  74. }
  75. Yii::$app->redis->executeCommand('HMSET',$params);
  76. }
  77. }