UserAddress.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 UserAddress extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'user_address';
  27. use ModelTrait;
  28. protected $insert = ['add_time'];
  29. protected $hidden = ['add_time', 'is_del', 'uid'];
  30. protected function setAddTimeAttr()
  31. {
  32. return time();
  33. }
  34. public static function setDefaultAddress($id,$uid)
  35. {
  36. self::beginTrans();
  37. $res1 = self::where('uid',$uid)->update(['is_default'=>0]);
  38. $res2 = self::where('id',$id)->where('uid',$uid)->update(['is_default'=>1]);
  39. $res =$res1 !== false && $res2 !== false;
  40. self::checkTrans($res);
  41. return $res;
  42. }
  43. public static function userValidAddressWhere($model=null,$prefix = '')
  44. {
  45. if($prefix) $prefix .='.';
  46. $model = self::getSelfModel($model);
  47. return $model->where("{$prefix}is_del",0);
  48. }
  49. public static function getUserValidAddressList($uid,$page=1,$limit=8,$field = '*')
  50. {
  51. if($page) return self::userValidAddressWhere()->where('uid',$uid)->order('add_time DESC')->field($field)->page((int)$page,(int)$limit)->select()->toArray()?:[];
  52. else return self::userValidAddressWhere()->where('uid',$uid)->order('add_time DESC')->field($field)->select()->toArray()?:[];
  53. }
  54. public static function getUserDefaultAddress($uid,$field = '*')
  55. {
  56. return self::userValidAddressWhere()->where('uid',$uid)->where('is_default',1)->field($field)->find();
  57. }
  58. }