UnSettmatchLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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->money_time = $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. $item2->status = 2;
  160. $item2->save();
  161. $mats = $item2->matchdatas;
  162. foreach ($mats as $item3) {
  163. $item3->result = 2;
  164. $item3->matchresult = '赛事撤销!';
  165. $item3->save();
  166. }
  167. }
  168. $dataArr = [
  169. 'game_code' => $this->game_code,
  170. 'match_id' => $this->match_id,
  171. 'settype' => 2,
  172. 'bettype' => 2,
  173. 'change_status' => 0,
  174. 'order_ids' => '',
  175. ];
  176. $url = 'http://127.0.0.1:9094/Settelement';
  177. post_curls($url, $dataArr);
  178. /*
  179. foreach ($strOrders as $item) {
  180. $dataArr['order_ids'] = $item->order_id;
  181. post_curls($url, $dataArr);
  182. }
  183. */
  184. return;
  185. }
  186. //得到某个账户的信息,主要是余额情况
  187. public function getAccountIdents($account_identity_array)
  188. {
  189. $model = new Account();
  190. $ret = [];
  191. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  192. if ($return) {
  193. foreach ($return as $item) {
  194. $ret[$item->identity] = $item;
  195. }
  196. }
  197. return $ret;
  198. }
  199. public function changeStatus($game_code, $match_id)
  200. {
  201. $table = 'st_' . $game_code . '_result';
  202. $ret = DB::name($table)->where('match_id', $match_id)->update(['status' => 4]);
  203. return $ret;
  204. }
  205. //得到比赛结果 ,看是不已结算等信息
  206. public function getResult($game_code, $match_id)
  207. {
  208. $table = 'st_' . $game_code . '_result';
  209. $ret = DB::table($table)->where('match_id', $match_id)->first();
  210. return $ret;
  211. }
  212. //得到 有此赛事的单式订单
  213. public function getSimpleOrders($game_code, $match_id)
  214. {
  215. $model = new MoneyBuySimplexModel();
  216. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  217. return $rets;
  218. }
  219. //得到 有此赛事的串式订单
  220. public function getStrOrders($game_code, $match_id)
  221. {
  222. $model = new MoneyBuyStrModel();
  223. $model2 = new MoneyBuyMatchModel();
  224. $rets2 = $model2->getByAttrs(['match_id' => $match_id, 'bet_type' => 2, 'game_code' => $game_code]);
  225. $orders = [];
  226. if ($rets2) {
  227. foreach ($rets2 as $item) {
  228. $orders[] = $item->order_id;
  229. }
  230. } else {
  231. return [];
  232. }
  233. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $orders)->get();
  234. return $rets;
  235. }
  236. //获取相关的订单费用及系统赚送的钱(用于返还)
  237. public function getMoneyDetail($ordesArray)
  238. {
  239. $model = new MoneyDetailsModel();
  240. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  241. return $ret;
  242. }
  243. //设置操作标志位,防止事务未处理完,再次提交的情况
  244. //opt 0删除 1设置 2查询
  245. public function optFlag($game_code, $match_id, $opt = 2)
  246. {
  247. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  248. $redis = $this->getRedis();
  249. if ($opt == 0) {
  250. $redis->del($key);
  251. return true;
  252. }
  253. if ($opt == 1) {
  254. $redis->setex($key, 300, time());
  255. return;
  256. }
  257. if ($opt == 2) {
  258. $ret = $redis->get($key);
  259. return $ret;
  260. }
  261. }
  262. public function getRedis()
  263. {
  264. if ($this->redis) {
  265. return $this->redis;
  266. }
  267. $config = GlobConfigs::getKey('redis');
  268. $redis = new \Redis();
  269. $redis->connect($config['host'], $config['port']);
  270. $redis->auth($config['passwd']);
  271. $redis->select($config['db']);
  272. $this->redis = $redis;
  273. return $redis;
  274. }
  275. }