PayDetailController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\controller\notify\RechargeController;
  4. use app\controller\Curd;
  5. use app\model\Goods;
  6. use app\model\GoodsSku;
  7. use app\model\Order;
  8. use app\model\OrderSheet;
  9. use app\model\PayDetail;
  10. use support\Db;
  11. use support\exception\BusinessException;
  12. use support\Request;
  13. use support\Response;
  14. class PayDetailController extends Curd
  15. {
  16. public function __construct()
  17. {
  18. $this->model = new PayDetail();
  19. }
  20. public function select(Request $request): Response
  21. {
  22. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  23. $order = $request->get('order', 'desc');
  24. $field = $field ?? 'pay_addtimes';
  25. // $where['pay_status'] = 'SUCCESS';
  26. if ($where['pay_category'] == 'OTHER') {
  27. $where['pay_status'] = 'SUCCESS';
  28. $where['pay_category'] = ['<>', 'RECHARGE'];
  29. }
  30. $query = $this->doSelect($where, $field, $order);
  31. return $this->doFormat($query, $format, $limit);
  32. }
  33. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  34. {
  35. $model = $this->model->with(['member', 'cert']);
  36. foreach ($where as $column => $value) {
  37. if (is_array($value)) {
  38. if ($value[0] === 'like' || $value[0] === 'not like') {
  39. $model = $model->where($column, $value[0], "%$value[1]%");
  40. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  41. $model = $model->where($column, $value[0], $value[1]);
  42. } elseif ($value[0] == 'in' && !empty($value[1])) {
  43. $valArr = $value[1];
  44. if (is_string($value[1])) {
  45. $valArr = explode(",", trim($value[1]));
  46. }
  47. $model = $model->whereIn($column, $valArr);
  48. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  49. $valArr = $value[1];
  50. if (is_string($value[1])) {
  51. $valArr = explode(",", trim($value[1]));
  52. }
  53. $model = $model->whereNotIn($column, $valArr);
  54. } elseif ($value[0] == 'null') {
  55. $model = $model->whereNull($column);
  56. } elseif ($value[0] == 'not null') {
  57. $model = $model->whereNotNull($column);
  58. } elseif ($value[0] !== '' || $value[1] !== '') {
  59. $model = $model->whereBetween($column, $value);
  60. }
  61. } else {
  62. $model = $model->where($column, $value);
  63. }
  64. }
  65. if ($field) {
  66. $model = $model->orderBy($field, $order);
  67. }
  68. return $model;
  69. }
  70. /**
  71. * @Desc 产品订单
  72. * @Author Gorden
  73. * @Date 2024/6/5 10:10
  74. *
  75. * @param Request $request
  76. * @return Response
  77. */
  78. public function goodsOrder(Request $request)
  79. {
  80. $orderId = $request->get('order_id', '');
  81. if (!$orderId) {
  82. return json_fail('参数异常');
  83. }
  84. $orderSheet = OrderSheet::with([
  85. 'order' => function ($query) {
  86. $query->select('order_id', 'order_amount_total', 'order_amount_pay', 'order_is_complete', 'order_category', 'order_status_system', 'order_status_payment', 'order_status_storage', 'order_addtimes');
  87. },
  88. 'goods' => function ($query) {
  89. $query->select('goods_id', 'goods_name', 'goods_status', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
  90. },
  91. 'sku' => function ($query) {
  92. $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
  93. }
  94. ])->where('join_sheet_order_id', $orderId)
  95. ->get()
  96. ->toArray();
  97. return json_success('', $orderSheet);
  98. }
  99. public function insertRecharge(Request $request): Response
  100. {
  101. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  102. return json_fail($this->validateClass->getError());
  103. }
  104. $password = $request->post('recharge_password','');
  105. if(!$password || $password != '123456'){
  106. return json_fail('充值密码错误');
  107. }
  108. try {
  109. Db::beginTransaction();
  110. $data = $this->insertRechargeInput($request);
  111. $payDetailId = $this->doInsert($data);
  112. (new RechargeController)->disposePaySuccess($payDetailId);
  113. Db::commit();
  114. } catch (BusinessException $customException) {
  115. Db::rollBack();
  116. return json_fail($customException->getMessage());
  117. } catch (\Exception $e) {
  118. Db::rollBack();
  119. dump($e->getMessage());
  120. return json_fail('数据写入失败11');
  121. }
  122. return json_success('success');
  123. }
  124. /**
  125. * @Desc
  126. * @Author Gorden
  127. * @Date 2024/6/5 13:39
  128. *
  129. * @param Request $request
  130. * @return array
  131. * @throws BusinessException
  132. */
  133. protected function insertRechargeInput(Request $request): array
  134. {
  135. $params = $request->post();
  136. $data = $this->inputFilter($params);
  137. $goods = Goods::find($params['goods_id']);
  138. $extendJson = [];
  139. if (!$params['is_custom']) {
  140. try {
  141. Db::beginTransaction();
  142. $data['pay_amount'] = $goods->goods_sales_price;
  143. // $orderId = "OD" . date("YmdHis") . random_string(6, 'up');
  144. // $orderGroupId = "OD" . date("YmdHis") . random_string(6, 'up');
  145. // $orderData = [
  146. // 'order_id' => $orderId,
  147. // 'order_groupby' => $orderGroupId,
  148. // 'join_order_member_id' => $data['join_pay_member_id'],
  149. // 'order_name' => date('Y-m-d H:i:s') . '-订单',
  150. // 'order_amount_total' => $data['pay_amount'],
  151. // 'order_amount_pay' => $data['pay_amount'],
  152. // 'order_amount_json' => '{"tax": 0, "freight": 0}',
  153. // 'order_status_payment' => 'SUCCESS',
  154. // 'order_category' => 'NORMAL',
  155. // 'order_status_system' => 'WAITING',
  156. // 'order_status_storage' => 'PENDING',
  157. // 'order_addtimes' => time()
  158. // ];
  159. // $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  160. // dump($sku);
  161. // Order::insert($orderData);
  162. // $orderSheetData = [
  163. // 'join_sheet_member_id' => $data['join_pay_member_id'],
  164. // 'join_sheet_order_id' => $orderId,
  165. // 'join_sheet_goods_id' => $params['goods_id'],
  166. // 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  167. // 'order_sheet_status' => 'PAYING',
  168. // 'order_sheet_category' => 'NORMAL',
  169. // 'order_sheet_num' => 1,
  170. // 'order_sheet_price' => $data['pay_amount'],
  171. // 'order_sheet_amount' => $data['pay_amount'],
  172. // 'order_sheet_pay' => $data['pay_amount'],
  173. // 'order_sheet_addtimes' => time()
  174. // ];
  175. // OrderSheet::insert($orderSheetData);
  176. Db::commit();
  177. } catch (\Exception $e) {
  178. dump($e->getMessage());
  179. Db::rollBack();
  180. throw new BusinessException("数据错误");
  181. }
  182. // $data['join_pay_order_id'] = $orderGroupId;
  183. // $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  184. $rate = 0;
  185. if (!empty($goods->goods_attribute_json)) {
  186. $attributeJson = json_decode($goods->goods_attribute_json, true);
  187. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  188. }
  189. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  190. $extendJson['added_rate'] = $rate;
  191. } else {
  192. // $extendJson['notify'] = 'success';
  193. $extendJson['is_custom'] = $params['is_custom'];
  194. }
  195. $data['pay_extend_json'] = json_encode($extendJson);
  196. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  197. $data['pay_prepayid'] = 0;
  198. return $data;
  199. }
  200. }