| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\District;
- use app\models\Md;
- use app\models\Shop;
- use app\models\User;
- use yii\base\Model;
- class ShopForm extends Model
- {
- public $id;
- public $store_id;
- public $user_id;
- public $mch_id;
- public $name;
- public $mobile;
- public $address;
- public $is_delete;
- public $longitude;
- public $latitude;
- public $score;
- public $cover_url;
- public $pic_url;
- public $shop_time;
- public $content;
- public $is_default;
- public $is_dada;
- public $business;
- public $city_name;
- public $area_name;
- public $contact_name;
- public $source_id;
- public $is_uu;
- public $province;
- public $city;
- public $district;
- public $shop_audit;
- public $refuse_desc;
- //搜索
- public $search_key;
- public $shop_audit_status;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_AUDIT = 'audit';
- const SCENARIO_LIST = 'list';
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'is_delete', 'score', 'is_default', 'is_dada', 'is_uu', 'business', 'source_id', 'province', 'city', 'district', 'user_id', 'shop_audit'], 'integer'],
- [['longitude', 'latitude', 'cover_url', 'pic_url', 'content', 'city_name', 'area_name', 'contact_name'], 'string'],
- [['name', 'mobile', 'address', 'shop_time', 'refuse_desc'], 'string', 'max' => 255],
- ['is_delete', 'default', 'value' => Shop::IS_DELETE_NO],
- ['shop_audit', 'default', 'value' => Shop::SHOP_AUDIT_YES],
- // 操作
- [['name', 'mobile', 'longitude', 'latitude', 'shop_time', 'content'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- ['id', 'string', 'on' => self::SCENARIO_DEL],
- [['id', 'shop_audit'], 'required', 'on' => self::SCENARIO_AUDIT],
- [['search_key', 'shop_audit_status'], 'string', 'on' => self::SCENARIO_LIST]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'user_id' => '用户id',
- 'name' => 'Name',
- 'mobile' => 'Mobile',
- 'address' => 'Address',
- 'is_delete' => 'Is Delete',
- 'longitude' => 'Longitude',
- 'latitude' => 'Latitude',
- 'score' => '评分 1~5',
- 'cover_url' => '自提点大图',
- 'pic_url' => '自提点小图',
- 'shop_time' => '营业时间',
- 'content' => '自提点介绍',
- 'is_default' => '是否设为默认 0--否 1--是 (只能设置一个自提点为默认自提点)',
- 'is_dada' => '是否同步到达达(0、不同步,1、同步)',
- 'business' => '达达业务类型',
- 'city_name' => '达达城市名称',
- 'area_name' => '达达区域名称',
- 'contact_name' => '达达联系姓名',
- 'source_id' => '达达商户id',
- 'is_uu' => '是否同步到UU(0、不同步,1、同步)',
- 'province' => '省份',
- 'city' => '城市',
- 'district' => '区县',
- 'shop_audit' => '审核状态(-1未通过,0待审核,1通过)',
- 'refuse_desc' => '拒接原因',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- public function saveShop()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- $is_default = null;
- $user_id = 0;
- $audit_status = 0;
- $refuse_desc = null;
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = Shop::findOne(['id' => $this->id]);
- $is_default = $model->is_default;
- $user_id = $model->user_id;
- $audit_status = $model->shop_audit;
- $refuse_desc = $model->refuse_desc;
- } else {
- $model = new Shop();
- }
- $this->content = str_replace(' ', ' ', $this->content);
- $this->content = str_replace('&nbsp;', ' ', $this->content);
- $this->city_name = District::getDistrict($this->city)->name;
- $this->area_name = District::getDistrict($this->district)->name;
- $this->is_default = $is_default == 1 ? 1 : 0;
- $this->user_id = $user_id;
- $this->refuse_desc = $refuse_desc;
- $this->shop_audit = $audit_status;
- $model->attributes = $this->attributes;
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->errors[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function searchShop()
- {
- $query = Md::find();
- $query->where(['store_id' => get_store_id(), 'is_delete' => MD::IS_DELETE_NO, 'open_status' => 1])->orderBy("id desc");
- // 搜索
- $this->search_key = $this->search_key ?? $this->name;
- if ($this->search_key) {
- $query->andWhere(['like', 'name', $this->search_key]);
- }
- if (isset($this->shop_audit_status) && (int)$this->shop_audit_status > -2) {
- $query->andWhere(['shop_audit' => (int)$this->shop_audit_status]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function delShop()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Shop::findOne(['id' => $this->id]);
- $model->is_delete = Shop::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function auditShop()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_AUDIT) {
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- $model = Shop::findOne(['id' => $this->id]);
- if (!$model || !in_array($this->shop_audit, [$model::SHOP_AUDIT_NO, $model::SHOP_AUDIT_YES])) {
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- if ($this->shop_audit == $model::SHOP_AUDIT_NO && empty($this->refuse_desc)) {
- return [
- 'code' => 1,
- 'msg' => '拒绝原因必须'
- ];
- }
- $model->shop_audit = $this->shop_audit;
- if ($model->user_id > 0) {
- $user = User::findOne(['id' => $model->user_id]);
- if ($model->shop_audit == $model::SHOP_AUDIT_YES) {
- $user->is_clerk = 1;
- $user->shop_id = $model->id;
- } else {
- $user->is_clerk = 0;
- $user->shop_id = 0;
- }
- $user->save();
- }
- $model->refuse_desc = $this->refuse_desc ?: '';
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $this->id
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-15
- * @desc: 获取自提点列表
- */
- public static function getShopList()
- {
- return Shop::find()->where(['store_id' => get_store_id(), 'is_delete' => Shop::IS_DELETE_NO])->orderBy('id desc')->select('*')->asArray()->all();
- }
- }
|