| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- namespace addons\Bargain\common\models;
- use addons\Bargain\common\enums\BargainEnum;
- use addons\GroupBuy\common\enums\GroupBuyEnum;
- use addons\GroupBuy\common\models\GroupBuyActiveGoodsAttr;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsMarketing;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_bargain_active".
- *
- * @property int $id
- * @property int $cate_id 分类
- * @property int $goods_id
- * @property int $attr_id
- * @property int $mall_id
- * @property float $min_price 最低价
- * @property int $begin_time 活动开始时间
- * @property int $end_time 活动结束时间
- * @property int $time 砍价小时数
- * @property int $type 是否允许中途下单 1--允许 2--不允许
- * @property int $stock 活动库存
- * @property string $mode_data 砍价方式数据
- * @property int $active_status 活动状态, 0--未开始,1--进行中, 2 -- 已结束
- * @property int $initiator 发起人数
- * @property int $participant 参与人数
- * @property int $min_price_goods 砍到最小价格数
- * @property int $underway 进行中的
- * @property int $success 成功的
- * @property int $fail 失败的
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property string title
- */
- class BargainActive extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_bargain_active}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules(): array
- {
- return [
- [['cate_id', 'attr_id', 'goods_id', 'mall_id', 'begin_time', 'end_time', 'time', 'type', 'stock', 'active_status', 'initiator', 'participant', 'min_price_goods', 'underway', 'success', 'fail', 'status', 'created_at', 'updated_at', 'is_repeat_bargain', 'bargain_time','join_time', 'is_creator_exempt'], 'integer'],
- [['min_price'], 'number'],
- [['mode_data'], 'string', 'max' => 255],
- [['introduction', 'description','title'], 'string'],
- [['join_limit_user_level', 'bargain_limit_user_level'], 'safe'],
- [['title'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels(): array
- {
- return [
- 'id' => 'ID',
- 'title' => '拼团活动名称',
- 'cate_id' => 'cate_id',
- 'goods_id' => 'Goods ID',
- 'attr_id' => 'Attr ID',
- 'mall_id' => 'Mall ID',
- 'min_price' => '最低价',
- 'begin_time' => '活动开始时间',
- 'end_time' => '活动结束时间',
- 'time' => '砍价小时数',
- 'type' => '是否允许中途下单 1--允许 2--不允许',
- 'stock' => '活动库存',
- 'mode_data' => '砍价方式数据',
- 'active_status' => '活动状态,-1 -- 已结束, 0--未开始,1--进行中',
- 'initiator' => '发起人数',
- 'participant' => '参与人数',
- 'min_price_goods' => '砍到最小价格数',
- 'underway' => '进行中的',
- 'success' => '成功的',
- 'fail' => '失败的',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- // 关联模型
- public function getGoods(): \yii\db\ActiveQuery
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getGoodsAttr(): \yii\db\ActiveQuery
- {
- return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']);
- }
- public function getCate(): \yii\db\ActiveQuery
- {
- return $this->hasOne(BargainCate::class, ['id' => 'cate_id']);
- }
- /**
- * @desc:编辑
- * @Author: hua
- * @Date: 2022/1/3
- * @Time: 10:16
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return BargainActive|false
- */
- public static function setData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if(!empty($params['mode_data'])) $params['mode_data'] = json_encode($params['mode_data']);
- if (!empty($params['goods_id'])) {
- $attr = GoodsAttr::findOne(['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]);
- $params['attr_id'] = $attr['id'] ?? 0;
- }
- if (!empty($params['active_date'][0])) $params['begin_time'] = strtotime($params['active_date'][0]);
- if (!empty($params['active_date'][1])) $params['end_time'] = strtotime($params['active_date'][1]);
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if ($model->begin_time < time()) $model->active_status = BargainEnum::ACTIVE_HANDING;
- if ($model->end_time < time()) $model->active_status = BargainEnum::ACTIVE_FINISH;
- $res = $model->save();
- if ($res == false) throw new \Exception($model->getErrorMessage());
- if ($model->active_status == BargainEnum::ACTIVE_FINISH) $params['status'] = StatusEnum::DELETE;
- $params['marketing_id'] = $model->id;
- $params['marketing_type'] = BargainEnum::ADDONS_NAME;
- $res = GoodsMarketing::setData($params);
- if ($res == false) throw new \Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:失效操作
- * @Author: hua
- * @Date: 2022/1/3
- * @Time: 10:10
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function invalidData($params): bool
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $res = BargainOrder::findOne(['bargain_active_id' => $params['id'], 'order_status' => [BargainEnum::ORDER_HANDING], 'status' => StatusEnum::ENABLED]);
- if ($res) throw new \Exception('该活动有会员参与中');
- $res = self::updateAll(['status' => StatusEnum::DISABLED, 'active_status' => BargainEnum::ACTIVE_FINISH], ['mall_id' => $params['mall_id'], 'id' => $params['id']]);
- if ($res == false) throw new \Exception(self::getStaticError());
- $params['marketing_id'] = $params['id'];
- $params['marketing_type'] = BargainEnum::ADDONS_NAME;
- $params['status'] = StatusEnum::DISABLED;
- $res = GoodsMarketing::setData($params);
- if ($res == false) throw new \Exception(GoodsMarketing::getStaticError());
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:活动列表
- * @Author: hua
- * @Date: 2022/1/5
- * @Time: 15:50
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $where
- * @param false $is_page
- * @return array
- */
- public static function getActiveList($where, $is_page = false): array
- {
- $params['where'] = $where;
- $params['order'] = 'status_order asc,id DESC';
- $params['select'] = 'id,title,goods_id,begin_time,end_time,active_status,min_price,stock,introduction,(CASE active_status WHEN 1 THEN 1 WHEN 0 THEN 2 ELSE 3 END) AS status_order';
- $params['with'] = [
- 'goods' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED])->select('id,goods_name,cover_pic')->with(['attr']);
- },
- ];
- $function = function (&$item) {
- $item['price'] = $item['goods']['attr'][0]['price'];
- $item['goods_name'] = $item['goods']['goods_name'];
- $item['cover_pic'] = $item['goods']['cover_pic'];
- $item['goods_id'] = $item['goods']['id'];
- $item['title'] = empty($item['title']) ? $item['goods_name'] : $item['title'];
- unset($item['goodsAttr']);
- unset($item['goods']);
- return $item;
- };
- if ($is_page) {
- $list = self::listPage($params, false);
- if ($list === false) return [];
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$item) {
- $function($item);
- }
- }
- } else {
- $list = self::lists($params);
- if ($list === false) return [];
- if (!empty($list)) {
- foreach ($list as &$item) {
- $function($item);
- }
- }
- }
- return $list;
- }
- public static function getActiveListByDiy($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) {
- $query->where(['status' => StatusEnum::ENABLED]);
- }];
- $params['limit'] = 10;
- $params['select'] = 'id,goods_id,created_at';
- $list = self::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']);
- unset($item['goods_id']);
- }
- }
- return $list;
- }
- /**
- * @desc:扣减规格库存
- * @Author: hua
- * @Date: 2022/1/5
- * @Time: 17:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $active_id
- * @param $num
- * @return bool
- */
- public static function handleStock($mall_id, $active_id, $num): bool
- {
- try {
- $model = self::findOne(['mall_id' => $mall_id, 'id' => $active_id]);
- if (empty($model)) throw new \Exception('规格不存在');
- if (!is_int($num)) throw new \Exception('库存数量必须为整数');
- if ($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
- $model->stock += $num;
- $model->fail += 1;
- $res = $model->save();
- if ($res === false) throw new \Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * @desc:分享砍价活动详情
- * @Author: hua
- * @Date: 2022/1/14
- * @Time: 17:14
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $active_id
- * @param $mall_id
- * @return array
- */
- public static function getShareDetail($active_id, $mall_id): array
- {
- $active = self::getOne([['id' => $active_id, 'mall_id' => $mall_id]], true, ['goods']);
- $data['item_id'] = $active['id'];
- $data['price'] = $active['goods']['price'];
- $data['min_price'] = $active['min_price'];
- $data['share_title'] = $active['goods']['goods_name'];
- $data['share_img'] = $active['goods']['cover_pic'];
- return $data;
- }
- }
|