SettlementOrder.php 16 KB

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