| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace common\models\elasticsearch\goods;
- use common\models\base\EsBaseActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use yii\elasticsearch\ActiveRecord;
- /**
- * Class Goods
- * @package addons\RfExample\common\models
- */
- class Goods extends EsBaseActiveRecord
- {
- public static $currentIndex;
- /**
- * 数据库名称
- * @return string
- */
- public static function index()
- {
- return 'es_goods';
- }
- /**
- * 表名
- *
- * @return string
- */
- public static function type()
- {
- return 'goods';
- }
- public function attributes()
- {
- $mapConfig = self::mapConfig();
- return array_keys($mapConfig['properties']);
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- // return [
- // [['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'],
- // [['price', 'original_price', 'cost_price', 'shipping_fee', 'free_shipping_money'], 'string'],
- // [['attr_groups', 'attr_groups_format', 'detail', 'area_limit'], 'string'],
- // [['goods_name'], 'string', 'max' => 120],
- // [['unit', 'cover_pic', 'bannar_pic', 'freight_type', 'video_url', 'services', 'labels'], 'string', 'max' => 255],
- // [['video_cover_pic'], 'string', 'max' => 500],
- // ];
- return [
- [['mall_id','goods_id', 'mch_id', 'created_at', 'updated_at'], 'integer'],
- [['goods_name'], 'string', 'max' => 120],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'goods_id' => '商品id',
- 'mch_id' => '商家id',
- 'goods_name' => '商品名称',
- // 'price' => '售价',
- // 'original_price' => '原价(划线价仅展示)',
- // 'cost_price' => '成本价',
- // 'unit' => '单位',
- // 'cover_pic' => '商品缩略图',
- // 'bannar_pic' => '商品展示bannar图',
- // 'is_on_sale' => '上架状态【1是 0否】',
- // 'is_attr' => '是否有规格【1是 0否】',
- // 'attr_groups' => '规格组',
- // 'attr_groups_format' => '格式化后的规格组',
- // 'status' => '状态[1正常 -1删除]',
- // 'stock' => '商品库存',
- // 'is_show_stock' => '是否显示库存',
- // 'is_show_sales' => '是否显示销量【1是 0否】',
- // 'virtual_sales' => '虚拟销量',
- // 'sales_num' => '累计销量',
- // 'buy_num_limit' => '购买数量限制',
- // 'freight_type' => '物流方式【1快递发货 2上门自提】逗号分隔',
- // 'freight_rules_type' => '运费规则 【1运费模板 2自定义运费】',
- // 'freight_id' => '运费模板ID',
- // 'shipping_fee' => '自定义运费金额',
- // 'free_shipping_num' => '商品满件包邮',
- // 'free_shipping_money' => '商品满额包邮',
- // 'goods_type' => '商品类型',
- // 'detail' => '商品详情,图文',
- // 'is_area_limit' => '是否开启区域限制',
- // 'area_limit' => '允许购买区域',
- // 'video_url' => '商品视频',
- // 'video_cover_pic' => '商品视频封面',
- // 'services' => '商品服务',
- // 'labels' => 'Labels',
- // 'sort' => '排序',
- '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'],
- 'goods_id' => ['type' => 'keyword'],
- 'mch_id' => ['type' => 'keyword'],
- 'goods_name' => ['type' => 'text', 'analyzer' => 'ik_max_word', 'search_analyzer' => 'ik_max_word'],//ik分词器
-
- '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']);
- }
- public static function indexExists()
- {
- $db = self::getDb();
- $command = $db->createCommand();
- if (!$command->indexExists(self::index())) return false;
- return 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
- * @throws \Exception
- */
- public static function setData($params)
- {
- if (!empty($params['id'])) {
- // $model = self::findOne(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'],]);
- if (empty($model)) $model = new self();
- } else {
- $model = new self();
- }
- $add_data = ['Goods' => $params];
- $model->load($add_data);
- $model->save();
- }
- /**
- * 获取商品
- * @param $params
- * @return mixed
- */
- public static function getGoodsList($params)
- {
- $where[] = ['mall_id' => $params['mall_id']];
- $where[] =['goods_name' => $params['keyword']];
- $params['where'] = $where;
- return self::listPage($params,true);
- }
- }
|