AdminMoneyLog.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\admin\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * @method static Query|self filterTypeCommission()
  7. */
  8. class AdminMoneyLog extends Model
  9. {
  10. const T_COMMISSION=1;
  11. protected $autoWriteTimestamp=true;
  12. protected $updateTime=null;
  13. public static $money_types=[
  14. self::T_COMMISSION=>'销售提成',
  15. ];
  16. /**
  17. * @return string[]
  18. */
  19. public static function getMoneyTypes(): array
  20. {
  21. return self::$money_types;
  22. }
  23. #退款中的或已退款的
  24. public function scopeFilterTypeCommission(Query $query){
  25. $query->where($this->__('type'),self::T_COMMISSION);
  26. }
  27. public function admin(){
  28. return $this->belongsTo(Admin::class)->setEagerlyType(0);
  29. }
  30. public static function money($admin_id,$amount,$type,$from_user_id,$target_id=null,$remark=null){
  31. $admin=Admin::where('id',$admin_id)->lock(true)->find();
  32. if(!$admin){
  33. return false;
  34. }
  35. $before=$admin['money'];
  36. $admin['money']=bcadd($admin['money'],$amount);
  37. if($admin['money']<0){
  38. throw_user('余额不足');
  39. }
  40. $admin->moneyLog()->save([
  41. 'type'=>$type,
  42. 'amount'=>$amount,
  43. 'before'=>$before,
  44. 'after'=>$admin['money'],
  45. 'target_id'=>$target_id,
  46. 'remark'=>$remark,
  47. 'from_user_id'=>$from_user_id
  48. ]);
  49. }
  50. }