GoodsCateRelate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. *
  8. * This is the model class for table "{{%goods_cate_relate}}".
  9. *
  10. * @property int $id
  11. * @property int|null $cate_id 分类id
  12. * @property int|null $goods_id 产品id
  13. * @property int $created_at 创建时间
  14. * @property int $updated_at 修改时间
  15. */
  16. class GoodsCateRelate extends \common\models\base\BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%goods_cate_relate}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['cate_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'id' => 'ID',
  41. 'cate_id' => '分类id',
  42. 'goods_id' => '产品id',
  43. 'created_at' => '创建时间',
  44. 'updated_at' => '修改时间',
  45. ];
  46. }
  47. /**
  48. * 改变商品分类
  49. * @Author bing
  50. * @DateTime 2021-08-23 09:38:52
  51. * @copyright: Copyright (c) 2020 广东七件事集团
  52. * @param integer $before_cate_id
  53. * @param integer $after_cate_id
  54. * @return void
  55. */
  56. public static function changeGoodsCate(int $before_cate_id, int $after_cate_id){
  57. try{
  58. $count = self::updateAll(
  59. ['cate_id' => $after_cate_id],
  60. ['cate_id' => $before_cate_id]
  61. );
  62. return $count;
  63. }catch(Exception $e){
  64. self::$static_error = $e->getMessage();
  65. return false;
  66. }
  67. }
  68. /**
  69. * 保存商品分类关系
  70. * @Author bing
  71. * @DateTime 2021-09-01 15:25:06
  72. * @copyright: Copyright (c) 2020 广东七件事集团
  73. * @param [type] $goods_id
  74. * @param [type] $cats
  75. * @return void
  76. */
  77. public static function setData($goods_id,$cats){
  78. try{
  79. if($goods_id && $cats){
  80. $count = self::deleteAll([
  81. 'and',
  82. ['goods_id' => $goods_id],
  83. ['not in','cate_id',$cats]
  84. ]);
  85. $exist_cate_ids = self::find()->select('cate_id')->where(array('goods_id'=>$goods_id))->column();
  86. $insert = [];
  87. $now = time();
  88. foreach($cats as $key => $cate_id){
  89. if(in_array($cate_id,$exist_cate_ids)) continue;
  90. $insert[] = [$goods_id,$cate_id,$now,$now];
  91. }
  92. $field = ['goods_id', 'cate_id','created_at','updated_at'];
  93. // 批量插入数据
  94. Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  95. }
  96. return true;
  97. }catch(Exception $e){
  98. self::$static_error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. /**
  103. * 通过商品ID获取商品详情
  104. *
  105. * @param array $goods_ids
  106. * @param string $field
  107. * @param boolean|int $cache
  108. * @return array
  109. */
  110. public static function getListByGoodsIds(array $goods_ids, string $field = '*', $cache = false)
  111. {
  112. $model = self::find()->where(['goods_id' => $goods_ids]);
  113. $model->select($field);
  114. $cache && $model->cache($cache);
  115. return $model->asArray()->all();
  116. }
  117. /**
  118. * 关联商品
  119. * @return \yii\db\ActiveQuery
  120. */
  121. public function getGoods()
  122. {
  123. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  124. }
  125. /**
  126. * 检查分类下是否有商品
  127. * @param array|null $cate_ids
  128. * @return bool
  129. */
  130. public static function checkCateHasGoods(?array $cate_ids): bool
  131. {
  132. $cateGoods = self::find()->where(['cate_id'=> $cate_ids])
  133. ->with('goods')
  134. ->asArray()
  135. ->all();
  136. $is_check = false;
  137. foreach($cateGoods as $cateGood){
  138. if(!empty($cateGood['goods'])){// 有商品则不能删除
  139. $is_check = true;
  140. break;
  141. }
  142. }
  143. return $is_check;
  144. }
  145. /**
  146. * 根据分类ID获取商品ID
  147. *
  148. * @param array $cate_ids
  149. * @return array
  150. */
  151. public static function getGoodsIdByCateIds(array $cate_ids)
  152. {
  153. return static::find()
  154. ->where(['cate_id' => $cate_ids])
  155. ->select(['goods_id'])
  156. ->column();
  157. }
  158. }