UnSettmatchLogic.php 11 KB

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