xhAdminToMerchantService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace common\services;
  3. use common\components\util;
  4. use Yii;
  5. use common\components\dict;
  6. use common\components\stringUtil;
  7. use linslin\yii2\curl;
  8. use common\components\wxUtil;
  9. use common\models\xhAdminToMerchant;
  10. class xhAdminToMerchantService {
  11. public static function getByAdminId($adminId)
  12. {
  13. return xhAdminToMerchant::find()->where(['adminId' => $adminId])->asArray()->one();
  14. }
  15. public static function add($data)
  16. {
  17. $relation = xhAdminToMerchant::add($data);
  18. $adminId = $relation['adminId'];
  19. $cacheKey = dict::getCacheKey('xhAdminToMerchant').'_'.$adminId;
  20. $params = [$cacheKey];
  21. if(!empty($relation)){
  22. foreach($relation as $key => $val){
  23. $params[] = $key;
  24. $params[] = $val;
  25. }
  26. }
  27. Yii::$app->redis->executeCommand('HMSET',$params);
  28. $sjId = $relation['sjId'];
  29. self::refreshByMerchantId($sjId);
  30. return $relation;
  31. }
  32. public static function getBySjId($sjId)
  33. {
  34. $cacheKey = dict::getCacheKey('xhAdminToMerchantList').'_'.$sjId;
  35. $relation = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  36. if(!empty($relation)){
  37. $arr = [];
  38. $i = 0;
  39. $x = 0;
  40. foreach($relation as $key => $val){
  41. $i++;
  42. if($i%2 == 0){
  43. $arr[$x]['value'] = $val;
  44. $x++;
  45. }else{
  46. $arr[$x]['key'] = $val;
  47. }
  48. }
  49. $relationArr = [];
  50. foreach($arr as $key => $val){
  51. $relationArr[$val['key']] = $val['value'];
  52. }
  53. return $relationArr;
  54. }
  55. $relation = xhAdminToMerchant::find()->where(['sjId' => $sjId])->asArray()->one();
  56. $params = [$cacheKey];
  57. if(!empty($relation)){
  58. foreach($relation as $key => $val){
  59. $params[] = $key;
  60. $params[] = $val;
  61. }
  62. Yii::$app->redis->executeCommand('HMSET',$params);
  63. }
  64. return $relation;
  65. }
  66. public static function getAdminByMerchantId($sjId)
  67. {
  68. $relation = self::getBySjId($sjId);
  69. $admin = [];
  70. if(!empty($relation)){
  71. $adminId = $relation['adminId'];
  72. $admin = xhAdminService::getById($adminId);
  73. }
  74. return $admin;
  75. }
  76. /**
  77. * 删除管理员
  78. * @param int $adminId
  79. */
  80. public static function delByAdminId($adminId)
  81. {
  82. xhAdminToMerchant::deleteByCondition(['adminId'=>$adminId]);
  83. $cacheKey = dict::getCacheKey('xhAdminToMerchant').'_'.$adminId;
  84. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  85. }
  86. /**
  87. * 更新商家的管理员缓存
  88. * @param int $sjId
  89. */
  90. public static function refreshByMerchantId($sjId)
  91. {
  92. $cacheKey = dict::getCacheKey('xhAdminToMerchantList').'_'.$sjId;
  93. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  94. }
  95. }