RechargeOrder.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use think\Model;
  6. /**
  7. * @mixin \think\Model
  8. */
  9. class RechargeOrder extends BaseModel
  10. {
  11. /**
  12. * 生成充值订单号
  13. * @param int $uid 用户UID
  14. * @return string
  15. */
  16. public function mkOrderSn($uid)
  17. {
  18. return 'RO' . time() . rand(1000, 9000) . $uid;
  19. }
  20. /**
  21. * 创建充值订单
  22. * @param int $uid 用户UID
  23. * @param int $rechargeId 充值配置ID(0为自定义充值)
  24. * @param float $price 充值金额
  25. * @param int $integral 获得积分
  26. * @param int $giveIntegral 赠送积分
  27. * @param string $payType 支付方式:wxpay/alipay/system
  28. * @param int $adminId 管理员ID(后台充值)
  29. * @param string $remark 备注
  30. * @param float $discount 折扣金额
  31. * @return string|null 订单号
  32. */
  33. public function createOrder($uid, $rechargeId, $price, $integral, $giveIntegral, $payType = 'wxpay', $adminId = 0, $remark = '', $discount = 0)
  34. {
  35. $orderSn = $this->mkOrderSn($uid);
  36. $totalIntegral = $integral + $giveIntegral;
  37. $bool = $this->insert([
  38. 'order_id' => $orderSn,
  39. 'uid' => $uid,
  40. 'recharge_id' => $rechargeId,
  41. 'price' => $price,
  42. 'integral' => $integral,
  43. 'give_integral' => $giveIntegral,
  44. 'total_integral' => $totalIntegral,
  45. 'pay_type' => $payType,
  46. 'paid' => 0,
  47. 'pay_time' => 0,
  48. 'remark' => $remark,
  49. 'admin_id' => $adminId,
  50. 'discount_amount' => $discount,
  51. 'status' => 0,
  52. 'add_time' => time(),
  53. 'update_time' => time()
  54. ]);
  55. return $bool ? $orderSn : null;
  56. }
  57. /**
  58. * 根据订单号获取订单
  59. * @param string $orderSn 订单号
  60. * @return array|null
  61. */
  62. public function getByOrderSn($orderSn)
  63. {
  64. return $this->where('order_id', $orderSn)->find();
  65. }
  66. /**
  67. * 根据ID获取订单
  68. * @param int $id 订单ID
  69. * @return array|null
  70. */
  71. public function getById($id)
  72. {
  73. return $this->where('id', $id)->find();
  74. }
  75. /**
  76. * 更新订单支付状态
  77. * @param string $orderSn 订单号
  78. * @param string $paySn 支付流水号
  79. * @param string $payJson 支付返回信息
  80. * @return bool
  81. */
  82. public function updatePayStatus($orderSn, $paySn, $payJson = '')
  83. {
  84. return $this->where('order_id', $orderSn)->update([
  85. 'paid' => 1,
  86. 'pay_sn' => $paySn,
  87. 'pay_json' => $payJson,
  88. 'pay_time' => time(),
  89. 'status' => 1,
  90. 'update_time' => time()
  91. ]);
  92. }
  93. /**
  94. * 获取用户充值订单列表
  95. * @param int $uid 用户UID
  96. * @param array $where 查询条件
  97. * @param string $field 查询字段
  98. * @param int $page 页码
  99. * @param int $pageCount 每页数量
  100. * @param string $order 排序
  101. * @return array
  102. */
  103. public function getUserList($uid, $where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  104. {
  105. $where['uid'] = $uid;
  106. $data = $this
  107. ->where($where)
  108. ->field($field)
  109. ->order($order)
  110. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  111. ->toArray();
  112. return [$data['total'], $data['data']];
  113. }
  114. /**
  115. * 获取充值订单列表(后台)
  116. * @param array $where 查询条件
  117. * @param string $field 查询字段
  118. * @param int $page 页码
  119. * @param int $pageCount 每页数量
  120. * @param string $order 排序
  121. * @return array
  122. */
  123. public function getAdminList($where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  124. {
  125. $data = $this
  126. ->alias('ro')
  127. ->field($field)
  128. ->leftJoin('user u', 'u.uid = ro.uid')
  129. ->when(!empty($where), function ($query) use ($where) {
  130. if (!empty($where['order_id'])) {
  131. $query->where('ro.order_id', $where['order_id']);
  132. }
  133. if (!empty($where['uid'])) {
  134. $query->where('ro.uid', $where['uid']);
  135. }
  136. if (!empty($where['nickname'])) {
  137. $query->whereLike('u.nickname', "%{$where['nickname']}%");
  138. }
  139. if (!empty($where['mobile'])) {
  140. $query->where('u.mobile', $where['mobile']);
  141. }
  142. if (!empty($where['pay_type'])) {
  143. $query->where('ro.pay_type', $where['pay_type']);
  144. }
  145. if (!empty($where['paid'])) {
  146. $query->where('ro.paid', $where['paid']);
  147. }
  148. if (!empty($where['status'])) {
  149. $query->where('ro.status', $where['status']);
  150. }
  151. if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
  152. $startTime = strtotime($where['time'][0]);
  153. $endTime = strtotime($where['time'][1]);
  154. $query->whereBetween('ro.add_time', "{$startTime},{$endTime}");
  155. }
  156. })
  157. ->order($order)
  158. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  159. ->toArray();
  160. return [$data['total'], $data['data']];
  161. }
  162. /**
  163. * 关闭订单
  164. * @param string $orderSn 订单号
  165. * @return bool
  166. */
  167. public function closeOrder($orderSn)
  168. {
  169. return $this->where('order_id', $orderSn)->update([
  170. 'status' => -1,
  171. 'update_time' => time()
  172. ]);
  173. }
  174. }