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

315 lines
10 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\store;
use app\components\FlashStorage;
use app\components\Oss;
use app\models\auth\RoleUser;
use app\models\CardUnion;
use app\models\District;
use app\models\Model;
use app\models\SysAdmin;
use app\models\User;
use app\modules\admin\models\AdminModel;
use app\models\common\CommonUserEditForm;
use app\modules\api\models\StoreCityForm;
class StoreEditForm extends AdminModel
{
public $model;
public $username;
public $password;
public $name;
public $status;
public $province_id;
public $city_id;
public $region_id;
public $location_detail;
public $lat;
public $lng;
public $begin_time;
public $end_time;
public $cover_pic;
public $pic_urls;
public $content;
public $user_id;
public $created_at;
public $updated_at;
public $is_delete;
public $deleted_at;
public $sort;
public $province;
public $city;
public $region;
public $ratio;
public $user_ratio;
public $card_id = 0;
public $role_ids;
public $cx_mch_id;
public $creator_user_id;
public $store_mobile;
public $interval; //包厢时间间隔
public $sn;
public $open_money;
public $content_page;
public function rules()
{
return [
[['name', 'province_id', 'city_id', 'region_id', 'location_detail', 'lat', 'lng', 'begin_time', 'end_time', 'cover_pic', 'content'], 'required'],
[['province_id', 'city_id', 'region_id', 'status', 'created_at', 'updated_at', 'is_delete', 'deleted_at', 'user_id', 'cx_mch_id', 'creator_user_id', 'user_ratio', 'ratio'], 'integer'],
[['content', 'content_page', 'pic_urls', 'store_mobile', 'sn', 'open_money'], 'string'],
[['role_ids', 'model'], 'safe'],
[['name', 'sort'], 'string', 'max' => 50],
[['ratio', 'user_ratio'], 'integer', 'min' => 0, 'max' => 100],
[['province', 'city', 'region', 'begin_time', 'end_time',], 'string', 'max' => 20],
[['location_detail', 'cover_pic', 'password'], 'string', 'max' => 255],
[['lat', 'lng'], 'string', 'max' => 32],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => '门店名称',
'province' => '省',
'province_id' => '省ID',
'city' => '市',
'city_id' => '市ID',
'region' => '区',
'region_id' => '区ID',
'location_detail' => '门店具体位置',
'content' => '门店内容',
'lat' => '纬度',
'lng' => '经度',
'begin_time' => '营业开始时间',
'end_time' => '营业结束时间',
'status' => '1-营业 0-停业',
'sort' => '排序',
'created_at' => '添加时间',
'updated_at' => '修改时间',
'is_delete' => '是否删除0=否1=是',
'deleted_at' => '删除时间',
'user_id' => '用户id',
'cover_pic' => '封面图',
'pic_urls' => '轮播图',
'ratio' => '收益比例',
'user_ratio' => '店长收益比例',
'store_mobile' => '门店电话',
];
}
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->sort = '100';
$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->location_detail = $this->location_detail;
$this->model->content = $this->content;
$this->model->lat = $this->lat;
$this->model->lng = $this->lng;
$this->model->content_page = $this->content_page;
$this->model->begin_time = $this->begin_time;
$this->model->end_time = $this->end_time;
$oss = new Oss();
if ($this->cover_pic) {
$file = $this->cover_pic;
//上传文件目录
$this->cover_pic = $oss->upload($file);
}
$this->model->cover_pic = $this->cover_pic;
$this->model->store_mobile = $this->store_mobile;
$this->model->updated_at = time();
$province = District::findOne($this->province_id);
if (empty($province)) {
return Model::asReturnError('用户所在省级行政区未找到');
}
$city = District::findOne($this->city_id);
if (empty($city)) {
return Model::asReturnError('用户所在市级行政区未找到');
}
$region = District::findOne($this->region_id);
if (empty($region)) {
return Model::asReturnError('用户所在区/县级行政区未找到');
}
// $card = CardUnion::findOne(['id' => $this->card_id, 'is_delete' => 0]);
// if (empty($card)) {
// return Model::asReturnError('所选银行卡不存在');
// }
$this->model->province = $province->name;
$this->model->city = $city->name;
$this->model->region = $region->name;
$this->model->province_id = $province->id;
$this->model->city_id = $city->id;
$this->model->region_id = $region->id;
$this->model->pic_urls = $this->pic_urls;
// $this->model->card_id = $this->card_id;
$this->model->ratio = $this->ratio;
$this->model->user_ratio = $this->user_ratio;
$this->model->sn = $this->sn;
$this->model->open_money = $this->open_money;
if (!$this->model->save()) {
$t->rollBack();
return $this->getModelError($this->model);
}
//保存角色
// $res = $this->saveUserRole($this->model->user_id, $this->role_ids, $this->cx_mch_id);
// if ($res['code'] != 0) {
// $t->rollBack();
// return $this->apiReturnError($res['msg']);
// }
//保存系统管理员
// $res = $this->saveSysAdmin($this->model->user_id, $this->creator_user_id, $this->cx_mch_id);
// if ($res['code'] != 0) {
// $t->rollBack();
// return $this->apiReturnError($res['msg']);
// }
$t->commit();
// 存入数据表
$redis_name = \Yii::$app->params['cacheKeyPrefix'] . ":api:store:zset";
try {
\Yii::$app->redis->geoadd($redis_name, $this->lng, $this->lat, $this->model->id);
} catch (\Exception $e) {
}
// 删除城市缓存
$obj = new StoreCityForm();
$cache_name = $obj->getCacheName();
FlashStorage::deleteCache($cache_name);
return $this->apiReturnSuccess('保存成功');
}
private function saveUser()
{
if ($this->model->isNewRecord) {
$form = new CommonUserEditForm();
$form->scenario = 'store_add';
$form->model = new User();
$form->cx_mch_id = 0;
$form->username = $this->username;
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
$form->real_name = $this->username;
$form->nickname = $this->username;
$form->avatar_url = User::DEFAULT_AVATAR_URL;
$form->access_token = \Yii::$app->security->generateRandomString();
$form->is_modify_un = 1;
$form->type = User::TYPE_STORE;
$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 $this->apiReturnError('USER_ID不能为空');
}
$user = User::findOne([
'id' => $this->model->user_id,
'is_delete' => 0,
'type' => User::TYPE_STORE
]);
if ($user == null) {
return $this->apiReturnError('用户不存在--' . $this->model->user_id);
}
$password = \Yii::$app->security->generatePasswordHash($this->password);
if (!empty($this->password) && $password != $user->password) {
$form = new CommonUserEditForm();
$form->scenario = 'store_add';
$form->model = $user;
$form->cx_mch_id = 0;
$form->username = $this->username;
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
$res = $form->save();
if ($res['code'] != 0) {
return $res;
}
}
}
return $this->apiReturnSuccess();
}
private function saveUserRole($user_id, $role_ids, $cx_mch_id = 0)
{
if (empty($role_ids)) {
return $this->apiReturnError("角色不能为空");
}
RoleUser::updateAll(['is_delete' => 1], ['is_delete' => 0, 'user_id' => $user_id, 'cx_mch_id' => $cx_mch_id]);
foreach ($role_ids as $role_id) {
$role_user = RoleUser::findOne(['user_id' => $user_id, 'role_id' => $role_id]);
if ($role_user == null) {
$role_user = new RoleUser();
$role_user->cx_mch_id = $cx_mch_id;
$role_user->user_id = $user_id;
$role_user->role_id = $role_id;
}
$role_user->is_delete = 0;
if (!$role_user->save()) {
return $this->getModelError($role_user);
}
}
return $this->apiReturnSuccess();
}
private function saveSysAdmin($user_id, $creator_user_id, $cx_mch_id)
{
$sys_admin = SysAdmin::findOne(['user_id' => $user_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0]);
if ($sys_admin == null) {
$sys_admin = new SysAdmin();
$sys_admin->user_id = $user_id;
$sys_admin->cx_mch_id = $cx_mch_id;
$sys_admin->creator_user_id = $creator_user_id;
}
$sys_admin->is_delete = 0;
if (!$sys_admin->save()) {
return $this->getModelError($sys_admin);
}
return $this->apiReturnSuccess();
}
}