OrderAction.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace common\models\elasticsearch\order;
  3. use common\models\user\User;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\elasticsearch\ActiveRecord;
  6. /**
  7. * Class OrderAction
  8. * @package addons\RfExample\common\models
  9. */
  10. class OrderAction extends ActiveRecord
  11. {
  12. public static $currentIndex;
  13. /**
  14. * 数据库名称
  15. * @return string
  16. */
  17. public static function index()
  18. {
  19. return 'es_order_action';
  20. }
  21. /**
  22. * 表名
  23. *
  24. * @return string
  25. */
  26. public static function type()
  27. {
  28. return 'log';
  29. }
  30. public function attributes()
  31. {
  32. $mapConfig = self::mapConfig();
  33. return array_keys($mapConfig['properties']);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id','order_id','action','member_id','member_name','order_status','order_status_text'], 'required'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'mall_id' => '商城id',
  48. 'order_id' => '订单id',
  49. 'action' => '动作内容',
  50. 'member_id' => '操作人id',
  51. 'member_name' => '操作人',
  52. 'order_status' => '订单状态',
  53. 'order_status_text' => '订单状态名称',
  54. 'created_at' => '创建时间',
  55. 'updated_at' => '更新时间',
  56. ];
  57. }
  58. /**
  59. * mapping配置(表字段说明)
  60. *
  61. * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
  62. * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
  63. * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
  64. *
  65. * @return array
  66. */
  67. public static function mapConfig()
  68. {
  69. return [
  70. 'properties' => [
  71. // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
  72. // index 默认可不设置
  73. 'mall_id' => ['type' => 'keyword'],
  74. 'order_id' => ['type' => 'keyword'],
  75. 'action' => ['type' => 'keyword'],
  76. 'member_id' => ['type' => 'keyword'],
  77. 'member_name' => ['type' => 'keyword'],
  78. 'order_status' => ['type' => 'keyword'],
  79. 'order_status_text' => ['type' => 'keyword'],
  80. 'created_at' => ['type' => 'long'],
  81. 'updated_at' => ['type' => 'long'],
  82. ],
  83. ];
  84. }
  85. /**
  86. * @return array
  87. */
  88. public static function mapping()
  89. {
  90. return [
  91. static::type() => self::mapConfig(),
  92. ];
  93. }
  94. /**
  95. * 更新字段
  96. *
  97. * @throws \yii\base\InvalidConfigException
  98. */
  99. public static function updateMapping()
  100. {
  101. $db = self::getDb();
  102. $command = $db->createCommand();
  103. if (!$command->indexExists(self::index())) {
  104. $command->createIndex(self::index());
  105. }
  106. $command->setMapping(self::index(), self::type(), self::mapping(),['include_type_name'=>'true']);
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function behaviors()
  112. {
  113. return [
  114. [
  115. 'class' => TimestampBehavior::class,
  116. 'attributes' => [
  117. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  118. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  119. ],
  120. ],
  121. ];
  122. }
  123. /**
  124. * 设置数据
  125. * @param $params
  126. */
  127. public static function setData($params){
  128. $add_data = ['OrderAction'=>$params];
  129. $model = new self();
  130. $model->load($add_data);
  131. $model->save();
  132. }
  133. /**
  134. * 获取列表
  135. * @param $params
  136. * @return array
  137. */
  138. public static function getList($params){
  139. $query = self::find()->where($params['where']);
  140. $limit = $params['limit']??20;
  141. $page = $params['page']?$params['page']-1:0;
  142. if($params['is_page']){
  143. $count = $query->count();
  144. $page_count = ceil($count/$limit);
  145. $query->offset($page*$limit);
  146. }
  147. $log_list = $query->limit($limit)->orderBy($params['order']??'created_at asc')->asArray()->all();
  148. $rows = $data = [];
  149. foreach ($log_list as $v){
  150. $rows[]=$v['_source'];
  151. }
  152. if($params['is_page']){
  153. $data['page'] = 1;
  154. $data['page_count'] = $page_count;
  155. $data['count'] = $count;
  156. $data['page_size'] = $limit;
  157. }
  158. $data['list'] = $rows;
  159. return $data;
  160. }
  161. }