StoreGoodsCateRelate.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use Yii;
  5. /**
  6. * This is the model class for table "qimall_addons_store_goods_cate_relate".
  7. *
  8. * @property int $id
  9. * @property int|null $cate_id 分类id
  10. * @property int|null $goods_id 产品id
  11. * @property int $created_at 创建时间
  12. * @property int $updated_at 修改时间
  13. */
  14. class StoreGoodsCateRelate extends BaseActiveRecord
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function tableName()
  20. {
  21. return '{{%addons_store_goods_cate_relate}}';
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['cate_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'id' => 'ID',
  39. 'cate_id' => '分类id',
  40. 'goods_id' => '产品id',
  41. 'created_at' => '创建时间',
  42. 'updated_at' => '修改时间',
  43. ];
  44. }
  45. public static function setData($goods_id,$cats){
  46. try{
  47. if($goods_id && $cats){
  48. $count = self::deleteAll([
  49. 'and',
  50. ['goods_id' => $goods_id],
  51. ['not in','cate_id',$cats]
  52. ]);
  53. $exist_cate_ids = self::find()->select('cate_id')->where(array('goods_id'=>$goods_id))->column();
  54. $insert = [];
  55. $now = time();
  56. foreach($cats as $key => $cate_id){
  57. if(in_array($cate_id,$exist_cate_ids)) continue;
  58. $insert[] = [$goods_id,$cate_id,$now,$now];
  59. }
  60. $field = ['goods_id', 'cate_id','created_at','updated_at'];
  61. // 批量插入数据
  62. Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  63. }
  64. return true;
  65. }catch(\Exception $e){
  66. self::$static_error = $e->getMessage();
  67. return false;
  68. }
  69. }
  70. /**
  71. * 改变商品分类
  72. * @Author bing
  73. * @DateTime 2021-08-23 09:38:52
  74. * @copyright: Copyright (c) 2020 广东七件事集团
  75. * @param integer $before_cate_id
  76. * @param integer $after_cate_id
  77. * @return int/bool
  78. */
  79. public static function changeGoodsCate(int $before_cate_id, int $after_cate_id){
  80. try{
  81. $count = self::updateAll(
  82. ['cate_id' => $after_cate_id],
  83. ['cate_id' => $before_cate_id]
  84. );
  85. return $count;
  86. }catch(\Exception $e){
  87. self::$static_error = $e->getMessage();
  88. return false;
  89. }
  90. }
  91. }