123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace app\models\lala;
- use app\models\mining\UserMiningMachine;
- use app\models\system\SystemMoney;
- use app\models\user\User;
- use app\models\user\UserMoney;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- /**
- * TODO 文章Model
- * Class Article
- * @package app\models\article
- */
- class UserAutoPink extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_auto_pink';
- use ModelTrait;
- /**
- * @param $uid
- * @param $id
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function open($uid, $id): bool
- {
- $activity = AutoPink::validWhere()->where('id', $id)->find();
- if (!$activity) return self::setErrorInfo('未找到有效活动');
- $user = User::getUserInfo($uid);
- if (!$user) return self::setErrorInfo('用户异常');
- $user_cost_money = UserMoney::initialUserMoney($uid, $activity['money_type']);
- if ($user_cost_money['money'] < $activity['money']) return self::setErrorInfo('账户' . $activity['money_type'] . '不足' . $activity['money']);
- $old = self::where('uid', $uid)->where('id', $activity['id'])->order('add_time', 'desc')->find();
- if (!$old) {
- $valid_time = time() + ($activity['valid_date'] * 3600 * 24);
- } else {
- if ($old['is_forever']) return self::setErrorInfo('已开通该拼购的自动拼购服务');
- $valid_time = $old['valid_time'] > time() ? ($old['valid_time'] + ($activity['valid_date'] * 3600 * 24)) : (time() + ($activity['valid_date'] * 3600 * 24));
- }
- BaseModel::beginTrans();
- try {
- //支付
- $res2 = UserMoney::expendMoney($uid, $activity['money_type'], $activity['money'], 'auto_pink', '开通自动拼购', '开通自动拼购花费' . $activity['money'] . $activity['money_type']);
- if (!$res2) {
- return BaseModel::setErrorInfo(UserMoney::getErrorInfo(), true);
- }
- $res = self::create([
- 'auto_id' => $activity['id'],
- 'lala_id' => $activity['lala_id'],
- 'valid_time' => $valid_time,
- 'uid' => $uid,
- 'is_forever' => $activity['is_forever'],
- 'add_time' => time(),
- ]);
- BaseModel::checkTrans($res);
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('开通失败');
- }
- } catch (Exception $e) {
- return BaseModel::setErrorInfo($e->getMessage(), true);
- }
- }
- }
|