| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace common\models\goods;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- /**
- *
- * This is the model class for table "{{%goods_cate_relate}}".
- *
- * @property int $id
- * @property int|null $cate_id 分类id
- * @property int|null $goods_id 产品id
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class GoodsCateRelate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_cate_relate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['cate_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'cate_id' => '分类id',
- 'goods_id' => '产品id',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 改变商品分类
- * @Author bing
- * @DateTime 2021-08-23 09:38:52
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param integer $before_cate_id
- * @param integer $after_cate_id
- * @return void
- */
- public static function changeGoodsCate(int $before_cate_id, int $after_cate_id){
- try{
- $count = self::updateAll(
- ['cate_id' => $after_cate_id],
- ['cate_id' => $before_cate_id]
- );
- return $count;
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 保存商品分类关系
- * @Author bing
- * @DateTime 2021-09-01 15:25:06
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $goods_id
- * @param [type] $cats
- * @return void
- */
- public static function setData($goods_id,$cats){
- try{
- if($goods_id && $cats){
- $count = self::deleteAll([
- 'and',
- ['goods_id' => $goods_id],
- ['not in','cate_id',$cats]
- ]);
- $exist_cate_ids = self::find()->select('cate_id')->where(array('goods_id'=>$goods_id))->column();
- $insert = [];
- $now = time();
- foreach($cats as $key => $cate_id){
- if(in_array($cate_id,$exist_cate_ids)) continue;
- $insert[] = [$goods_id,$cate_id,$now,$now];
- }
- $field = ['goods_id', 'cate_id','created_at','updated_at'];
- // 批量插入数据
- Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- }
- return true;
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 通过商品ID获取商品详情
- *
- * @param array $goods_ids
- * @param string $field
- * @param boolean|int $cache
- * @return array
- */
- public static function getListByGoodsIds(array $goods_ids, string $field = '*', $cache = false)
- {
- $model = self::find()->where(['goods_id' => $goods_ids]);
- $model->select($field);
- $cache && $model->cache($cache);
- return $model->asArray()->all();
- }
- /**
- * 关联商品
- * @return \yii\db\ActiveQuery
- */
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- }
- /**
- * 检查分类下是否有商品
- * @param array|null $cate_ids
- * @return bool
- */
- public static function checkCateHasGoods(?array $cate_ids): bool
- {
- $cateGoods = self::find()->where(['cate_id'=> $cate_ids])
- ->with('goods')
- ->asArray()
- ->all();
- $is_check = false;
- foreach($cateGoods as $cateGood){
- if(!empty($cateGood['goods'])){// 有商品则不能删除
- $is_check = true;
- break;
- }
- }
- return $is_check;
- }
- /**
- * 根据分类ID获取商品ID
- *
- * @param array $cate_ids
- * @return array
- */
- public static function getGoodsIdByCateIds(array $cate_ids)
- {
- return static::find()
- ->where(['cate_id' => $cate_ids])
- ->select(['goods_id'])
- ->column();
- }
- }
|