UnSettmatchLogic.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/15
  6. * Time: 16:51
  7. */
  8. namespace App\Logic;
  9. use App\Models\Account;
  10. use Illuminate\Database\Capsule\Manager as DB;
  11. use datainf\pplus\Instance;
  12. use datainf\lib\GlobConfigs;
  13. use App\Lib\ModelBase;
  14. use App\Http\Response\Response;
  15. use App\Models\SportsNoteList as MoneyBuySimplexModel;
  16. use App\Models\MoneyBuyStr as MoneyBuyStrModel;
  17. use App\Models\MoneyBuyMatch as MoneyBuyMatchModel;
  18. use App\Models\Money_details as MoneyDetailsModel;
  19. use App\Models\Setinfo as SetinfoModel;
  20. //按赛事撤销操作(批量优化版)
  21. class UnSettmatchLogic
  22. {
  23. use Instance;
  24. protected $redis = '';
  25. private $fanshui = 0.01; //默认返水比例
  26. protected $game_code = '';
  27. protected $match_id = 0;
  28. protected $resultObj = ''; //比赛结果记录对像
  29. public function doUnsetMatch($request, $response, $datas)
  30. {
  31. $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
  32. $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
  33. if (!in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  34. goto PARAEORROR;
  35. }
  36. $result = $this->getResult($game_code, $match_id);
  37. if (empty($match_id) || empty($game_code) || empty($result)) {
  38. PARAEORROR:
  39. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误!";
  40. unset($datas['token']);
  41. $this->backandret($response, 0, $msg, $datas);
  42. return false;
  43. }
  44. $this->game_code = $game_code;
  45. $this->match_id = $match_id;
  46. $this->resultObj = $request;
  47. //这里只做已结算过,又要撤单的情况 ,未结算的,由后台直接处理掉了
  48. if (!in_array($result->status, [3])) {
  49. unset($datas['token']);
  50. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--数据状态值不为[3]!";
  51. $this->backandret($response, 0, $msg, $datas);
  52. return false;
  53. }
  54. if ($this->optFlag($game_code, $match_id, 2)) {
  55. unset($datas['token']);
  56. $msg = date("Y-m-d H:i:s") . " 正在处理中,不能重复提交!";
  57. $this->backandret($response, 0, $msg, $datas);
  58. return false;
  59. }
  60. $this->getFanshui();
  61. $this->optFlag($game_code, $match_id, 1);
  62. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--开始begin:game_code= $game_code match_id= $match_id ";
  63. $this->backandret($response, 1, $msg, $datas, 1);
  64. DB::beginTransaction();
  65. try {
  66. $this->doSimple();
  67. $this->doStr();
  68. $this->changeStatus($game_code, $match_id);
  69. $this->changeMatchResult($game_code, $match_id);
  70. DB::commit();
  71. } catch (\Exception $e) {
  72. DB::rollBack();
  73. unset($datas['token']);
  74. $this->optFlag($game_code, $match_id, 0);
  75. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--异常结束:game_code= $game_code match_id= $match_id Emsg: " . $e->getMessage();
  76. $this->backandret($response, 0, '撤单失败!' . $msg, $datas);
  77. return;
  78. }
  79. $this->optFlag($game_code, $match_id, 0);
  80. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--结束end:game_code= $game_code match_id= $match_id ";
  81. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  82. return;
  83. }
  84. //撤销单个订单
  85. public function doUnsetMatchOneOrder($request, $response, $datas)
  86. {
  87. $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
  88. $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
  89. $order_id = isset($datas['order_id']) ? trim($datas['order_id']) : 0;
  90. $token = isset($datas['token']) ? trim($datas['token']) : '';
  91. if (!in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  92. goto PARAEORROR;
  93. }
  94. $result = $this->getResult($game_code, $match_id);
  95. if (empty($match_id) || empty($game_code) || empty($result) || empty($order_id) || empty($token)) {
  96. PARAEORROR:
  97. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!";
  98. unset($datas['token']);
  99. $this->backandret($response, 0, $msg, $datas);
  100. return false;
  101. }
  102. $bet_type = 1;
  103. $order_main = $this->getSimpleOrders($game_code, $match_id, $order_id);
  104. if (empty($order_main)) {
  105. $order_main = $this->getStrOrders($game_code, $match_id, $order_id);
  106. $bet_type = 2;
  107. }
  108. if (empty($order_main) || empty($order_main->matchdatas)) {
  109. goto PARAEORROR;
  110. return;
  111. }
  112. if ($order_main->status != 1) {
  113. $msg = date("Y-m-d H:i:s") . " 已撤销过的订单(不用处理)!--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  114. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  115. return;
  116. }
  117. foreach ($order_main->matchdatas as $matitem) {
  118. if ($matitem->order_id == $order_id) {
  119. $matitem->result = 2;
  120. $matitem->matchresult = '手动订单取消(平局)';
  121. $matitem->save();
  122. }
  123. }
  124. $dataArr = [
  125. 'game_code' => $game_code,
  126. 'match_id' => $match_id,
  127. 'settype' => 2,
  128. 'bettype' => $bet_type,
  129. 'change_status' => 0,
  130. 'order_ids' => $order_id,
  131. 'token' => $token,
  132. ];
  133. $url = 'http://127.0.0.1:9094/Settelement';
  134. $ret_str = post_curls($url, $dataArr);
  135. if (empty($ret_str)) {
  136. goto ERRORCURL;
  137. } else {
  138. $ret = json_decode($ret_str, true);
  139. if (empty($ret) || !isset($ret['status']) || $ret['status'] != 1) {
  140. goto ERRORCURL;
  141. } else {
  142. sleep(2);
  143. $order_main->status = 3;
  144. $order_main->settlement_time = date('Y-m-d H:i:s');
  145. $order_main->use_mark = '';
  146. $order_main->game_status = 2;
  147. $order_main->save();
  148. goto SUCCESSCUL;
  149. }
  150. }
  151. ERRORCURL:
  152. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id .$ret_str ";
  153. $this->backandret($response, 0, '撤销失败!' . $msg, []);
  154. return;
  155. SUCCESSCUL:
  156. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  157. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  158. return;
  159. }
  160. //撤销串式订单(整个订单撤销)
  161. public function UnSettelementOneStringOrder($request, $response, $datas)
  162. {
  163. $order_id = isset($datas['order_id']) ? trim($datas['order_id']) : 0;
  164. $token = isset($datas['token']) ? trim($datas['token']) : '';
  165. if (empty($order_id) || empty($token)) {
  166. SECPARAEORROR:
  167. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!";
  168. unset($datas['token']);
  169. $this->backandret($response, 0, $msg, $datas);
  170. return false;
  171. }
  172. $order_main = $this->getByOrderString($order_id);
  173. if (empty($order_main)) {
  174. $msg = date("Y-m-d H:i:s") . " 撤销整个串式要单失败,数据为空.order_id= $order_id ";
  175. $this->backandret($response, 0, '撤销失败!' . $msg, []);
  176. return;
  177. }
  178. if ($order_main->status == 2 || $order_main->status == 3) {
  179. $msg = date("Y-m-d H:i:s") . " 已撤销过的订单(不用处理)!--end:order_id=$order_id ";
  180. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  181. return;
  182. }
  183. foreach ($order_main->matchdatas as $matitem) {
  184. if ($matitem->order_id == $order_id) {
  185. $matitem->result = 2;
  186. $matitem->matchresult = '手动订单取消(平局)';
  187. $matitem->save();
  188. }
  189. }
  190. $game_code = $order_main->matchdatas['0']->game_code;
  191. $match_id = $order_main->matchdatas['0']->match_id;
  192. $dataArr = [
  193. 'game_code' => $game_code,
  194. 'match_id' => $match_id,
  195. 'settype' => 2,
  196. 'bettype' => 2,
  197. 'change_status' => 0,
  198. 'order_ids' => $order_id,
  199. 'token' => $token,
  200. ];
  201. $url = 'http://127.0.0.1:9094/Settelement';
  202. $ret_str = post_curls($url, $dataArr);
  203. if (empty($ret_str)) {
  204. goto ERRORCURL;
  205. } else {
  206. $ret = json_decode($ret_str, true);
  207. if (empty($ret) || !isset($ret['status']) || $ret['status'] != 1) {
  208. goto ERRORCURL;
  209. } else {
  210. sleep(2);
  211. $order_main->status = 2;
  212. $order_main->settlement_time = date('Y-m-d H:i:s');
  213. $order_main->use_mark = '';
  214. $order_main->save();
  215. goto SUCCESSCUL;
  216. }
  217. }
  218. ERRORCURL:
  219. $msg = date("Y-m-d H:i:s") . " 串式订单取消处理--end:game_code= $game_code match_id= $match_id .$ret_str ";
  220. $this->backandret($response, 0, '撤销失败!' . $msg, []);
  221. return;
  222. SUCCESSCUL:
  223. $msg = date("Y-m-d H:i:s") . " 串式订单取消--end:game_code= $game_code match_id= $match_id order_id=$order_id ";
  224. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  225. return;
  226. }
  227. //返回消息和写日志
  228. public function backandret($response, $code, $msg, $data, $onlylog = 0)
  229. {
  230. $dataRet = Response::generate('', $code, $data, $msg);
  231. echo $dataRet . "\n";
  232. if ($onlylog) {
  233. return;
  234. }
  235. $response->end($dataRet);
  236. return;
  237. }
  238. //得到返水的比例
  239. public function getFanshui()
  240. {
  241. $ret = (new SetinfoModel())->getInfo(1001);
  242. if (is_array($ret)) {
  243. $val = floatval($ret['infocontent']);
  244. $this->fanshui = $val;
  245. return $val;
  246. }
  247. }
  248. //处理单式订单相关逻辑
  249. public function doSimple()
  250. {
  251. $simples = $this->getSimpleOrders($this->game_code, $this->match_id);
  252. if (empty($simples)) {
  253. return;
  254. }
  255. $useraccs = [];
  256. foreach ($simples as $item) {
  257. if (!isset($useraccs[$item->account_identity])) {
  258. $useraccs[$item->account_identity] = $item->account_identity;
  259. }
  260. }
  261. //关联用户
  262. $userInents = $this->getAccountIdents($useraccs);
  263. //用户资金变化记录,以后好一次性更新用户余额
  264. $time = date("Y-m-d H:i:s");
  265. $userMoneyChange = []; //用户资金变化记录,好一次性更新
  266. $userMoneyChange_less = []; //用户余额情况
  267. $orders_array = [];
  268. foreach ($simples as $item) {
  269. $gain_money = floatval($item->gain_money);
  270. $accountIdent = $item->account_identity;
  271. $orders_array[] = $item->order_id;
  272. $mModel = new MoneyDetailsModel();
  273. $mModel->info_identity = UUID();
  274. $mModel->trade_id = $item->order_id;
  275. $mModel->account_name = $userInents[$accountIdent]->account;
  276. $mModel->account_identity = $accountIdent;
  277. $mModel->money_time = $time;
  278. $mModel->money_type = 3;
  279. $mModel->trade_type = 3;
  280. $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id;
  281. if ($gain_money * 100 <= 1) {
  282. $return_money = $item->money * (1 - $this->fanshui);
  283. $mModel->money_type = 1;
  284. $mModel->money = $return_money;
  285. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money;
  286. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] + $userMoneyChange[$accountIdent];
  287. } else {
  288. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  289. $mModel->money_type = 2;
  290. $mModel->money = $return_money;
  291. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money;
  292. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money;
  293. }
  294. $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]);
  295. $mModel->save();
  296. $item->status = 2;
  297. $item->gain_money = 0;
  298. $item->game_status = 2;
  299. $item->settlement_time = date('Y-m-d H:i:s');
  300. $item->use_mark = '';
  301. $item->save();
  302. }
  303. if ($userMoneyChange) {
  304. foreach ($userMoneyChange as $uident => $money) {
  305. if (abs($money) > 0) {
  306. $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money;
  307. $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money;
  308. $userInents[$uident]->detail->save();
  309. }
  310. }
  311. }
  312. return true;
  313. }
  314. //处理串式订单相关逻辑
  315. public function doStr()
  316. {
  317. $strOrders = $this->getStrOrders($this->game_code, $this->match_id);
  318. if (empty($strOrders)) {
  319. return;
  320. }
  321. $userMoneyArr = [];
  322. //这里只把结果改为平局,再提交到旧的重新结算订单接口处理
  323. foreach ($strOrders as $item2) {
  324. //如果存在中奖的,要先扣回赢的钱,再以平处理
  325. if ($item2->gain_money > 0) {
  326. $acc = $item2->account_indentity;
  327. $nowmoney = $item2->gain_money;
  328. $userMoneyArr[$acc] = isset($userMoneyArr[$acc]) ? $userMoneyArr[$acc] + $nowmoney : $nowmoney;
  329. }
  330. $item2->status = 2;
  331. $item2->gain_money = 0;
  332. $item2->game_status = 2;
  333. $item2->settlement_time = date('Y-m-d H:i:s');
  334. $item2->save();
  335. $mats = $item2->matchdatas;
  336. foreach ($mats as $item3) {
  337. $item3->result = 2;
  338. $item3->matchresult = '通过设置赛事比分作废!';
  339. $item3->save();
  340. }
  341. }
  342. if ($userMoneyArr) {
  343. $this->AccountMoneydec($userMoneyArr);
  344. }
  345. $dataArr = [
  346. 'game_code' => $this->game_code,
  347. 'match_id' => $this->match_id,
  348. 'settype' => 2,
  349. 'bettype' => 2,
  350. 'change_status' => 0,
  351. 'order_ids' => '',
  352. ];
  353. $url = 'http://127.0.0.1:9094/Settelement';
  354. post_curls($url, $dataArr);
  355. /*
  356. foreach ($strOrders as $item) {
  357. $dataArr['order_ids'] = $item->order_id;
  358. post_curls($url, $dataArr);
  359. }
  360. */
  361. return;
  362. }
  363. //对于已结算的又撤单的要扣回钱
  364. public function AccountMoneydec($account_identity_array)
  365. {
  366. foreach ($account_identity_array as $acc => $money) {
  367. $accarr[] = $acc;
  368. }
  369. $objs = $this->etAccountIdents($accarr);
  370. foreach ($account_identity_array as $acc2 => $money) {
  371. $nowmoney = $objs[$acc2]->detail->available_cash - $money;
  372. $objs[$acc2]->detail->available_cash = $nowmoney;
  373. $objs[$acc2]->detail->cash = $nowmoney;
  374. $objs[$acc2]->detail->save();
  375. }
  376. return true;
  377. }
  378. //得到某个账户的信息,主要是余额情况
  379. public function getAccountIdents($account_identity_array)
  380. {
  381. $model = new Account();
  382. $ret = [];
  383. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  384. if ($return) {
  385. foreach ($return as $item) {
  386. $ret[$item->identity] = $item;
  387. }
  388. }
  389. return $ret;
  390. }
  391. public function changeStatus($game_code, $match_id)
  392. {
  393. $table = 'st_' . $game_code . '_result';
  394. $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]);
  395. return $ret;
  396. }
  397. public function changeMatchResult($game_code, $match_id, $order_id = 0)
  398. {
  399. $table = 'money_buy_match';
  400. if (empty($order_id)) {
  401. $ret = DB::table($table)->where('match_id', $match_id)->where('game_code', $game_code)->update(['result' => 2, 'matchresult' => '撤单平局!']);
  402. } else {
  403. $ret = DB::table($table)->where([['match_id', '=', $match_id], ['game_code', '=', $game_code], ['order_id', '=', $order_id]])->update(['result' => 2, 'matchresult' => '撤单平局.']);
  404. }
  405. return $ret;
  406. }
  407. //得到比赛结果 ,看是不已结算等信息
  408. public function getResult($game_code, $match_id)
  409. {
  410. $table = 'st_' . $game_code . '_result';
  411. $ret = DB::table($table)->where('match_id', $match_id)->first();
  412. return $ret;
  413. }
  414. //得到 有此赛事的单式订单
  415. public function getSimpleOrders($game_code, $match_id, $order_id = 0)
  416. {
  417. $model = new MoneyBuySimplexModel();
  418. if (empty($order_id)) {
  419. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  420. } else {
  421. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id, 'order_id' => $order_id])->first();
  422. }
  423. return $rets;
  424. }
  425. //得到 有此赛事的串式订单
  426. public function getStrOrders($game_code, $match_id, $order_id = 0)
  427. {
  428. $model = new MoneyBuyStrModel();
  429. if (empty($order_id)) {
  430. $model2 = new MoneyBuyMatchModel();
  431. $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]);
  432. $orders = [];
  433. if ($rets2) {
  434. foreach ($rets2 as $item) {
  435. $orders[] = $item->order_id;
  436. }
  437. } else {
  438. return [];
  439. }
  440. $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get();
  441. return $rets;
  442. }
  443. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  444. return $rets;
  445. }
  446. //按订单查询(串式)
  447. public function getByOrderString($order_id)
  448. {
  449. /*
  450. $ret = [];
  451. $model2 = new MoneyBuyMatchModel();
  452. $rets2 = $model2->getByAttrs([['order_ID', '=', $order_id]]);
  453. if (empty($rets2)) {
  454. return $ret;
  455. }
  456. */
  457. $model = new MoneyBuyStrModel();
  458. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  459. return $rets;
  460. }
  461. //获取相关的订单费用及系统赚送的钱(用于返还)
  462. public function getMoneyDetail($ordesArray)
  463. {
  464. $model = new MoneyDetailsModel();
  465. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  466. return $ret;
  467. }
  468. //设置操作标志位,防止事务未处理完,再次提交的情况
  469. //opt 0删除 1设置 2查询
  470. public function optFlag($game_code, $match_id, $opt = 2)
  471. {
  472. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  473. $redis = $this->getRedis();
  474. if ($opt == 0) {
  475. $redis->del($key);
  476. return true;
  477. }
  478. if ($opt == 1) {
  479. $redis->setex($key, 300, time());
  480. return;
  481. }
  482. if ($opt == 2) {
  483. $ret = $redis->get($key);
  484. return $ret;
  485. }
  486. }
  487. public function getRedis()
  488. {
  489. if ($this->redis) {
  490. return $this->redis;
  491. }
  492. $config = GlobConfigs::getKey('redis');
  493. $redis = new \Redis();
  494. $redis->connect($config['host'], $config['port']);
  495. $redis->auth($config['passwd']);
  496. $redis->select($config['db']);
  497. $this->redis = $redis;
  498. return $redis;
  499. }
  500. }