LuckyMoneyTake.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/12/1
  6. * Time: 10:03
  7. */
  8. namespace App\Api\Model;
  9. use \System\Model;
  10. class LuckyMoneyTake extends Model {
  11. protected $table = 'lucky_money_take';
  12. /**
  13. * 插入领取记录到表
  14. * @param string $account_identity
  15. * @param int $room_id
  16. * @param string $lm_id
  17. * @param string $lm_oid
  18. * @param float $money
  19. * @param int $type
  20. * @return mixed
  21. */
  22. public function addLuckyMoneyTake(string $account_identity, int $room_id, string $lm_id, string $lm_oid, float $money, int $type = 0) {
  23. $data = [
  24. 'account_identity' => $account_identity,
  25. 'money' => $money,
  26. 'room_id' => $room_id,
  27. 'lm_id' => $lm_id,
  28. 'md_order_id' => $lm_oid,
  29. 'time' => time(),
  30. 'hash' => md5($room_id . $lm_id . $account_identity),
  31. 'type' => $type,
  32. ];
  33. $ret = self::insertGetId($data);
  34. return $ret > 0 ? $lm_oid : -7029;
  35. }
  36. /**
  37. * 抢红包函数
  38. * @param string $account_identity
  39. * @param int $room_id
  40. * @param string $lm_id
  41. * @param float $money
  42. * @return string
  43. */
  44. public function handleLuckyMoneyTake(string $account_identity, int $room_id, string $lm_id, float $money): string {
  45. $hash = md5($room_id . $lm_id . $account_identity);
  46. $data = self::where('hash', $hash)->first(['md_order_id']);
  47. if ($data) return $data->md_order_id;
  48. $obj = C()->get('AutoLottery');
  49. $obj->account_identity = $account_identity;
  50. $obj->operation_time = date('Y-m-d H:i:s', time());
  51. $account = lm('Account', 'api')->where('identity', $account_identity)->first();
  52. if (!$account) {
  53. return -51017;//用户不存在
  54. }
  55. $accountDetail = lm('AccountDetail', 'Api')->where('account_identity', $account_identity)->first();
  56. if (!$accountDetail) {
  57. return -51017;//用户不存在
  58. }
  59. $obj->account_name = $account->account;//用户账号
  60. $obj->money = number_format($money, 2,'.','');//充值金额
  61. $obj->money_cash = $accountDetail->cash + $money;
  62. $obj->order_id = OrderID();//生成订单号
  63. $obj->trade_type = 16;//红包打赏;
  64. $obj->rate = 0;//流水倍数;
  65. $obj->money_type = 1;
  66. $obj->operator = 'system_auto';//审核人
  67. _beginTransaction();
  68. $updateAccountDetail = $obj->incrAccountDetailMoney($account_identity, $money);
  69. if ($updateAccountDetail != 1) {
  70. _rollback();
  71. // dd(1111, $updateAccountDetail);
  72. return $updateAccountDetail;
  73. //更新余额失败
  74. }
  75. $detail = "{$obj->account_name}于{$obj->operation_time}在{$room_id}号房间领取了红包,金额为{$money}元,红包编号为【{$lm_id}】";//备注
  76. $opDetail = $obj->addMoneyDetailRec($detail, '领取红包', 2);
  77. if ($opDetail == -50014) {
  78. _rollback();
  79. // dd(222, $opDetail, $obj);
  80. return $opDetail;
  81. }
  82. $mr = $obj->addMoneyRechargeRec('领取红包', '领取红包', '红包打赏');
  83. if (intval($mr) < 0) {
  84. _rollback();//资金记录创建失败
  85. return intval($mr);
  86. }
  87. $opLmRec = self::addLuckyMoneyTake($account_identity, $room_id, $lm_id, $opDetail, $money);
  88. if ($opLmRec < 0) {
  89. _rollback();
  90. return $opDetail;
  91. }
  92. $lm = new LuckyMoney();
  93. $lmObj = $lm->where('lm_order_id', $lm_id)->first();
  94. if ($lmObj) {
  95. $num = $lmObj->num; //红包总个数
  96. $take_num = $lmObj->take_num; //红包已被领取个数
  97. if ($num > $take_num) {
  98. $lmObj->take_num++;
  99. $lmObj->take_money += $money;
  100. $lmObj->save();
  101. } else {
  102. _rollback();
  103. return -7012; //红包已领完
  104. }
  105. } else {
  106. _rollback();
  107. return -70161; //红包ID不存在
  108. }
  109. //todo:用户操作日志添加
  110. _commit();
  111. return $opLmRec;
  112. // return self::where('id', $opLmRec)->first()->md_order_id;
  113. }
  114. /**
  115. * 用户抢红包历史记录
  116. * @param string $account_identity
  117. * @param int $page
  118. * @param int $limit
  119. * @return mixed
  120. */
  121. public function getHistory(string $account_identity, int $page, int $limit) {
  122. $offset = $page * $limit;
  123. return $ret = self::where('account_identity', $account_identity)
  124. ->orderBy('time', 'desc')
  125. ->limit($offset, $limit)
  126. ->select($this->table . '.money', $this->table . '.time')
  127. ->get();
  128. }
  129. /**
  130. * 根据红包ID查询红包领取记录
  131. * @param $lm_id
  132. * @param $page
  133. * @param $limit
  134. * @return mixed
  135. */
  136. public function getHisByLuckyMoneyId($lm_id, $page, $limit) {
  137. $ret = $this
  138. ->leftJoin('account', "{$this->table}.account_identity", '=', 'account.identity')
  139. ->where($this->table . '.lm_id', $lm_id)
  140. ->leftJoin('account_detailed', $this->table . '.account_identity', '=', 'account_detailed.account_identity')
  141. ->orderBy($this->table . '.time', 'DESC')
  142. ->limit($page * $limit, $limit)
  143. ->select($this->table . '.money', $this->table . '.time', $this->table . '.room_id', 'account.account',
  144. 'account_detailed.img_url', 'account_detailed.img_id')
  145. ->get()
  146. ->toArray();
  147. $myInfo = [];
  148. if ($ret) {
  149. $room_id = $ret[0]['room_id'];
  150. $nickNames = (new Chat_rooms())->getNickNames($room_id);
  151. $room_mangers_id = $nickNames['room_mangers_id'];
  152. unset($nickNames['room_mangers_id']);
  153. foreach ($ret as $k => $v) {
  154. if ($v['account'] === $_SESSION['uinfo']['account']) {
  155. $myInfo['account'] = $v['account'];
  156. $myInfo['money'] = $v['money'];
  157. }
  158. if (isset($nickNames[$v['account']])) {
  159. $v['account'] = $nickNames[$v['account']];
  160. } else {
  161. if(!in_array($_SESSION['uinfo']['account'],explode(',',$room_mangers_id))){
  162. $strHeader = substr($v['account'], 0, -4);
  163. $strEnd = substr($v['account'], -1);
  164. $v['account'] = $strHeader . '***' . $strEnd;
  165. }
  166. }
  167. $ret[$k] = $v;
  168. }
  169. }
  170. return ['list' => $ret, 'myInfo' => (Object)$myInfo];
  171. }
  172. }