cxfoot/modules/admin/models/detection/DetectionEditForm.php
2023-10-24 14:54:18 +08:00

167 lines
5.4 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-5
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\models\detection;
use app\components\EncryptHelper;
use app\models\common\CommonUserEditForm;
use app\models\Model;
use app\models\User;
use app\modules\admin\models\AdminModel;
class DetectionEditForm extends AdminModel
{
public $model;
public $name;
public $mobile_phone;
public $store_id;
public $user_id;
public $created_at;
public $updated_at;
public $is_delete;
public $deleted_at;
public function rules()
{
return [
[['name','mobile_phone'], 'required'],
[['store_id', 'user_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
[['name'], 'string', 'max' => 50],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => '检测员名称',
'store_id' => '门店id',
'user_id' => '用户id',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'is_delete' => 'Is Delete',
'deleted_at' => 'Deleted At',
];
}
public function edit()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
if($this->model->isNewRecord){
$this->model->is_delete = 0;
$this->model->deleted_at = 0;
$this->model->created_at = time();
}
$r_u = self::saveUser();
if($r_u['code'] != 0){
$t->rollBack();
return $r_u;
}
$this->model->name = $this->name;
$this->model->store_id = $this->store_id;
$this->model->updated_at = time();
if(!$this->model->save()){
$t->rollBack();
return $this->getModelError($this->model);
}
$t->commit();
return $this->apiReturnSuccess('保存成功');
}
private function saveUser()
{
$mobile_phone = EncryptHelper::encryptMobilePhone($this->mobile_phone);
$model = User::findOne([
'is_delete' => 0,
'mobile_phone' => $mobile_phone
]);
if($this->model->isNewRecord){
if($model == null){
$form = new CommonUserEditForm();
$form->scenario = 'detection_add';
$form->model = new User();
$form->cx_mch_id = 0;
$form->username = User::generateUsername();
$form->password = \Yii::$app->security->generatePasswordHash('12345678');
$form->mobile_phone = $this->mobile_phone;
$form->mobile_prefix = '86';
$form->nickname = $this->name;
$form->real_name = $this->name;
$form->avatar_url = User::DEFAULT_AVATAR_URL;
$form->access_token = \Yii::$app->security->generateRandomString();
$form->is_modify_un = 1;
$form->type = User::TYPE_USER;
$res = $form->save();
if($res['code'] != 0){
return $res;
}
$user_id = $res['data']['user_id'];
$this->model->user_id = $user_id;
}else{
$form = new CommonUserEditForm();
$form->scenario = 'detection_add';
$form->model = $model;
$form->cx_mch_id = 0;
$form->mobile_phone = $this->mobile_phone;
$form->mobile_prefix = '86';
if(empty($model->real_name)){
$form->real_name = $this->name;
}
$res = $form->save();
if($res['code'] != 0){
return $res;
}
$user_id = $res['data']['user_id'];
$this->model->user_id = $user_id;
}
} else {
if(empty($this->model->user_id)){
return Model::asReturnError("当前检测员USER_ID异常,请联系技术人员");
}
//旧数据
$user_coach= User::findOne([
'is_delete' => 0,
'id' => $this->model->user_id,
]);
if(empty($user_coach)){
return Model::asReturnError("当前检测员USER异常,请联系技术人员");
}
//输入的手机号存在且不是自己
if(!empty($model) && $model->id != $this->model->user_id){
return Model::asReturnError("登陆使用手机号已被【ID:{$model->id}-{$model->nickname}】注册,请更换");
}
$form = new CommonUserEditForm();
$form->scenario = 'detection_add';
$form->model = $model;
$form->cx_mch_id = 0;
$form->mobile_phone = $this->mobile_phone;
$form->mobile_prefix = '86';
if(empty($model->real_name)){
$form->real_name = $this->name;
}
$res = $form->save();
if($res['code'] != 0){
return $res;
}
$user_id = $res['data']['user_id'];
$this->model->user_id = $user_id;
}
return $this->apiReturnSuccess();
}
}