UnSettmatchLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. $ret = $rets['0'];
  167. $uadd = $ret['1'];
  168. $ulose = $ret['2'];
  169. $return = $ulose - $uadd;
  170. return $return;
  171. }
  172. //撤销串式订单(整个订单撤销)
  173. public function UnSettelementOneStringOrder($request, $response, $datas)
  174. {
  175. return $this->doUnsetMatchOneOrder($request, $response, $datas);
  176. }
  177. //返回消息和写日志
  178. public function backandret($response, $code, $msg, $data, $onlylog = 0)
  179. {
  180. $dataRet = Response::generate('', $code, $data, $msg);
  181. echo $dataRet . "\n";
  182. if ($onlylog) {
  183. return;
  184. }
  185. $response->end($dataRet);
  186. return;
  187. }
  188. //得到返水的比例
  189. public function getFanshui()
  190. {
  191. $ret = (new SetinfoModel())->getInfo(1001);
  192. if (is_array($ret)) {
  193. $val = floatval($ret['infocontent']);
  194. $this->fanshui = $val;
  195. return $val;
  196. }
  197. }
  198. //处理单式订单相关逻辑
  199. public function doSimple()
  200. {
  201. $simples = $this->getSimpleOrders($this->game_code, $this->match_id);
  202. if (empty($simples)) {
  203. return;
  204. }
  205. $useraccs = [];
  206. foreach ($simples as $item) {
  207. if (!isset($useraccs[$item->account_identity])) {
  208. $useraccs[$item->account_identity] = $item->account_identity;
  209. }
  210. }
  211. //关联用户
  212. $userInents = $this->getAccountIdents($useraccs);
  213. //用户资金变化记录,以后好一次性更新用户余额
  214. $time = date("Y-m-d H:i:s");
  215. $userMoneyChange = []; //用户资金变化记录,好一次性更新
  216. $userMoneyChange_less = []; //用户余额情况
  217. $orders_array = [];
  218. foreach ($simples as $item) {
  219. $gain_money = floatval($item->gain_money);
  220. $accountIdent = $item->account_identity;
  221. $orders_array[] = $item->order_id;
  222. $mModel = new MoneyDetailsModel();
  223. $mModel->info_identity = UUID();
  224. $mModel->trade_id = $item->order_id;
  225. $mModel->account_name = $userInents[$accountIdent]->account;
  226. $mModel->account_identity = $accountIdent;
  227. $mModel->money_time = $time;
  228. $mModel->money_type = 3;
  229. $mModel->trade_type = 3;
  230. $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id;
  231. if ($gain_money * 100 <= 1) {
  232. $return_money = $item->money * (1 - $this->fanshui);
  233. $mModel->money_type = 1;
  234. $mModel->money = $return_money;
  235. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money;
  236. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] + $userMoneyChange[$accountIdent];
  237. } else {
  238. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  239. $mModel->money_type = 2;
  240. $mModel->money = $return_money;
  241. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money;
  242. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money;
  243. }
  244. $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]);
  245. $mModel->save();
  246. $item->status = 2;
  247. $item->gain_money = 0;
  248. $item->game_status = 2;
  249. $item->settlement_time = date('Y-m-d H:i:s');
  250. $item->use_mark = '';
  251. $item->save();
  252. }
  253. if ($userMoneyChange) {
  254. foreach ($userMoneyChange as $uident => $money) {
  255. if (abs($money) > 0) {
  256. $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money;
  257. $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money;
  258. $userInents[$uident]->detail->save();
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. //处理串式订单相关逻辑
  265. public function doStr()
  266. {
  267. $strOrders = $this->getStrOrders($this->game_code, $this->match_id);
  268. if (empty($strOrders)) {
  269. return;
  270. }
  271. $userMoneyArr = [];
  272. //这里只把结果改为平局,再提交到旧的重新结算订单接口处理
  273. foreach ($strOrders as $item2) {
  274. //如果存在中奖的,要先扣回赢的钱,再以平处理
  275. if ($item2->gain_money > 0) {
  276. $acc = $item2->account_indentity;
  277. $nowmoney = $item2->gain_money;
  278. $userMoneyArr[$acc] = isset($userMoneyArr[$acc]) ? $userMoneyArr[$acc] + $nowmoney : $nowmoney;
  279. }
  280. $item2->status = 2;
  281. $item2->gain_money = 0;
  282. $item2->game_status = 2;
  283. $item2->settlement_time = date('Y-m-d H:i:s');
  284. $item2->save();
  285. $mats = $item2->matchdatas;
  286. foreach ($mats as $item3) {
  287. $item3->result = 2;
  288. $item3->matchresult = '通过设置赛事比分作废!';
  289. $item3->save();
  290. }
  291. }
  292. if ($userMoneyArr) {
  293. $this->AccountMoneydec($userMoneyArr);
  294. }
  295. $dataArr = [
  296. 'game_code' => $this->game_code,
  297. 'match_id' => $this->match_id,
  298. 'settype' => 2,
  299. 'bettype' => 2,
  300. 'change_status' => 0,
  301. 'order_ids' => '',
  302. ];
  303. $url = 'http://127.0.0.1:9094/Settelement';
  304. post_curls($url, $dataArr);
  305. /*
  306. foreach ($strOrders as $item) {
  307. $dataArr['order_ids'] = $item->order_id;
  308. post_curls($url, $dataArr);
  309. }
  310. */
  311. return;
  312. }
  313. //对于已结算的又撤单的要扣回钱
  314. public function AccountMoneydec($account_identity_array)
  315. {
  316. foreach ($account_identity_array as $acc => $money) {
  317. $accarr[] = $acc;
  318. }
  319. $objs = $this->etAccountIdents($accarr);
  320. foreach ($account_identity_array as $acc2 => $money) {
  321. $nowmoney = $objs[$acc2]->detail->available_cash - $money;
  322. $objs[$acc2]->detail->available_cash = $nowmoney;
  323. $objs[$acc2]->detail->cash = $nowmoney;
  324. $objs[$acc2]->detail->save();
  325. }
  326. return true;
  327. }
  328. //得到某个账户的信息,主要是余额情况
  329. public function getAccountIdents($account_identity_array)
  330. {
  331. $model = new Account();
  332. $ret = [];
  333. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  334. if ($return) {
  335. foreach ($return as $item) {
  336. $ret[$item->identity] = $item;
  337. }
  338. }
  339. return $ret;
  340. }
  341. public function changeStatus($game_code, $match_id)
  342. {
  343. $table = 'st_' . $game_code . '_result';
  344. $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]);
  345. return $ret;
  346. }
  347. public function changeMatchResult($game_code, $match_id, $order_id = 0)
  348. {
  349. $table = 'money_buy_match';
  350. if (empty($order_id)) {
  351. $ret = DB::table($table)->where('match_id', $match_id)->where('game_code', $game_code)->update(['result' => 2, 'matchresult' => '撤单平局!']);
  352. } else {
  353. $ret = DB::table($table)->where([['match_id', '=', $match_id], ['game_code', '=', $game_code], ['order_id', '=', $order_id]])->update(['result' => 2, 'matchresult' => '撤单平局.']);
  354. }
  355. return $ret;
  356. }
  357. //得到比赛结果 ,看是不已结算等信息
  358. public function getResult($game_code, $match_id)
  359. {
  360. $table = 'st_' . $game_code . '_result';
  361. $ret = DB::table($table)->where('match_id', $match_id)->first();
  362. return $ret;
  363. }
  364. //得到 有此赛事的单式订单
  365. public function getSimpleOrders($game_code, $match_id, $order_id = 0)
  366. {
  367. $model = new MoneyBuySimplexModel();
  368. if (empty($order_id)) {
  369. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  370. } else {
  371. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id, 'order_id' => $order_id])->first();
  372. }
  373. return $rets;
  374. }
  375. //得到 有此赛事的串式订单
  376. public function getStrOrders($game_code, $match_id, $order_id = 0)
  377. {
  378. $model = new MoneyBuyStrModel();
  379. if (empty($order_id)) {
  380. $model2 = new MoneyBuyMatchModel();
  381. $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]);
  382. $orders = [];
  383. if ($rets2) {
  384. foreach ($rets2 as $item) {
  385. $orders[] = $item->order_id;
  386. }
  387. } else {
  388. return [];
  389. }
  390. $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get();
  391. return $rets;
  392. }
  393. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  394. return $rets;
  395. }
  396. //按订单查询(串式)
  397. public function getByOrderString($order_id)
  398. {
  399. /*
  400. $ret = [];
  401. $model2 = new MoneyBuyMatchModel();
  402. $rets2 = $model2->getByAttrs([['order_ID', '=', $order_id]]);
  403. if (empty($rets2)) {
  404. return $ret;
  405. }
  406. */
  407. $model = new MoneyBuyStrModel();
  408. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  409. return $rets;
  410. }
  411. //获取相关的订单费用及系统赚送的钱(用于返还)
  412. public function getMoneyDetail($ordesArray)
  413. {
  414. $model = new MoneyDetailsModel();
  415. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  416. return $ret;
  417. }
  418. //设置操作标志位,防止事务未处理完,再次提交的情况
  419. //opt 0删除 1设置 2查询
  420. public function optFlag($game_code, $match_id, $opt = 2)
  421. {
  422. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  423. $redis = $this->getRedis();
  424. if ($opt == 0) {
  425. $redis->del($key);
  426. return true;
  427. }
  428. if ($opt == 1) {
  429. $redis->setex($key, 300, time());
  430. return;
  431. }
  432. if ($opt == 2) {
  433. $ret = $redis->get($key);
  434. return $ret;
  435. }
  436. }
  437. public function getRedis()
  438. {
  439. if ($this->redis) {
  440. return $this->redis;
  441. }
  442. $config = GlobConfigs::getKey('redis');
  443. $redis = new \Redis();
  444. $redis->connect($config['host'], $config['port']);
  445. $redis->auth($config['passwd']);
  446. $redis->select($config['db']);
  447. $this->redis = $redis;
  448. return $redis;
  449. }
  450. }