PkActivity.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace addons\Pk\common\models;
  3. use addons\Pk\common\enums\PkEnum;
  4. use common\enums\StatusEnum;
  5. use common\enums\UserWalletEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\common\PaymentTrade;
  8. use common\models\common\PaymentTradeOrder;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%addons_pk_activity}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id
  15. * @property string $name 活动名称
  16. * @property int $is_enable 是否开启
  17. * @property string|null $begin_time 开始时间
  18. * @property string|null $end_time 结束时间
  19. * @property int $performance_type 类型;1拉新;2业绩
  20. * @property int $people_max_amount 人数最大数量;-1为不限制
  21. * @property int $order_amount 业绩
  22. * @property float $price 费用;金额;
  23. * @property int $activity_status 0未开始;1进行中;2已结束;3已取消
  24. * @property string $prize 奖品
  25. * @property string $share_title 分享标题
  26. * @property string $share_pic 分享图片
  27. * @property int $activity_type 1个人;2战队;
  28. * @property int $new_num 拉新人数
  29. * @property int|null $settlement_type 结算类型
  30. * @property int $member_num 活动参与会员数量
  31. * @property int $status 状态[-1:删除;0:禁用;1启用]
  32. * @property int $created_at
  33. * @property int $updated_at
  34. */
  35. class PkActivity extends \common\models\base\BaseActiveRecord
  36. {
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%addons_pk_activity}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['mall_id', 'name', 'performance_type', 'prize', 'activity_type', 'created_at', 'updated_at'], 'required'],
  51. [['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'],
  52. [['begin_time', 'end_time'], 'safe'],
  53. [['price', 'order_amount'], 'number'],
  54. [['prize'], 'string'],
  55. [['name', 'share_title'], 'string', 'max' => 255],
  56. [['share_pic'], 'string', 'max' => 1024],
  57. ];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'mall_id' => 'Mall ID',
  67. 'name' => '活动名称',
  68. 'is_enable' => '是否开启',
  69. 'begin_time' => '开始时间',
  70. 'end_time' => '结束时间',
  71. 'performance_type' => '类型;1拉新;2业绩',
  72. 'settlement_type' => '结算方式:1支付后;2订单完成',
  73. 'people_max_amount' => '人数最大数量;-1为不限制',
  74. 'order_amount' => '业绩',
  75. 'price' => '费用;金额;',
  76. 'activity_status' => '0未开始;1进行中;2已结束;3已取消',
  77. 'prize' => '奖品',
  78. 'share_title' => '分享标题',
  79. 'share_pic' => '分享图片',
  80. 'activity_type' => '1个人;2战队;',
  81. 'new_num' => '拉新人数',
  82. 'settlement_type' => '结算类型',
  83. 'member_num' => '活动参与会员数量',
  84. 'status' => '状态[-1:删除;0:禁用;1启用]',
  85. 'created_at' => 'Created At',
  86. 'updated_at' => 'Updated At',
  87. ];
  88. }
  89. /**
  90. * 是否合法开始时间
  91. * @param $begin_time
  92. * @param $mall_id
  93. * @param $current_time
  94. * @return bool
  95. */
  96. public static function isLegalBeginTime($begin_time, $mall_id, $current_time, $activity_type, $id = null)
  97. {
  98. if (strtotime($begin_time) <= $current_time) {
  99. return false;
  100. }
  101. $query = self::find()->where(['mall_id' => $mall_id, 'status' => 1, 'activity_type' => $activity_type])->andWhere(['>=', 'end_time', $begin_time])
  102. ->andWhere(['in', 'activity_status', [PkEnum::STATUS_NOT_START, PkEnum::STATUS_STARTED, PkEnum::STATUS_END]]);
  103. if ($id) {
  104. $query->andWhere(['!=', 'id', $id]);
  105. }
  106. $exist = $query->asArray()->one();
  107. if ($exist) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. public static function setData(array $params)
  113. {
  114. $t = Yii::$app->db->beginTransaction();
  115. try {
  116. $is_new = PkEnum::STATISTIC_0;
  117. $num = 0;
  118. if (!empty($params['id'])) {
  119. $model = self::getOne([['id' => $params['id']]]);
  120. if (!$model) {
  121. throw new \Exception('活动数据不存在');
  122. }
  123. } else {
  124. $num = 1;
  125. $is_new = PkEnum::STATISTIC_1;
  126. $params['activity_num'] = $num;
  127. $params['date'] = date('Ymd');
  128. PkDateStatistic::setData($params);
  129. PkStatistic::setData($params);
  130. $model = new self();
  131. }
  132. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  133. if (isset($params['is_enable'])) {
  134. if ($params['is_enable'] == PkEnum::IS_ENABLE) {
  135. if(!$params['id']){
  136. $where[] = ['=','mall_id',$params['mall_id']];
  137. $where[] = ['=','status',StatusEnum::ENABLED];
  138. $where[] = ['>=','end_time',$params['begin_time']];
  139. $activity_data = self::getOne($where,true);
  140. if ($activity_data) {
  141. throw new \Exception("该时间段已经有活动了,请选择没有占用的时间段", 1002);
  142. }
  143. }
  144. $model->activity_status = PkEnum::STATUS_NOT_START;
  145. } else {
  146. $is_new = PkEnum::STATUS_CANCEL;
  147. $num = -1;
  148. $model->activity_status = PkEnum::STATUS_CANCEL;
  149. }
  150. }
  151. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  152. $t->commit();
  153. return $model;
  154. } catch (\Exception $e) {
  155. $t->rollBack();
  156. self::$static_error = $e->getMessage();
  157. return false;
  158. }
  159. }
  160. /**
  161. * @param $mall_id
  162. * @param $activity_type
  163. * @param int $limit
  164. * @param bool $is_array
  165. * @return array|\yii\db\ActiveRecord[]
  166. */
  167. public static function getFinishActivityList($mall_id, $activity_type, $limit = 10, $is_array = true)
  168. {
  169. return self::find()
  170. ->select('id,name')
  171. ->where(array('mall_id' => $mall_id, 'activity_type' => $activity_type, 'activity_status' => PkEnum::STATUS_END, 'status' => StatusEnum::ENABLED))
  172. ->limit($limit)
  173. ->orderBy('id DESC')
  174. ->asArray($is_array)
  175. ->all();
  176. }
  177. /**
  178. * 获取单条数据
  179. * @param $id
  180. * @param $mall_id
  181. * @return array|\yii\db\ActiveRecord|null
  182. */
  183. public function getOneItem($id, $mall_id, $as_array = false)
  184. {
  185. return self::find()->where([
  186. 'id' => $id,
  187. 'mall_id' => $mall_id,
  188. ])
  189. ->asArray($as_array)->one();
  190. }
  191. /**
  192. * 个人活动报名状态
  193. * @param $activity_id
  194. * @param $user_id
  195. * @param $mall_id
  196. * @return int
  197. */
  198. public static function getPersonalActivityApplyStatus($activity_id, $user_id, $mall_id)
  199. {
  200. $personal_member = PkActivityStatistic::findOne([
  201. 'activity_id' => $activity_id,
  202. 'mall_id' => $mall_id,
  203. 'user_id' => $user_id
  204. ]);
  205. if ($personal_member) {
  206. return PkEnum::PERSON_ACTIVITY_APPLY_PASS;//已经加入了
  207. }
  208. if (self::isPersonActivityNumFull($activity_id, $mall_id)) {
  209. return PkEnum::PERSON_ACTIVITY_APPLY_STATUS_FULL;//已经满人
  210. }
  211. return PkEnum::PERSON_ACTIVITY_APPLY_ALLOW;//可以申请
  212. }
  213. /**
  214. * 个人活动队员是否已满
  215. * @param $activity_id
  216. * @param $mall_id
  217. * @return bool
  218. */
  219. public static function isPersonActivityNumFull($activity_id, $mall_id)
  220. {
  221. $activity = self::getOneActivity($activity_id, $mall_id);
  222. if ($activity && $activity['member_num'] >= $activity['people_max_amount'] && $activity['people_max_amount'] != PkEnum::PEOPLE_NUM_NO_LIMIT) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. /**
  228. * 获取指定活动
  229. * @param $activity_id
  230. * @param $mall_id
  231. * @return array|\yii\db\ActiveRecord|null
  232. */
  233. public static function getOneActivity($activity_id, $mall_id)
  234. {
  235. return PkActivity::find()->where([
  236. 'id' => $activity_id,
  237. 'mall_id' => $mall_id])
  238. ->select('people_max_amount,member_num')
  239. ->asArray(true)
  240. ->one();
  241. }
  242. /**
  243. * 参加活动是否已超时
  244. */
  245. public static function isLegalApplyTime($activity, $now_time = null)
  246. {
  247. if (!$now_time) {
  248. $now_time = time();
  249. }
  250. $PkSetting = \Yii::$app->services->setting->addons->get($activity->mall_id, PkEnum::ADDONS_NAME, PkEnum::KEY_RULES);
  251. $not_allow_apply_hour = $PkSetting['not_allow_apply_hour'] ?? 0;
  252. if (!$not_allow_apply_hour) {
  253. return true;
  254. }
  255. if ($not_allow_apply_hour == PkSetting::ALLOW_APPLY_HOUR) {
  256. return true;
  257. }
  258. $not_allow_apply_seconds = $not_allow_apply_hour * 3600; //小时转秒数;
  259. $unix_end_time_seconds = strtotime($activity->end_time);
  260. $unix_remaining_time_seconds = $unix_end_time_seconds - $now_time;
  261. if ($unix_remaining_time_seconds < $not_allow_apply_seconds) {
  262. return false;
  263. }
  264. return true;
  265. }
  266. // 获取当前的PK活动
  267. public static function getCurrentPkActivity($mall_id)
  268. {
  269. $where[] = ['=','status',StatusEnum::ENABLED];
  270. $where[] = ['=','mall_id', $mall_id];
  271. $where[] = ['<=', 'begin_time', date('Y-m-d H:i:s')];
  272. $where[] = ['>=', 'end_time', date('Y-m-d H:i:s')];
  273. $get_active = self::getOne($where,true, [], ['id' => SORT_DESC]);
  274. if (!$get_active) {
  275. $get_active = self::getOne([
  276. ['=','status',StatusEnum::ENABLED],
  277. ['=','mall_id', $mall_id],
  278. ['<=', 'begin_time', date('Y-m-d H:i:s')]
  279. ],true, [], ['id' => SORT_DESC]);
  280. }
  281. if ($get_active) {
  282. $get_active['prize'] = json_decode($get_active['prize'] ?? "", true);
  283. }
  284. return $get_active;
  285. }
  286. }