UnSettmatchLogic.php 10 KB

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