HeadService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\models\CommunityHead;
  4. use common\enums\StatusEnum;
  5. use common\models\common\Region;
  6. use yii\helpers\Json;
  7. class HeadService
  8. {
  9. public function update(int $mall_id, array $params, int $user_id)
  10. {
  11. $data = CommunityHead::getOne([
  12. ['mall_id' => $mall_id],
  13. ['user_id' => $user_id],
  14. ['status' => StatusEnum::ENABLED]
  15. ]);
  16. if (empty($data)) return '您还没有入驻';
  17. $data->mobile = "{$params['contact_phone']}";
  18. $data->desc = $params['intro'];
  19. $data->name = $params['name'];
  20. $data->logo = $params['logo'];
  21. $data->license_img = $params['license_img'];
  22. $data->idcard_img = Json::encode($params['id_card_img']);
  23. $data->province_id = $params['province_id'];
  24. $data->city_id = $params['city_id'];
  25. $data->district_id = $params['district_id'];
  26. $data->longitude = "{$params['lng']}";
  27. $data->latitude = "{$params['lat']}";
  28. $data->address = $params['street'];
  29. if (!$data->save()) {
  30. \Yii::error($data->getErrorMessage());
  31. return '修改失败';
  32. }
  33. return true;
  34. }
  35. /**
  36. * @param int $mall_id
  37. * @param int $user_id
  38. * @return array|mixed
  39. */
  40. public function detail(int $mall_id, int $user_id)
  41. {
  42. $data = CommunityHead::getOne([
  43. ['mall_id' => $mall_id],
  44. ['user_id' => $user_id],
  45. ['status' => StatusEnum::ENABLED]
  46. ], true, [], null, 'id, created_at, contacts, mobile, name, desc, logo, license_img, idcard_img, province_id,
  47. city_id, district_id, address, longitude, latitude');
  48. if (!empty($data)) {
  49. $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
  50. $data['contact_name'] = $data['contacts'];
  51. $data['contact_phone'] = $data['mobile'];
  52. $data['intro'] = $data['desc'];
  53. $data['id_card_img'] = Json::decode($data['idcard_img']);
  54. $data['street'] = $data['address'];
  55. $data['lng'] = $data['longitude'];
  56. $data['lat'] = $data['latitude'];
  57. unset($data['contacts'], $data['mobile'], $data['desc'], $data['address'], $data['idcard_img'], $data['longitude'], $data['latitude']);
  58. }
  59. return $data;
  60. }
  61. /**
  62. * @param int $mall_id
  63. * @param int $user_id
  64. * @return int
  65. */
  66. public function existByUserId(int $mall_id, int $user_id): int
  67. {
  68. $cnt = CommunityHead::find()->where(['mall_id' => $mall_id])
  69. ->andWhere(['user_id' => $user_id])
  70. ->andWhere(['status' => StatusEnum::ENABLED])
  71. ->count();
  72. return $cnt > 0 ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  73. }
  74. /**
  75. * 获取团长信息
  76. * @param int $mall_id
  77. * @param int $head_id
  78. * @return mixed|null
  79. */
  80. public function pickupInfo(int $mall_id, int $head_id)
  81. {
  82. return CommunityHead::getOne([
  83. ['mall_id' => $mall_id],
  84. ['id' => $head_id],
  85. ['status' => StatusEnum::ENABLED],
  86. ['check_status' => StatusEnum::ENABLED]
  87. ], true, [], false, 'address, contacts as contact_name, mobile as contact_phone, name, logo, longitude as lng, latitude as lat');
  88. }
  89. /**
  90. * 订单详情获取团长信息
  91. * @param int $mall_id
  92. * @param int $head_id
  93. * @return mixed|null
  94. */
  95. public function pickupInfoOrder(int $mall_id, int $head_id)
  96. {
  97. return CommunityHead::getOne([
  98. ['mall_id' => $mall_id],
  99. ['id' => $head_id],
  100. ['check_status' => StatusEnum::ENABLED]
  101. ], true, [], false, 'address, contacts as contact_name, mobile as contact_phone, name, logo, longitude as lng, latitude as lat');
  102. }
  103. /**
  104. * 获取自提点信息
  105. * @param int $mall_id
  106. * @param int $head_id
  107. * @return mixed|null
  108. */
  109. public function getHeadInfo(int $mall_id, int $head_id)
  110. {
  111. $head = CommunityHead::getOne([
  112. ['mall_id' => $mall_id],
  113. ['id' => $head_id],
  114. ['status' => StatusEnum::ENABLED],
  115. ['check_status' => StatusEnum::ENABLED]
  116. ], true, [], false, 'province_id, city_id, district_id, address');
  117. if (!empty($head)) {
  118. $head['province'] = Region::find()->where(['id' => $head['province_id']])->select('name')->scalar() ?? '';
  119. $head['city'] = Region::find()->where(['id' => $head['city_id']])->select('name')->scalar() ?? '';
  120. $head['district'] = Region::find()->where(['id' => $head['district_id']])->select('name')->scalar() ?? '';
  121. }
  122. return $head;
  123. }
  124. /**
  125. * @param int $mall_id
  126. * @param int $head_id
  127. * @return bool
  128. */
  129. public static function validHeadExist(int $mall_id, int $head_id)
  130. {
  131. return !empty(CommunityHead::find()->where(['mall_id' => $mall_id])
  132. ->andWhere(['id' => $head_id])
  133. ->andWhere(['status' => StatusEnum::ENABLED])
  134. ->andWhere(['check_status' => StatusEnum::ENABLED])
  135. ->one());
  136. }
  137. }