UnSettmatchLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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") . " 取消赛事处理--参数错误!\n";
  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]!\n";
  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") . " 正在处理中,不能重复提交!\n";
  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 \n";
  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 \n";
  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. if (!in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  90. goto PARAEORROR;
  91. }
  92. $result = $this->getResult($game_code, $match_id);
  93. if (empty($match_id) || empty($game_code) || empty($result) || empty($order_id)) {
  94. PARAEORROR:
  95. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!\n";
  96. unset($datas['token']);
  97. $this->backandret($response, 0, $msg, $datas);
  98. return false;
  99. }
  100. $bet_type = 1;
  101. $order_main = $this->getSimpleOrders($game_code, $match_id, $order_id);
  102. if (empty($order_main)) {
  103. $order_main = $this->getStrOrders($game_code, $match_id, $order_id);
  104. $bet_type = 2;
  105. }
  106. if (empty($order_main) || empty($order_main->matchdatas)) {
  107. goto PARAEORROR;
  108. return;
  109. }
  110. foreach ($order_main->matchdatas as $matitem) {
  111. if ($matitem->order_id == $order_id) {
  112. $matitem->result = 2;
  113. $matitem->matchresult = '手动订单取消(平局)';
  114. $matitem->save();
  115. }
  116. $order_main->status = 3;
  117. $order_main->settlement_time = date('Y-m-d H:i:s');
  118. $order_main->use_mark = '注单所属赛事作废';
  119. $order_main->save();
  120. }
  121. $dataArr = [
  122. 'game_code' => $game_code,
  123. 'match_id' => $match_id,
  124. 'settype' => 2,
  125. 'bettype' => $bet_type,
  126. 'change_status' => 0,
  127. 'order_ids' => $order_id,
  128. ];
  129. $url = 'http://127.0.0.1:9094/Settelement';
  130. $ret_str = post_curls($url, $dataArr);
  131. if (empty($ret_str)) {
  132. goto ERRORCURL;
  133. } else {
  134. $ret = json_decode($ret_str, true);
  135. if (empty($ret) || !isset($ret['status']) || $ret['status'] != 1) {
  136. goto ERRORCURL;
  137. } else {
  138. goto SUCCESSCUL;
  139. }
  140. }
  141. ERRORCURL:
  142. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id .$ret_str \n";
  143. $this->backandret($response, 0, '撤销失败!' . $msg, []);
  144. return;
  145. SUCCESSCUL:
  146. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id order_id=$order_id\n";
  147. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  148. return;
  149. }
  150. //返回消息和写日志
  151. public function backandret($response, $code, $msg, $data, $onlylog = 0)
  152. {
  153. $dataRet = Response::generate('', $code, $data, $msg);
  154. echo $dataRet . "\n";
  155. if ($onlylog) {
  156. return;
  157. }
  158. $response->end($dataRet);
  159. return;
  160. }
  161. //得到返水的比例
  162. public function getFanshui()
  163. {
  164. $ret = (new SetinfoModel())->getInfo(1001);
  165. if (is_array($ret)) {
  166. $val = floatval($ret->infocontent);
  167. $this->fanshui = $val;
  168. return $val;
  169. }
  170. }
  171. //处理单式订单相关逻辑
  172. public function doSimple()
  173. {
  174. $simples = $this->getSimpleOrders($this->game_code, $this->match_id);
  175. if (empty($simples)) {
  176. return;
  177. }
  178. $useraccs = [];
  179. foreach ($simples as $item) {
  180. if (!isset($useraccs[$item->account_identity])) {
  181. $useraccs[$item->account_identity] = $item->account_identity;
  182. }
  183. }
  184. //关联用户
  185. $userInents = $this->getAccountIdents($useraccs);
  186. //用户资金变化记录,以后好一次性更新用户余额
  187. $time = date("Y-m-d H:i:s");
  188. $userMoneyChange = []; //用户资金变化记录,好一次性更新
  189. $userMoneyChange_less = []; //用户余额情况
  190. $orders_array = [];
  191. foreach ($simples as $item) {
  192. $gain_money = floatval($item->gain_money);
  193. $accountIdent = $item->account_identity;
  194. $orders_array[] = $item->order_id;
  195. $mModel = new MoneyDetailsModel();
  196. $mModel->info_identity = UUID();
  197. $mModel->trade_id = $item->order_id;
  198. $mModel->account_name = $userInents[$accountIdent]->account;
  199. $mModel->account_identity = $accountIdent;
  200. $mModel->money_time = $time;
  201. $mModel->money_type = 3;
  202. $mModel->trade_type = 3;
  203. $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id;
  204. if ($gain_money * 100 <= 1) {
  205. $return_money = $item->money * (1 - $this->fanshui);
  206. $mModel->money_type = 1;
  207. $mModel->money = $return_money;
  208. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money;
  209. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $useraccs[$item->account_identity]->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] + $userMoneyChange[$accountIdent];
  210. } else {
  211. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  212. $mModel->money_type = 2;
  213. $mModel->money = $return_money;
  214. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money;
  215. $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $useraccs[$item->account_identity]->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money;
  216. }
  217. $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]);
  218. $mModel->save();
  219. $item->status = 3;
  220. $item->settlement_time = date('Y-m-d H:i:s');
  221. $item->use_mark = '注单所属赛事作废!';
  222. $item->save();
  223. }
  224. if ($userMoneyChange) {
  225. foreach ($userMoneyChange as $uident => $money) {
  226. if (abs($money) > 0) {
  227. $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money;
  228. $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money;
  229. $userInents[$uident]->detail->save();
  230. }
  231. }
  232. }
  233. return true;
  234. }
  235. //处理串式订单相关逻辑
  236. public function doStr()
  237. {
  238. $strOrders = $this->getStrOrders($this->game_code, $this->match_id);
  239. if (empty($strOrders)) {
  240. return;
  241. }
  242. //这里只把结果改为平局,再提交到旧的重新结算订单接口处理
  243. foreach ($strOrders as $item2) {
  244. $item2->status = 2;
  245. $item2->settlement_time = date('Y-m-d H:i:s');
  246. $item2->save();
  247. $mats = $item2->matchdatas;
  248. foreach ($mats as $item3) {
  249. $item3->result = 2;
  250. $item3->matchresult = '赛事撤销!';
  251. $item3->save();
  252. }
  253. }
  254. $dataArr = [
  255. 'game_code' => $this->game_code,
  256. 'match_id' => $this->match_id,
  257. 'settype' => 2,
  258. 'bettype' => 2,
  259. 'change_status' => 0,
  260. 'order_ids' => '',
  261. ];
  262. $url = 'http://127.0.0.1:9094/Settelement';
  263. post_curls($url, $dataArr);
  264. /*
  265. foreach ($strOrders as $item) {
  266. $dataArr['order_ids'] = $item->order_id;
  267. post_curls($url, $dataArr);
  268. }
  269. */
  270. return;
  271. }
  272. //得到某个账户的信息,主要是余额情况
  273. public function getAccountIdents($account_identity_array)
  274. {
  275. $model = new Account();
  276. $ret = [];
  277. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  278. if ($return) {
  279. foreach ($return as $item) {
  280. $ret[$item->identity] = $item;
  281. }
  282. }
  283. return $ret;
  284. }
  285. public function changeStatus($game_code, $match_id)
  286. {
  287. $table = 'st_' . $game_code . '_result';
  288. $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]);
  289. return $ret;
  290. }
  291. public function changeMatchResult($game_code, $match_id, $order_id = 0)
  292. {
  293. $table = 'money_buy_match';
  294. if (empty($order_id)) {
  295. $ret = DB::table($table)->where('match_id', $match_id)->where('game_code', $game_code)->update(['result' => 2, 'matchresult' => '撤单平局!']);
  296. } else {
  297. $ret = DB::table($table)->where([['match_id', '=', $match_id], ['game_code', '=', $game_code], ['order_id', '=', $order_id]])->update(['result' => 2, 'matchresult' => '撤单平局.']);
  298. }
  299. return $ret;
  300. }
  301. //得到比赛结果 ,看是不已结算等信息
  302. public function getResult($game_code, $match_id)
  303. {
  304. $table = 'st_' . $game_code . '_result';
  305. $ret = DB::table($table)->where('match_id', $match_id)->first();
  306. return $ret;
  307. }
  308. //得到 有此赛事的单式订单
  309. public function getSimpleOrders($game_code, $match_id, $order_id = 0)
  310. {
  311. $model = new MoneyBuySimplexModel();
  312. if (empty($order_id)) {
  313. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  314. } else {
  315. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id, 'order_id' => $order_id])->first();
  316. }
  317. return $rets;
  318. }
  319. //得到 有此赛事的串式订单
  320. public function getStrOrders($game_code, $match_id, $order_id = 0)
  321. {
  322. $model = new MoneyBuyStrModel();
  323. if (empty($order_id)) {
  324. $model2 = new MoneyBuyMatchModel();
  325. $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]);
  326. $orders = [];
  327. if ($rets2) {
  328. foreach ($rets2 as $item) {
  329. $orders[] = $item->order_id;
  330. }
  331. } else {
  332. return [];
  333. }
  334. $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get();
  335. return $rets;
  336. }
  337. $rets = $model->with('matchdatas')->where('order_id', $order_id)->first();
  338. return $rets;
  339. }
  340. //获取相关的订单费用及系统赚送的钱(用于返还)
  341. public function getMoneyDetail($ordesArray)
  342. {
  343. $model = new MoneyDetailsModel();
  344. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  345. return $ret;
  346. }
  347. //设置操作标志位,防止事务未处理完,再次提交的情况
  348. //opt 0删除 1设置 2查询
  349. public function optFlag($game_code, $match_id, $opt = 2)
  350. {
  351. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  352. $redis = $this->getRedis();
  353. if ($opt == 0) {
  354. $redis->del($key);
  355. return true;
  356. }
  357. if ($opt == 1) {
  358. $redis->setex($key, 300, time());
  359. return;
  360. }
  361. if ($opt == 2) {
  362. $ret = $redis->get($key);
  363. return $ret;
  364. }
  365. }
  366. public function getRedis()
  367. {
  368. if ($this->redis) {
  369. return $this->redis;
  370. }
  371. $config = GlobConfigs::getKey('redis');
  372. $redis = new \Redis();
  373. $redis->connect($config['host'], $config['port']);
  374. $redis->auth($config['passwd']);
  375. $redis->select($config['db']);
  376. $this->redis = $redis;
  377. return $redis;
  378. }
  379. }