UserAddressRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\repositories\BaseRepository;
  4. use app\common\dao\user\UserAddressDao as dao;
  5. use app\common\repositories\store\shipping\CityRepository;
  6. /**
  7. * Class UserAddressRepository
  8. * @package app\common\repositories\user
  9. * @day 2020/6/3
  10. * @mixin dao
  11. */
  12. class UserAddressRepository extends BaseRepository
  13. {
  14. /**
  15. * @var dao
  16. */
  17. protected $dao;
  18. /**
  19. * UserAddressRepository constructor.
  20. * @param dao $dao
  21. */
  22. public function __construct(dao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * @param int $id
  28. * @param int $uid
  29. * @return bool
  30. * @author Qinii
  31. */
  32. public function fieldExists(int $id,int $uid)
  33. {
  34. return $this->dao->userFieldExists($this->dao->getPk(),$id,$uid);
  35. }
  36. /**
  37. * @param int $uid
  38. * @return bool
  39. * @author Qinii
  40. */
  41. public function defaultExists(int $uid)
  42. {
  43. return $this->dao->userFieldExists('is_default',1,$uid);
  44. }
  45. /**
  46. * @param int $id
  47. * @return mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @author Qinii
  52. */
  53. public function checkDefault(int $id)
  54. {
  55. $res = $this->dao->getWhere([$this->dao->getPk() => $id]);
  56. return $res['is_default'];
  57. }
  58. /**
  59. * @param $province
  60. * @param $city
  61. * @return mixed
  62. * @author Qinii
  63. */
  64. public function getCityId($province,$city)
  65. {
  66. $make = app()->make(CityRepository::class);
  67. $provinceData = $make->getWhere(['name' => $province]);
  68. $cityData = $make->getWhere(['name' => $city,'parent_id' => $provinceData['city_id']]);
  69. if(!$cityData)$cityData = $make->getWhere([['name','like','直辖'.'%'],['parent_id' ,'=', $provinceData['city_id']]]);
  70. return $cityData['city_id'];
  71. }
  72. /**
  73. * @param $uid
  74. * @param $page
  75. * @param $limit
  76. * @return array
  77. * @author Qinii
  78. */
  79. public function getList($uid)
  80. {
  81. $list = $this->dao->getAll($uid)->order('is_default desc')->select();
  82. return compact('list');
  83. }
  84. /**
  85. * @param $id
  86. * @param $uid
  87. * @return array|\think\Model|null
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @author Qinii
  92. */
  93. public function get($id,$uid)
  94. {
  95. return $this->dao->getWhere(['address_id' => $id,'uid' => $uid])->append(['area']);
  96. }
  97. }