| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\common\model;
- use app\api\model\taxi\MotorRecord;
- use app\common\model\UsersBalanceRecord;
- class Users extends BaseModel
- {
- /**
- * @var string
- */
- protected $name = 'users';
- /**
- * @var array
- */
- protected $auto = [];
- /**
- * @var array
- */
- protected $insert = ['created_at','updated_at'];
- /**
- * @var array
- */
- protected $update = ['updated_at'];
- /**
- * @var array
- */
- protected $hidden = ['updated_at'];
- /**
- * 自关联推荐用户
- * @return \think\model\relation\BelongsTo
- */
- public function recommend()
- {
- return $this->belongsTo(self::class,'parent_id','id');
- }
- /**
- * 用户资金变动
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/14 10:34
- *
- * @param int $userId 用户ID
- * @param float $amount 变动金额
- * @param string $remark 备注说明
- * @param bool $inc 是否增长
- * @throws \think\exception\PDOException
- */
- public function changeBalance($userId,$amount, $remark = "", $inc = false)
- {
- $action = $inc ? 'setInc' : 'setDec';
- $this->startTrans();
- try {
- // 查余额
- $user = $this->lock(true)->where(['id' => $userId])->find();
- // 资金变动
- $this->where(['id' => $userId])->{$action}('balance',$amount);
- // 记录写入
- $data = [
- 'user_id' => $userId,
- 'action' => $inc ? 1 : 0,
- 'remark' => $remark
- ];
- // 加减
- if ($inc){
- $data['inc_amount'] = $amount;
- $data['aft_amount'] = sprintf("%.2f", round($user['balance'] + $amount, 2));
- }else{
- $data['dec_amount'] = $amount;
- $data['aft_amount'] = sprintf("%.2f", round($user['balance'] - $amount, 2));
- }
- UsersBalanceRecord::create($data,true);
- $this->commit();
- }catch (\Exception $e){
- $this->rollback();
- }
- }
- /**
- * 用户资产变动
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/14 10:34
- *
- * @param int $userId 用户ID
- * @param float $amount 变动金额
- * @param string $remark 备注说明
- * @param bool $inc 是否增长
- * @throws \think\exception\PDOException
- */
- public function changeProperty($userId,$amount, $remark = "", $inc = false)
- {
- $action = $inc ? 'setInc' : 'setDec';
- $this->startTrans();
- try {
- // 查余额
- $user = $this->lock(true)->where(['id' => $userId])->find();
- // 资金变动
- $this->where(['id' => $userId])->{$action}('property',$amount);
- // 记录写入
- $data = [
- 'user_id' => $userId,
- 'action' => $inc ? 1 : 0,
- 'remark' => $remark
- ];
- // 加减
- if ($inc){
- $data['inc_amount'] = $amount;
- $data['aft_amount'] = sprintf("%.3f", round($user['property'] + $amount, 3));
- }else{
- $data['dec_amount'] = $amount;
- $data['aft_amount'] = sprintf("%.3f", round($user['property'] - $amount, 3));
- }
- UsersPropertyRecord::create($data,true);
- $this->commit();
- }catch (\Exception $e){
- $this->rollback();
- }
- }
- /**
- * 用户资金变动
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/14 10:34
- *
- * @param int $userId 用户ID
- * @param float $amount 变动金额
- * @param string $remark 备注说明
- * @param string $type 类型:10-分红,20-提现,30-资产司机收入
- * @param bool $inc 是否增长
- * @throws \think\exception\PDOException
- */
- public function changePartnership($userId,$amount, $remark = "", $type=30, $inc = false)
- {
- $action = $inc ? 'setInc' : 'setDec';
- $this->startTrans();
- try {
- // 查余额
- $user = $this->lock(true)->where(['id' => $userId])->find();
- // 资金变动
- $this->where(['id' => $userId])->{$action}('partnership',$amount);
- // 记录写入
- $data = [
- 'type' => $type,
- 'money' => $amount,
- 'user_id' => $userId,
- 'tag' => $inc ? 10 : 20,
- 'created_at' => time(),
- 'updated_at' => time(),
- 'memo' => $remark
- ];
- // 加减
- if ($inc){
- $data['before'] = $user['partnership'];
- $data['after'] = sprintf("%.2f", round($user['partnership'] + $amount, 2));
- }else{
- $data['before'] = $user['partnership'];
- $data['after'] = sprintf("%.2f", round($user['partnership'] - $amount, 2));
- }
- MotorRecord::create($data,true);
- $this->commit();
- return true;
- }catch (\Exception $e){
- $this->rollback();
- return false;
- }
- }
- }
|