RechargeOrder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use think\facade\Cache;
  6. use think\Model;
  7. /**
  8. * @mixin \think\Model
  9. */
  10. class RechargeOrder extends BaseModel
  11. {
  12. /**
  13. * 生成充值订单号
  14. * @param int $uid 用户UID
  15. * @return string
  16. */
  17. public function mkOrderSn($uid)
  18. {
  19. return 'RO' . time() . rand(1000, 9000) . $uid;
  20. }
  21. /**
  22. * 创建充值订单
  23. * @param int $uid 用户UID
  24. * @param int $rechargeId 充值配置ID(0为自定义充值)
  25. * @param float $price 充值金额
  26. * @param int $integral 获得积分
  27. * @param int $giveIntegral 赠送积分
  28. * @param string $payType 支付方式:wxpay/alipay/system
  29. * @param int $adminId 管理员ID(后台充值)
  30. * @param string $remark 备注
  31. * @param float $discount 折扣金额
  32. * @return string|null 订单号
  33. */
  34. public function createOrder($uid, $rechargeId, $price, $integral, $giveIntegral, $payType = 'wxpay', $adminId = 0, $remark = '', $discount = 0)
  35. {
  36. try {
  37. $orderSn = $this->mkOrderSn($uid);
  38. $totalIntegral = $integral + $giveIntegral;
  39. @file_put_contents('quanju.txt', $uid."这到底什么问题\r\n", 8);
  40. $bool = $this->insert([
  41. 'order_id' => $orderSn,
  42. 'uid' => $uid,
  43. 'recharge_id' => $rechargeId,
  44. 'price' => $price,
  45. 'integral' => $integral,
  46. 'give_integral' => $giveIntegral,
  47. 'total_integral' => $totalIntegral,
  48. 'pay_type' => $payType,
  49. 'paid' => 0,
  50. 'pay_time' => 0,
  51. 'remark' => $remark,
  52. 'admin_id' => $adminId,
  53. 'discount_amount' => $discount,
  54. 'status' => 0,
  55. 'add_time' => time(),
  56. 'update_time' => time()
  57. ]);
  58. return $bool;
  59. } catch (\Exception $e) {
  60. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [createOrder Error] ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine() . "创建订单报错\r\n", 8);
  61. return null;
  62. }
  63. }
  64. /**
  65. * 根据订单号获取订单
  66. * @param string $orderSn 订单号
  67. * @return array|null
  68. */
  69. public function getByOrderSn($orderSn)
  70. {
  71. return $this->where('order_id', $orderSn)->find();
  72. }
  73. /**
  74. * 根据ID获取订单
  75. * @param int $id 订单ID
  76. * @return array|null
  77. */
  78. public function getById($id)
  79. {
  80. return $this->where('id', $id)->find();
  81. }
  82. /**
  83. * 更新订单支付状态
  84. * @param string $orderSn 订单号
  85. * @param string $paySn 支付流水号
  86. * @param string $payJson 支付返回信息
  87. * @return bool
  88. */
  89. public function updatePayStatus($orderSn, $paySn, $payJson = '')
  90. {
  91. return $this->where('order_id', $orderSn)->update([
  92. 'paid' => 1,
  93. 'pay_sn' => $paySn,
  94. 'pay_json' => $payJson,
  95. 'pay_time' => time(),
  96. 'status' => 1,
  97. 'update_time' => time()
  98. ]);
  99. }
  100. /**
  101. * 获取用户充值订单列表
  102. * @param int $uid 用户UID
  103. * @param array $where 查询条件
  104. * @param string $field 查询字段
  105. * @param int $page 页码
  106. * @param int $pageCount 每页数量
  107. * @param string $order 排序
  108. * @return array
  109. */
  110. public function getUserList($uid, $where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  111. {
  112. $where['uid'] = $uid;
  113. $data = $this
  114. ->where($where)
  115. ->field($field)
  116. ->order($order)
  117. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  118. ->toArray();
  119. return [$data['total'], $data['data']];
  120. }
  121. /**
  122. * 获取充值订单列表(后台)
  123. * @param array $where 查询条件
  124. * @param string $field 查询字段
  125. * @param int $page 页码
  126. * @param int $pageCount 每页数量
  127. * @param string $order 排序
  128. * @return array
  129. */
  130. public function getAdminList($where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  131. {
  132. $data = $this
  133. ->alias('ro')
  134. ->field($field)
  135. ->leftJoin('user u', 'u.uid = ro.uid')
  136. ->when(!empty($where), function ($query) use ($where) {
  137. if (!empty($where['order_id'])) {
  138. $query->where('ro.order_id', $where['order_id']);
  139. }
  140. if (!empty($where['uid'])) {
  141. $query->where('ro.uid', $where['uid']);
  142. }
  143. if (!empty($where['nickname'])) {
  144. $query->whereLike('u.nickname', "%{$where['nickname']}%");
  145. }
  146. if (!empty($where['mobile'])) {
  147. $query->where('u.mobile', $where['mobile']);
  148. }
  149. if (!empty($where['pay_type'])) {
  150. $query->where('ro.pay_type', $where['pay_type']);
  151. }
  152. if (!empty($where['paid'])) {
  153. $query->where('ro.paid', $where['paid']);
  154. }
  155. if (!empty($where['status'])) {
  156. $query->where('ro.status', $where['status']);
  157. }
  158. if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
  159. $startTime = strtotime($where['time'][0]);
  160. $endTime = strtotime($where['time'][1]);
  161. $query->whereBetween('ro.add_time', "{$startTime},{$endTime}");
  162. }
  163. })
  164. ->order($order)
  165. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  166. ->toArray();
  167. return [$data['total'], $data['data']];
  168. }
  169. /**
  170. * 关闭订单
  171. * @param string $orderSn 订单号
  172. * @return bool
  173. */
  174. public function closeOrder($orderSn)
  175. {
  176. return $this->where('order_id', $orderSn)->update([
  177. 'status' => -1,
  178. 'update_time' => time()
  179. ]);
  180. }
  181. /**
  182. * 增加用户积分(内部调用)
  183. * @param int $uid 用户ID
  184. * @param int $totalIntegral 总积分
  185. * @param string $orderSn 订单号
  186. * @param float $price 充值金额
  187. * @param int $orderId 订单ID
  188. * @return bool
  189. */
  190. private function addUserIntegral($uid, $totalIntegral, $orderSn, $price, $orderId)
  191. {
  192. try {
  193. $userModel = new User();
  194. $user = $userModel->where('uid', $uid)->find();
  195. if ($user) {
  196. // 更新用户积分和累计充值金额
  197. // $res=$userModel->where('uid', $uid)->inc('score', $totalIntegral)->inc('score_in', $totalIntegral)->update();
  198. // @file_put_contents('quanju.txt', $res. "-增加积分怎么样\r\n", 8);
  199. // 记录积分明细
  200. $scoreDetail = new UserScoreDetail();
  201. $scoreDetail->incomeScore($uid, $totalIntegral, $orderSn, 'income_score', [
  202. 'o_id' => $orderId
  203. ], $orderId,'充值积分',"充值".$price."元获得".$totalIntegral."积分");
  204. }
  205. // 清除用户折扣缓存,以便下次充值生成新的折扣金额
  206. Cache::store('redis')->delete('recharge_discount_' . $uid);
  207. return true;
  208. } catch (\Exception $e) {
  209. // 将错误信息保存到 quanju.txt 文件
  210. @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-增加用户积分报错内容\r\n", 8);
  211. return false;
  212. }
  213. }
  214. /**
  215. * 充值成功回调处理(内部调用)
  216. * @param string $orderSn 订单号
  217. * @param string $paySn 支付流水号
  218. * @param string $payJson 支付返回信息
  219. * @return bool
  220. */
  221. public function paySuccess($orderSn, $paySn, $payJson = '')
  222. {
  223. try {
  224. @file_put_contents('quanju.txt', $orderSn . "-orderSn\r\n", 8);
  225. @file_put_contents('quanju.txt', $paySn . "-paySn\r\n", 8);
  226. $rechargeOrder = new RechargeOrder();
  227. $order_id=(new PayTrade)->where('pay_no', $orderSn)->value('order_id');
  228. $orderInfo = $rechargeOrder->getByOrderSn($order_id);
  229. @file_put_contents('quanju.txt', json_encode($orderInfo) . "-充值订单信息\r\n", 8);
  230. if (!$orderInfo) {
  231. return false;
  232. }
  233. // 更新订单支付状态
  234. $updateResult = $rechargeOrder->updatePayStatus($order_id, $paySn, $payJson);
  235. @file_put_contents('quanju.txt', json_encode($updateResult) . "-更新订单支付状态\r\n", 8);
  236. if (!$updateResult) {
  237. return false;
  238. }
  239. // 增加用户积分
  240. $this->addUserIntegral($orderInfo['uid'], $orderInfo['total_integral'], $orderSn, $orderInfo['price'], $orderInfo['id']);
  241. return true;
  242. } catch (\Exception $e) {
  243. // 将错误信息保存到 quanju.txt 文件
  244. // $logMessage = date('Y-m-d H:i:s') . " [paySuccess Error] OrderSn: {$orderSn}, PaySn: {$paySn}, Error: " . $e->getMessage() . "\n";
  245. // file_put_contents('quanju.txt', $logMessage, FILE_APPEND);
  246. @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-充值完成报错内容\r\n", 8);
  247. return false;
  248. }
  249. }
  250. }