LuckyMoney.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Biz\Money\LotteryMoneyConfirm;
  10. use \System\Model;
  11. class LuckyMoney extends Model
  12. {
  13. protected $table = 'lucky_money';
  14. private $lm_oid;
  15. /**
  16. * 插入红包发起记录
  17. * @param string $account_identity
  18. * @param string $lm_oid
  19. * @param float $money
  20. * @param int $room_id
  21. * @param int $num
  22. * @param int $type
  23. * @param string $message
  24. * @return int
  25. */
  26. public function addLuckyMoneyRec(string $account_identity, string $lm_oid, float $money, int $room_id, int $num, $type = 0, string $message = '')
  27. {
  28. $data = [
  29. 'account_identity' => $account_identity,
  30. 'lm_order_id' => $lm_oid,
  31. 'money' => $money,
  32. 'room_id' => $room_id,
  33. 'num' => $num,
  34. 'type' => $type,
  35. 'message' => $message,
  36. 'created_at' => date('Y-m-d H:i:s')
  37. ];
  38. $ret = self::insertGetId($data);
  39. return $ret > 0 ? $lm_oid : -7030;
  40. }
  41. protected function updateLuckyMoneyRec(int $lm_id, float $take_money, int $take_num, float $ret_money = 0, string $ret_order_id = '')
  42. {
  43. $data = [
  44. 'take_money' => $take_money,
  45. 'take_num' => $take_num,
  46. ];
  47. if ($ret_money > 0) {
  48. $data['return_money'] = $ret_money;
  49. $data['return_order_id'] = $ret_order_id;
  50. }
  51. $data['updated_at'] = time();
  52. return $ret = self::where('id', $lm_id)->update($data);
  53. }
  54. /**
  55. * 添加红包记录
  56. * @param string $account_identity
  57. * @param float $money
  58. * @param int $num
  59. * @param int $room_id
  60. * @param int $type
  61. * @param string $message 红包祝福语
  62. * @param callable $cb_red_pack_file_init
  63. * @return mixed
  64. */
  65. public function handleLuckyMoney(string $account_identity, float $money, int $num, int $room_id, int $type = 0, $message = '', callable $cb_red_pack_file_init)
  66. {
  67. // $obj = C()->get('AutoLottery');
  68. $obj = new LotteryMoneyConfirm();
  69. $obj->account_identity = $account_identity;
  70. $obj->operation_time = date('Y-m-d H:i:s', time());
  71. $account = lm('Account', 'api')->where('identity', $account_identity)->first();
  72. if (!$account) {
  73. return -51017;//用户不存在
  74. }
  75. $accountDetail = lm('AccountDetail', 'Api')->where('account_identity', $account_identity)->first();
  76. if (!$accountDetail) {
  77. return -51017;//用户不存在
  78. }
  79. $obj->account_name = $account->account;//用户账号
  80. $obj->money = $money;//发起金额
  81. $obj->money_cash = $accountDetail->cash - $money;
  82. $obj->order_id = OrderID();//生成订单号
  83. $obj->money_type = 2;//扣款操作
  84. $obj->trade_type = 16;//红包打赏;
  85. $obj->rate = 1;//流水倍数;
  86. $obj->operator = 'system_auto';//审核人
  87. _beginTransaction();
  88. $updateAccountDetail = $obj->decrAccountDetailMoney($account_identity, $money);
  89. if ($updateAccountDetail != 1) {
  90. // dd(1111, $updateAccountDetail);
  91. _rollback();
  92. return $updateAccountDetail;
  93. //更新余额失败
  94. }
  95. $detail = "{$obj->account_name}于{$obj->operation_time}在{$room_id}号房间发起了红包,金额为{$money}元";//备注
  96. $opDetail = $obj->addMoneyDetailRec($detail,'发起红包',2);
  97. if ($opDetail == -50014) {
  98. // dd(2222, $opDetail);;
  99. _rollback();
  100. return $opDetail;
  101. }
  102. $this->lm_oid = $opDetail;
  103. $opLmRec = self::addLuckyMoneyRec($account_identity, $this->lm_oid, $money, $room_id, $num, $type, $message);
  104. if ($opLmRec < 0) {
  105. // dd(3333, $updateAccountDetail);
  106. _rollback();
  107. return $opDetail;
  108. }
  109. $file = $cb_red_pack_file_init($this->lm_oid, $money, $num, $type);
  110. if ($file !== 1) {
  111. _rollback();//红包文件创建失败
  112. return -7009;
  113. }
  114. //todo:用户操作日志添加
  115. _commit();
  116. //return $opLmRec;
  117. // $ret = self::where('lm_order_id', $opLmRec)->first();
  118. // return $ret->lm_order_id;
  119. return $opLmRec;
  120. }
  121. public function getHistory(string $account_identity, int $page, int $limit)
  122. {
  123. $offset = $page * $limit;
  124. return $ret = self::where('account_identity', $account_identity)
  125. ->orderBy('id', 'desc')
  126. // ->select('lm_order_id as lm_id','money','created_at','num')
  127. ->limit($offset, $limit)
  128. ->get();
  129. }
  130. public function getDetail(string $lm_id)
  131. {
  132. return $this ->leftJoin('account_detailed','lucky_money.account_identity','=','account_detailed.account_identity')
  133. ->where('lucky_money.lm_order_id', $lm_id)
  134. ->select('lucky_money.lm_order_id', 'lucky_money.num', 'lucky_money.take_num', 'lucky_money.money', 'lucky_money.take_money', 'lucky_money.message','account_detailed.img_url','account_detailed.img_id')
  135. ->first();
  136. }
  137. //检测红包是否存在 anton 2019-01-27
  138. public function checkRed($where)
  139. {
  140. return $this->where('created_at','>=',$where['start'])
  141. ->where('created_at','<=',$where['end'])
  142. ->where('type','=','3')
  143. ->get();
  144. }
  145. }