| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- namespace addons\Pk\common\models;
- use addons\Pk\common\enums\PkEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\PaymentTrade;
- use common\models\common\PaymentTradeOrder;
- use Yii;
- /**
- * This is the model class for table "{{%addons_pk_activity}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property string $name 活动名称
- * @property int $is_enable 是否开启
- * @property string|null $begin_time 开始时间
- * @property string|null $end_time 结束时间
- * @property int $performance_type 类型;1拉新;2业绩
- * @property int $people_max_amount 人数最大数量;-1为不限制
- * @property int $order_amount 业绩
- * @property float $price 费用;金额;
- * @property int $activity_status 0未开始;1进行中;2已结束;3已取消
- * @property string $prize 奖品
- * @property string $share_title 分享标题
- * @property string $share_pic 分享图片
- * @property int $activity_type 1个人;2战队;
- * @property int $new_num 拉新人数
- * @property int|null $settlement_type 结算类型
- * @property int $member_num 活动参与会员数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class PkActivity extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_pk_activity}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'name', 'performance_type', 'prize', 'activity_type', 'created_at', 'updated_at'], 'required'],
- [['mall_id', 'is_enable', 'performance_type', 'people_max_amount', 'activity_status', 'activity_type', 'new_num', 'settlement_type', 'member_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['begin_time', 'end_time'], 'safe'],
- [['price', 'order_amount'], 'number'],
- [['prize'], 'string'],
- [['name', 'share_title'], 'string', 'max' => 255],
- [['share_pic'], 'string', 'max' => 1024],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'name' => '活动名称',
- 'is_enable' => '是否开启',
- 'begin_time' => '开始时间',
- 'end_time' => '结束时间',
- 'performance_type' => '类型;1拉新;2业绩',
- 'settlement_type' => '结算方式:1支付后;2订单完成',
- 'people_max_amount' => '人数最大数量;-1为不限制',
- 'order_amount' => '业绩',
- 'price' => '费用;金额;',
- 'activity_status' => '0未开始;1进行中;2已结束;3已取消',
- 'prize' => '奖品',
- 'share_title' => '分享标题',
- 'share_pic' => '分享图片',
- 'activity_type' => '1个人;2战队;',
- 'new_num' => '拉新人数',
- 'settlement_type' => '结算类型',
- 'member_num' => '活动参与会员数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 是否合法开始时间
- * @param $begin_time
- * @param $mall_id
- * @param $current_time
- * @return bool
- */
- public static function isLegalBeginTime($begin_time, $mall_id, $current_time, $activity_type, $id = null)
- {
- if (strtotime($begin_time) <= $current_time) {
- return false;
- }
- $query = self::find()->where(['mall_id' => $mall_id, 'status' => 1, 'activity_type' => $activity_type])->andWhere(['>=', 'end_time', $begin_time])
- ->andWhere(['in', 'activity_status', [PkEnum::STATUS_NOT_START, PkEnum::STATUS_STARTED, PkEnum::STATUS_END]]);
- if ($id) {
- $query->andWhere(['!=', 'id', $id]);
- }
- $exist = $query->asArray()->one();
- if ($exist) {
- return false;
- }
- return true;
- }
- public static function setData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $is_new = PkEnum::STATISTIC_0;
- $num = 0;
- if (!empty($params['id'])) {
- $model = self::getOne([['id' => $params['id']]]);
- if (!$model) {
- throw new \Exception('活动数据不存在');
- }
- } else {
- $num = 1;
- $is_new = PkEnum::STATISTIC_1;
- $params['activity_num'] = $num;
- $params['date'] = date('Ymd');
- PkDateStatistic::setData($params);
- PkStatistic::setData($params);
- $model = new self();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (isset($params['is_enable'])) {
- if ($params['is_enable'] == PkEnum::IS_ENABLE) {
- if(!$params['id']){
- $where[] = ['=','mall_id',$params['mall_id']];
- $where[] = ['=','status',StatusEnum::ENABLED];
- $where[] = ['>=','end_time',$params['begin_time']];
- $activity_data = self::getOne($where,true);
- if ($activity_data) {
- throw new \Exception("该时间段已经有活动了,请选择没有占用的时间段", 1002);
- }
- }
- $model->activity_status = PkEnum::STATUS_NOT_START;
- } else {
- $is_new = PkEnum::STATUS_CANCEL;
- $num = -1;
- $model->activity_status = PkEnum::STATUS_CANCEL;
- }
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @param $mall_id
- * @param $activity_type
- * @param int $limit
- * @param bool $is_array
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getFinishActivityList($mall_id, $activity_type, $limit = 10, $is_array = true)
- {
- return self::find()
- ->select('id,name')
- ->where(array('mall_id' => $mall_id, 'activity_type' => $activity_type, 'activity_status' => PkEnum::STATUS_END, 'status' => StatusEnum::ENABLED))
- ->limit($limit)
- ->orderBy('id DESC')
- ->asArray($is_array)
- ->all();
- }
- /**
- * 获取单条数据
- * @param $id
- * @param $mall_id
- * @return array|\yii\db\ActiveRecord|null
- */
- public function getOneItem($id, $mall_id, $as_array = false)
- {
- return self::find()->where([
- 'id' => $id,
- 'mall_id' => $mall_id,
- ])
- ->asArray($as_array)->one();
- }
- /**
- * 个人活动报名状态
- * @param $activity_id
- * @param $user_id
- * @param $mall_id
- * @return int
- */
- public static function getPersonalActivityApplyStatus($activity_id, $user_id, $mall_id)
- {
- $personal_member = PkActivityStatistic::findOne([
- 'activity_id' => $activity_id,
- 'mall_id' => $mall_id,
- 'user_id' => $user_id
- ]);
- if ($personal_member) {
- return PkEnum::PERSON_ACTIVITY_APPLY_PASS;//已经加入了
- }
- if (self::isPersonActivityNumFull($activity_id, $mall_id)) {
- return PkEnum::PERSON_ACTIVITY_APPLY_STATUS_FULL;//已经满人
- }
- return PkEnum::PERSON_ACTIVITY_APPLY_ALLOW;//可以申请
- }
- /**
- * 个人活动队员是否已满
- * @param $activity_id
- * @param $mall_id
- * @return bool
- */
- public static function isPersonActivityNumFull($activity_id, $mall_id)
- {
- $activity = self::getOneActivity($activity_id, $mall_id);
- if ($activity && $activity['member_num'] >= $activity['people_max_amount'] && $activity['people_max_amount'] != PkEnum::PEOPLE_NUM_NO_LIMIT) {
- return true;
- }
- return false;
- }
- /**
- * 获取指定活动
- * @param $activity_id
- * @param $mall_id
- * @return array|\yii\db\ActiveRecord|null
- */
- public static function getOneActivity($activity_id, $mall_id)
- {
- return PkActivity::find()->where([
- 'id' => $activity_id,
- 'mall_id' => $mall_id])
- ->select('people_max_amount,member_num')
- ->asArray(true)
- ->one();
- }
- /**
- * 参加活动是否已超时
- */
- public static function isLegalApplyTime($activity, $now_time = null)
- {
- if (!$now_time) {
- $now_time = time();
- }
- $PkSetting = \Yii::$app->services->setting->addons->get($activity->mall_id, PkEnum::ADDONS_NAME, PkEnum::KEY_RULES);
- $not_allow_apply_hour = $PkSetting['not_allow_apply_hour'] ?? 0;
- if (!$not_allow_apply_hour) {
- return true;
- }
- if ($not_allow_apply_hour == PkSetting::ALLOW_APPLY_HOUR) {
- return true;
- }
- $not_allow_apply_seconds = $not_allow_apply_hour * 3600; //小时转秒数;
- $unix_end_time_seconds = strtotime($activity->end_time);
- $unix_remaining_time_seconds = $unix_end_time_seconds - $now_time;
- if ($unix_remaining_time_seconds < $not_allow_apply_seconds) {
- return false;
- }
- return true;
- }
- // 获取当前的PK活动
- public static function getCurrentPkActivity($mall_id)
- {
- $where[] = ['=','status',StatusEnum::ENABLED];
- $where[] = ['=','mall_id', $mall_id];
- $where[] = ['<=', 'begin_time', date('Y-m-d H:i:s')];
- $where[] = ['>=', 'end_time', date('Y-m-d H:i:s')];
- $get_active = self::getOne($where,true, [], ['id' => SORT_DESC]);
- if (!$get_active) {
- $get_active = self::getOne([
- ['=','status',StatusEnum::ENABLED],
- ['=','mall_id', $mall_id],
- ['<=', 'begin_time', date('Y-m-d H:i:s')]
- ],true, [], ['id' => SORT_DESC]);
- }
- if ($get_active) {
- $get_active['prize'] = json_decode($get_active['prize'] ?? "", true);
- }
- return $get_active;
- }
- }
|