| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Clerk;
- use app\models\Shop;
- use app\models\User;
- use yii\base\Model;
- class ClerkForm extends Model
- {
- public $id;
- public $store_id;
- public $user_id;
- public $shop_id;
- public $order_count;
- public $money_count;
- public $card_count;
- public $is_delete;
- //搜索
- public $search_key;
- public $search_is_show;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id', 'store_id', 'user_id', 'shop_id', 'order_count', 'card_count', 'is_delete'], 'integer'],
- [['money_count'], 'number'],
- ['is_delete', 'default', 'value' => Clerk::IS_DELETE_NO],
- [['order_count', 'card_count', 'money_count'], 'default', 'value' => 0],
- [['user_id', 'shop_id', 'order_count', 'card_count'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'store_id' => '商城id',
- 'user_id' => '用户id',
- 'shop_id' => '自提点id',
- 'order_count' => '核销订单数',
- 'money_count' => '核销总额',
- 'card_count' => '核销卡券数',
- 'is_delete' => '是否删除',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- public function saveClerk()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = Clerk::findOne(['id' => $this->id]);
- } else {
- $model = Clerk::findOne(['user_id' => $this->user_id]);
- $shop = Shop::findOne(['user_id' => $this->user_id, 'is_delete' => Shop::IS_DELETE_NO]);
- if($model || $shop){
- return [
- 'code' => 1,
- 'msg' => '该用户已是核销员'
- ];
- }
- $model = new Clerk();
- }
- $model->attributes = $this->attributes;
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- // 判断社区是否绑定会员,如果没有绑定,将核销元绑定社区
- $shop = Shop::findOne($this->shop_id);
- if (!$shop->user_id) {
- $shop->user_id = $this->user_id;
- if (!$shop->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '绑定出错'
- ];
- }
- }
- // 核銷園同步到user表
- if ($model->user_id > 0) {
- $user = User::findOne(['id' => $model->user_id]);
- if ($model->is_delete == $model::IS_DELETE_NO) {
- $user->is_clerk = 1;
- $user->shop_id = $model->id;
- } else {
- $user->is_clerk = 0;
- $user->shop_id = 0;
- }
- $user->save();
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function searchClerk()
- {
- $query = Clerk::find()->alias('c')
- ->leftJoin(['u' => User::tableName()], 'u.id=c.user_id')
- ->leftJoin(['s' => Shop::tableName()], 's.id=c.shop_id')
- ->select('c.*, u.nickname user_name, u.avatar_url user_head_url, s.name shop_name');
- $query->where(['c.is_delete' => Clerk::IS_DELETE_NO])->orderBy("id desc");
- // 搜索
- if ($this->search_key) {
- $query->andWhere(['like', 'u.nickname', $this->search_key]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- }
- $shop_list = ShopForm::getShopList();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'type_list' => $shop_list,
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function delClerk()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Clerk::findOne(['id' => $this->id]);
- $model->is_delete = Clerk::IS_DELETE_YES;
- // 核銷園同步到user表
- if ($model->user_id > 0) {
- $user = User::findOne(['id' => $model->user_id]);
- if ($user) {
- $user->is_clerk = 0;
- $user->shop_id = 0;
- $user->save();
- }
- }
- 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 static function getClerkList()
- {
- return Clerk::find()->where(['is_delete' => Clerk::IS_DELETE_NO])->orderBy(['id desc'])->select('*')->asArray()->all();
- }
- }
|