SettlementOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/25
  6. * Time: 14:10
  7. */
  8. namespace App\Lib\Settlement;
  9. set_time_limit(600);
  10. ini_set('memory_limit', '256M');
  11. use Illuminate\Support\Facades\DB;
  12. use App\Lib\Settlement\SettlementBase;
  13. use App\Models\SettlementMiddleDetail as SettlementMiddleDetailModel;
  14. /**
  15. * 按订单结算或重结算
  16. */
  17. class SettlementOrder extends SettlementBase
  18. {
  19. private $orderId = ''; //订单ID
  20. private $orderType = 1; //订单类型 1单式 2串式;
  21. private $BuyDatasMainModel = []; //订单豪华版 单式一条 串式可能多条
  22. private $BuyDatas = []; //订单豪华版 单式一条 串式可能多条
  23. private $match_id = 0;
  24. private $game_code = '';
  25. private $SN = 0; //结算批次号
  26. private $hissettlementDetail = [];
  27. private $set_type = 0;
  28. //返回数据
  29. public static function makeData($status = 1, $message = 'success', $data = '')
  30. {
  31. return [
  32. 'status' => $status,
  33. 'message' => $message,
  34. 'data' => $data,
  35. ];
  36. }
  37. ///$order_ids 某场比塞全部订单数组, $bettype=2 订单类型: 1单式 2串式(默认), $settype = 1 结算次数:1首次(默认) 2非首次或重结算
  38. public function reSettlement($order_ids, $bettype = 2, $settype = 1, $game_code = 0, $match_id = 0)
  39. {
  40. if (!in_array($bettype, [1, 2])) {
  41. return self::makeData(6, '订单类型参数错误!');
  42. }
  43. $this->orderType = $bettype;
  44. if (!in_array($settype, [1, 2])) {
  45. return self::makeData(6, '结算参数错误');
  46. }
  47. $this->set_type = $settype;
  48. if (empty($order_ids)) {
  49. $needs_matchs = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  50. if (count($needs_matchs) <= 0) {
  51. $this->cgStatus($game_code, $match_id);
  52. return self::makeData();
  53. } else {
  54. return self::makeData(5, '订单号不能为空');
  55. }
  56. }
  57. if ($bettype == 1) {
  58. $chekArr = $this->Match_check($order_ids, $bettype);
  59. if (empty($chekArr) || count($chekArr) != 1) {
  60. return self::makeData(10, '不同场比赛不能同时结算!');
  61. }
  62. }
  63. $this->match_id = $match_id;
  64. $this->game_code = $game_code;
  65. try {
  66. DB::beginTransaction();
  67. $this->UndoSettlement($this->game_code, $this->match_id, $order_ids, $bettype);
  68. foreach ($order_ids as $order_id) {
  69. $this->BuyDatasMainModel = $this->orderTypeGet($order_id, $bettype);
  70. if ($this->BuyDatasMainModel->settle_status == 2) {
  71. continue;
  72. }
  73. if ($bettype == 1) {
  74. $this->SingOrder($order_id);
  75. } else {
  76. $this->ChuanOrder($order_id);
  77. }
  78. }
  79. $this->cgStatus($game_code, $match_id);
  80. DB::commit();
  81. } catch (\Exception $e) {
  82. DB::rollBack();
  83. return self::makeData(-2, $e->getMessage());
  84. }
  85. return self::makeData();
  86. }
  87. public function orderTypeGet($order_id, $bettype)
  88. {
  89. if ($bettype == 1) {
  90. $datas = DB::table('money_buy_simplex')->where('order_id', $order_id)->first();
  91. $this->orderType = 1;
  92. } else {
  93. $datas = DB::table('money_buy_str')->where('order_id', $order_id)->first();
  94. $this->orderType = 2;
  95. }
  96. if (!$datas) {
  97. throw new \Exception('没有主订单信息');
  98. }
  99. return $datas;
  100. }
  101. /**
  102. * 单式注单结算
  103. * @param mixed $order_id 注单ID
  104. */
  105. public function singOrder($order_id)
  106. {
  107. // 查询订单下所有的单式注单
  108. $simplexData = $this->BuyDatasMainModel;
  109. $matchData = DB:: table('money_buy_match')->where(['bet_type' => 1, 'batch_id' => $simplexData->batch_id, 'match_id' => $simplexData->match_id])->get();
  110. if (count($matchData) <= 0) {
  111. throw new \Exception("没有数据的错误!");
  112. }
  113. foreach ($matchData as $val) {
  114. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  115. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id);
  116. }
  117. }
  118. // 计算总回款
  119. $settlementBase = new \App\Lib\Settlement\SettlementBase;
  120. $returnMoney = 0;
  121. foreach ($matchData as $k => $v) {
  122. if ($v->result == -1) {
  123. continue;
  124. }
  125. $oddsResult[0]['winOrLose'] = $v->result;
  126. $oddsResult[0]['odds'] = $v->odds;
  127. $getReturnMoney = $settlementBase->stringOdds($oddsResult);
  128. $returnMoney += $getReturnMoney['returnMoney'] * $v->bet_money;
  129. }
  130. $returnMoney = sprintf("%.2f", substr(sprintf("%.3f", $returnMoney), 0, -1));
  131. // 判断盈亏 1 赢 2 输 3 平
  132. $game_status = $returnMoney > $simplexData->money ? 1 : ($returnMoney == $simplexData->money ? 3 : 2);
  133. // 修改投注表状态及盈亏
  134. DB:: table('money_buy_simplex')
  135. ->where(['order_id' => $order_id])
  136. ->update(['settle_status' => 2, 'game_status' => $game_status, 'gain_money' => $returnMoney, 'settlementTime' => date('Y-m-d H:i:s')]);
  137. $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $returnMoney);
  138. $this->insertData(
  139. $order_id,
  140. $returnMoney,
  141. $simplexData->account_identity,
  142. 1,
  143. $simplexData->game_code,
  144. $simplexData->info_identity,
  145. $simplexData->money,
  146. $this->match_id
  147. );
  148. }
  149. /**
  150. * 结算数据填入
  151. * @param mixed $order_id 注单ID
  152. * @param mixed $returnMoney 返现金额
  153. * @param mixed $account_identity 用户ID
  154. * @param mixed $type 1单式 2串式
  155. * @param mixed $game_name 游戏名(zq,lq)
  156. * @param mixed $buy_identity 游戏投注id
  157. * @param mixed $money 投注金额
  158. * @param mixed $match_id 赛场ID
  159. */
  160. public function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0)
  161. {
  162. // 查询用户当前剩余金额
  163. $accountInfo = DB:: table('account_detailed')
  164. ->join('account', 'account_detailed.account_identity', 'account.identity')
  165. ->select(['available_cash', 'cash', 'account', 'account_identity'])
  166. ->where(['account_identity' => $account_identity])
  167. ->first();
  168. // 计算用户回账后余额
  169. $available_cash = $accountInfo->available_cash + $returnMoney;
  170. $cash = $accountInfo->cash + $returnMoney;
  171. // 添加流水记录
  172. $info_identity = UUID();
  173. $money_time = date('Y-m-d H:i:s', time());
  174. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  175. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  176. DB:: table('money_details')->insert([
  177. 'info_identity' => $info_identity,
  178. 'trade_id' => $order_id,
  179. 'account_name' => $accountInfo->account,
  180. 'account_identity' => $accountInfo->account_identity,
  181. 'money' => $returnMoney,
  182. 'money_time' => $money_time,
  183. 'money_type' => 1,
  184. 'money_cash' => $available_cash,
  185. 'trade_type' => 4,
  186. 'trade_desc' => $trade_desc,
  187. 'reason' => $reason,
  188. 'sysetem_user' => '系统',
  189. 'status' => '1',
  190. ]);
  191. // 修改用余额
  192. DB:: table('account_detailed')
  193. ->where(['account_identity' => $account_identity])
  194. ->update(['available_cash' => $available_cash, 'cash' => $cash]);
  195. // 新增用户中奖信息
  196. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  197. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  198. DB:: table('account_news')->insert([
  199. 'identity' => $info_identity,
  200. 'account_identity' => $account_identity,
  201. 'title' => '投注订单回款通知',
  202. 'content' => $content,
  203. 'details' => $content,
  204. 'write_time' => $money_time,
  205. 'read_status' => -1,
  206. 'type' => 1,
  207. ]);
  208. // 新增中奖记录表
  209. $old = DB:: table('money_prize')->where(['buy_identity' => $buy_identity])->orWhere(['order_id' => $order_id])->first();
  210. if (count($old)) {
  211. DB:: table('money_prize')->where(['buy_identity' => $buy_identity])->orWhere(['order_id' => $order_id])->update(['money' => $money, 'prize_money' => $returnMoney, 'get_money' => $returnMoney - $money]);
  212. } else {
  213. DB:: table('money_prize')->insert([
  214. 'info_identity' => $info_identity,
  215. 'order_id' => $order_id,
  216. 'account_identity' => $account_identity,
  217. 'account_name' => $accountInfo->account,
  218. 'game_name' => $game_name,
  219. 'buy_identity' => $buy_identity,
  220. 'money' => $money,
  221. 'money_time' => $money_time,
  222. 'status' => 1,
  223. 'prize_money' => $returnMoney,
  224. 'get_money' => $returnMoney - $money,
  225. ]);
  226. }
  227. }
  228. //单个串式订单的处理
  229. public function ChuanOrder($order_id)
  230. {
  231. $batch_id = $this->BuyDatasMainModel->batch_id;
  232. $matchModels = DB::table('money_buy_match')->where(['batch_id' => $batch_id, 'bet_type' => 2])->get();
  233. if ($matchModels->count() <= 0) {
  234. throw new \Exception('match 数据异常');
  235. }
  236. $newTime = date('Y-m-d H:i:s');
  237. if ($this->BuyDatasMainModel->status == 1) {
  238. $in_array = [];
  239. foreach ($matchModels as $val) {
  240. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  241. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id);
  242. }
  243. if ($val->result == -1) {
  244. DB::table('money_buy_str')->where('batch_id', $batch_id)->update(['wait_match_num' => 0, 'prize_note' => 0, 'game_status' => 3, 'settle_status' => 2, 'gain_money' => 0, 'settlementTime' => date('Y-m-d H:i:s')]);
  245. return true;
  246. }
  247. $in_array[] = ['odds' => $val->odds, 'winOrLose' => $val->result];
  248. }
  249. $chuanNum = intval(substr($this->BuyDatasMainModel->str_type, 0, 1));
  250. $lasPeilv = $this->stringComputing([$in_array, $chuanNum]);
  251. $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv);
  252. } else {
  253. $money = $this->BuyDatasMainModel->money;
  254. }
  255. $ret = DB::update('update money_buy_str set settle_status=2 , game_status=1 , "settlementTime"=? , gain_money=? where order_id = ?', [$newTime, $money, $order_id]);
  256. if (!($ret || $ret === 0)) {
  257. throw new \Exception('更新数据出错3!');
  258. }
  259. $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $money);
  260. $this->insertData($order_id, $money, $this->BuyDatasMainModel->account_identity, 2, $val->game_code, $this->BuyDatasMainModel->info_identity, $this->BuyDatasMainModel->money, $this->match_id);
  261. return true;
  262. }
  263. //校验是否同一场比赛的订单处理
  264. private function Match_check($idsArray, $betType)
  265. {
  266. array_walk($idsArray, function (&$item, $key) {
  267. $item = "'" . $item . "'";
  268. });
  269. $idString = implode(",", $idsArray);
  270. $table = "money_buy_simplex";
  271. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  272. $ret = DB::select($sql);
  273. return $ret;
  274. }
  275. //取消某个赛事订单的结算数据
  276. public function UndoSettlement($game_code, $match_id, $order_ids, $bettype)
  277. {
  278. if ($this->set_type == 1) {
  279. return;
  280. }
  281. $modelSMD = new SettlementMiddleDetailModel();
  282. $hisData = $modelSMD->getSettledatas($game_code, $match_id, $bettype);
  283. if (count($hisData) <= 0) {
  284. return true;
  285. }
  286. $this->hissettlementDetail = $hisData;
  287. $orderList = [];
  288. if ($bettype == 1) {
  289. $order_list = DB::table('money_buy_simplex')->where(['game_code' => $game_code, 'match_id' => $match_id, 'status' => 1])->whereIn('order_id', $order_ids)->get();
  290. foreach ($order_list as $val) {
  291. $orderList[$val->order_id] = $val;
  292. }
  293. } else {
  294. $order_list = DB::table('money_buy_str')->where(['status' => 1])->whereIn('order_id', $order_ids)->get();
  295. foreach ($order_list as $val) {
  296. $orderList[$val->order_id] = $val;
  297. }
  298. }
  299. foreach ($hisData as $oid => $val) {
  300. if (!isset($orderList[$val->order_id])) {
  301. throw new \Exception("订单数据缺失异常!($game_code,$match_id,$oid)");
  302. }
  303. $money = $val->money * (-1);
  304. if (intval($money) * 100 == 0) {
  305. continue;
  306. }
  307. $this->insertData($oid, $money, $val->account_identity, $bettype, $game_code, $orderList[$oid]->info_identity, $money, $this->match_id);
  308. }
  309. $table = $bettype == 1 ? 'money_buy_simplex' : 'money_buy_str';
  310. if ($bettype == 1) {
  311. $ret1 = DB::table($table)->where(['game_code' => $game_code, 'match_id' => $match_id, 'status' => 1])->whereIn('order_id', $order_ids)->update(['settle_status' => 1, 'gain_money' => 0, 'game_status' => 0]);
  312. } else {
  313. $ret1 = DB::table($table)->where(['status' => 1])->whereIn('order_id', $order_ids)->update(['settle_status' => 1, 'gain_money' => 0]);
  314. }
  315. $ret2 = DB::table('settlement_middle_detail')->where(['game_code' => $game_code, 'match_id' => $match_id, 'bet_type' => $bettype])->whereIn('order_id', $order_ids)->update(['money' => 0]);
  316. if ($ret1 && $ret2) {
  317. return true;
  318. } else {
  319. throw new \Exception("取消结算更新数据状态出错操作中止($ret1 $ret2)!");
  320. }
  321. }
  322. private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
  323. {
  324. if (isset($this->hissettlementDetail[$order_id])) {
  325. $ret = DB::table('settlement_middle_detail')
  326. ->where(['game_code' => $game_code, 'match_id' => $match_id, 'bet_type' => $bet_type, 'order_id' => $order_id])
  327. ->update(['money' => $money]);
  328. } else {
  329. $ret = DB::table('settlement_middle_detail')
  330. ->insertGetId([
  331. 'game_code' => $game_code,
  332. 'match_id' => $match_id,
  333. 'account_identity' => $account_ident,
  334. 'bet_type' => $bet_type,
  335. 'order_id' => $order_id,
  336. 'money' => $money
  337. ]);
  338. }
  339. if (!$ret) {
  340. throw new \Exception("更新数据异常(bt:$bet_type od:$order_id ret:$ret)");
  341. }
  342. return true;
  343. }
  344. private function cgStatus($game_code, $match_id)
  345. {
  346. DB::table("st_" . $game_code . "_result")->where(['match_id' => $match_id])->update(['status' => 3]);
  347. DB::table("st_" . $game_code . "_competition")->where(['match_id' => $match_id])->update(['status' => 3]);
  348. }
  349. }