UserBank.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/25
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\traits\ModelTrait;
  10. /**
  11. * TODO 用户收货银行卡
  12. * Class UserAddress
  13. * @package app\models\user
  14. */
  15. class UserBank extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'user_bank';
  27. use ModelTrait;
  28. protected $insert = ['add_time'];
  29. protected $hidden = ['add_time', 'uid'];
  30. protected function setAddTimeAttr()
  31. {
  32. return time();
  33. }
  34. /**
  35. * 设置默认收货银行卡
  36. * @return bool
  37. */
  38. public static function setDefaultAddress($id,$uid)
  39. {
  40. self::beginTrans();
  41. $res1 = self::where('uid',$uid)->update(['is_default'=>0]);
  42. $res2 = self::where('id',$id)->where('uid',$uid)->update(['is_default'=>1]);
  43. $res =$res1 !== false && $res2 !== false;
  44. self::checkTrans($res);
  45. return $res;
  46. }
  47. /**
  48. * 设置用户银行卡查询初始条件
  49. * @param null $model
  50. * @param string $prefix
  51. * @return \think\Model
  52. */
  53. public static function userValidAddressWhere($model=null,$prefix = '')
  54. {
  55. if($prefix) $prefix .='.';
  56. $model = self::getSelfModel($model);
  57. return $model->where("{$prefix}is_del",0);
  58. }
  59. /**
  60. * 获取用户收货银行卡并分页
  61. * @param $uid 用户uid
  62. * @param int $page 页码
  63. * @param int $limit 展示条数
  64. * @param string $field 展示字段
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public static function getUserValidAddressList($uid,$page=1,$limit=8,$field = '*')
  71. {
  72. if($page) return self::userValidAddressWhere()->where('uid',$uid)->order('add_time DESC')->field($field)->page((int)$page,(int)$limit)->select()->toArray()?:[];
  73. else return self::userValidAddressWhere()->where('uid',$uid)->order('add_time DESC')->field($field)->select()->toArray()?:[];
  74. }
  75. /**
  76. * 获取用户默认收货银行卡
  77. * @param $uid 用户uid
  78. * @param string $field 展示字段
  79. * @return array|\think\Model|null
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public static function getUserDefaultAddress($uid,$field = '*')
  85. {
  86. return self::userValidAddressWhere()->where('uid',$uid)->where('is_default',1)->field($field)->find();
  87. }
  88. }