ApplyService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace addons\Community\common\service;
  3. use addons\Community\common\enums\ConfigEnum;
  4. use addons\Community\common\models\CommunityHead;
  5. use addons\Community\common\models\CommunityHeadApply;
  6. use addons\Community\common\models\CommunityHeadCheck;
  7. use common\enums\StatusEnum;
  8. use common\models\common\Region;
  9. use common\models\user\User;
  10. use yii\helpers\Json;
  11. class ApplyService
  12. {
  13. /**
  14. * @param int $mall_id
  15. * @param array $params
  16. * @return array|false|mixed|null
  17. */
  18. public function page(int $mall_id, array $params)
  19. {
  20. // 组织条件
  21. $user_ids = [];
  22. if (!empty($params["user_id"])) {
  23. $user_ids[] = $params["user_id"];
  24. }
  25. if (!empty($params["member"])) {
  26. $user_or[] = 'or';
  27. $user_or[] = ['like', 'mobile', $params['member']];
  28. $user_or[] = ['like', 'nickname', $params['member']];
  29. $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($user_or)->column();
  30. if (!empty($u_ids)) {
  31. $user_ids = array_merge($user_ids, $u_ids);
  32. }
  33. }
  34. $where = [];
  35. if (!empty($params['status'])) {
  36. $where[] = ['apply_status' => $params['status']];
  37. }
  38. if (!empty($params['name'])) {
  39. $where[] = ['like', 'name', $params['name']];
  40. }
  41. if (!empty($params['date_start']) && !empty($params['date_end'])) {
  42. $s = strtotime($params['date_start']);
  43. $e = strtotime($params['date_end']);
  44. $where[] = ['between', 'created_at', $s, $e];
  45. } elseif (!empty($params['date_start'])) {
  46. $s = strtotime($params['date_start']);
  47. $where[] = ['<=', 'created_at', $s];
  48. } elseif (!empty($params['date_end'])) {
  49. $e = strtotime($params['date_end']);
  50. $where[] = ['>=', 'created_at', $e];
  51. }
  52. if (!empty($user_ids)) $where[] = ['in', 'user_id', $user_ids];
  53. $where[] = ["mall_id" => $mall_id];
  54. $map["where"] = $where;
  55. $map["select"]= "created_at, user_id, id, name, intro, logo, license_pic, id_card_pic, contact_name, contact_phone, street, area, apply_status";
  56. $map["limit"] = $params["limit"];
  57. $map["page"] = $params["page"];
  58. $map["order"]= 'id desc';
  59. $map["with"] = ['user' => function($query) {
  60. $query->select('id,mobile,nickname,avatar_url');
  61. }, 'check' => function($query) {
  62. $query->orderBy('id desc');
  63. }];
  64. $ret = CommunityHeadApply::listPage($map);
  65. if (!empty(CommunityHeadApply::$static_error)) return CommunityHeadApply::$static_error;
  66. if (!empty($ret['list'])) {
  67. foreach ($ret['list'] as &$item) {
  68. $item['id_card_pic'] = Json::decode($item['id_card_pic']);
  69. }
  70. }
  71. return $ret;
  72. }
  73. /**
  74. * 审核
  75. * @param int $mall_id
  76. * @param array $params
  77. * @param int $admin_id
  78. * @return string|true
  79. */
  80. public function check(int $mall_id, array $params, int $admin_id)
  81. {
  82. $data = CommunityHeadApply::getOne([
  83. ['mall_id' => $mall_id],
  84. ['status' => StatusEnum::ENABLED],
  85. ['id' => $params['id']]
  86. ]);
  87. if (!$data) return '记录不存在';
  88. if ($data->apply_status != ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) return '当前状态不可审核';
  89. $t = \Yii::$app->db->beginTransaction();
  90. try {
  91. $data->apply_status = $params['status'];
  92. $data->check_count += 1;
  93. if (!$data->save()) throw new \Exception('审核失败');
  94. $check = new CommunityHeadCheck();
  95. $check->mall_id = $mall_id;
  96. $check->apply_id = $data->id;
  97. $check->check_status = $data->apply_status;
  98. $check->admin_id = $admin_id;
  99. $check->remark = $params['remark'];
  100. if (!$check->save()) throw new \Exception('记录日志失败' . $check->getErrorMessage());
  101. if ($data->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
  102. if (!CommunityHead::setData([
  103. 'contacts' => $data->contact_name,
  104. 'mobile' => $data->contact_phone,
  105. 'name' => $data->name,
  106. 'desc' => $data->intro,
  107. 'logo' => $data->logo,
  108. 'license_img' => $data->license_pic,
  109. 'idcard_img' => $data->id_card_pic,
  110. 'province_id' => $data->province_id,
  111. 'city_id' => $data->city_id,
  112. 'district_id' => $data->district_id,
  113. 'address' => "{$data->area}{$data->street}",
  114. 'longitude' => $data->lng,
  115. 'latitude' => $data->lat,
  116. 'check_status' => StatusEnum::ENABLED,
  117. 'mall_id' => $mall_id,
  118. 'user_id' => $data->user_id,
  119. 'is_home_delivery' => $data->is_home_delivery
  120. ], 0, true)) {
  121. throw new \Exception(CommunityHead::$static_error);
  122. }
  123. }
  124. $t->commit();
  125. } catch (\Exception $e) {
  126. $t->rollBack();
  127. return $e->getMessage();
  128. }
  129. return true;
  130. }
  131. /**
  132. * @param int $mall_id
  133. * @param array $params
  134. * @param int $user_id
  135. * @return string|true
  136. */
  137. public function create(int $mall_id, array $params, int $user_id)
  138. {
  139. if (!empty($params['id'])) {
  140. $model = CommunityHeadApply::getOne([
  141. ['mall_id' => $mall_id],
  142. ['id' => $params['id']],
  143. ['status' => StatusEnum::ENABLED],
  144. ['user_id' => $user_id]
  145. ]);
  146. if (!$model) return '记录不存在';
  147. if (!in_array($model->apply_status, [ConfigEnum::COMMUNITY_CHECK_STATUS_REJECT,
  148. ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC])) return '当前状态不可修改';
  149. $model->submit_count += 1;
  150. } else {
  151. $model = new CommunityHeadApply();
  152. $model->user_id = $user_id;
  153. $model->mall_id = $mall_id;
  154. $model->submit_count = 1;
  155. }
  156. $model->name = $params['name'];
  157. $model->intro = $params['intro'];
  158. $model->logo = $params['logo'];
  159. $model->license_pic = $params['license_pic'];
  160. $model->id_card_pic = Json::encode($params['id_card_pic']);
  161. $model->contact_name = $params['contact_name'];
  162. $model->contact_phone = "{$params['contact_phone']}";
  163. $model->province_id = $params['province_id'];
  164. $model->city_id = $params['city_id'];
  165. $model->district_id = $params['district_id'] ?? 0;
  166. $model->street = $params['street'];
  167. $model->area = $params['area'];
  168. $model->lng = $params['lng'];
  169. $model->lat = $params['lat'];
  170. $model->is_home_delivery = $params['is_home_delivery'] ?? StatusEnum::DISABLED;
  171. $model->apply_status = ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC; // 修改为待审核
  172. if (!$model->save()) return $model->getErrorMessage();
  173. return true;
  174. }
  175. /**
  176. * @param int $mall_id
  177. * @param int $user_id
  178. * @return string|true
  179. */
  180. public function createProtocol(int $mall_id, int $user_id)
  181. {
  182. // 判断是否已经有了
  183. $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
  184. ->andWhere(['user_id' => $user_id])
  185. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  186. ->select('id, apply_status')->one();
  187. if ($model) {
  188. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
  189. return '您提交的入驻申请还在审核中';
  190. }
  191. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
  192. return '您已经是团长了';
  193. }
  194. return true;
  195. }
  196. $model = new CommunityHeadApply();
  197. $model->user_id = $user_id;
  198. $model->mall_id = $mall_id;
  199. $model->apply_status = StatusEnum::DISABLED;
  200. $model->step = 1;
  201. if (!$model->save()) return '入驻失败';
  202. return true;
  203. }
  204. /**
  205. * 入驻第二部,填写联系方式
  206. * @param int $mall_id
  207. * @param array $params
  208. * @param int $user_id
  209. * @return string|true
  210. */
  211. public function createContact(int $mall_id, array $params, int $user_id)
  212. {
  213. $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
  214. ->andWhere(['user_id' => $user_id])
  215. ->andWhere(['status' => StatusEnum::ENABLED])
  216. ->one();
  217. if (empty($model)) {
  218. return '入驻记录不存在';
  219. }
  220. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
  221. return '您提交的入驻申请还在审核中';
  222. }
  223. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
  224. return '您已经是团长了';
  225. }
  226. $model->step = 2;
  227. $model->contact_name = $params['contact_name'];
  228. $model->contact_phone = $params['contact_phone'];
  229. $model->apply_status = StatusEnum::DISABLED;
  230. if (!$model->save()) return '提交失败';
  231. return true;
  232. }
  233. /**
  234. * 入驻第三步
  235. * @param int $mall_id
  236. * @param array $params
  237. * @param int $user_id
  238. * @return string|true
  239. */
  240. public function createHeadInfo(int $mall_id, array $params, int $user_id)
  241. {
  242. $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
  243. ->andWhere(['user_id' => $user_id])
  244. ->andWhere(['status' => StatusEnum::ENABLED])
  245. ->one();
  246. if (empty($model)) return '入驻记录不存在';
  247. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
  248. return '您提交的入驻申请还在审核中';
  249. }
  250. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
  251. return '您已经是团长了';
  252. }
  253. $model->step = 3;
  254. $model->name = $params['name'];
  255. $model->intro = $params['intro'];
  256. $model->logo = $params['logo'];
  257. $model->license_pic = $params['license_pic'];
  258. $model->id_card_pic = Json::encode($params['id_card_pic']);
  259. $model->province_id = $params['province_id'];
  260. $model->city_id = $params['city_id'];
  261. $model->district_id = $params['district_id'];
  262. $model->street = $params['street'];
  263. $model->area = $params['area'];
  264. $model->lng = $params['lng'];
  265. $model->lat = $params['lat'];
  266. $model->is_home_delivery = $params['is_home_delivery'];
  267. $model->apply_status = StatusEnum::DISABLED;
  268. if (!$model->save()) return '提交失败';
  269. return true;
  270. }
  271. /**
  272. * 入驻第四步
  273. * @param int $mall_id
  274. * @param int $user_id
  275. * @return string|true
  276. */
  277. public function createSubmit(int $mall_id, int $user_id)
  278. {
  279. $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
  280. ->andWhere(['user_id' => $user_id])
  281. ->andWhere(['status' => StatusEnum::ENABLED])
  282. ->one();
  283. if (empty($model)) return '入驻记录不存在';
  284. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
  285. return '您提交的入驻申请还在审核中';
  286. }
  287. if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
  288. return '您已经是团长了';
  289. }
  290. $model->step = 4;
  291. $model->apply_status = StatusEnum::ENABLED; // 更新至待审核状态
  292. if (!$model->save()) return '提交失败';
  293. return true;
  294. }
  295. /**
  296. * 获取详情
  297. * @param int $mall_id
  298. * @param int $user_id
  299. * @return array|mixed
  300. */
  301. public function fetch(int $mall_id, int $user_id)
  302. {
  303. $data = CommunityHeadApply::getOne([
  304. ['mall_id' => $mall_id],
  305. ['user_id' => $user_id],
  306. ['status' => StatusEnum::ENABLED]
  307. ], true, [], null,
  308. 'created_at, user_id, id, name, intro, logo, license_pic, id_card_pic,
  309. contact_name, contact_phone, street, area, apply_status, lng, lat, step');
  310. if (!empty($data)) {
  311. $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
  312. $data['apply_status_txt'] = ConfigEnum::COMMUNITY_CHECK_STATUS_REMARKS[$data['apply_status']];
  313. $data['id_card_pic'] = Json::decode($data['id_card_pic']);
  314. $data['remark'] = '';
  315. if($data['apply_status'] == ConfigEnum::COMMUNITY_CHECK_STATUS_REJECT){
  316. $data['remark'] = (string)CommunityHeadCheck::find()->where(['apply_id' => $data['id']])
  317. ->select('remark')->scalar();
  318. }
  319. }
  320. return $data;
  321. }
  322. }