| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\ProfitSharingReceiver;
- use app\utils\Wechat\WechatShare;
- use yii\base\Model;
- class ProfitReceiverForm extends Model
- {
- public $id;
- public $type;
- public $account;
- public $name;
- public $description;
- public $ratio;
- public $search_key;
- public $max_amount;
- public static $valid_type = [
- [
- 'type' => 1,
- 'type_flag' => WechatShare::RECEIVE_PERSONAL_OPENID,
- ],
- [
- 'type' => 2,
- 'type_flag' => WechatShare::RECEIVE_MERCHANT_ID,
- ],
- [
- 'type' => 3,
- 'type_flag' => WechatShare::RECEIVE_PERSONAL_SUB_OPENID,
- ]
- ];
- public function rules()
- {
- return [
- [['type'], 'integer'],
- [['name', 'description', 'account', 'id'], 'string', 'max' => 255],
- [['name', 'description', 'account'], 'trim'],
- ];
- }
- /**
- * @return array
- * @throws \yii\db\Exception
- */
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if (!in_array($this->type, array_column(self::$valid_type, 'type'))) {
- return [
- 'code' => 1,
- 'msg' => '接收方类型错误'
- ];
- }
- $model = ProfitSharingReceiver::findOne($this->id);
- if (!$model) {
- $model = new ProfitSharingReceiver();
- $model->created_at = time();
- $model->store_id = get_store_id();
- }
- $model->type = $this->type;
- $model->account = $this->account;
- $model->name = $this->name;
- $model->max_amount = $this->max_amount;
- $model->ratio = $this->ratio;
- $model->description = $this->description;
- if (!$model->save()) {
- return [
- 'code' => 1,
- 'msg' => $model->errors[0]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- }
- public function search()
- {
- $query = ProfitSharingReceiver::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
- if (!empty($this->search_key)) {
- $query->andWhere(['like', 'name', $this->search_key]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['type'] = intval($val['type']);
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'type' => self::$valid_type
- ]
- ];
- }
- }
|