PayDetailController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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\MemberAccount;
  8. use app\model\Order;
  9. use app\model\OrderSheet;
  10. use app\model\PayDetail;
  11. use support\Db;
  12. use support\exception\BusinessException;
  13. use support\Request;
  14. use support\Response;
  15. use function PHPSTORM_META\type;
  16. class PayDetailController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new PayDetail();
  21. }
  22. public function select(Request $request): Response
  23. {
  24. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  25. $order = $request->get('order', 'desc');
  26. $field = $field ?? 'pay_addtimes';
  27. // $where['pay_status'] = 'SUCCESS';
  28. if ($where['pay_category'] == 'OTHER') {
  29. $where['pay_status'] = 'SUCCESS';
  30. $where['pay_category'] = ['<>', 'RECHARGE'];
  31. }
  32. // else if ($where['pay_category'] == 'RECHARGE') {
  33. // $where['pay_status'] = 'SUCCESS';
  34. // $where['pay_category'] = 'RECHARGE';
  35. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  36. // }
  37. $query = $this->doSelect($where, $field, $order);
  38. return $this->doFormat($query, $format, $limit);
  39. }
  40. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  41. {
  42. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  43. foreach ($where as $column => $value) {
  44. if (is_array($value)) {
  45. if ($value[0] === 'like' || $value[0] === 'not like') {
  46. $model = $model->where($column, $value[0], "%$value[1]%");
  47. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  48. $model = $model->where($column, $value[0], $value[1]);
  49. } elseif ($value[0] == 'in' && !empty($value[1])) {
  50. $valArr = $value[1];
  51. if (is_string($value[1])) {
  52. $valArr = explode(",", trim($value[1]));
  53. }
  54. $model = $model->whereIn($column, $valArr);
  55. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  56. $valArr = $value[1];
  57. if (is_string($value[1])) {
  58. $valArr = explode(",", trim($value[1]));
  59. }
  60. $model = $model->whereNotIn($column, $valArr);
  61. } elseif ($value[0] == 'null') {
  62. $model = $model->whereNull($column);
  63. } elseif ($value[0] == 'not null') {
  64. $model = $model->whereNotNull($column);
  65. } elseif ($value[0] !== '' || $value[1] !== '') {
  66. $model = $model->whereBetween($column, $value);
  67. }
  68. } else {
  69. $model = $model->where($column, $value);
  70. }
  71. }
  72. $model = $model->where(function ($query) {
  73. $query->whereIn('pay_prepayid',['ALIPAY','WXPAY'])
  74. ->orWhere('pay_prepayid','like','%CASH%');
  75. });
  76. if ($field) {
  77. $model = $model->orderBy($field, $order);
  78. }
  79. return $model;
  80. }
  81. public function selectWelfare(Request $request): Response
  82. {
  83. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  84. $order = $request->get('order', 'desc');
  85. $field = $field ?? 'pay_addtimes';
  86. // $where['pay_status'] = 'SUCCESS';
  87. if ($where['pay_category'] == 'OTHER') {
  88. $where['pay_status'] = 'SUCCESS';
  89. $where['pay_category'] = ['<>', 'RECHARGE'];
  90. }
  91. // else if ($where['pay_category'] == 'RECHARGE') {
  92. // $where['pay_status'] = 'SUCCESS';
  93. // $where['pay_category'] = 'RECHARGE';
  94. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  95. // }
  96. $query = $this->doSelectWelfare($where, $field, $order);
  97. return $this->doFormat($query, $format, $limit);
  98. }
  99. protected function doSelectWelfare(array $where, string $field = null, string $order = 'desc')
  100. {
  101. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  102. foreach ($where as $column => $value) {
  103. if (is_array($value)) {
  104. if ($value[0] === 'like' || $value[0] === 'not like') {
  105. $model = $model->where($column, $value[0], "%$value[1]%");
  106. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  107. $model = $model->where($column, $value[0], $value[1]);
  108. } elseif ($value[0] == 'in' && !empty($value[1])) {
  109. $valArr = $value[1];
  110. if (is_string($value[1])) {
  111. $valArr = explode(",", trim($value[1]));
  112. }
  113. $model = $model->whereIn($column, $valArr);
  114. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  115. $valArr = $value[1];
  116. if (is_string($value[1])) {
  117. $valArr = explode(",", trim($value[1]));
  118. }
  119. $model = $model->whereNotIn($column, $valArr);
  120. } elseif ($value[0] == 'null') {
  121. $model = $model->whereNull($column);
  122. } elseif ($value[0] == 'not null') {
  123. $model = $model->whereNotNull($column);
  124. } elseif ($value[0] !== '' || $value[1] !== '') {
  125. $model = $model->whereBetween($column, $value);
  126. }
  127. } else {
  128. $model = $model->where($column, $value);
  129. }
  130. }
  131. $model = $model->where(function ($query) {
  132. $query->Where('pay_prepayid','like','%WELFARE%');
  133. });
  134. if ($field) {
  135. $model = $model->orderBy($field, $order);
  136. }
  137. return $model;
  138. }
  139. public function afterQuery($items)
  140. {
  141. foreach ($items as &$item) {
  142. $prepayId = explode('-', $item->pay_prepayid);
  143. if (count($prepayId) > 1) {
  144. $item->pay_prepayid = $prepayId[1];
  145. }
  146. $memberAccount = [];
  147. if(!empty($item->memberAccount)){
  148. foreach($item->memberAccount as $account){
  149. $memberAccount[strtolower($account->member_account_classify)] = $account;
  150. }
  151. $item->memberAccount = $memberAccount;
  152. }
  153. }
  154. return $items;
  155. }
  156. /**
  157. * @Desc 产品订单
  158. * @Author Gorden
  159. * @Date 2024/6/5 10:10
  160. *
  161. * @param Request $request
  162. * @return Response
  163. */
  164. public function goodsOrder(Request $request)
  165. {
  166. $orderId = $request->get('order_id', '');
  167. if (!$orderId) {
  168. return json_fail('参数异常');
  169. }
  170. $orderSheet = OrderSheet::with([
  171. 'order' => function ($query) {
  172. $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');
  173. },
  174. 'goods' => function ($query) {
  175. $query->select('goods_id', 'goods_name', 'goods_status', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
  176. },
  177. 'sku' => function ($query) {
  178. $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
  179. }
  180. ])->where('join_sheet_order_id', $orderId)
  181. ->get()
  182. ->toArray();
  183. return json_success('', $orderSheet);
  184. }
  185. /**
  186. * 余额账户充值
  187. */
  188. public function insertRecharge(Request $request): Response
  189. {
  190. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  191. return json_fail($this->validateClass->getError());
  192. }
  193. $password = $request->post('recharge_password', '');
  194. if (!$password || $password != '123456') {
  195. return json_fail('充值密码错误');
  196. }
  197. Db::beginTransaction();
  198. try {
  199. $data = $this->insertRechargeInput($request);
  200. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
  201. $payDetailId = $this->doInsert($data);
  202. (new RechargeController)->disposePaySuccess($payDetailId);
  203. Db::commit();
  204. } catch (BusinessException $customException) {
  205. Db::rollBack();
  206. return json_fail($customException->getMessage());
  207. } catch (\Exception $e) {
  208. Db::rollBack();
  209. dump($e->getMessage());
  210. return json_fail('数据写入失败11');
  211. }
  212. return json_success('success');
  213. }
  214. /**
  215. * 福利账户充值
  216. */
  217. public function insertRechargeWelfare(Request $request): Response
  218. {
  219. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  220. return json_fail($this->validateClass->getError());
  221. }
  222. $password = $request->post('recharge_password', '');
  223. if (!$password || $password != '123456') {
  224. return json_fail('充值密码错误');
  225. }
  226. Db::beginTransaction();
  227. try {
  228. $data = $this->insertRechargeInput($request, 'WELFARE');
  229. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
  230. $this->doInsert($data);
  231. // 金额累加到福利账户
  232. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'WELFARE')->first();
  233. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  234. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  235. $memberAccount->save();
  236. Db::commit();
  237. } catch (BusinessException $customException) {
  238. Db::rollBack();
  239. return json_fail($customException->getMessage());
  240. } catch (\Exception $e) {
  241. Db::rollBack();
  242. dump($e->getMessage());
  243. return json_fail('数据写入失败11');
  244. }
  245. return json_success('success');
  246. }
  247. /**
  248. * @Desc
  249. * @Author Gorden
  250. * @Date 2024/6/5 13:39
  251. *
  252. * @param Request $request
  253. * @return array
  254. * @throws BusinessException
  255. */
  256. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  257. {
  258. $params = $request->post();
  259. $data = $this->inputFilter($params);
  260. $goods = Goods::find($params['goods_id']);
  261. $extendJson = [];
  262. // if (!$params['is_custom']) {
  263. Db::beginTransaction();
  264. try {
  265. if (!$params['is_custom']) {
  266. $data['pay_amount'] = $goods->goods_sales_price;
  267. }
  268. $orderId = "OD" . date("YmdHis") . random_string(6, 'up');
  269. $orderGroupId = "OD" . date("YmdHis") . random_string(6, 'up');
  270. $orderData = [
  271. 'order_id' => $orderId,
  272. 'order_groupby' => $orderGroupId,
  273. 'join_order_member_id' => $data['join_pay_member_id'],
  274. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  275. 'order_amount_total' => $data['pay_amount'],
  276. 'order_amount_pay' => $data['pay_amount'],
  277. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  278. 'order_status_payment' => 'SUCCESS',
  279. 'order_category' => 'RECHARGE',
  280. 'order_status_system' => 'PAYING',
  281. 'order_status_storage' => 'PENDING',
  282. 'order_addtimes' => time()
  283. ];
  284. if ($type == 'WELFARE') {
  285. $orderData['order_is_complete'] = 'Y';
  286. $orderData['order_status_system'] = 'DONE';
  287. $orderData['order_status_storage'] = 'DONE';
  288. }
  289. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  290. Order::insert($orderData);
  291. $orderSheetData = [
  292. 'join_sheet_member_id' => $data['join_pay_member_id'],
  293. 'join_sheet_order_id' => $orderId,
  294. 'join_sheet_goods_id' => $params['goods_id'],
  295. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  296. 'order_sheet_status' => 'DONE',
  297. 'order_sheet_category' => 'RECHARGE',
  298. 'order_sheet_num' => 1,
  299. 'order_sheet_price' => $data['pay_amount'],
  300. 'order_sheet_amount' => $data['pay_amount'],
  301. 'order_sheet_pay' => $data['pay_amount'],
  302. 'order_sheet_addtimes' => time()
  303. ];
  304. OrderSheet::insert($orderSheetData);
  305. Db::commit();
  306. } catch (\Exception $e) {
  307. dump($e->getMessage());
  308. Db::rollBack();
  309. throw new BusinessException("数据错误");
  310. }
  311. $data['join_pay_order_id'] = $orderGroupId;
  312. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  313. $rate = 0;
  314. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  315. $attributeJson = json_decode($goods->goods_attribute_json, true);
  316. if (isset($attributeJson['added'])) {
  317. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  318. }
  319. }
  320. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  321. $extendJson['added_rate'] = $rate;
  322. // } else {
  323. // // $extendJson['notify'] = 'success';
  324. // $extendJson['is_custom'] = $params['is_custom'];
  325. // }
  326. $data['pay_extend_json'] = json_encode($extendJson);
  327. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  328. $data['pay_prepayid'] = 0;
  329. return $data;
  330. }
  331. }