BorrowMoneyProduct.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 BorrowMoneyProduct extends BaseModel
  9. {
  10. /**
  11. * 数据表主键
  12. * @var string
  13. */
  14. protected $pk = 'id';
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'borrow_money_product';
  20. use ModelTrait;
  21. /**
  22. * @return BorrowMoneyProduct
  23. */
  24. public static function valid()
  25. {
  26. return self::where('is_del', 0);
  27. }
  28. /**
  29. * @param int $page
  30. * @param int $limit
  31. * @param array $where
  32. * @return array
  33. */
  34. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  35. {
  36. $model = self::valid();
  37. $count = $model->count();
  38. $list = $model->page($page, $limit)->select()->toArray();
  39. return compact('count', 'list');
  40. }
  41. public static function buyPoroduct($id, $uid, $num)
  42. {
  43. $info = self::valid()->where('id', $id)->find();
  44. if (!$info) {
  45. return self::setErrorInfo('产品已下架或不存在');
  46. }
  47. if ($info['personal_limit'] <
  48. UserBorrowMoney::where('mid', $id)
  49. ->where('uid', $uid)
  50. ->where('finish_time', null)
  51. ->sum('money') + $num
  52. ) {
  53. return self::setErrorInfo('借贷上限不足' . $num);
  54. }
  55. $money_type = init_money_type();
  56. BaseModel::beginTrans();
  57. try {
  58. $res1 = UserMoney::incomeMoney($uid, $info['money_type'], $num, 'borrow_money', '借贷', '借贷' . $info['name'] . '*' . $num . $money_type[$info['money_type']]);
  59. if ($res1) {
  60. $res = UserBorrowMoney::create([
  61. 'uid' => $uid,
  62. 'mid' => $id,
  63. 'ratio' => $info['ratio'],
  64. 'money_type' => $info['money_type'],
  65. 'money' => $num,
  66. 'add_time' => time(),
  67. 'send_start_time' => bcadd(time(), $info['stand_time'] * 3600 * 24),
  68. 'status' => $info['stand_time'] == 0 ? 1 : 0,
  69. 'next_send_time' => $info['time'] > 0 ? date('Y-m-d', strtotime('+1day')) : null,
  70. ]);
  71. if (!$res) {
  72. return BaseModel::setErrorInfo('贷款失败', true);
  73. }
  74. } else {
  75. return BaseModel::setErrorInfo('放款失败', true);
  76. }
  77. BaseModel::commitTrans();
  78. return true;
  79. } catch (Exception $e) {
  80. return BaseModel::setErrorInfo($e->getMessage(), true);
  81. }
  82. }
  83. }