| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/12/1
- * Time: 10:03
- */
- namespace App\Api\Model;
- use Biz\Money\LotteryMoneyConfirm;
- use \System\Model;
- class LuckyMoney extends Model
- {
- protected $table = 'lucky_money';
- private $lm_oid;
- /**
- * 插入红包发起记录
- * @param string $account_identity
- * @param string $lm_oid
- * @param float $money
- * @param int $room_id
- * @param int $num
- * @param int $type
- * @param string $message
- * @return int
- */
- public function addLuckyMoneyRec(string $account_identity, string $lm_oid, float $money, int $room_id, int $num, $type = 0, string $message = '')
- {
- $data = [
- 'account_identity' => $account_identity,
- 'lm_order_id' => $lm_oid,
- 'money' => $money,
- 'room_id' => $room_id,
- 'num' => $num,
- 'type' => $type,
- 'message' => $message,
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $ret = self::insertGetId($data);
- return $ret > 0 ? $lm_oid : -7030;
- }
- protected function updateLuckyMoneyRec(int $lm_id, float $take_money, int $take_num, float $ret_money = 0, string $ret_order_id = '')
- {
- $data = [
- 'take_money' => $take_money,
- 'take_num' => $take_num,
- ];
- if ($ret_money > 0) {
- $data['return_money'] = $ret_money;
- $data['return_order_id'] = $ret_order_id;
- }
- $data['updated_at'] = time();
- return $ret = self::where('id', $lm_id)->update($data);
- }
- /**
- * 添加红包记录
- * @param string $account_identity
- * @param float $money
- * @param int $num
- * @param int $room_id
- * @param int $type
- * @param string $message 红包祝福语
- * @param callable $cb_red_pack_file_init
- * @return mixed
- */
- public function handleLuckyMoney(string $account_identity, float $money, int $num, int $room_id, int $type = 0, $message = '', callable $cb_red_pack_file_init)
- {
- // $obj = C()->get('AutoLottery');
- $obj = new LotteryMoneyConfirm();
- $obj->account_identity = $account_identity;
- $obj->operation_time = date('Y-m-d H:i:s', time());
- $account = lm('Account', 'api')->where('identity', $account_identity)->first();
- if (!$account) {
- return -51017;//用户不存在
- }
- $accountDetail = lm('AccountDetail', 'Api')->where('account_identity', $account_identity)->first();
- if (!$accountDetail) {
- return -51017;//用户不存在
- }
- $obj->account_name = $account->account;//用户账号
- $obj->money = $money;//发起金额
- $obj->money_cash = $accountDetail->cash - $money;
- $obj->order_id = OrderID();//生成订单号
- $obj->money_type = 2;//扣款操作
- $obj->trade_type = 16;//红包打赏;
- $obj->rate = 1;//流水倍数;
- $obj->operator = 'system_auto';//审核人
- _beginTransaction();
- $updateAccountDetail = $obj->decrAccountDetailMoney($account_identity, $money);
- if ($updateAccountDetail != 1) {
- // dd(1111, $updateAccountDetail);
- _rollback();
- return $updateAccountDetail;
- //更新余额失败
- }
- $detail = "{$obj->account_name}于{$obj->operation_time}在{$room_id}号房间发起了红包,金额为{$money}元";//备注
- $opDetail = $obj->addMoneyDetailRec($detail,'发起红包',2);
- if ($opDetail == -50014) {
- // dd(2222, $opDetail);;
- _rollback();
- return $opDetail;
- }
- $this->lm_oid = $opDetail;
- $opLmRec = self::addLuckyMoneyRec($account_identity, $this->lm_oid, $money, $room_id, $num, $type, $message);
- if ($opLmRec < 0) {
- // dd(3333, $updateAccountDetail);
- _rollback();
- return $opDetail;
- }
- $file = $cb_red_pack_file_init($this->lm_oid, $money, $num, $type);
- if ($file !== 1) {
- _rollback();//红包文件创建失败
- return -7009;
- }
- //todo:用户操作日志添加
- _commit();
- //return $opLmRec;
- // $ret = self::where('lm_order_id', $opLmRec)->first();
- // return $ret->lm_order_id;
- return $opLmRec;
- }
- public function getHistory(string $account_identity, int $page, int $limit)
- {
- $offset = $page * $limit;
- return $ret = self::where('account_identity', $account_identity)
- ->orderBy('id', 'desc')
- // ->select('lm_order_id as lm_id','money','created_at','num')
- ->limit($offset, $limit)
- ->get();
- }
- public function getDetail(string $lm_id)
- {
- return $this ->leftJoin('account_detailed','lucky_money.account_identity','=','account_detailed.account_identity')
- ->where('lucky_money.lm_order_id', $lm_id)
- ->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')
- ->first();
- }
- //检测红包是否存在 anton 2019-01-27
- public function checkRed($where)
- {
- return $this->where('created_at','>=',$where['start'])
- ->where('created_at','<=',$where['end'])
- ->where('type','=','3')
- ->get();
- }
- }
|