cxfoot/modules/admin/models/report/StoreEditForm.php
2023-10-25 17:19:39 +08:00

175 lines
5.3 KiB
PHP
Raw 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\EncryptHelper;
use app\components\FlashStorage;
use app\models\Admin;
use app\models\auth\RoleUser;
use app\models\CardUnion;
use app\models\District;
use app\models\Model;
use app\models\StoreUser;
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;
use Wechat\Wechat;
class StoreEditForm extends AdminModel
{
public $model;
public $name;
public $status;
public $province_id;
public $city_id;
public $region_id;
public $location_detail;
public $phone;
public $begin_time;
public $end_time;
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 $role_ids;
public $cx_mch_id;
public $creator_user_id;
public $area;
public function rules()
{
return [
[['province_id', 'city_id', 'region_id', 'status', 'created_at', 'updated_at', 'is_delete', 'deleted_at','cx_mch_id','creator_user_id','ratio','user_id'], 'integer'],
[['area'], 'string'],
[['role_ids', 'model'], 'safe'],
[['name', 'sort'], 'string', 'max' => 50],
[['phone'], 'string', 'max' => 20],
[['ratio'], 'integer', 'min' => 0,'max' => 100],
[['province', 'city', 'region','begin_time', 'end_time',], 'string', 'max' => 20],
[['location_detail'], 'string', 'max' => 255],
[['name', 'province_id', 'city_id', 'region_id', 'location_detail', 'begin_time', 'end_time','area','phone'], 'required'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => '门店名称',
'province' => '省',
'province_id' => '省ID',
'city' => '市',
'city_id' => '市ID',
'region' => '区',
'region_id' => '区ID',
'location_detail' => '门店具体位置',
'begin_time' => '营业开始时间',
'end_time' => '营业结束时间',
'status' => '1-营业 0-停业',
'sort' => '排序',
'created_at' => '添加时间',
'updated_at' => '修改时间',
'is_delete' => '是否删除0=否1=是',
'deleted_at' => '删除时间',
'ratio' => '收益比例',
'area' => '区域坐标',
'phone' => '客服电话',
];
}
public function edit()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->model->isNewRecord){
$this->model->is_delete = 0;
$this->model->deleted_at = 0;
$this->model->sort = '100';
$this->model->created_at = time();
}
$this->model->name = $this->name;
$this->model->location_detail = $this->location_detail;
$this->model->begin_time = $this->begin_time;
$this->model->end_time = $this->end_time;
$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('用户所在区/县级行政区未找到');
}
$area_json = [];
$area_arr = json_decode($this->area,true);
foreach ($area_arr as $index => $item){
$area_json[$item['area_type']] = $item;
}
$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->ratio = $this->ratio;
$this->model->area = json_encode($area_json,JSON_UNESCAPED_UNICODE);
$this->model->lat = strval($area_json['store']['paths'][0]['lat']);
$this->model->lng = strval($area_json['store']['paths'][0]['lng']);
$this->model->phone = $this->phone;
if(!$this->model->save()){
return $this->getModelError($this->model);
}
// 存入数据表
$redis_name = \Yii::$app->params['cacheKeyPrefix'].":api:store:zset";
try{
\Yii::$app->redis->geoadd($redis_name,$this->model->lat,$this->model->lng,$this->model->id);
}catch (\Exception $e){
}
// 删除城市缓存
$obj = new StoreCityForm();
$cache_name = $obj->getCacheName();
FlashStorage::deleteCache($cache_name);
return $this->apiReturnSuccess('保存成功');
}
}