分账剂数还原
This commit is contained in:
@@ -97,54 +97,80 @@ class StoreController extends BaseAppController
|
||||
*/
|
||||
public function actionStoreChange()
|
||||
{
|
||||
$post=\Yii::$app->request->post();
|
||||
|
||||
$this->requestValidate($post,[
|
||||
['store_id','required']
|
||||
$post = \Yii::$app->request->post();
|
||||
$this->requestValidate($post, [
|
||||
['store_id', 'required']
|
||||
]);
|
||||
$t=\Yii::$app->db->beginTransaction();
|
||||
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$userId = \Yii::$app->user->identity->getId();
|
||||
|
||||
$store = Store::findOne($post['store_id']);
|
||||
if(!$store)throw new Exception('门店不存在');
|
||||
if (!$store) {
|
||||
throw new Exception('门店不存在');
|
||||
}
|
||||
|
||||
|
||||
StoreUser::updateAll(['is_online' => 0],[
|
||||
'user_id' => \Yii::$app->user->identity->getId(),
|
||||
// 先将该用户所有在线门店置为离线
|
||||
StoreUser::updateAll(['is_online' => 0], [
|
||||
'user_id' => $userId,
|
||||
'is_delete' => 0,
|
||||
'is_online' => 1
|
||||
]);
|
||||
|
||||
$StoreUser=StoreUser::find()->where([
|
||||
'user_id' => \Yii::$app->user->identity->getId(),
|
||||
// 先查找未删除的绑定记录
|
||||
$StoreUser = StoreUser::find()->where([
|
||||
'user_id' => $userId,
|
||||
'store_id' => $post['store_id'],
|
||||
'is_delete' => 0,
|
||||
])->one();
|
||||
if (!$StoreUser){//绑定门店
|
||||
|
||||
if (!$StoreUser) {
|
||||
// 未找到有效记录,尝试恢复已删除的同门店记录
|
||||
$deletedStoreUser = StoreUser::find()->where([
|
||||
'user_id' => $userId,
|
||||
'store_id' => $post['store_id'],
|
||||
'is_delete' => 1,
|
||||
])->one();
|
||||
|
||||
if ($deletedStoreUser) {
|
||||
// 恢复旧记录,避免唯一键冲突
|
||||
$StoreUser = $deletedStoreUser;
|
||||
$StoreUser->is_delete = 0;
|
||||
} else {
|
||||
// 完全新建绑定
|
||||
$StoreUser = new StoreUser();
|
||||
$StoreUser->store_id = $post['store_id'];
|
||||
$StoreUser->user_id = \Yii::$app->user->identity->getId();
|
||||
//如果是搜索小程序进入后扫码其他门店,删除默认门店数据
|
||||
$StoreUser->user_id = $userId;
|
||||
|
||||
StoreUser::updateAll(['is_delete' => 1],[
|
||||
'user_id' => \Yii::$app->user->identity->getId(),
|
||||
// 清除之前的默认门店(仅针对全新绑定)
|
||||
StoreUser::updateAll(['is_delete' => 1], [
|
||||
'user_id' => $userId,
|
||||
'is_delete' => 0,
|
||||
'type' => 1
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新登录信息并保存
|
||||
$StoreUser->last_login_time = time();
|
||||
$StoreUser->type=2;
|
||||
$StoreUser->is_online=1;
|
||||
$StoreUser->type = 2;
|
||||
$StoreUser->is_online = 1;
|
||||
$StoreUser->saveOrFail();
|
||||
|
||||
$user=User::find()->where(['id'=>\Yii::$app->user->identity->getId()])->one();
|
||||
if (!$user) throw new Exception('用户不存在');
|
||||
$user->current_store_id=$post['store_id'];
|
||||
// 更新用户当前门店
|
||||
$user = User::find()->where(['id' => $userId])->one();
|
||||
if (!$user) {
|
||||
throw new Exception('用户不存在');
|
||||
}
|
||||
$user->current_store_id = $post['store_id'];
|
||||
$user->saveOrFail();
|
||||
|
||||
$t->commit();
|
||||
return ['success'];
|
||||
}catch (\Exception $exception){
|
||||
} catch (\Exception $exception) {
|
||||
$t->rollBack();
|
||||
ds([1], $exception->getMessage());
|
||||
throw new Exception($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user