| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace common\models\elasticsearch\order;
- use common\models\user\User;
- use yii\behaviors\TimestampBehavior;
- use yii\elasticsearch\ActiveRecord;
- /**
- * Class OrderAction
- * @package addons\RfExample\common\models
- */
- class OrderAction extends ActiveRecord
- {
- public static $currentIndex;
- /**
- * 数据库名称
- * @return string
- */
- public static function index()
- {
- return 'es_order_action';
- }
- /**
- * 表名
- *
- * @return string
- */
- public static function type()
- {
- return 'log';
- }
- public function attributes()
- {
- $mapConfig = self::mapConfig();
- return array_keys($mapConfig['properties']);
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','order_id','action','member_id','member_name','order_status','order_status_text'], 'required'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'mall_id' => '商城id',
- 'order_id' => '订单id',
- 'action' => '动作内容',
- 'member_id' => '操作人id',
- 'member_name' => '操作人',
- 'order_status' => '订单状态',
- 'order_status_text' => '订单状态名称',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * mapping配置(表字段说明)
- *
- * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
- * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
- * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
- *
- * @return array
- */
- public static function mapConfig()
- {
- return [
- 'properties' => [
- // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
- // index 默认可不设置
- 'mall_id' => ['type' => 'keyword'],
- 'order_id' => ['type' => 'keyword'],
- 'action' => ['type' => 'keyword'],
- 'member_id' => ['type' => 'keyword'],
- 'member_name' => ['type' => 'keyword'],
- 'order_status' => ['type' => 'keyword'],
- 'order_status_text' => ['type' => 'keyword'],
- 'created_at' => ['type' => 'long'],
- 'updated_at' => ['type' => 'long'],
- ],
- ];
- }
- /**
- * @return array
- */
- public static function mapping()
- {
- return [
- static::type() => self::mapConfig(),
- ];
- }
- /**
- * 更新字段
- *
- * @throws \yii\base\InvalidConfigException
- */
- public static function updateMapping()
- {
- $db = self::getDb();
- $command = $db->createCommand();
- if (!$command->indexExists(self::index())) {
- $command->createIndex(self::index());
- }
- $command->setMapping(self::index(), self::type(), self::mapping(),['include_type_name'=>'true']);
- }
- /**
- * @return array
- */
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
- ],
- ],
- ];
- }
- /**
- * 设置数据
- * @param $params
- */
- public static function setData($params){
- $add_data = ['OrderAction'=>$params];
- $model = new self();
- $model->load($add_data);
- $model->save();
- }
- /**
- * 获取列表
- * @param $params
- * @return array
- */
- public static function getList($params){
- $query = self::find()->where($params['where']);
- $limit = $params['limit']??20;
- $page = $params['page']?$params['page']-1:0;
- if($params['is_page']){
- $count = $query->count();
- $page_count = ceil($count/$limit);
- $query->offset($page*$limit);
- }
- $log_list = $query->limit($limit)->orderBy($params['order']??'created_at asc')->asArray()->all();
- $rows = $data = [];
- foreach ($log_list as $v){
- $rows[]=$v['_source'];
- }
- if($params['is_page']){
- $data['page'] = 1;
- $data['page_count'] = $page_count;
- $data['count'] = $count;
- $data['page_size'] = $limit;
- }
- $data['list'] = $rows;
- return $data;
- }
- }
|