Goods.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace common\models\elasticsearch\goods;
  3. use common\models\base\EsBaseActiveRecord;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\elasticsearch\ActiveRecord;
  6. /**
  7. * Class Goods
  8. * @package addons\RfExample\common\models
  9. */
  10. class Goods extends EsBaseActiveRecord
  11. {
  12. public static $currentIndex;
  13. /**
  14. * 数据库名称
  15. * @return string
  16. */
  17. public static function index()
  18. {
  19. return 'es_goods';
  20. }
  21. /**
  22. * 表名
  23. *
  24. * @return string
  25. */
  26. public static function type()
  27. {
  28. return 'goods';
  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','goods_id', 'mch_id', 'is_on_sale', 'is_attr', 'status', 'stock', 'is_show_stock', 'is_show_sales', 'virtual_sales', 'sales_num', 'buy_num_limit', 'freight_rules_type', 'freight_id', 'free_shipping_num', 'goods_type', 'sort', 'created_at', 'updated_at', 'is_area_limit'], 'integer'],
  42. // [['price', 'original_price', 'cost_price', 'shipping_fee', 'free_shipping_money'], 'string'],
  43. // [['attr_groups', 'attr_groups_format', 'detail', 'area_limit'], 'string'],
  44. // [['goods_name'], 'string', 'max' => 120],
  45. // [['unit', 'cover_pic', 'bannar_pic', 'freight_type', 'video_url', 'services', 'labels'], 'string', 'max' => 255],
  46. // [['video_cover_pic'], 'string', 'max' => 500],
  47. // ];
  48. return [
  49. [['mall_id','goods_id', 'mch_id', 'created_at', 'updated_at'], 'integer'],
  50. [['goods_name'], 'string', 'max' => 120],
  51. ];
  52. }
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'mall_id' => '商城id',
  58. 'goods_id' => '商品id',
  59. 'mch_id' => '商家id',
  60. 'goods_name' => '商品名称',
  61. // 'price' => '售价',
  62. // 'original_price' => '原价(划线价仅展示)',
  63. // 'cost_price' => '成本价',
  64. // 'unit' => '单位',
  65. // 'cover_pic' => '商品缩略图',
  66. // 'bannar_pic' => '商品展示bannar图',
  67. // 'is_on_sale' => '上架状态【1是 0否】',
  68. // 'is_attr' => '是否有规格【1是 0否】',
  69. // 'attr_groups' => '规格组',
  70. // 'attr_groups_format' => '格式化后的规格组',
  71. // 'status' => '状态[1正常 -1删除]',
  72. // 'stock' => '商品库存',
  73. // 'is_show_stock' => '是否显示库存',
  74. // 'is_show_sales' => '是否显示销量【1是 0否】',
  75. // 'virtual_sales' => '虚拟销量',
  76. // 'sales_num' => '累计销量',
  77. // 'buy_num_limit' => '购买数量限制',
  78. // 'freight_type' => '物流方式【1快递发货 2上门自提】逗号分隔',
  79. // 'freight_rules_type' => '运费规则 【1运费模板 2自定义运费】',
  80. // 'freight_id' => '运费模板ID',
  81. // 'shipping_fee' => '自定义运费金额',
  82. // 'free_shipping_num' => '商品满件包邮',
  83. // 'free_shipping_money' => '商品满额包邮',
  84. // 'goods_type' => '商品类型',
  85. // 'detail' => '商品详情,图文',
  86. // 'is_area_limit' => '是否开启区域限制',
  87. // 'area_limit' => '允许购买区域',
  88. // 'video_url' => '商品视频',
  89. // 'video_cover_pic' => '商品视频封面',
  90. // 'services' => '商品服务',
  91. // 'labels' => 'Labels',
  92. // 'sort' => '排序',
  93. 'created_at' => '创建时间',
  94. 'updated_at' => '修改时间',
  95. ];
  96. }
  97. /**
  98. * mapping配置(表字段说明)
  99. *
  100. * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
  101. * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
  102. * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
  103. *
  104. * @return array
  105. */
  106. public static function mapConfig()
  107. {
  108. return [
  109. 'properties' => [
  110. // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
  111. // index 默认可不设置
  112. 'mall_id' => ['type' => 'keyword'],
  113. 'goods_id' => ['type' => 'keyword'],
  114. 'mch_id' => ['type' => 'keyword'],
  115. 'goods_name' => ['type' => 'text', 'analyzer' => 'ik_max_word', 'search_analyzer' => 'ik_max_word'],//ik分词器
  116. 'created_at' => ['type' => 'long'],
  117. 'updated_at' => ['type' => 'long'],
  118. ],
  119. ];
  120. }
  121. /**
  122. * @return array
  123. */
  124. public static function mapping()
  125. {
  126. return [
  127. static::type() => self::mapConfig(),
  128. ];
  129. }
  130. /**
  131. * 更新字段
  132. *
  133. * @throws \yii\base\InvalidConfigException
  134. */
  135. public static function updateMapping()
  136. {
  137. $db = self::getDb();
  138. $command = $db->createCommand();
  139. if (!$command->indexExists(self::index())) {
  140. $command->createIndex(self::index());
  141. }
  142. $command->setMapping(self::index(), self::type(), self::mapping(), ['include_type_name' => 'true']);
  143. }
  144. public static function indexExists()
  145. {
  146. $db = self::getDb();
  147. $command = $db->createCommand();
  148. if (!$command->indexExists(self::index())) return false;
  149. return true;
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function behaviors()
  155. {
  156. return [
  157. [
  158. 'class' => TimestampBehavior::class,
  159. 'attributes' => [
  160. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  161. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  162. ],
  163. ],
  164. ];
  165. }
  166. /**
  167. * 设置数据
  168. * @param $params
  169. * @throws \Exception
  170. */
  171. public static function setData($params)
  172. {
  173. if (!empty($params['id'])) {
  174. // $model = self::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'],]);
  175. if (empty($model)) $model = new self();
  176. } else {
  177. $model = new self();
  178. }
  179. $add_data = ['Goods' => $params];
  180. $model->load($add_data);
  181. $model->save();
  182. }
  183. /**
  184. * 获取商品
  185. * @param $params
  186. * @return mixed
  187. */
  188. public static function getGoodsList($params)
  189. {
  190. $where[] = ['mall_id' => $params['mall_id']];
  191. $where[] =['goods_name' => $params['keyword']];
  192. $params['where'] = $where;
  193. return self::listPage($params,true);
  194. }
  195. }