| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace addons\BlindBox\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_blind_box_goods".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $goods_id 商品id
- * @property int $cate_id 分类id
- * @property float $contribution 贡献值
- * @property int $contribution_percent 贡献值类型:1:百分比;0:固定值
- * @property float $profit 利润值
- * @property int $profit_percent 利润值类型:1:百分比;0:固定值
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class BlindBoxGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_blind_box_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'cate_id', 'contribution_percent', 'profit_percent', 'status', 'created_at', 'updated_at'], 'integer'],
- [['contribution', 'profit'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'goods_id' => '商品id',
- 'cate_id' => '分类id',
- 'contribution' => '贡献值',
- 'contribution_percent' => '贡献值类型:1:百分比;0:固定值',
- 'profit' => '利润值',
- 'profit_percent' => '利润值类型:1:百分比;0:固定值',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public static function setData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- $res = $model->save();
- if ($res == false) throw new \Exception($model->getErrorMessage());
- BlindBoxCate::updateGoodsNum($model['cate_id']);
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function addData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- foreach ($params['goods_id'] as $goods_id) {
- $where = ['goods_id' => $goods_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- if (!empty($params['mall_id'])) $where['mall_id'] = $params['mall_id'];
- $model = self::findOne($where);
- if (empty($model)) $model = new self();
- $data['goods_id'] = (int)$goods_id;
- $data['mall_id'] = (int)$params['mall_id'];
- $data['contribution_percent'] = $params['contribution_percent'];
- $data['contribution'] = $params['contribution'];
- $data['profit_percent'] = $params['profit_percent'];
- $data['profit'] = $params['profit'];
- $data['cate_id'] = $params['cate_id'];
- $model->loadDefaultValues();
- if (!$model->load($data, '')) throw new \Exception($model->getErrorMessage());
- $res = $model->save();
- if ($res == false) throw new \Exception($model->getErrorMessage());
- }
- BlindBoxCate::updateGoodsNum($params['cate_id']);
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getGoodsList($mall_id, $goods_ids)
- {
- $where[] = ['mall_id' => $mall_id];
- if (!empty($goods_ids)) $where[] = ['goods_id' => $goods_ids];
- $where[] = ['status' => StatusEnum::ENABLED];
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['with'] = ['goods' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED]);
- }];
- $params['limit'] = 10;
- $params['select'] = 'id,goods_id,created_at';
- $list = BlindBoxGoods::lists($params);
- if (!empty($list)) {
- foreach ($list as &$item) {
- $item['goods_name'] = $item['cover_pic'] = '';
- $item['price']= 0 ;
- if(isset($item['goods'])){
- $item['goods_name'] = $item['goods']['goods_name'];
- $item['cover_pic'] = $item['goods']['cover_pic'];
- $item['price'] = $item['goods']['price'];
- unset($item['goods']);
- }
- }
- }
- return $list;
- }
- public static function del($mall_id, $ids)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $res = BlindBoxGoods::updateAll(['status' => StatusEnum::DELETE], ['id' => $ids, 'mall_id' => $mall_id]);
- if ($res === false) throw new \Exception(BlindBoxGoods::getStaticError());
- $list = BlindBoxGoods::find()->where(['id' => $ids, 'mall_id' => $mall_id])->select('id,cate_id')->all();
- foreach ($list as $item) {
- BlindBoxCate::updateGoodsNum($item['cate_id']);
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getGoodsListByDiy($mall_id,$post){
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- if (!empty($post['keyword'])) {
- $goods_list = Goods::lists(['where' => [['mall_id' => $mall_id], ['like', 'goods_name', trim($post['keyword']) . '%', false], ['status' => StatusEnum::ENABLED]], 'select' => 'id']);
- $goods_id_arr = array_column($goods_list, 'id');
- $where[] = ['goods_id' => $goods_id_arr];
- }
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['with'] = ['goods' => function ($query) use($mall_id){
- $query->where(['mall_id'=>$mall_id,'status' => StatusEnum::ENABLED]);
- }];
- $params['limit'] = 10;
- $params['select']='id,goods_id,created_at';
- $list = BlindBoxGoods::listPage($params,false,true);
- if(!empty($list['list'])){
- foreach ($list['list'] as &$item){
- $item['img'] = $item['goods']['cover_pic'];
- $item['title'] = $item['goods']['goods_name'];
- $item['sales_num'] = $item['goods']['sales_num'];
- $item['price'] = '';
- $item['original_price'] = '';
- $item['subtitle'] = '';
- unset($item['goods']);
- }
- }
- return $list;
- }
- }
|