| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace addons\Area\common\models;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_area_goods".
- *
- * @property int $id ID
- * @property int $mall_id 商城id
- * @property int $user_id 会员id
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $goods_id 商品ID
- */
- class AreaGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_area_goods';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'goods_id'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'goods_id' => 'Goods ID',
- ];
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @param array $goods
- * @return void
- * @throws \yii\db\Exception
- */
- public static function batchSave(int $mall_id, int $user_id, int $agent_id, array $goods)
- {
- AreaGoods::updateAll(['status' => StatusEnum::DELETE], ['and',
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]],
- ['mall_id' => $mall_id],
- ['agent_id' => $agent_id],
- ['user_id' => $user_id]]);
- $data = [];
- // 添加
- foreach ($goods as $good) {
- $data[] = [
- 'user_id' => $user_id,
- 'goods_id' => $good['goods_id'],
- 'mall_id' => $mall_id,
- 'created_at' => time(),
- 'updated_at' => time(),
- 'agent_id' => $agent_id
- ];
- }
- // 新增数据
- AreaGoods::find()->createCommand()->batchInsert(AreaGoods::tableName(), array_keys($data[0]), $data)->execute();
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @return array
- */
- public static function findAllGoods(int $mall_id, int $user_id)
- {
- $ids = AreaGoods::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('goods_id')
- ->column();
- if (!empty($ids)) {
- Goods::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['id' => $ids]
- ],
- 'select'=> 'id, goods_name, cover_pic, price',
- 'order' => 'id desc'
- ]);
- }
- return [];
- }
- /**
- * @param int $mall_id
- * @param int $agent_id
- * @param int $goods_id
- * @return mixed|null
- */
- public static function existByAgentIdAndGoodsId(int $mall_id, int $agent_id, int $goods_id)
- {
- return AreaGoods::getOne([
- ['mall_id' => $mall_id],
- ['agent_id' => $agent_id],
- ['goods_id' => $goods_id],
- ['status' => StatusEnum::ENABLED]
- ], false, [], null, 'id');
- }
- }
|