AreaGoods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace addons\Area\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\goods\Goods;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_area_goods".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城id
  11. * @property int $user_id 会员id
  12. * @property int $status 状态[-1:删除;0:禁用;1启用]
  13. * @property int $created_at 创建时间
  14. * @property int $updated_at 修改时间
  15. * @property int $goods_id 商品ID
  16. */
  17. class AreaGoods extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return 'qimall_addons_area_goods';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'goods_id'], 'integer'],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'mall_id' => 'Mall ID',
  43. 'user_id' => 'User ID',
  44. 'status' => 'Status',
  45. 'created_at' => 'Created At',
  46. 'updated_at' => 'Updated At',
  47. 'goods_id' => 'Goods ID',
  48. ];
  49. }
  50. public function getGoods()
  51. {
  52. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  53. }
  54. /**
  55. * @param int $mall_id
  56. * @param int $user_id
  57. * @param array $goods
  58. * @return void
  59. * @throws \yii\db\Exception
  60. */
  61. public static function batchSave(int $mall_id, int $user_id, int $agent_id, array $goods)
  62. {
  63. AreaGoods::updateAll(['status' => StatusEnum::DELETE], ['and',
  64. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
  65. ['mall_id' => $mall_id],
  66. ['agent_id' => $agent_id],
  67. ['user_id' => $user_id]]);
  68. $data = [];
  69. // 添加
  70. foreach ($goods as $good) {
  71. $data[] = [
  72. 'user_id' => $user_id,
  73. 'goods_id' => $good['goods_id'],
  74. 'mall_id' => $mall_id,
  75. 'created_at' => time(),
  76. 'updated_at' => time(),
  77. 'agent_id' => $agent_id
  78. ];
  79. }
  80. // 新增数据
  81. AreaGoods::find()->createCommand()->batchInsert(AreaGoods::tableName(), array_keys($data[0]), $data)->execute();
  82. }
  83. /**
  84. * @param int $mall_id
  85. * @param int $user_id
  86. * @return array
  87. */
  88. public static function findAllGoods(int $mall_id, int $user_id)
  89. {
  90. $ids = AreaGoods::find()
  91. ->where(['mall_id' => $mall_id])
  92. ->andWhere(['user_id' => $user_id])
  93. ->andWhere(['status' => StatusEnum::ENABLED])
  94. ->select('goods_id')
  95. ->column();
  96. if (!empty($ids)) {
  97. Goods::lists([
  98. 'where' => [
  99. ['mall_id' => $mall_id],
  100. ['status' => StatusEnum::ENABLED],
  101. ['id' => $ids]
  102. ],
  103. 'select'=> 'id, goods_name, cover_pic, price',
  104. 'order' => 'id desc'
  105. ]);
  106. }
  107. return [];
  108. }
  109. /**
  110. * @param int $mall_id
  111. * @param int $agent_id
  112. * @param int $goods_id
  113. * @return mixed|null
  114. */
  115. public static function existByAgentIdAndGoodsId(int $mall_id, int $agent_id, int $goods_id)
  116. {
  117. return AreaGoods::getOne([
  118. ['mall_id' => $mall_id],
  119. ['agent_id' => $agent_id],
  120. ['goods_id' => $goods_id],
  121. ['status' => StatusEnum::ENABLED]
  122. ], false, [], null, 'id');
  123. }
  124. }