CenterService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace addons\OperationCenter\api\services;
  3. use addons\OperationCenter\common\models\UserAffiliation;
  4. use addons\OperationCenter\common\models\UserCenter;
  5. use addons\OperationCenter\common\services\CenterService as ServicesCenterService;
  6. use Exception;
  7. use Yii;
  8. /**
  9. * CenterService
  10. * @package addons\OperationCenter\api\services
  11. */
  12. class CenterService extends ServicesCenterService
  13. {
  14. /**
  15. * 通过距离排序分页
  16. *
  17. * @param array $params
  18. * @return array
  19. */
  20. public function getPageListOrderByDistance(int $user_id, array $params = [])
  21. {
  22. $lat = $params['lat'];
  23. $lon = $params['lon'];
  24. $data = UserCenter::getPageList(
  25. $this->getPage($params), $this->getLimit($params), function ($query)
  26. use ($lat, $lon)
  27. {
  28. $query->select([
  29. 'id', 'center_name', 'province', 'city',
  30. 'district', 'town', 'operation_address', 'logo',
  31. UserCenter::getDistanceSql($lon, $lat)
  32. ]);
  33. $query->andWhere(['mall_id' => $this->mall_id]);
  34. }
  35. , 'distance ASC');
  36. // 当前
  37. $data['current'] = UserAffiliation::getUserAffiliationCenterByUserId($user_id, $lon, $lat);
  38. return $data;
  39. }
  40. /**
  41. * 选择运营中心
  42. *
  43. * @param integer $user_id
  44. * @param integer $operation_center_id
  45. * @return boolean
  46. */
  47. public function selectOperation(int $user_id, int $operation_center_id)
  48. {
  49. $t = Yii::$app->db->beginTransaction();
  50. try {
  51. $user_affiliation = UserAffiliation::getUserAffiliationByUserId($user_id);
  52. if (!empty($user_affiliation)) $user_affiliation->delSelect();
  53. UserAffiliation::addSelect($this->mall_id, $user_id, $operation_center_id);
  54. } catch (Exception $e) {
  55. $t->rollBack();
  56. return $this->error($e->getMessage());
  57. }
  58. $t->commit();
  59. return true;
  60. }
  61. /**
  62. * 当前选择的运营中心
  63. *
  64. * @param integer $user_id
  65. * @return array
  66. */
  67. public function currentOperation(int $user_id)
  68. {
  69. $user_affiliation = UserAffiliation::getUserAffiliationByUserId($user_id);
  70. if (empty($user_affiliation)) {
  71. return [];
  72. }
  73. $center = $user_affiliation->center;
  74. return empty($center) ? [] : [
  75. 'id' => $center->id,
  76. 'center_name' => $center->center_name,
  77. 'logo' => $center->logo,
  78. 'province' => $center->province,
  79. 'city' => $center->city,
  80. 'district' => $center->district,
  81. 'town' => $center->town,
  82. 'operation_address' => $center->operation_address,
  83. ];
  84. }
  85. /**
  86. * 是否强制运营中心
  87. *
  88. * @param integer $user_id
  89. * @return boolean
  90. */
  91. public function getOrderLimit(int $user_id)
  92. {
  93. if ($this->getSettingService()->getOrderLimit()) {
  94. $user_affiliation = UserAffiliation::getUserAffiliationByUserId($user_id);
  95. return empty($user_affiliation);
  96. }
  97. return false;
  98. }
  99. }