cxgj/modules/admin/models/coach/CoachEditForm.php
2023-11-27 09:45:13 +08:00

196 lines
7.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-5
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\models\coach;
use app\components\EncryptHelper;
use app\models\Coach;
use app\models\common\CommonUserEditForm;
use app\models\Model;
use app\models\Store;
use app\models\User;
use app\modules\admin\models\AdminModel;
class CoachEditForm extends AdminModel
{
public $model;
public $name;
public $coach_phone;
public $mobile_phone;
public $title;
public $coache_begin_time;
public $status;
public $store_id;
public $content;
public $coach_photo;
public $qualification;
public $number;
public $desc;
public $user_id;
public $created_at;
public $is_delete;
public $updated_at;
public $deleted_at;
public function rules()
{
return [
[['name', 'coach_phone', 'mobile_phone', 'title', 'coache_begin_time', 'status', 'store_id', 'content', 'coach_photo', 'number'], 'required'],
[['content'], 'string'],
[['status', 'store_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at', 'user_id'], 'integer'],
[['mobile_phone'], 'string', 'max' => 128],
[['desc'], 'string', 'max' => 255],
[['title', 'number', 'coache_begin_time'], 'string', 'max' => 50],
[['coach_photo', 'qualification'], 'string', 'max' => 2048],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'mobile_phone' => '教练手机号',
'title' => '头衔',
'content' => '教练介绍',
'coache_begin_time' => '开始执教时间',
'coach_photo' => '教练照片',
'qualification' => '资格证书',
'status' => '状态 1-正常 0-关闭',
'store_id' => '门店ID',
'created_at' => '添加时间',
'updated_at' => '修改时间',
'is_delete' => '是否删除0=否1=是',
'deleted_at' => '删除时间',
'number' => '教练编号',
'user_id' => '用户id',
'desc' => '教练简介',
];
}
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->mobile_phone = $this->coach_phone;
$this->model->title = $this->title;
$this->model->content = $this->content;
$this->model->coache_begin_time = strtotime($this->coache_begin_time);
$this->model->coach_photo = $this->coach_photo;
$this->model->qualification = empty($this->qualification) ? '' : $this->qualification;
$this->model->store_id = $this->store_id;
$this->model->updated_at = time();
$this->model->number = $this->number;
$this->model->status = $this->status;
$this->model->desc = $this->desc;
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 ($model == null) {
$form = new CommonUserEditForm();
$form->scenario = 'coach_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 {
$store = Store::findOne(['user_id' => $model->id, 'is_delete' => 0]);
if (!empty($store)) {
return Model::asReturnError("登陆使用手机号已是门店身份,请更换手机号");
}
$coach = Coach::findOne(['user_id' => $model->id, 'is_delete' => 0]);
if(!empty($this->model->user_id) && $model->id != $this->model->user_id){
if (!empty($coach)) {
return Model::asReturnError("登陆使用手机号已是教练身份,请更换手机号");
}
//旧数据
$user_coach = User::findOne([
'is_delete' => 0,
'id' => $this->model->user_id,
]);
if (empty($user_coach)) {
return Model::asReturnError("当前教练USER异常,请联系技术人员");
}
if ($user_coach->type == User::TYPE_COACH) {
$user_coach->type = User::TYPE_USER;
$user_coach->save();
}
}
$form = new CommonUserEditForm();
$form->scenario = 'coach_add';
$form->model = $model;
$form->cx_mch_id = 0;
$form->mobile_phone = $this->mobile_phone;
$form->mobile_prefix = '86';
$form->type = User::TYPE_USER;
$form->nickname = $this->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();
}
}