UserAutoPink.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\models\lala;
  3. use app\models\mining\UserMiningMachine;
  4. use app\models\system\SystemMoney;
  5. use app\models\user\User;
  6. use app\models\user\UserMoney;
  7. use Exception;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. /**
  14. * TODO 文章Model
  15. * Class Article
  16. * @package app\models\article
  17. */
  18. class UserAutoPink extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'user_auto_pink';
  30. use ModelTrait;
  31. /**
  32. * @param $uid
  33. * @param $id
  34. * @return bool
  35. * @throws DataNotFoundException
  36. * @throws DbException
  37. * @throws ModelNotFoundException
  38. */
  39. public static function open($uid, $id): bool
  40. {
  41. $activity = AutoPink::validWhere()->where('id', $id)->find();
  42. if (!$activity) return self::setErrorInfo('未找到有效活动');
  43. $user = User::getUserInfo($uid);
  44. if (!$user) return self::setErrorInfo('用户异常');
  45. $user_cost_money = UserMoney::initialUserMoney($uid, $activity['money_type']);
  46. if ($user_cost_money['money'] < $activity['money']) return self::setErrorInfo('账户' . $activity['money_type'] . '不足' . $activity['money']);
  47. $old = self::where('uid', $uid)->where('id', $activity['id'])->order('add_time', 'desc')->find();
  48. if (!$old) {
  49. $valid_time = time() + ($activity['valid_date'] * 3600 * 24);
  50. } else {
  51. if ($old['is_forever']) return self::setErrorInfo('已开通该拼购的自动拼购服务');
  52. $valid_time = $old['valid_time'] > time() ? ($old['valid_time'] + ($activity['valid_date'] * 3600 * 24)) : (time() + ($activity['valid_date'] * 3600 * 24));
  53. }
  54. BaseModel::beginTrans();
  55. try {
  56. //支付
  57. $res2 = UserMoney::expendMoney($uid, $activity['money_type'], $activity['money'], 'auto_pink', '开通自动拼购', '开通自动拼购花费' . $activity['money'] . $activity['money_type']);
  58. if (!$res2) {
  59. return BaseModel::setErrorInfo(UserMoney::getErrorInfo(), true);
  60. }
  61. $res = self::create([
  62. 'auto_id' => $activity['id'],
  63. 'lala_id' => $activity['lala_id'],
  64. 'valid_time' => $valid_time,
  65. 'uid' => $uid,
  66. 'is_forever' => $activity['is_forever'],
  67. 'add_time' => time(),
  68. ]);
  69. BaseModel::checkTrans($res);
  70. if ($res) {
  71. return true;
  72. } else {
  73. return self::setErrorInfo('开通失败');
  74. }
  75. } catch (Exception $e) {
  76. return BaseModel::setErrorInfo($e->getMessage(), true);
  77. }
  78. }
  79. }