Order.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\api\model\recharge;
  3. use app\common\model\recharge\Order as OrderModel;
  4. use app\api\model\Setting as SettingModel;
  5. use app\api\model\recharge\Plan as PlanModel;
  6. use app\api\model\recharge\OrderPlan as OrderPlanModel;
  7. use app\common\service\Order as OrderService;
  8. use app\common\enum\recharge\order\PayStatus as PayStatusEnum;
  9. use app\common\enum\recharge\order\RechargeType as RechargeTypeEnum;
  10. use app\common\exception\BaseException;
  11. /**
  12. * 用户充值订单模型
  13. * Class Order
  14. * @package app\api\model\recharge
  15. */
  16. class Order extends OrderModel
  17. {
  18. /**
  19. * 隐藏字段
  20. * @var array
  21. */
  22. protected $hidden = [
  23. 'wxapp_id',
  24. ];
  25. /**
  26. * 获取订单列表
  27. * @param $userId
  28. * @return \think\Paginator
  29. * @throws \think\exception\DbException
  30. */
  31. public function getList($userId)
  32. {
  33. // 获取列表数据
  34. return $this->where('user_id', '=', $userId)
  35. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  36. ->order(['create_time' => 'desc'])
  37. ->paginate(15, false, [
  38. 'query' => request()->request()
  39. ]);
  40. }
  41. /**
  42. * 获取订单详情(待付款状态)
  43. * @param $orderNo
  44. * @return self|null
  45. * @throws \think\exception\DbException
  46. */
  47. public static function getPayDetail($orderNo)
  48. {
  49. return self::detail(['order_no' => $orderNo, 'pay_status' => PayStatusEnum::PENDING]);
  50. }
  51. /**
  52. * 创建充值订单
  53. * @param \app\api\model\User $user 当前用户信息
  54. * @param int $planId 套餐id
  55. * @param double $customMoney 自定义充值金额
  56. * @return bool|false|int
  57. * @throws BaseException
  58. * @throws \think\exception\DbException
  59. */
  60. public function createOrder($user, $planId = null, $customMoney = 0.00)
  61. {
  62. // 确定充值方式
  63. $rechargeType = $planId > 0 ? RechargeTypeEnum::PLAN : RechargeTypeEnum::CUSTOM;
  64. // 验证用户输入
  65. if (!$this->validateForm($rechargeType, $planId, $customMoney)) {
  66. $this->error = $this->error ?: '数据验证错误';
  67. return false;
  68. }
  69. // 获取订单数据
  70. $data = $this->getOrderData($user, $rechargeType, $planId, $customMoney);
  71. // 记录订单信息
  72. return $this->saveOrder($data);
  73. }
  74. /**
  75. * 保存订单记录
  76. * @param $data
  77. * @return bool|false|int
  78. */
  79. private function saveOrder($data)
  80. {
  81. // 写入订单记录
  82. $this->save($data['order']);
  83. // 记录订单套餐快照
  84. if (!empty($data['plan'])) {
  85. $PlanModel = new OrderPlanModel;
  86. return $PlanModel->add($this['order_id'], $data['plan']);
  87. }
  88. return true;
  89. }
  90. /**
  91. * 生成充值订单
  92. * @param $user
  93. * @param $rechargeType
  94. * @param $planId
  95. * @param $customMoney
  96. * @return array
  97. * @throws BaseException
  98. * @throws \think\exception\DbException
  99. */
  100. private function getOrderData($user, $rechargeType, $planId, $customMoney)
  101. {
  102. // 订单信息
  103. $data = [
  104. 'order' => [
  105. 'user_id' => $user['user_id'],
  106. 'order_no' => 'RC' . OrderService::createOrderNo(),
  107. 'recharge_type' => $rechargeType,
  108. 'gift_money' => 0.00,
  109. 'wxapp_id' => self::$wxapp_id,
  110. ],
  111. 'plan' => [] // 订单套餐快照
  112. ];
  113. // 自定义金额充值
  114. if ($rechargeType == RechargeTypeEnum::CUSTOM) {
  115. $this->createDataByCustom($data, $customMoney);
  116. }
  117. // 套餐充值
  118. if ($rechargeType == RechargeTypeEnum::PLAN) {
  119. $this->createDataByPlan($data, $planId);
  120. }
  121. // 实际到账金额
  122. $data['order']['actual_money'] = bcadd($data['order']['pay_price'], $data['order']['gift_money'], 2);
  123. return $data;
  124. }
  125. /**
  126. * 创建套餐充值订单数据
  127. * @param $order
  128. * @param $planId
  129. * @return bool
  130. * @throws BaseException
  131. * @throws \think\exception\DbException
  132. */
  133. private function createDataByPlan(&$order, $planId)
  134. {
  135. // 获取套餐详情
  136. $planInfo = PlanModel::detail($planId);
  137. if (empty($planInfo)) {
  138. throw new BaseException(['msg' => '充值套餐不存在']);
  139. }
  140. $order['plan'] = $planInfo;
  141. $order['order']['plan_id'] = $planInfo['plan_id'];
  142. $order['order']['gift_money'] = $planInfo['gift_money'];
  143. $order['order']['pay_price'] = $planInfo['money'];
  144. return true;
  145. }
  146. /**
  147. * 创建自定义充值订单数据
  148. * @param $order
  149. * @param $customMoney
  150. * @return bool
  151. */
  152. private function createDataByCustom(&$order, $customMoney)
  153. {
  154. // 用户支付金额
  155. $order['order']['pay_price'] = $customMoney;
  156. // 充值设置
  157. $setting = SettingModel::getItem('recharge');
  158. if ($setting['is_custom'] == false) {
  159. return true;
  160. }
  161. // 根据自定义充值金额匹配满足的套餐
  162. if ($setting['is_match_plan'] == true) {
  163. $matchPlanInfo = (new PlanModel)->getMatchPlan($customMoney);
  164. if (!empty($matchPlanInfo)) {
  165. $order['plan'] = $matchPlanInfo;
  166. $order['order']['plan_id'] = $matchPlanInfo['plan_id'];
  167. $order['order']['gift_money'] = $matchPlanInfo['gift_money'];
  168. }
  169. }
  170. return true;
  171. }
  172. /**
  173. * 表单验证
  174. * @param $rechargeType
  175. * @param $planId
  176. * @param $customMoney
  177. * @return bool
  178. */
  179. private function validateForm($rechargeType, $planId, $customMoney)
  180. {
  181. if (empty($planId) && $customMoney <= 0) {
  182. $this->error = '请选择充值套餐或输入充值金额';
  183. return false;
  184. }
  185. // 验证自定义的金额
  186. if ($rechargeType == RechargeTypeEnum::CUSTOM && $customMoney <= 0) {
  187. $this->error = '请选择充值套餐或输入充值金额';
  188. return false;
  189. }
  190. return true;
  191. }
  192. }