| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- namespace addons\Community\common\service;
- use addons\Community\common\enums\ConfigEnum;
- use addons\Community\common\models\CommunityHead;
- use addons\Community\common\models\CommunityHeadApply;
- use addons\Community\common\models\CommunityHeadCheck;
- use common\enums\StatusEnum;
- use common\models\common\Region;
- use common\models\user\User;
- use yii\helpers\Json;
- class ApplyService
- {
- /**
- * @param int $mall_id
- * @param array $params
- * @return array|false|mixed|null
- */
- public function page(int $mall_id, array $params)
- {
- // 组织条件
- $user_ids = [];
- if (!empty($params["user_id"])) {
- $user_ids[] = $params["user_id"];
- }
- if (!empty($params["member"])) {
- $user_or[] = 'or';
- $user_or[] = ['like', 'mobile', $params['member']];
- $user_or[] = ['like', 'nickname', $params['member']];
- $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($user_or)->column();
- if (!empty($u_ids)) {
- $user_ids = array_merge($user_ids, $u_ids);
- }
- }
- $where = [];
- if (!empty($params['status'])) {
- $where[] = ['apply_status' => $params['status']];
- }
- if (!empty($params['name'])) {
- $where[] = ['like', 'name', $params['name']];
- }
- if (!empty($params['date_start']) && !empty($params['date_end'])) {
- $s = strtotime($params['date_start']);
- $e = strtotime($params['date_end']);
- $where[] = ['between', 'created_at', $s, $e];
- } elseif (!empty($params['date_start'])) {
- $s = strtotime($params['date_start']);
- $where[] = ['<=', 'created_at', $s];
- } elseif (!empty($params['date_end'])) {
- $e = strtotime($params['date_end']);
- $where[] = ['>=', 'created_at', $e];
- }
- if (!empty($user_ids)) $where[] = ['in', 'user_id', $user_ids];
- $where[] = ["mall_id" => $mall_id];
- $map["where"] = $where;
- $map["select"]= "created_at, user_id, id, name, intro, logo, license_pic, id_card_pic, contact_name, contact_phone, street, area, apply_status";
- $map["limit"] = $params["limit"];
- $map["page"] = $params["page"];
- $map["order"]= 'id desc';
- $map["with"] = ['user' => function($query) {
- $query->select('id,mobile,nickname,avatar_url');
- }, 'check' => function($query) {
- $query->orderBy('id desc');
- }];
- $ret = CommunityHeadApply::listPage($map);
- if (!empty(CommunityHeadApply::$static_error)) return CommunityHeadApply::$static_error;
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- $item['id_card_pic'] = Json::decode($item['id_card_pic']);
- }
- }
- return $ret;
- }
- /**
- * 审核
- * @param int $mall_id
- * @param array $params
- * @param int $admin_id
- * @return string|true
- */
- public function check(int $mall_id, array $params, int $admin_id)
- {
- $data = CommunityHeadApply::getOne([
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['id' => $params['id']]
- ]);
- if (!$data) return '记录不存在';
- if ($data->apply_status != ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) return '当前状态不可审核';
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data->apply_status = $params['status'];
- $data->check_count += 1;
- if (!$data->save()) throw new \Exception('审核失败');
- $check = new CommunityHeadCheck();
- $check->mall_id = $mall_id;
- $check->apply_id = $data->id;
- $check->check_status = $data->apply_status;
- $check->admin_id = $admin_id;
- $check->remark = $params['remark'];
- if (!$check->save()) throw new \Exception('记录日志失败' . $check->getErrorMessage());
- if ($data->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
- if (!CommunityHead::setData([
- 'contacts' => $data->contact_name,
- 'mobile' => $data->contact_phone,
- 'name' => $data->name,
- 'desc' => $data->intro,
- 'logo' => $data->logo,
- 'license_img' => $data->license_pic,
- 'idcard_img' => $data->id_card_pic,
- 'province_id' => $data->province_id,
- 'city_id' => $data->city_id,
- 'district_id' => $data->district_id,
- 'address' => "{$data->area}{$data->street}",
- 'longitude' => $data->lng,
- 'latitude' => $data->lat,
- 'check_status' => StatusEnum::ENABLED,
- 'mall_id' => $mall_id,
- 'user_id' => $data->user_id,
- 'is_home_delivery' => $data->is_home_delivery
- ], 0, true)) {
- throw new \Exception(CommunityHead::$static_error);
- }
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param int $mall_id
- * @param array $params
- * @param int $user_id
- * @return string|true
- */
- public function create(int $mall_id, array $params, int $user_id)
- {
- if (!empty($params['id'])) {
- $model = CommunityHeadApply::getOne([
- ['mall_id' => $mall_id],
- ['id' => $params['id']],
- ['status' => StatusEnum::ENABLED],
- ['user_id' => $user_id]
- ]);
- if (!$model) return '记录不存在';
- if (!in_array($model->apply_status, [ConfigEnum::COMMUNITY_CHECK_STATUS_REJECT,
- ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC])) return '当前状态不可修改';
- $model->submit_count += 1;
- } else {
- $model = new CommunityHeadApply();
- $model->user_id = $user_id;
- $model->mall_id = $mall_id;
- $model->submit_count = 1;
- }
- $model->name = $params['name'];
- $model->intro = $params['intro'];
- $model->logo = $params['logo'];
- $model->license_pic = $params['license_pic'];
- $model->id_card_pic = Json::encode($params['id_card_pic']);
- $model->contact_name = $params['contact_name'];
- $model->contact_phone = "{$params['contact_phone']}";
- $model->province_id = $params['province_id'];
- $model->city_id = $params['city_id'];
- $model->district_id = $params['district_id'] ?? 0;
- $model->street = $params['street'];
- $model->area = $params['area'];
- $model->lng = $params['lng'];
- $model->lat = $params['lat'];
- $model->is_home_delivery = $params['is_home_delivery'] ?? StatusEnum::DISABLED;
- $model->apply_status = ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC; // 修改为待审核
- if (!$model->save()) return $model->getErrorMessage();
- return true;
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @return string|true
- */
- public function createProtocol(int $mall_id, int $user_id)
- {
- // 判断是否已经有了
- $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('id, apply_status')->one();
- if ($model) {
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
- return '您提交的入驻申请还在审核中';
- }
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
- return '您已经是团长了';
- }
- return true;
- }
- $model = new CommunityHeadApply();
- $model->user_id = $user_id;
- $model->mall_id = $mall_id;
- $model->apply_status = StatusEnum::DISABLED;
- $model->step = 1;
- if (!$model->save()) return '入驻失败';
- return true;
- }
- /**
- * 入驻第二部,填写联系方式
- * @param int $mall_id
- * @param array $params
- * @param int $user_id
- * @return string|true
- */
- public function createContact(int $mall_id, array $params, int $user_id)
- {
- $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->one();
- if (empty($model)) {
- return '入驻记录不存在';
- }
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
- return '您提交的入驻申请还在审核中';
- }
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
- return '您已经是团长了';
- }
- $model->step = 2;
- $model->contact_name = $params['contact_name'];
- $model->contact_phone = $params['contact_phone'];
- $model->apply_status = StatusEnum::DISABLED;
- if (!$model->save()) return '提交失败';
- return true;
- }
- /**
- * 入驻第三步
- * @param int $mall_id
- * @param array $params
- * @param int $user_id
- * @return string|true
- */
- public function createHeadInfo(int $mall_id, array $params, int $user_id)
- {
- $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->one();
- if (empty($model)) return '入驻记录不存在';
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
- return '您提交的入驻申请还在审核中';
- }
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
- return '您已经是团长了';
- }
- $model->step = 3;
- $model->name = $params['name'];
- $model->intro = $params['intro'];
- $model->logo = $params['logo'];
- $model->license_pic = $params['license_pic'];
- $model->id_card_pic = Json::encode($params['id_card_pic']);
- $model->province_id = $params['province_id'];
- $model->city_id = $params['city_id'];
- $model->district_id = $params['district_id'];
- $model->street = $params['street'];
- $model->area = $params['area'];
- $model->lng = $params['lng'];
- $model->lat = $params['lat'];
- $model->is_home_delivery = $params['is_home_delivery'];
- $model->apply_status = StatusEnum::DISABLED;
- if (!$model->save()) return '提交失败';
- return true;
- }
- /**
- * 入驻第四步
- * @param int $mall_id
- * @param int $user_id
- * @return string|true
- */
- public function createSubmit(int $mall_id, int $user_id)
- {
- $model = CommunityHeadApply::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->one();
- if (empty($model)) return '入驻记录不存在';
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_BASIC) {
- return '您提交的入驻申请还在审核中';
- }
- if ($model->apply_status == ConfigEnum::COMMUNITY_CHECK_STATUS_AGREE) {
- return '您已经是团长了';
- }
- $model->step = 4;
- $model->apply_status = StatusEnum::ENABLED; // 更新至待审核状态
- if (!$model->save()) return '提交失败';
- return true;
- }
- /**
- * 获取详情
- * @param int $mall_id
- * @param int $user_id
- * @return array|mixed
- */
- public function fetch(int $mall_id, int $user_id)
- {
- $data = CommunityHeadApply::getOne([
- ['mall_id' => $mall_id],
- ['user_id' => $user_id],
- ['status' => StatusEnum::ENABLED]
- ], true, [], null,
- 'created_at, user_id, id, name, intro, logo, license_pic, id_card_pic,
- contact_name, contact_phone, street, area, apply_status, lng, lat, step');
- if (!empty($data)) {
- $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
- $data['apply_status_txt'] = ConfigEnum::COMMUNITY_CHECK_STATUS_REMARKS[$data['apply_status']];
- $data['id_card_pic'] = Json::decode($data['id_card_pic']);
- $data['remark'] = '';
- if($data['apply_status'] == ConfigEnum::COMMUNITY_CHECK_STATUS_REJECT){
- $data['remark'] = (string)CommunityHeadCheck::find()->where(['apply_id' => $data['id']])
- ->select('remark')->scalar();
- }
- }
- return $data;
- }
- }
|