MobileUserHistory.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * 配置模型
  7. * @method Query admin($adminId)
  8. */
  9. class MobileUserHistory extends Model
  10. {
  11. protected $type=[
  12. 'info'=>'json',
  13. ];
  14. protected $hidden=[
  15. 'info',
  16. ];
  17. protected $autoWriteTimestamp=true;
  18. /**
  19. *
  20. */
  21. public static function add($user,Mobile $mobile){
  22. if($user && $mobile['type']==$mobile::BEAUTI){
  23. $user->mobileHistory()->where('mobile_id',$mobile['id'])->delete();
  24. $user->mobileHistory()->save([
  25. 'mobile_id'=>$mobile['id'],
  26. 'no'=>$mobile['no'],
  27. 'amount'=>$mobile['amount'],
  28. 'network'=>$mobile['network'],
  29. 'province'=>$mobile['province'],
  30. 'city'=>$mobile['city'],
  31. 'info'=>$mobile->toArray(),
  32. ]);
  33. #删除50条之外的
  34. $ids=$user->mobileHistory()
  35. ->order('mobile_user_history_id','desc')
  36. ->limit(50)
  37. ->column('mobile_user_history_id');
  38. if(count($ids)==50){
  39. $id=end($ids);
  40. $user->mobileHistory()->where('mobile_user_history_id','<',$id)->delete();
  41. }
  42. }
  43. }
  44. }