Order.php 6.4 KB

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