TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], ], ]; } // /** // * @return BaseActiveQuery // */ // public static function find() // { // return \Yii::createObject(ActiveQuery::class, [get_called_class()]); // } /** * 除去空参数 * @Author bing * @DateTime 2021-12-21 15:23:24 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $params * @return array */ public static function exceptNullVal($params){ foreach($params as $key => $val){ if($val === null) unset($params[$key]); } return $params; } /** * 统计 * @Author bing * @DateTime 2020-10-27 15:23:39 * @copyright: Copyright (c) 2020 广东七件事集团 * @param string $where * @return void|mixed */ public static function count($params,$limit = 0){ $model = static::find(); self::paramPack($params, $model); if($limit > 0) $model->limit($limit); self::$_sql[] = $model->createCommand()->getRawSql(); return $model->count(); } /** * 获取一个 * @Author bing * @DateTime 2020-10-27 15:24:02 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $where * @param [type] $is_array * @param [type] $with * @param boolean $order * @param string $select * @return void|mixed */ public static function getOne($where,$is_array=false,$with=[],$order=false,$select = '*',$index=''){ $query = self::find(); self::paramPack(['where'=>$where], $query); if($order) $query->orderBy($order); if($with) $query->with($with); $query->select($select); $query->asArray($is_array); if(!empty($index)) $query->indexBy($index); self::$_sql[] = $query->createCommand()->getRawSql(); return $query->one(); } /** * 获取一条关联查询 * @Author bing * @DateTime 2020-10-27 15:24:36 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return void|mixed */ public static function getUnionOne($params=[]){ $params['limit'] = 1; $list = self::lists($params); if($list === false){ return false; } return isset($list[0]) ? $list[0] : []; } /** * 获取数据列表总数 * @Author bing * @DateTime 2020-10-27 15:25:34 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return void|mixed */ public static function listCount($params=[]){ $query = static::find(); self::paramPack($params, $query); self::$_sql[] = $query->createCommand()->getRawSql(); $count = $query->count(); return $count; } /** * 获取数据列表 * @Author bing * @DateTime 2020-10-27 15:25:51 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @param boolean $is_array * @return mixed */ public static function lists($params=[],$is_array=true){ $list = []; $query = static::find(); try{ self::paramPack($params, $query); if(!empty($params['limit'])){ $query->limit($params['limit']); } if(!empty($params['offset'])){ $query->offset($params['offset']); } elseif(!empty($params['page']) && !empty($params['limit'])){ $query->offset(($params['page']-1)*$params['limit']); } self::$_sql[] = $query->createCommand()->getRawSql(); $list = $query->AsArray($is_array)->all(); return $list; }catch (\Exception $e){ var_dump($e->getMessage()); self::$static_error = $e->getMessage(); return false; } } /** * 通用分页列表数据集获取方法 * @Author bing * @DateTime 2020-10-27 15:26:13 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params 参数集合 * @param boolean|int $count 总数 如果传进来,则不再执行查询总数 否则默认会查询 * @param boolean $is_array * @return void|mixed */ public static function listPage($params=[], $count=false,$is_array=true,$is_get_query=false){ $return = ['list' => [], 'count' => 0]; $query = static::find(); try{ self::paramPack($params, $query); $return['page'] = (Yii::$app->request->get('page') ?? Yii::$app->request->post('page')) ?? ($params['page'] ?? null) ?? 1; $return['page'] = $return['page'] > 0 ? $return['page'] : 1 ; $return['page_size'] = $params['limit'] ?? 20; if($count === false){ //预加载分页优化 $page_count_query = clone $query; $return['row_count'] = $page_count_query->count(); //后面10页的数量 $t = microtime(true); $offset_rows_count = $page_count_query->offset(($return['page'] - 1) * $return['page_size'])->limit(10 * $return['page_size'])->orderBy(null)->all(); $offset_rows_count = count($offset_rows_count ?? []); if(YII_DEBUG) $return['sql'][] = self::$_sql[] = $page_count_query->createCommand()->getRawSql(); unset($page_count_query); //前面的页码数量 $prev_rows_count = $return['page_size'] * ($return['page'] - 1); $return['count'] = $offset_rows_count + $prev_rows_count; }else{ $return['count'] = $count; } $pages = new Pagination(['totalCount' => $return['count'] ?? 1]); $pages->pageSizeParam = "limit"; $pages->defaultPageSize = $return['page_size']; $pages->setPageSize($return['page_size']); $return['pagination'] = $pages; //总页数 $return['page_count'] = (!empty($return['count'] ) && $return['count'] > 0) ? ceil($return['count'] / $return['page_size']) : 1; //边界处理 if($return['page'] > $return['page_count']) $return['page'] = $return['page_count']; $query->limit($return['page_size']); $query->offset(($return['page'] - 1) * $return['page_size']); // $return['pagination'] = $pages; if($is_get_query) return $query; $return['list'] = $is_array ? $query->AsArray()->all() : $query->all(); // echo '数据查询耗时'.(microtime(true) - $t).'s'.PHP_EOL; if(YII_DEBUG) $return['sql'][] = self::$_sql[] = $query->createCommand()->getRawSql(); }catch (\Exception $exc){ self::$static_error = $exc->getMessage(); //$this->errors = $exc->getMessage(); return false; } return $return; } /** * SQL组装 * @Author bing * @DateTime 2020-10-27 15:27:08 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @param [type] $query * @return void|mixed */ public static function paramPack(array $params = [], &$query = null) { if(!empty($params['alias'])) $alias = $params['alias']; if(!empty($params['table'])){ $table = isset($alias) ? ($params['table'] . ' ' . $alias) : $params['table']; }else{ $table = isset($alias) ? (static::tableName() . ' ' . $alias) : static::tableName(); } $query->from($table); if(!empty($params['where'])){ if(is_array($params['where'])){ foreach($params['where'] as $where){ $query->andWhere($where); } }elseif(is_string($params['where'])){ $query->andWhere($params['where']); } } if (!empty($params['and_where'])) { $query->where($params['and_where']); } if (!empty($params['or_where'])) { $query->orWhere($params['or_where']); } if(!empty($params['order'])) $query->orderBy($params['order']); if(!empty($params['join']) && is_array($params['join'])){ foreach($params['join'] as $join){ list($joinType,$joinTable,$joinOn) = $join; $query->join($joinType,$joinTable,$joinOn); } } if(!empty($params['with'])) $query->with($params['with']); if(!empty($params['joinWith'])) $query->joinWith($params['joinWith']); if(!empty($params['group'])) $query->groupBy($params['group']); if(!empty($params['having'])) $query->having($params['having']); if(!empty($params['select'])) $query->select($params['select']); if(!empty($params['index'])) $query->indexBy($params['index']); } /** * 获取错误信息 * @Author bing * @DateTime 2020-10-16 15:01:06 * @copyright: Copyright (c) 2020 广东七件事集团 * @return mixed 错误信息 */ public static function getSql(){ $sql = self::$_sql; if(empty($sql)) $sql = ''; return $sql; } /** * 获取模型错误 * @Author bing * @DateTime 2020-10-27 15:27:47 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $model * @return void|mixed */ public function getErrorMessage($model = null){ if (!$model) $model = $this; if(!empty($model->errors)){ $msg = current($model->errors)[0]; }else{ $msg = self::$static_error ?? '未知错误'; } return $msg; } /** * 保存生成操作日志 * @Author: lun * @DateTime: 2023/4/24 0024 15:09 * @Copyright: copyright (c) 2021 广东七件事集团 * @param bool $insert * @param array $changedAttributes * @throws \yii\base\InvalidConfigException */ public function afterSave($insert, $changedAttributes) { try { $formName = $this->formName();// 获取当前操作的模型 // 后台用户登录信息 if(!isset(Yii::$app->params['is_save_backend_log'])||Yii::$app->params['is_save_backend_log']==1){ if(isset(Yii::$app->user)) { $admin = Yii::$app->user->identity; $ignore_model = ['BackendLog', 'AuthItemChild'];// 忽略的模型 if ($admin && isset($admin['account']) && !in_array($formName, $ignore_model) && $changedAttributes) { $beforeUpdate = $changedAttributes;// 更新前的数据 $afterUpdate = $this->attributes;// 更新后的数据 $remark = '新增'; if (isset($afterUpdate['updated_at']) && isset($afterUpdate['created_at']) && $afterUpdate['updated_at'] > $afterUpdate['created_at']) { $remark = "修改"; } if (isset($afterUpdate['status'])) { if ($afterUpdate['status'] == 0) $remark = '禁用'; if ($afterUpdate['status'] == -1) $remark = '删除'; } // 获取修改过的数据 $newBeforeUpdate = $newAfterUpdate = []; foreach ($beforeUpdate as $key => $item) { if (in_array($key, ['updated_at'])) continue; if ($item != $afterUpdate[$key]) { $newBeforeUpdate[$key] = $item; $newAfterUpdate[$key] = $afterUpdate[$key]; } } // 新增操作日志 if ($newBeforeUpdate || $newAfterUpdate) { $model = new BackendLog(); $model->mall_id = $afterUpdate['mall_id'] ?? 0; $model->operate_id = $admin->id; $model->operate_name = $admin->account; $model->operate_ip = $_SERVER['REMOTE_ADDR'] ?? ''; $model->modules = StringHelper::strUcwords(Yii::$app->controller->module->id); $model->model_id = intval(str_replace(["_"], "", $this->attributes['id'])); $model->model = $formName; $model->remark = $remark; $model->before_content = json_encode($newBeforeUpdate, JSON_UNESCAPED_UNICODE); $model->after_content = json_encode($newAfterUpdate, JSON_UNESCAPED_UNICODE); if (!$model->save()) throw new Exception('sql:' . $model->getErrorMessage()); } } } } } catch (\Exception $e) { Yii::error('后台操作日志存储失败:第' . $e->getLine() .'行,原因:'. $e->getMessage()); } parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub } /** * 获取分页数据 * * @param integer $limit * @param callable $callback * @param string $order_by * @return array */ public static function getPaginationList( int $limit, callable $callback = null, string $order_by = 'id' ) :array { $model = static::find(); if (!empty($callback)) call_user_func($callback, $model); $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]); $model->limit($pagination->limit)->offset($pagination->offset); $list = $model ->orderBy($order_by) ->asArray() ->all(); $data['list'] = $list; $data['pagination'] = $pagination; $data['page_count'] = $pagination->pageCount; $data['row_count'] = $pagination->totalCount; $data['page_size'] = $pagination->getLimit(); return $data; } }