SettlementOrder.php 16 KB

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