UnSettmatchLogic.php 19 KB

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