Order.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\recharge;
  13. use app\api\model\Setting as SettingModel;
  14. use app\api\model\recharge\Plan as PlanModel;
  15. use app\api\model\recharge\OrderPlan as OrderPlanModel;
  16. use app\api\service\User as UserService;
  17. use app\common\library\helper;
  18. use app\common\model\recharge\Order as OrderModel;
  19. use app\common\service\Order as OrderService;
  20. use app\common\enum\recharge\order\PayStatus as PayStatusEnum;
  21. use app\common\enum\recharge\order\RechargeType as RechargeTypeEnum;
  22. use app\common\exception\BaseException;
  23. /**
  24. * 用户充值订单模型
  25. * Class Order
  26. * @package app\api\model\recharge
  27. */
  28. class Order extends OrderModel
  29. {
  30. /**
  31. * 隐藏字段
  32. * @var array
  33. */
  34. protected $hidden = [
  35. 'transaction_id',
  36. 'store_id',
  37. 'create_time',
  38. 'update_time',
  39. ];
  40. /**
  41. * 获取订单列表
  42. * @return \think\Paginator
  43. * @throws BaseException
  44. * @throws \think\db\exception\DbException
  45. */
  46. public function getList()
  47. {
  48. // 当前用户ID
  49. $userId = UserService::getCurrentLoginUserId();
  50. // 获取列表数据
  51. return $this->where('user_id', '=', $userId)
  52. ->where('pay_status', '=', PayStatusEnum::SUCCESS)
  53. ->order(['create_time' => 'desc'])
  54. ->paginate(15);
  55. }
  56. /**
  57. * 获取订单详情(待付款状态)
  58. * @param $orderNo
  59. * @return array|null|static
  60. */
  61. public static function getPayDetail(string $orderNo)
  62. {
  63. return self::detail(['order_no' => $orderNo, 'pay_status' => PayStatusEnum::PENDING]);
  64. }
  65. /**
  66. * 创建充值订单
  67. * @param int|null $planId
  68. * @param float $customMoney
  69. * @return bool|int
  70. * @throws BaseException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function createOrder(int $planId = null, float $customMoney = 0.00)
  76. {
  77. // 确定充值方式
  78. $rechargeType = $planId > 0 ? RechargeTypeEnum::PLAN : RechargeTypeEnum::CUSTOM;
  79. // 验证用户输入
  80. if (!$this->validateForm($rechargeType, $planId, $customMoney)) {
  81. $this->error = $this->error ?: '数据验证错误';
  82. return false;
  83. }
  84. // 获取订单数据
  85. $data = $this->getOrderData($rechargeType, $planId, $customMoney);
  86. // 记录订单信息
  87. return $this->saveOrder($data);
  88. }
  89. /**
  90. * 保存订单记录
  91. * @param $data
  92. * @return bool|false|int
  93. */
  94. private function saveOrder(array $data)
  95. {
  96. // 写入订单记录
  97. $this->save($data['order']);
  98. // 记录订单套餐快照
  99. if (!empty($data['plan'])) {
  100. $PlanModel = new OrderPlanModel;
  101. return $PlanModel->add($this['order_id'], $data['plan']);
  102. }
  103. return true;
  104. }
  105. /**
  106. * 生成充值订单
  107. * @param int $rechargeType 充值方式
  108. * @param int $planId 方案ID
  109. * @param float $customMoney 自定义金额
  110. * @return array|array[]|bool
  111. * @throws BaseException
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. private function getOrderData(int $rechargeType, int $planId, float $customMoney)
  117. {
  118. // 订单信息
  119. $data = [
  120. 'order' => [
  121. 'user_id' => UserService::getCurrentLoginUserId(),
  122. 'order_no' => 'RC' . OrderService::createOrderNo(),
  123. 'recharge_type' => $rechargeType,
  124. 'gift_money' => 0.00,
  125. 'store_id' => self::$storeId,
  126. ],
  127. 'plan' => [] // 订单套餐快照
  128. ];
  129. // 自定义金额充值
  130. if ($rechargeType == RechargeTypeEnum::CUSTOM) {
  131. $data = $this->createDataByCustom($data, $customMoney);
  132. }
  133. // 套餐充值
  134. if ($rechargeType == RechargeTypeEnum::PLAN) {
  135. $data = $this->createDataByPlan($data, $planId);
  136. }
  137. // 实际到账金额
  138. $data['order']['actual_money'] = helper::bcadd($data['order']['pay_price'], $data['order']['gift_money']);
  139. return $data;
  140. }
  141. /**
  142. * 创建套餐充值订单数据
  143. * @param array $order
  144. * @param int $planId
  145. * @return array
  146. * @throws BaseException
  147. */
  148. private function createDataByPlan(array $order, int $planId)
  149. {
  150. // 获取套餐详情
  151. $planInfo = PlanModel::detail($planId);
  152. if (empty($planInfo)) {
  153. throwError('充值套餐不存在');
  154. }
  155. $order['plan'] = $planInfo;
  156. $order['order']['plan_id'] = $planInfo['plan_id'];
  157. $order['order']['gift_money'] = $planInfo['gift_money'];
  158. $order['order']['pay_price'] = $planInfo['money'];
  159. return $order;
  160. }
  161. /**
  162. * 创建自定义充值订单数据
  163. * @param array $order
  164. * @param float $customMoney
  165. * @return array|bool
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. private function createDataByCustom(array $order, float $customMoney)
  171. {
  172. // 用户支付金额
  173. $order['order']['pay_price'] = $customMoney;
  174. // 充值设置
  175. $setting = SettingModel::getItem('recharge');
  176. if ($setting['is_custom'] == false) {
  177. return true;
  178. }
  179. // 根据自定义充值金额匹配满足的套餐
  180. if ($setting['is_match_plan'] == true) {
  181. $matchPlanInfo = (new PlanModel)->getMatchPlan($customMoney);
  182. if (!empty($matchPlanInfo)) {
  183. $order['plan'] = $matchPlanInfo;
  184. $order['order']['plan_id'] = $matchPlanInfo['plan_id'];
  185. $order['order']['gift_money'] = $matchPlanInfo['gift_money'];
  186. }
  187. }
  188. return $order;
  189. }
  190. /**
  191. * 表单验证
  192. * @param $rechargeType
  193. * @param $planId
  194. * @param $customMoney
  195. * @return bool
  196. */
  197. private function validateForm($rechargeType, $planId, $customMoney)
  198. {
  199. if (empty($planId) && $customMoney <= 0) {
  200. $this->error = '请选择充值套餐或输入充值金额';
  201. return false;
  202. }
  203. // 验证自定义的金额
  204. if ($rechargeType == RechargeTypeEnum::CUSTOM && $customMoney <= 0) {
  205. $this->error = '请选择充值套餐或输入充值金额';
  206. return false;
  207. }
  208. return true;
  209. }
  210. }