PointPlan.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\models\point_plan;
  3. use app\models\user\UserMoney;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. use think\Collection;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Exception;
  11. class PointPlan extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'point_plan';
  23. use ModelTrait;
  24. public static function joinPlan($id, $uid)
  25. {
  26. $info = self::where('is_del', 0)->where('id', $id)->find();
  27. $money = UserMoney::initialUserMoney($uid, $info['buy_money_type']);
  28. $money_num = bcmul($info['buy_num'], $info['buy_price'], 8);
  29. if ($money['money'] < $money_num) return self::setErrorInfo('账户不足!');
  30. BaseModel::beginTrans();
  31. try {
  32. $res = UserMoney::expendMoney($uid, $info['buy_money_type'], $money_num, 'join_point_plan', '认购节点', '认购' . $info['name'] . '节点');
  33. if (!$res) {
  34. return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  35. }
  36. $res = UserPointPlan::create([
  37. 'buy_num' => $info['buy_num'],
  38. 'buy_all_price' => $money_num,
  39. 'release_ratio' => $info['release_ratio'],
  40. 'release_money_type' => $info['release_money_type'],
  41. 'buy_money_type' => $info['buy_money_type'],
  42. 'check_end_time' => bcadd(time(), bcmul($info['exam_time'], 24 * 3600)),
  43. 'layer_award_ratio' => $info['layer_award_ratio'],
  44. 'add_time' => time(),
  45. 'layer_award_layer' => $info['layer_award_layer'],
  46. 'uid' => $uid,
  47. 'release_time_all' => $info['release_day'],
  48. 'plan_id' => $info['id'],
  49. ]);
  50. if ($res) {
  51. BaseModel::commitTrans();
  52. return $res;
  53. } else {
  54. return self::setErrorInfo('认购失败', true);
  55. }
  56. } catch (Exception $e) {
  57. return self::setErrorInfo($e->getMessage(), true);
  58. }
  59. }
  60. /**
  61. * @param $where
  62. * @return array
  63. */
  64. public static function getList($where)
  65. {
  66. $model = new self();
  67. if (isset($where['title']) && $where['title'] != '') {
  68. $model->where('id|plan_name', 'like', "%{$where['title']}%");
  69. }
  70. $count = $model->count();
  71. $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  72. $item['join_number'] = UserPointPlan::where('plan_id', $item['id'])->count();
  73. $item['_start_time'] = date('Y-m-d H:i:s', $item['start_time']);
  74. });
  75. return compact('count', 'data');
  76. }
  77. }