UnSettmatchLogic.php 18 KB

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