SettlementOrder.php 16 KB

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