UnSettmatchLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.1; //默认返水比例
  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. $result = $this->getResult($game_code, $match_id);
  34. if (empty($match_id) || empty($game_code) || empty($result) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
  35. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误!\n";
  36. unset($datas['token']);
  37. $this->backandret($response, 0, $msg, $datas);
  38. return false;
  39. }
  40. $this->game_code = $game_code;
  41. $this->match_id = $match_id;
  42. $this->resultObj = $request;
  43. if (!in_array($result->status, [3])) {
  44. unset($datas['token']);
  45. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--数据状态值不为[3]!\n";
  46. $this->backandret($response, 0, $msg, $datas);
  47. return false;
  48. }
  49. if ($this->optFlag($game_code, $match_id, 2)) {
  50. unset($datas['token']);
  51. $msg = date("Y-m-d H:i:s") . " 正在处理中,不能重复提交!\n";
  52. $this->backandret($response, 0, $msg, $datas);
  53. return false;
  54. }
  55. $this->getFanshui();
  56. $this->optFlag($game_code, $match_id, 1);
  57. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--开始begin:game_code= $game_code match_id= $match_id \n";
  58. $this->backandret($response, 1, $msg, $datas, 1);
  59. DB::beginTransaction();
  60. try {
  61. $this->doSimple();
  62. $this->doStr();
  63. $this->changeStatus($game_code, $match_id);
  64. DB::commit();
  65. } catch (\Exception $e) {
  66. DB::rollBack();
  67. unset($datas['token']);
  68. $this->optFlag($game_code, $match_id, 0);
  69. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--异常结束:game_code= $game_code match_id= $match_id Emsg: " . $e->getMessage();
  70. $this->backandret($response, 0, '撤单失败!' . $msg, $datas);
  71. return;
  72. }
  73. $this->optFlag($game_code, $match_id, 0);
  74. $msg = date("Y-m-d H:i:s") . " 取消赛事处理--结束end:game_code= $game_code match_id= $match_id \n";
  75. $this->backandret($response, 1, '撤销成功!' . $msg, []);
  76. return;
  77. }
  78. //返回消息和写日志
  79. public function backandret($response, $code, $msg, $data, $onlylog = 0)
  80. {
  81. $dataRet = Response::generate('', $code, $data, $msg);
  82. echo $dataRet . "\n";
  83. if ($onlylog) {
  84. return;
  85. }
  86. $response->end($dataRet);
  87. return;
  88. }
  89. //得到返水的比例
  90. public function getFanshui()
  91. {
  92. $ret = (new SetinfoModel())->getInfo(1001);
  93. if (is_array($ret)) {
  94. $val = floatval($ret->infocontent);
  95. $this->fanshui = $val;
  96. return $val;
  97. }
  98. }
  99. //处理单式订单相关逻辑
  100. public function doSimple()
  101. {
  102. $simples = $this->getSimpleOrders($this->game_code, $this->match_id);
  103. if (empty($simples)) {
  104. return;
  105. }
  106. $useraccs = [];
  107. foreach ($simples as $item) {
  108. if (!isset($useraccs[$item->account_identity])) {
  109. $useraccs[$item->account_identity] = $item->account_identity;
  110. }
  111. }
  112. //关联用户
  113. $userInents = $this->getAccountIdents($useraccs);
  114. //用户资金变化记录,以后好一次性更新用户余额
  115. $time = date("Y-m-d H:i:s");
  116. $userMoneyChange = []; //用户资金变化记录,好一次性更新
  117. foreach ($simples as $item) {
  118. $gain_money = floatval($item->gain_money);
  119. $accountIdent = $item->account_identity;
  120. $mModel = new MoneyDetailsModel();
  121. $mModel->info_identity = UUID();
  122. $mModel->trade_id = $item->order_id;
  123. $mModel->account_name = $userInents[$accountIdent]->account;
  124. $mModel->account_identity = $accountIdent;
  125. $mModel->money_time = $time;
  126. $mModel->money_type = 3;
  127. $mModel->trade_type = 3;
  128. $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id;
  129. if ($gain_money <= 0.01) {
  130. $return_money = $item->money * (1 - $this->fanshui);
  131. $mModel->money_type = 1;
  132. $mModel->money = $return_money;
  133. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money;
  134. } else {
  135. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  136. $mModel->money_type = 2;
  137. $mModel->money = $return_money;
  138. $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money;
  139. }
  140. $mModel->save();
  141. $item->status = 3;
  142. $item->save();
  143. }
  144. if ($userMoneyChange) {
  145. foreach ($userMoneyChange as $uident => $money) {
  146. if (abs($money) > 0) {
  147. $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money;
  148. $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money;
  149. $userInents[$uident]->detail->save();
  150. }
  151. }
  152. }
  153. return true;
  154. }
  155. //处理串式订单相关逻辑
  156. public function doStr()
  157. {
  158. $strOrders = $this->getStrOrders($this->game_code, $this->match_id);
  159. if (empty($strOrders)) {
  160. return;
  161. }
  162. //这里只把结果改为平局,再提交到旧的重新结算订单接口处理
  163. foreach ($strOrders as $item2) {
  164. $item2->status = 2;
  165. $item2->save();
  166. $mats = $item2->matchdatas;
  167. foreach ($mats as $item3) {
  168. $item3->result = 2;
  169. $item3->matchresult = '赛事撤销!';
  170. $item3->save();
  171. }
  172. }
  173. $dataArr = [
  174. 'game_code' => $this->game_code,
  175. 'match_id' => $this->match_id,
  176. 'settype' => 2,
  177. 'bettype' => 2,
  178. 'change_status' => 0,
  179. 'order_ids' => '',
  180. ];
  181. $url = 'http://127.0.0.1:9094/Settelement';
  182. post_curls($url, $dataArr);
  183. /*
  184. foreach ($strOrders as $item) {
  185. $dataArr['order_ids'] = $item->order_id;
  186. post_curls($url, $dataArr);
  187. }
  188. */
  189. return;
  190. }
  191. //得到某个账户的信息,主要是余额情况
  192. public function getAccountIdents($account_identity_array)
  193. {
  194. $model = new Account();
  195. $ret = [];
  196. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  197. if ($return) {
  198. foreach ($return as $item) {
  199. $ret[$item->identity] = $item;
  200. }
  201. }
  202. return $ret;
  203. }
  204. public function changeStatus($game_code, $match_id)
  205. {
  206. $table = 'st_' . $game_code . '_result';
  207. $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]);
  208. return $ret;
  209. }
  210. //得到比赛结果 ,看是不已结算等信息
  211. public function getResult($game_code, $match_id)
  212. {
  213. $table = 'st_' . $game_code . '_result';
  214. $ret = DB::table($table)->where('match_id', $match_id)->first();
  215. return $ret;
  216. }
  217. //得到 有此赛事的单式订单
  218. public function getSimpleOrders($game_code, $match_id)
  219. {
  220. $model = new MoneyBuySimplexModel();
  221. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  222. return $rets;
  223. }
  224. //得到 有此赛事的串式订单
  225. public function getStrOrders($game_code, $match_id)
  226. {
  227. $model = new MoneyBuyStrModel();
  228. $model2 = new MoneyBuyMatchModel();
  229. $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]);
  230. $orders = [];
  231. if ($rets2) {
  232. foreach ($rets2 as $item) {
  233. $orders[] = $item->order_id;
  234. }
  235. } else {
  236. return [];
  237. }
  238. $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get();
  239. return $rets;
  240. }
  241. //获取相关的订单费用及系统赚送的钱(用于返还)
  242. public function getMoneyDetail($ordesArray)
  243. {
  244. $model = new MoneyDetailsModel();
  245. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  246. return $ret;
  247. }
  248. //设置操作标志位,防止事务未处理完,再次提交的情况
  249. //opt 0删除 1设置 2查询
  250. public function optFlag($game_code, $match_id, $opt = 2)
  251. {
  252. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  253. $redis = $this->getRedis();
  254. if ($opt == 0) {
  255. $redis->del($key);
  256. return true;
  257. }
  258. if ($opt == 1) {
  259. $redis->setex($key, 300, time());
  260. return;
  261. }
  262. if ($opt == 2) {
  263. $ret = $redis->get($key);
  264. return $ret;
  265. }
  266. }
  267. public function getRedis()
  268. {
  269. if ($this->redis) {
  270. return $this->redis;
  271. }
  272. $config = GlobConfigs::getKey('redis');
  273. $redis = new \Redis();
  274. $redis->connect($config['host'], $config['port']);
  275. $redis->auth($config['passwd']);
  276. $redis->select($config['db']);
  277. $this->redis = $redis;
  278. return $redis;
  279. }
  280. }