ManageMoneyProduct.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\models\manage;
  3. use app\models\user\UserMoney;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. use think\db\Query;
  7. use think\Exception;
  8. class ManageMoneyProduct extends BaseModel
  9. {
  10. /**
  11. * 数据表主键
  12. * @var string
  13. */
  14. protected $pk = 'id';
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'manage_money_product';
  20. protected static $unit = [
  21. 1 => ['chs' => '天', 'time' => 'day'],
  22. 2 => ['chs' => '月', 'time' => 'month'],
  23. 3 => ['chs' => '年', 'time' => 'year'],
  24. ];
  25. use ModelTrait;
  26. /**
  27. * @return ManageMoneyProduct
  28. */
  29. public static function valid()
  30. {
  31. return self::where('is_del', 0);
  32. }
  33. /**
  34. * @param int $page
  35. * @param int $limit
  36. * @param array $where
  37. * @return array
  38. */
  39. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  40. {
  41. $model = self::valid();
  42. $count = $model->count();
  43. $data = $model->page($page, $limit)->select()->each(function ($item) {
  44. $item['_unit'] = self::$unit[$item['unit']]['chs'];
  45. })->toArray();
  46. return compact('count', 'data');
  47. }
  48. public static function buyPoroduct($id, $uid, $num)
  49. {
  50. $info = self::valid()->where('id', $id)->find();
  51. if (!$info) {
  52. return self::setErrorInfo('产品已下架或不存在');
  53. }
  54. if ($info['personal_limit'] <
  55. UserManageMoney::where('mid', $id)
  56. ->where('uid', $uid)
  57. ->where('real_finish_time', null)
  58. ->where('end_time', '<', time())
  59. ->sum('money') + $num
  60. ) {
  61. return self::setErrorInfo('产品购入上限不足' . $num);
  62. }
  63. $money_type = init_money_type();
  64. $user_money = UserMoney::initialUserMoney($uid, $info['money_type']);
  65. if ($user_money['money'] < $num) {
  66. return self::setErrorInfo('购入理财产品所需的' . $money_type[$info['money_type']] . '不足');
  67. }
  68. BaseModel::beginTrans();
  69. try {
  70. $res1 = UserMoney::expendMoney($uid, $info['money_type'], $num, 'buy_manage_product', '购入理财产品', '购入理财产品' . $info['name'] . '*' . $num . $money_type[$info['money_type']]);
  71. if ($res1) {
  72. $res = UserManageMoney::create([
  73. 'uid' => $uid,
  74. 'mid' => $id,
  75. 'ratio' => $info['ratio'],
  76. 'money_type' => $info['money_type'],
  77. 'money' => $num,
  78. 'time' => $info['time'],
  79. 'unit' => $info['unit'],
  80. // 'stand_money_type' => $info['stand_money_type'],
  81. 'add_time' => time(),
  82. 'end_time' => $info['time'] > 0 ? strtotime('+' . $info['time'] . ' ' . self::$unit[$info['unit']]['en'] . ($info['time'] > 1 ? 's' : '')) : 9999999999,
  83. 'send_start_time' => bcadd(time(), $info['stand_time'] * 3600 * 24),
  84. 'status' => $info['stand_time'] == 0 ? 1 : 0,
  85. 'next_send_time' => $info['time'] > 0 ? date('Y-m-d', strtotime('+' . $info['time'] . ' ' . self::$unit[$info['unit']]['en'] . ($info['time'] > 1 ? 's' : ''))) : null,
  86. ]);
  87. if (!$res) {
  88. return BaseModel::setErrorInfo('购入失败', true);
  89. }
  90. } else {
  91. return BaseModel::setErrorInfo('支付失败', true);
  92. }
  93. BaseModel::commitTrans();
  94. return true;
  95. } catch (Exception $e) {
  96. return BaseModel::setErrorInfo($e->getMessage(), true);
  97. }
  98. }
  99. }