UnSettmatchLogic.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $userInents_money = [];
  112. foreach ($simples as $item) {
  113. $gain_money = $item->gain_money;
  114. if ($gain_money <= 0.01) {
  115. $return_money = $item->money * (1 - $this->fanshui);
  116. } else {
  117. $return_money = $gain_money - $item->money * (1 - $this->fanshui);
  118. }
  119. }
  120. }
  121. //处理串式订单相关逻辑
  122. public function doStr()
  123. {
  124. $simples = $this->getStrOrders($this->game_code, $this->match_id);
  125. if (empty($simples)) {
  126. return;
  127. }
  128. }
  129. //得到某个账户的信息,主要是余额情况
  130. public function getAccountIdents($account_identity_array)
  131. {
  132. $model = new Account();
  133. $ret = [];
  134. $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get();
  135. if ($return) {
  136. foreach ($return as $item) {
  137. $ret[$item->identity] = $item;
  138. }
  139. }
  140. return $ret;
  141. }
  142. public function changeStatus($game_code, $match_id)
  143. {
  144. $table = 'st_' . $game_code . '_result';
  145. $ret = DB::name($table)->where('match_id', $match_id)->update(['status' => 4]);
  146. return $ret;
  147. }
  148. //得到比赛结果 ,看是不已结算等信息
  149. public function getResult($game_code, $match_id)
  150. {
  151. $table = 'st_' . $game_code . '_result';
  152. $ret = DB::table($table)->where(['match_id', $match_id])->first();
  153. return $ret;
  154. }
  155. //得到 有此赛事的单式订单
  156. public function getSimpleOrders($game_code, $match_id)
  157. {
  158. $model = new MoneyBuySimplexModel();
  159. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  160. return $rets;
  161. }
  162. //得到 有此赛事的串式订单
  163. public function getStrOrders($game_code, $match_id)
  164. {
  165. $model = new MoneyBuyStrModel();
  166. $model2 = new MoneyBuyMatchModel();
  167. $rets2 = $model2->getByAttrs(['match_id' => $match_id, 'bet_type' => 2, 'game_code' => $game_code]);
  168. $orders = [];
  169. if ($rets2) {
  170. foreach ($rets2 as $item) {
  171. $orders[] = $item->order_id;
  172. }
  173. } else {
  174. return [];
  175. }
  176. $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $orders)->get();
  177. return $rets;
  178. }
  179. //获取相关的订单费用及系统赚送的钱(用于返还)
  180. public function getMoneyDetail($ordesArray)
  181. {
  182. $model = new MoneyDetailsModel();
  183. $ret = $model->whereIn('trace_id', $ordesArray)->get();
  184. return $ret;
  185. }
  186. //设置操作标志位,防止事务未处理完,再次提交的情况
  187. //opt 0删除 1设置 2查询
  188. public function optFlag($game_code, $match_id, $opt = 2)
  189. {
  190. $key = 'Unsetmatch:' . $game_code . '_' . $match_id;
  191. $redis = $this->getRedis();
  192. if ($opt == 0) {
  193. $redis->del($key);
  194. return true;
  195. }
  196. if ($opt == 1) {
  197. $redis->setex($key, 300, time());
  198. return;
  199. }
  200. if ($opt == 2) {
  201. $ret = $redis->get($key);
  202. return $ret;
  203. }
  204. }
  205. public function getRedis()
  206. {
  207. if ($this->redis) {
  208. return $this->redis;
  209. }
  210. $config = GlobConfigs::getKey('redis');
  211. $redis = new \Redis();
  212. $redis->connect($config['host'], $config['port']);
  213. $redis->auth($config['auth']);
  214. $redis->select($config['db']);
  215. $this->redis = $redis;
  216. return $redis;
  217. }
  218. }