delivery_type; if (empty($data)) { $data = ['express', 'shop']; } $this->delivery_type = Json::encode($data); }], ['self_delivery_type', function ($attr, $params) { $data = $this->self_delivery_type; if (empty($data)) { $data = ['express', 'shop']; } $this->self_delivery_type = Json::encode($data); }], [['is_set_distance', 'is_single', 'store_id', 'is_delete', 'province', 'city', 'district', 'user_id', 'shop_audit', 'shop_time_type', 'is_time_forbid', 'open_status','status', 'cat_id'], 'integer'], [['longitude', 'latitude', 'cover_url', 'pic_url', 'contact', 'manager_name', 'delivery', 'user_name', 'password'], 'string'], [['name', 'mobile', 'address', 'refuse_desc', 'start_time', 'end_time'], 'string', 'max' => 255], ['is_delete', 'default', 'value' => 0], [['rate', 'clerk_rate', 'distance'], 'number'], ['shop_audit', 'default', 'value' => 1], ['rate', 'default', 'value' => 0], // 操作 [['name', 'contact', 'mobile', 'longitude', 'latitude'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]], [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]], ['id', 'string', 'on' => self::SCENARIO_DEL], [['id', 'shop_audit'], 'required', 'on' => self::SCENARIO_AUDIT], [['search_key', 'shop_audit_status'], 'string', 'on' => self::SCENARIO_LIST], [['delivery_type', 'self_delivery_type', 'saas_id'], 'safe'] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'user_id' => '用户id', 'name' => 'Name', 'mobile' => 'Mobile', 'address' => 'Address', 'is_delete' => 'Is Delete', 'longitude' => 'Longitude', 'latitude' => 'Latitude', 'cover_url' => '自提点大图', 'pic_url' => '自提点小图', 'shop_time' => '营业时间', 'province' => '省份', 'city' => '城市', 'district' => '区县', 'shop_audit' => '审核状态(-1未通过,0待审核,1通过)', 'refuse_desc' => '拒接原因', ]; } public function scenarios() { $scenarios = parent::scenarios(); return $scenarios; } public function saveMd() { if ($this->validate()) { $t = \Yii::$app->db->beginTransaction(); $user_id = 0; $audit_status = 0; $refuse_desc = null; $is_new = false; if ($this->scenario == self::SCENARIO_EDIT) { $model = Md::findOne(['id' => $this->id]); $user_id = $model->user_id; $audit_status = $model->shop_audit; $refuse_desc = $model->refuse_desc; } else { $model = new Md(); $is_new = true; $user_id = $this->saas_id; $admin = Admin::find()->where(['username' => $this->user_name, 'is_delete' => 0])->one(); $md = Md::find()->where(['store_id' => $this->store_id, 'user_name' => $this->user_name, 'is_delete' => 0])->one(); if (!empty($admin) || !empty($md)) { return [ 'code' => 1, 'msg' => '用户名已经存在,请更换' ]; } $md_staff = MdStaff::findOne(['saas_user_id' => $this->saas_id, 'is_delete' => 0, 'store_id' => $this->store_id]); if ($md_staff) { return [ 'code' => 1, 'msg' => '由于您属于其他门店员工,暂时无法创建门店' ]; } $admin = Md::find()->where(['store_id' => $this->store_id, 'mobile' => $this->mobile, 'is_delete' => 0])->andWhere(['in', 'shop_audit', [0, 1]])->one(); if ($admin) { return [ 'code' => 1, 'msg' => '当前注册手机号店铺已存在或存在待审核记录' ]; } $this->rate = 0; $this->clerk_rate = 0; $this->distance = 0; $audit_status = Md::SHOP_AUDIT_YES; $model->manager = $user_id; } $this->user_id = $user_id; $this->refuse_desc = $refuse_desc; $this->shop_audit = $audit_status; $user_name = $this->user_name; $password = $this->password; unset($this->attributes['password']); unset($this->attributes['user_name']); $model->attributes = $this->attributes; if ($is_new && empty($this->self_delivery_type)) { $model->self_delivery_type = Json::encode(['express', 'shop']); } if (!$is_new && !empty($this->delivery_type)) { $model->delivery_type = $this->delivery_type; } if (!$is_new && !empty($this->self_delivery_type)) { $model->self_delivery_type = $this->self_delivery_type; } if (isset($this->cat_id) && $this->cat_id > 0) { $mdCategory = MdCategory::getList($model->store_id, $this->cat_id); if (!$mdCategory) { return [ 'code' => 1, 'msg' => '门店分类不存在' ]; } $model->cat_id = $this->cat_id; } if (!$model->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $model->errors[0] ]; } if ($this->scenario == self::SCENARIO_EDIT) { $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->andWhere(['NOT', ['type' => 'md', 'type_id' => $this->id]])->one(); } else { $user_admin = Admin::find()->where(['username' => $user_name, 'is_delete' => 0])->one(); } if ($user_admin) { $t->rollBack(); return [ 'code' => 1, 'msg' => '登录账户已存在,请更换账户名' ]; } $admin = Admin::find()->where(['type' => 'md', 'type_id' => $model->id, 'is_delete' => 0])->one(); if ($is_new || empty($admin)) { // 自动创建admin账号 $admin = new Admin(); $admin->store_id = get_store_id(); $admin->access_token = \Yii::$app->security->generateRandomString(); $admin->mobile = $model->mobile; $admin->avatar = $model->cover_url; $admin->name = $model->name; $admin->type = 'md'; $admin->type_id = $model->id; } $admin->username = $user_name ?: $model->mobile; $admin->password = \Yii::$app->security->generatePasswordHash(empty($password) ? $model->mobile : $password); if ($admin->save()) { \Yii::warning(['创建admin账号成功', $admin->id]); } else { \Yii::warning(['创建admin账号错误', $admin->errors[0]]); } //如果为新增就增加店长 if ($is_new) { $md_staff = new MdStaff(); $md_staff->store_id = $this->store_id; $md_staff->md_id = $model->id; $md_staff->saas_user_id = $user_id; $md_staff->name = $this->contact; $md_staff->mobile = $model->mobile; $md_staff->is_manager = 1; if (!$md_staff->save()) { return $this->asJson([ 'code' => 1, 'msg' => $md_staff->errors[0] ]); } } $t->commit(); return [ 'code' => 0, 'msg' => '保存成功' ]; } else { // 验证失败:$errors 是一个包含错误信息的数组 return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } } public function searchMd() { $query = Md::find()->alias('m')->leftJoin(['su' => SaasUser::tableName()], 'su.id=m.manager'); $query->where(['m.store_id' => get_store_id(), 'm.is_delete' => Md::IS_DELETE_NO])->orderBy("m.created_at desc,m.updated_at desc"); if ($this->name) { $query->andWhere(['like', 'm.name', $this->name]); } if ($this->manager_name) { $query->andWhere(['like', 'su.name', $this->manager_name]); } if ($this->delivery) { $query->andWhere(['like', 'm.delivery_type', $this->delivery]); } if ($this->cat_id) { $query->andWhere(['m.cat_id' => $this->cat_id]); } if ($this->mobile) { $query->andWhere(['LIKE', 'm.mobile', $this->mobile]); } if (in_array($this->is_single, [0, 1]) && !is_null($this->is_single)) { $query->andWhere(['m.is_single' => $this->is_single]); } // HKG 修改查询方式 switch ($this->status){ case 1: $query->andWhere(['m.shop_audit'=>0]); break; case 2: $query->andWhere(['m.shop_audit'=>2]); break; case 3: $query->andWhere(['m.open_status'=>1]); break; case 4: $query->andWhere(['m.open_status'=>0]); break; default: break; } // if (in_array($this->open_status, [0, 1]) && !is_null($this->open_status)) { // $query->andWhere(['m.open_status' => $this->open_status]); // } // HKG 时间筛选 if(!empty($this->start_time)){ $query->andWhere(['>=','m.created_at',strtotime($this->start_time)]); } if(!empty($this->end_time)){ $query->andWhere(['<=','m.created_at',strtotime($this->end_time)]); } $list = pagination_make($query); foreach ($list['list'] as &$val) { $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']); $saasUser = SaasUser::findOne($val['manager']); $user = User::find()->where(['store_id' => get_store_id(),'binding' => $saasUser->mobile])->one(); $val['manager_name'] = $user ? $user->nickname : '-'; $val['manager_avatar_url'] = $user ? $user->avatar_url : '-'; $val['manager_mobile'] = $user ? $user->binding : '-'; $val['detail_address'] = $val['address']; $province = District::findOne($val['province']); $city = District::findOne($val['city']); $district = District::findOne($val['district']); if ($province && $city && $district) { $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address']; } $val['sale_profit'] = MdProfit::find()->where(['md_id' => $val['id'], 'is_send' => 1])->sum('sale_profit') ?: 0; $val['cat_name'] = ''; $mdCategory = MdCategory::getList($val['store_id'], $val['cat_id'] ?: -1); if ($mdCategory) { $val['cat_name'] = $mdCategory[0]['name'] ?: ''; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ] ]; } public function searchAudit() { $query = Md::find()->alias('m'); $query->where(['store_id' => get_store_id(), 'is_delete' => 0])->orderBy("created_at desc,updated_at desc"); if ($this->name) { $query->andWhere(['like', 'name', $this->name]); } if ($this->mobile) { $query->andWhere(['mobile' => $this->mobile]); } if (in_array($this->shop_audit, [0, 1, 2]) && !is_null($this->shop_audit)) { $query->andWhere(['shop_audit' => $this->shop_audit]); } $list = pagination_make($query); foreach ($list['list'] as &$val) { $val['detail_address'] = $val['address']; $province = District::findOne($val['province']); $city = District::findOne($val['city']); $district = District::findOne($val['district']); if ($province && $city && $district) { $val['detail_address'] = $province->name . $city->name . $district->name . $val['detail_address']; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ] ]; } public function delMd() { if ($this->validate()) { $t = \Yii::$app->db->beginTransaction(); if ($this->scenario !== self::SCENARIO_DEL) { return [ 'code' => 1, 'msg' => '删除失败' ]; } $model = Md::findOne(['id' => $this->id]); if($model->is_single){ $model->is_single = 0; if (!$model->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => array_shift($model->getFirstErrors()), ]; } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } $model->is_delete = Md::IS_DELETE_YES; if (!$model || !$model->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $model->getErrorSummary(false)[0] ]; } // 删除门店员工 $staff = MdStaff::find()->where(['md_id' => $this->id,'is_delete'=>0])->asArray()->all(); if (!empty($staff)) { $update_num = MdStaff::updateAll(['is_delete' => 1], ['in', 'id', array_column($staff, 'id')]); if ($update_num != count($staff)) { $t->rollBack(); return [ 'code' => 1, 'msg' => '门店员工数据删除失败' ]; } } // Admin $admin = Admin::findOne(['type_id' => $model->id, 'type' => 'md', 'is_delete' => 0]); if ($admin) { $admin->is_delete = 1; if (!$admin->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '请求错误' ]; } } $t->commit(); return [ 'code' => 0, 'msg' => '删除成功' ]; // 所有输入数据都有效 all inputs are valid } else { // 验证失败:$errors 是一个包含错误信息的数组 return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } } public function auditMd() { if ($this->validate()) { $t = \Yii::$app->db->beginTransaction(); if ($this->scenario !== self::SCENARIO_AUDIT) { return [ 'code' => 1, 'msg' => '操作失败' ]; } $model = Md::findOne(['id' => $this->id]); if (!$model || !in_array($this->shop_audit, [$model::SHOP_AUDIT_NO, $model::SHOP_AUDIT_YES])) { return [ 'code' => 1, 'msg' => '操作失败' ]; } $model->shop_audit = $this->shop_audit; if (!$model->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $model->errors[0] ]; } $t->commit(); return [ 'code' => 0, 'msg' => $this->id ]; // 所有输入数据都有效 all inputs are valid } else { // 验证失败:$errors 是一个包含错误信息的数组 return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-15 * @desc: 获取自提点列表 */ public static function getMdList($store_id = 0, $field = '', $filter_md_id = []) { $field = $field ?: '*'; $query = Md::find()->where(['store_id' => $store_id ?: get_store_id(), 'is_delete' => Md::IS_DELETE_NO]); if (!empty($filter_md_id)) { $query->andWhere(['NOT IN', 'id', $filter_md_id]); } return $query->orderBy('id desc')->select($field)->asArray()->all(); } }