UnSettmatchLogic.php 18 KB

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