UserAddressRepository.php 3.1 KB

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