CashTradeOrder.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <?php
  2. namespace app\models\trade;
  3. use app\models\user\User;
  4. use app\models\user\UserBanks;
  5. use app\models\user\UserBill;
  6. use app\models\user\UserMoney;
  7. use app\models\user\UserMoneyOrder;
  8. use crmeb\repositories\ShortLetterRepositories;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\Exception;
  15. use think\Model;
  16. class CashTradeOrder extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'cash_trade_order';
  28. use ModelTrait;
  29. /**
  30. * @param $money_type
  31. * @return int|string|null
  32. */
  33. public static function averagePrice($money_type)
  34. {
  35. $all_money = self::whereTime('trade_time', 'yesterday')->where('status', 2)->where('money_type', $money_type)->sum('money_num');
  36. $all_cash = self::whereTime('trade_time', 'yesterday')->where('status', 2)->where('money_type', $money_type)->sum('all_price');
  37. if ($all_money > 0)
  38. return bcdiv($all_cash, $all_money, 8);
  39. else
  40. return "0";
  41. }
  42. /**
  43. * @param $where
  44. * @return self
  45. */
  46. public static function setStatus($where)
  47. {
  48. if (is_array($where)) {
  49. if (isset($where['status'])) {
  50. $status = $where['status'];
  51. } else {
  52. return new self();
  53. }
  54. } else {
  55. $status = $where;
  56. }
  57. switch ($status) {
  58. case 1://挂出中
  59. return self::where('status', 'in', [0, -1])->where('end_time', '>', time())->where('trade_uid', null);
  60. case 2://已完成
  61. return self::where('status', 1);
  62. case 3://挂出时间结束
  63. return self::where('status', 'in', [0, -1, 2])->where('end_time', '<', time());
  64. case 4://挂出时间结束,未领回
  65. return self::where('status', 'in', [0, -1])->where('end_time', '<', time());
  66. case 5://已领回
  67. return self::where('status', 2);
  68. case 6://交易中,已上传凭证
  69. return self::where('status', 3);
  70. case 7://未结束 但是关闭
  71. return self::where('status', 'in', [0, -1])->where('end_time', '>', time())->where('close_time', '<', time());
  72. default:
  73. return new self();
  74. }
  75. }
  76. /**
  77. * @param $where
  78. * @return array
  79. */
  80. public static function getList($where)
  81. {
  82. $model = self::setStatus($where);
  83. // if (isset($where['uid']) && $where['uid']) {
  84. // $model = $model->where('uid|trade_uid', $where['uid']);
  85. // }
  86. if (isset($where['trade_type']) && $where['trade_type'] != '') {
  87. if (isset($where['uid']) && $where['uid']) {
  88. $model = $model->where(function ($query) use ($where) {
  89. $query->where('trade_uid', $where['uid'])
  90. ->where('trade_type', $where['trade_type']);
  91. })->whereOr(function ($query) use ($where) {
  92. $query->where('uid', $where['uid'])
  93. ->where('trade_type', '<>', $where['trade_type']);
  94. });
  95. } else {
  96. $model = $model->where('trade_type', $where['trade_type']);
  97. }
  98. }
  99. if (isset($where['money_type']) && $where['money_type'] != '') {
  100. $model = $model->where('money_type', $where['money_type']);
  101. }
  102. if (isset($where['order_id']) && $where['order_id'] != '') {
  103. $model = $model->where('order_id', $where['order_id']);
  104. }
  105. if (isset($where['trade_uid']) && $where['trade_uid'] != '') {
  106. $model = $model->where('trade_uid', $where['trade_uid']);
  107. }
  108. $count = $model->count();
  109. // var_dump(self::getLastSql());
  110. $list = $model->order('trade_start_time desc,add_time desc')->page((int)$where['page'], (int)$where['limit'])->select()
  111. // var_dump(self::getLastSql());
  112. ->each(function ($item) {
  113. $item['user'] = User::getUserInfo($item['uid']);
  114. $item['_trade_user'] = $item['trade_uid'] === 0 ? '系统/0' : ($item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--');
  115. $item['trade_user'] = $item['trade_uid'] === 0 ? [] : User::getUserInfo($item['trade_uid']);
  116. // $item['_add_time'] = time_format($item['add_time']);
  117. // $item['_end_time'] = time_format($item['end_time']);
  118. // $item['_trade_time'] = time_format($item['trade_time']);
  119. $money_type = init_money_type();
  120. $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  121. $item['bank'] = json_decode($item['bank_info'], true);
  122. $item['_name_text'] = '--';
  123. $item['_account_text'] = '--';
  124. $item['_code_text'] = '--';
  125. $item['_name'] = '--';
  126. $item['_account'] = '--';
  127. $item['_code'] = '--';
  128. if ($item['bank']) {
  129. if ($item['bank']['type'] == 1) {
  130. $item['_name_text'] = '支付宝用户';
  131. $item['_account_text'] = '支付宝账号';
  132. $item['_code_text'] = '支付宝二维码';
  133. $item['_name'] = $item['bank']['name'];
  134. $item['_account'] = $item['bank']['account'];
  135. $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
  136. } else if ($item['bank']['type'] == 2) {
  137. $item['_name_text'] = '微信用户';
  138. $item['_account_text'] = '微信账号';
  139. $item['_code_text'] = '微信二维码';
  140. $item['_name'] = $item['bank']['name'];
  141. $item['_account'] = $item['bank']['account'];
  142. $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
  143. } else {
  144. $item['_name_text'] = '开户人';
  145. $item['_account_text'] = '银行卡号';
  146. $item['_code_text'] = '开户行';
  147. $item['_name'] = $item['bank']['name'];
  148. $item['_account'] = $item['bank']['account'];
  149. $item['_code'] = $item['bank']['code'];
  150. }
  151. }
  152. $item['_add_time'] = time_format($item['add_time']);
  153. $item['_end_time'] = $item['end_time'] ? time_format($item['end_time']) : '--';
  154. $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
  155. $item['_money_type'] = $money_type[$item['money_type']];
  156. $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
  157. $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
  158. switch ($item['status']) {
  159. case -1:
  160. $item['_status'] = '未支付';
  161. break;
  162. case 0:
  163. $item['_status'] = '未完成';
  164. break;
  165. case 1:
  166. $item['_status'] = '已完成';
  167. break;
  168. case 2:
  169. $item['_status'] = '已收回';
  170. break;
  171. case 3:
  172. $item['_status'] = '交易中【‘已提交交易凭证’】';
  173. break;
  174. default:
  175. $item['_status'] = '未知';
  176. break;
  177. }
  178. });
  179. // var_dump($list);
  180. return compact('count', 'list');
  181. }
  182. public static function getInfo($id)
  183. {
  184. $item = self::get($id);
  185. $item['user'] = User::getUserInfo($item['uid']);
  186. $item['_trade_user'] = $item['trade_uid'] === 0 ? '系统/0' : ($item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--');
  187. $item['trade_user'] = $item['trade_uid'] === 0 ? [] : User::getUserInfo($item['trade_uid']);
  188. // $item['_add_time'] = time_format($item['add_time']);
  189. // $item['_end_time'] = time_format($item['end_time']);
  190. // $item['_trade_time'] = time_format($item['trade_time']);
  191. $money_type = init_money_type();
  192. $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  193. $item['bank'] = json_decode($item['bank_info'], true);
  194. $item['_name_text'] = '--';
  195. $item['_account_text'] = '--';
  196. $item['_code_text'] = '--';
  197. $item['_name'] = '--';
  198. $item['_account'] = '--';
  199. $item['_code'] = '--';
  200. if ($item['bank']) {
  201. if ($item['bank']['type'] == 1) {
  202. $item['_name_text'] = '支付宝用户';
  203. $item['_account_text'] = '支付宝账号';
  204. $item['_code_text'] = '支付宝二维码';
  205. $item['_name'] = $item['bank']['name'];
  206. $item['_account'] = $item['bank']['account'];
  207. $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
  208. } else if ($item['bank']['type'] == 2) {
  209. $item['_name_text'] = '微信用户';
  210. $item['_account_text'] = '微信账号';
  211. $item['_code_text'] = '微信二维码';
  212. $item['_name'] = $item['bank']['name'];
  213. $item['_account'] = $item['bank']['account'];
  214. $item['_code'] = "<img src='" . $item['bank']['code'] . "'>";
  215. } else {
  216. $item['_name_text'] = '开户人';
  217. $item['_account_text'] = '银行卡号';
  218. $item['_code_text'] = '开户行';
  219. $item['_name'] = $item['bank']['name'];
  220. $item['_account'] = $item['bank']['account'];
  221. $item['_code'] = $item['bank']['code'];
  222. }
  223. }
  224. $item['_add_time'] = time_format($item['add_time']);
  225. $item['_end_time'] = $item['end_time'] ? time_format($item['end_time']) : '--';
  226. $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
  227. $item['_money_type'] = $money_type[$item['money_type']];
  228. $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
  229. $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
  230. switch ($item['status']) {
  231. case -1:
  232. $item['_status'] = '未支付';
  233. break;
  234. case 0:
  235. $item['_status'] = '未完成';
  236. break;
  237. case 1:
  238. $item['_status'] = '已完成';
  239. break;
  240. case 2:
  241. $item['_status'] = '已收回';
  242. break;
  243. case 3:
  244. $item['_status'] = '交易中【‘已提交交易凭证’】';
  245. break;
  246. default:
  247. $item['_status'] = '未知';
  248. break;
  249. }
  250. return $item;
  251. }
  252. public static function getOneUserList($where)
  253. {
  254. $model = self::setStatus($where);
  255. $model = $model->where('uid|trade_uid', $where['uid']);
  256. // if (isset($where['trade_type']) && $where['trade_type'] != '') {
  257. // if (isset($where['uid']) && $where['uid']) {
  258. // $model = $model->where(function ($query) use ($where) {
  259. // $query->where('trade_uid', $where['uid'])
  260. // ->where('trade_type', $where['trade_type']);
  261. // })->whereOr(function ($query) use ($where) {
  262. // $query->where('uid', $where['uid'])
  263. // ->where('trade_type', '<>', $where['trade_type']);
  264. // });
  265. // } else {
  266. // $model = $model->where('trade_type', $where['trade_type']);
  267. // }
  268. // }
  269. // if (isset($where['money_type']) && $where['money_type'] != '') {
  270. // $model = $model->where('money_type', $where['money_type']);
  271. // }
  272. // $count = $model->count();
  273. // var_dump(self::getLastSql());
  274. $list = $model->page((int)$where['page'], (int)$where['limit'])->select()
  275. // var_dump(self::getLastSql());
  276. ->each(function ($item) use ($where) {
  277. $money_type = init_money_type();
  278. $item['_user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  279. $item['_trade_user'] = $item['trade_uid'] ? User::getUserInfo($item['trade_uid'])['nickname'] . '/' . $item['trade_uid'] : '--';
  280. $item['_add_time'] = time_format($item['add_time']);
  281. $item['_end_time'] = $item['_end_time'] ? time_format($item['end_time']) : '--';
  282. $item['_trade_time'] = $item['trade_time'] ? time_format($item['trade_time']) : '--';
  283. $item['_money_type'] = $money_type[$item['money_type']];
  284. $item['_trade_type'] = $item['trade_type'] == 2 ? '挂买' : '挂卖';
  285. $item['_proof'] = $item['proof'] ? "<img src='" . $item['proof'] . "'>" : '--';
  286. switch ($item['status']) {
  287. case -1:
  288. $item['_status'] = '未支付';
  289. break;
  290. case 0:
  291. $item['_status'] = '未完成';
  292. break;
  293. case 1:
  294. $item['_status'] = '已完成';
  295. break;
  296. case 2:
  297. $item['_status'] = '已收回';
  298. break;
  299. case 3:
  300. $item['_status'] = '交易中【‘已提交交易凭证’】';
  301. break;
  302. default:
  303. $item['_status'] = '未知';
  304. break;
  305. }
  306. });
  307. return $list;
  308. }
  309. public static function hangSale($uid, $money_type, $money_num, $price, $hang_time, $bank_id)
  310. {
  311. $user = User::getUserInfo($uid);
  312. if (!$user['sale_switch']) {
  313. return self::setErrorInfo('当前用户不可挂卖!');
  314. }
  315. if (!$bank_id) {
  316. return self::setErrorInfo('请选择收款方式!');
  317. }
  318. $bank = UserBanks::get($bank_id);
  319. if (!$bank || $bank['uid'] != $uid) {
  320. return self::setErrorInfo('收款方式错误!');
  321. }
  322. $money = UserMoney::initialUserMoney($uid, $money_type);
  323. if ($money['money'] < $money_num) return self::setErrorInfo('账户不足!');
  324. $sum = bcmul($money_num, $price, 2);
  325. BaseModel::beginTrans();
  326. try {
  327. $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
  328. if (!$res) {
  329. return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  330. }
  331. $res = self::create([
  332. 'order_id' => self::getOrderId(),
  333. 'uid' => $uid,
  334. 'money_type' => $money_type,
  335. 'money_num' => $money_num,
  336. 'price' => $price,
  337. 'all_price' => $sum,
  338. 'trade_type' => 1,
  339. 'add_time' => time(),
  340. 'hang_time' => $hang_time,
  341. 'end_time' => bcadd(time(), bcmul($hang_time, 3600)),
  342. 'bank_info' => json_encode($bank)
  343. ]);
  344. if ($res) {
  345. BaseModel::commitTrans();
  346. return $res;
  347. } else {
  348. return self::setErrorInfo('挂卖失败', true);
  349. }
  350. } catch (Exception $e) {
  351. return self::setErrorInfo($e->getMessage(), true);
  352. }
  353. }
  354. public static function fastSale($uid, $money_type, $money_num, $price, $bank_id)
  355. {
  356. // $user = User::getUserInfo($uid);
  357. // if (!$user['bank_code'] && !$user['alipay_code'] && !$user['alipay_account'] && !$user['wechat_code'] && !$user['wechat_account']) {
  358. // return self::setErrorInfo('请先设置收款方式!');
  359. // }
  360. if (!$bank_id) {
  361. return self::setErrorInfo('请选择收款方式!');
  362. }
  363. $bank = UserBanks::get($bank_id);
  364. if (!$bank || $bank['uid'] != $uid) {
  365. return self::setErrorInfo('收款方式错误!');
  366. }
  367. $money = UserMoney::initialUserMoney($uid, $money_type);
  368. if ($money['money'] < $money_num) return self::setErrorInfo('账户不足!');
  369. $sum = bcmul($money_num, $price, 2);
  370. BaseModel::beginTrans();
  371. try {
  372. $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
  373. if (!$res) {
  374. return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  375. }
  376. $res = self::create([
  377. 'order_id' => self::getOrderId(),
  378. 'uid' => $uid,
  379. 'money_type' => $money_type,
  380. 'money_num' => $money_num,
  381. 'price' => $price,
  382. 'all_price' => $sum,
  383. 'trade_type' => 1,
  384. 'add_time' => time(),
  385. 'hang_time' => 0,
  386. 'trade_uid' => 0,
  387. 'status' => -1,
  388. 'end_time' => PHP_INT_MAX,
  389. 'bank_info' => json_encode($bank)
  390. ]);
  391. if ($res) {
  392. self::commitTrans();
  393. return true;
  394. } else {
  395. return self::setErrorInfo('挂卖失败', true);
  396. }
  397. } catch (Exception $e) {
  398. return self::setErrorInfo($e->getMessage(), true);
  399. }
  400. }
  401. public static function hangBuy2($uid, $money_type, $money_num, $price, $hang_time)
  402. {
  403. $sum = bcmul($money_num, $price, 2);
  404. BaseModel::beginTrans();
  405. try {
  406. // $res = UserMoney::expendMoney($uid, $money_type, $money_num, 'hang_sale', '挂卖', '挂卖' . $money_num);
  407. // if (!$res) {
  408. // return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  409. // }
  410. $res = self::create([
  411. 'order_id' => self::getOrderId(),
  412. 'uid' => $uid,
  413. 'money_type' => $money_type,
  414. 'money_num' => $money_num,
  415. 'price' => $price,
  416. 'all_price' => $sum,
  417. 'trade_type' => 2,
  418. 'add_time' => time(),
  419. 'hang_time' => $hang_time,
  420. 'status' => -1,//未支付虚拟货币
  421. 'end_time' => bcadd(time(), bcmul($hang_time, 3600)),
  422. ]);
  423. if ($res) {
  424. BaseModel::commitTrans();
  425. return $res;
  426. } else {
  427. return self::setErrorInfo('挂买失败', true);
  428. }
  429. } catch (Exception $e) {
  430. return self::setErrorInfo($e->getMessage(), true);
  431. }
  432. }
  433. public static function hangBuy($uid, $money_type, $money_num, $price, $hang_time)
  434. {
  435. $sum = bcmul($money_num, $price, 2);
  436. BaseModel::beginTrans();
  437. try {
  438. $res = CashTradeOrderPayment::create([
  439. 'uid' => $uid,
  440. 'order_id' => CashTradeOrderPayment::getOrderId(),
  441. 'order_type' => 1,
  442. 'pay_money' => $sum,
  443. 'add_time' => time(),
  444. 'order_info' => json_encode([
  445. 'uid' => $uid,
  446. 'money_type' => $money_type,
  447. 'money_num' => $money_num,
  448. 'price' => $price,
  449. 'all_price' => $sum,
  450. 'trade_type' => 2,
  451. 'hang_time' => $hang_time,]),
  452. ]);
  453. if ($res) {
  454. BaseModel::commitTrans();
  455. return $res;
  456. } else {
  457. return self::setErrorInfo('订单生成失败', true);
  458. }
  459. } catch (Exception $e) {
  460. return self::setErrorInfo($e->getMessage(), true);
  461. }
  462. }
  463. public static function getOrderId()
  464. {
  465. list($msec, $sec) = explode(' ', microtime());
  466. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  467. do {
  468. $str = 'ct' . $msectime . mt_rand(10000, 99999);
  469. } while (self::be(['order_id' => $str]));
  470. return $str;
  471. }
  472. public static function sale($uid, $id, $bank_id)
  473. {
  474. if (!$bank_id) {
  475. return self::setErrorInfo('请选择收款方式!');
  476. }
  477. $bank = UserBanks::get($bank_id);
  478. if (!$bank || $bank['uid'] != $uid) {
  479. return self::setErrorInfo('收款方式错误!');
  480. }
  481. $order = self::setStatus(1)->where('id', $id)->find();
  482. if (!$order) {
  483. return self::setErrorInfo('订单不存在或已结束挂出');
  484. }
  485. // $user = User::getUserInfo($uid);
  486. // if (!$user['bank_code'] && !$user['alipay_code'] && !$user['alipay_account'] && !$user['wechat_code'] && !$user['wechat_account']) {
  487. // return self::setErrorInfo('请先设置收款方式!');
  488. // }
  489. $money = UserMoney::initialUserMoney($uid, $order['money_type']);
  490. if ($money['money'] < $order['money_num']) return self::setErrorInfo('账户不足!');
  491. BaseModel::beginTrans();
  492. try {
  493. $res = UserMoney::expendMoney($uid, $order['money_type'], $order['money_num'], 'sale', '售币成功', '售币成功,购买方:' . $order['uid']);
  494. if (!$res) {
  495. return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  496. }
  497. $res = self::where('id', $id)->update([
  498. 'status' => 0,
  499. 'trade_uid' => $uid,
  500. 'bank_info' => json_encode($bank),
  501. 'trade_start_time' => time(),
  502. //'trade_way' => $order['pay_type'],
  503. // 'trade_time' => time(),
  504. ]);
  505. // $userInfo = User::getUserInfo($uid);
  506. // $res = $res && UserMoney::incomeMoney($order['uid'], $order['money_type'], $order['money_num'], 'buy_success', '购币成功', '购币成功,出售方:' . $uid);
  507. // $res = $res && (false !== User::bcInc($userInfo['uid'], 'now_money', $order['all_price'], 'uid'));
  508. // $res = $res && UserBill::income('售币成功', $userInfo['uid'], 'now_money', 'sale_success', $order['all_price'], $order['id'], $userInfo['now_money'] + $order['all_price'], '售币成功,购买方:' . $order['uid']);
  509. if ($res) {
  510. BaseModel::commitTrans();
  511. $user = User::getUserInfo($order['uid']);
  512. ShortLetterRepositories::SMSSend($user['phone'], ['code' => 0], 'START_TRADE');
  513. return $res;
  514. } else {
  515. return self::setErrorInfo('出售失败', true);
  516. }
  517. } catch (Exception $e) {
  518. return self::setErrorInfo($e->getMessage(), true);
  519. }
  520. }
  521. public static function buy($uid, $id)
  522. {
  523. $order = self::setStatus(1)->where('id', $id)->find();
  524. if (!$order) {
  525. return self::setErrorInfo('订单不存在或已结束挂出');
  526. }
  527. BaseModel::beginTrans();
  528. try {
  529. $res = CashTradeOrderPayment::create([
  530. 'uid' => $uid,
  531. 'order_id' => CashTradeOrderPayment::getOrderId(),
  532. 'bind_order' => $id,
  533. 'order_type' => 2,
  534. 'pay_money' => $order['all_price'],
  535. 'add_time' => time()
  536. ]);
  537. if ($res) {
  538. BaseModel::commitTrans();
  539. return $res;
  540. } else {
  541. return self::setErrorInfo('订单生成失败', true);
  542. }
  543. } catch (Exception $e) {
  544. return self::setErrorInfo($e->getMessage(), true);
  545. }
  546. }
  547. public static function buy2($uid, $id)
  548. {
  549. $order = self::setStatus(1)->where('id', $id)->find();
  550. if (!$order) {
  551. return self::setErrorInfo('订单不存在或已结束挂出');
  552. }
  553. // $money = UserMoney::initialUserMoney($uid, $order['money_type']);
  554. // if ($money['money'] < $order['money_num']) return self::setErrorInfo('账户不足!');
  555. BaseModel::beginTrans();
  556. try {
  557. // $res = UserMoney::expendMoney($uid, $order['money_type'], $order['money_num'], 'sale', '售币成功', '售币成功,购买方:' . $order['uid']);
  558. // if (!$res) {
  559. // return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  560. // }
  561. $res = self::where('id', $id)->update([
  562. 'status' => -1,
  563. 'trade_uid' => $uid,
  564. 'close_time' => time() + 30 * 60,
  565. 'trade_start_time' => time(),
  566. ]);
  567. if ($res) {
  568. BaseModel::commitTrans();
  569. return $res;
  570. } else {
  571. return self::setErrorInfo('申请购买失败', true);
  572. }
  573. } catch (Exception $e) {
  574. return self::setErrorInfo($e->getMessage(), true);
  575. }
  576. }
  577. public static function getBack()
  578. {
  579. $list = self::setStatus(4)->select();
  580. BaseModel::beginTrans();
  581. $res = true;
  582. foreach ($list as $v) {
  583. switch ($v['trade_type']) {
  584. case 1:
  585. //退回币
  586. $res = $res && UserMoney::incomeMoney($v['uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
  587. $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
  588. break;
  589. case 2:
  590. // $userInfo = User::getUserInfo($v['uid']);
  591. // $res = $res && (false !== User::bcInc($v['uid'], 'now_money', $v['all_price'], 'uid'));
  592. // $res = $res && UserBill::income('售币成功', $v['uid'], 'now_money', 'hang_buy_send_back', $v['all_price'], $v['id'], $userInfo['now_money'] + $v['all_price'], '挂买时间结束,退回余额');
  593. // $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
  594. if ($v['trade_uid'])
  595. $res = $res && UserMoney::incomeMoney($v['trade_uid'], $v['money_type'], $v['money_num'], 'hang_buy_send_back', '挂买退回', '挂买时间结束,退回钱包');
  596. $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
  597. break;
  598. default:
  599. break;
  600. }
  601. }
  602. BaseModel::checkTrans($res);
  603. return $res;
  604. }
  605. public static function orderClose()
  606. {
  607. $list = self::setStatus(7)->select();
  608. // var_dump($list);
  609. BaseModel::beginTrans();
  610. $res = true;
  611. try {
  612. foreach ($list as $v) {
  613. switch ($v['trade_type']) {
  614. case 1:
  615. // var_dump(1111);
  616. //退回币
  617. // $res = $res && UserMoney::incomeMoney($v['uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
  618. $res = $res && self::where('id', $v['id'])->update(['trade_uid' => null, 'status' => 0, 'close_time' => null, 'trade_start_time' => null,]);
  619. break;
  620. case 2:
  621. // var_dump(2222);
  622. // $userInfo
  623. // = User::getUserInfo($v['uid']);
  624. // $res = $res && (false !== User::bcInc($v['uid'], 'now_money', $v['all_price'], 'uid'));
  625. // $res = $res && UserBill::income('售币成功', $v['uid'], 'now_money', 'hang_buy_send_back', $v['all_price'], $v['id'], $userInfo['now_money'] + $v['all_price'], '挂买时间结束,退回余额');
  626. // $res = $res && self::where('id', $v['id'])->update(['status' => 2]);
  627. if ($v['trade_uid'])
  628. $res = $res && UserMoney::incomeMoney($v['trade_uid'], $v['money_type'], $v['money_num'], 'hang_sale_send_back', '挂卖退回', '挂卖时间结束,退回钱包');
  629. $res = $res && self::where('id', $v['id'])->update(['trade_uid' => null, 'status' => -1, 'close_time' => null, 'trade_start_time' => null,]);
  630. break;
  631. default:
  632. break;
  633. }
  634. }
  635. } catch (Exception $e) {
  636. BaseModel::rollbackTrans();
  637. @file_put_contents('orderClose.error.log', '[' . date('Y-m-d H:i:s') . ']' . $e->getMessage() . PHP_EOL, FILE_APPEND);
  638. }
  639. BaseModel::commitTrans();
  640. return $res;
  641. }
  642. public static function brokerage($id)
  643. {
  644. $info = self::get($id);
  645. // if ($info['trade_uid'] !== 0) return true;
  646. if ($info['trade_type'] == 2)
  647. $user = User::getUserInfo($info['trade_uid']);
  648. else
  649. $user = User::getUserInfo($info['uid']);
  650. $uper = User::getUserInfo($user['spread_uid']);
  651. // var_dump($info);
  652. // var_dump($user['uid']);
  653. // var_dump($user['spread_uid']);
  654. // exit;
  655. $send = 0;
  656. $res = true;
  657. while ($uper) {
  658. $ratio = bcdiv($uper['return_uper'], 100, 2);
  659. if ($ratio > 0) {
  660. $brokerage = bcmul($ratio, $info['money_num'], 8);
  661. if ($brokerage > $send) {
  662. $brokerage = bcsub($brokerage, $send, 8);
  663. $res = $res && UserMoney::incomeMoney($uper['uid'], $info['money_type'], $brokerage, 'return_uper', '佣金', $user['nickname'] . '发生交易,获得佣金');
  664. $send = bcadd($brokerage, $send, 8);
  665. }
  666. }
  667. $uper = User::getUserInfo($uper['spread_uid']);
  668. }
  669. return $res;
  670. }
  671. }