UserAddress.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/12/25
  5. */
  6. namespace app\admin\model\user;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\traits\ModelTrait;
  9. class UserAddress extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'user_address';
  21. use ModelTrait;
  22. protected $insert = ['add_time'];
  23. protected function setAddTimeAttr()
  24. {
  25. return time();
  26. }
  27. public static function setDefaultAddress($id,$uid)
  28. {
  29. self::beginTrans();
  30. $res1 = self::where('uid',$uid)->update(['is_default'=>0]);
  31. $res2 = self::where('id',$id)->where('uid',$uid)->update(['is_default'=>1]);
  32. $res =$res1 !== false && $res2 !== false;
  33. self::checkTrans($res);
  34. return $res;
  35. }
  36. public static function userValidAddressWhere($model=null,$prefix = '')
  37. {
  38. if($prefix) $prefix .='.';
  39. $model = self::getSelfModel($model);
  40. return $model->where("{$prefix}is_del",0);
  41. }
  42. public static function getUserValidAddressList($uid,$field = '*')
  43. {
  44. return self::userValidAddressWhere()->where('uid',$uid)->order('add_time DESC')->field($field)->select()->toArray()?:[];
  45. }
  46. public static function getUserDefaultAddress($uid,$field = '*')
  47. {
  48. return self::userValidAddressWhere()->where('uid',$uid)->where('is_default',1)->field($field)->find();
  49. }
  50. }