UserAddressRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * User: Qinii
  4. * Date: 2020-06-03
  5. * Time: 09:41
  6. */
  7. namespace app\common\repositories\user;
  8. use app\common\repositories\BaseRepository;
  9. use app\common\dao\user\UserAddressDao as dao;
  10. use app\common\repositories\store\shipping\CityRepository;
  11. use app\entity\data\user\UserAddressEntity;
  12. /**
  13. * Class UserAddressRepository
  14. * @package app\common\repositories\user
  15. * @day 2020/6/3
  16. * @mixin dao
  17. */
  18. class UserAddressRepository extends BaseRepository
  19. {
  20. /**
  21. * @var dao
  22. */
  23. protected $dao;
  24. /**
  25. * UserAddressRepository constructor.
  26. * @param dao $dao
  27. */
  28. public function __construct(dao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * @param int $id
  34. * @param int $uid
  35. * @return bool
  36. * @author Qinii
  37. */
  38. public function fieldExists(int $id,int $uid)
  39. {
  40. return $this->dao->userFieldExists($this->dao->getPk(),$id,$uid);
  41. }
  42. /**
  43. * @param int $uid
  44. * @return bool
  45. * @author Qinii
  46. */
  47. public function defaultExists(int $uid)
  48. {
  49. return $this->dao->userFieldExists('is_default',1,$uid);
  50. }
  51. /**
  52. * @param int $id
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @author Qinii
  58. */
  59. public function checkDefault(int $id)
  60. {
  61. $res = $this->dao->getWhere([$this->dao->getPk() => $id]);
  62. return $res['is_default'];
  63. }
  64. /**
  65. * @param $province
  66. * @param $city
  67. * @return mixed
  68. * @author Qinii
  69. */
  70. public function getCityId($province,$city)
  71. {
  72. $make = app()->make(CityRepository::class);
  73. $provinceData = $make->getWhere(['name' => $province]);
  74. $cityData = $make->getWhere(['name' => $city,'parent_id' => $provinceData['city_id']]);
  75. if(!$cityData)$cityData = $make->getWhere([['name','like','直辖'.'%'],['parent_id' ,'=', $provinceData['city_id']]]);
  76. return $cityData['city_id'];
  77. }
  78. /**
  79. * @param $uid
  80. * @param $page
  81. * @param $limit
  82. * @return array
  83. * @author Qinii
  84. */
  85. public function getList($uid)
  86. {
  87. $list = $this->dao->getAll($uid)->order('is_default desc')->select();
  88. foreach ($list as &$address){
  89. $code_list = explode(',',$address['code_list']);
  90. if (isset($code_list[3])) {
  91. $address['town'] = \think\facade\Db::name('regions')->where('id', $code_list[3])->value('name');
  92. }
  93. }
  94. return compact('list');
  95. }
  96. /**
  97. * @param $id
  98. * @param $uid
  99. * @return array|\think\Model|null
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @author Qinii
  104. */
  105. public function get($id,$uid)
  106. {
  107. $address = $this->dao->getWhere(['address_id' => $id,'uid' => $uid]) ?? [] ;
  108. $code_list = explode(',',$address['code_list']);
  109. if (isset($code_list[3])) {
  110. $address['town'] = \think\facade\Db::name('regions')->where('id', $code_list[3])->value('name');
  111. }
  112. return $address;
  113. }
  114. /**
  115. * 根据 地址ID 获取地址实体
  116. *
  117. * @param $addressId
  118. * @return UserAddressEntity
  119. */
  120. public function getAddressEntityByAddressId($addressId): UserAddressEntity
  121. {
  122. /** @var UserAddressEntity $addressEntity */
  123. $addressEntity = $this->dao->getAddressEntityByAddressId($addressId);
  124. return $addressEntity;
  125. }
  126. }