UserAddressRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\dao\user\UserAddressDao as dao;
  14. use app\common\repositories\store\shipping\CityRepository;
  15. /**
  16. * Class UserAddressRepository
  17. * @package app\common\repositories\user
  18. * @day 2020/6/3
  19. * @mixin dao
  20. */
  21. class UserAddressRepository extends BaseRepository
  22. {
  23. /**
  24. * @var dao
  25. */
  26. protected $dao;
  27. /**
  28. * UserAddressRepository constructor.
  29. * @param dao $dao
  30. */
  31. public function __construct(dao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param int $id
  37. * @param int $uid
  38. * @return bool
  39. * @author Qinii
  40. */
  41. public function fieldExists(int $id,int $uid)
  42. {
  43. return $this->dao->userFieldExists($this->dao->getPk(),$id,$uid);
  44. }
  45. /**
  46. * @param int $uid
  47. * @return bool
  48. * @author Qinii
  49. */
  50. public function defaultExists(int $uid)
  51. {
  52. return $this->dao->userFieldExists('is_default',1,$uid);
  53. }
  54. /**
  55. * @param int $id
  56. * @return mixed
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @author Qinii
  61. */
  62. public function checkDefault(int $id)
  63. {
  64. $res = $this->dao->getWhere([$this->dao->getPk() => $id]);
  65. return $res['is_default'];
  66. }
  67. /**
  68. * @param $province
  69. * @param $city
  70. * @return mixed
  71. * @author Qinii
  72. */
  73. public function getCityId($province,$city)
  74. {
  75. $make = app()->make(CityRepository::class);
  76. $provinceData = $make->getWhere(['name' => $province]);
  77. $cityData = $make->getWhere(['name' => $city,'parent_id' => $provinceData['city_id']]);
  78. if(!$cityData)$cityData = $make->getWhere([['name','like','直辖'.'%'],['parent_id' ,'=', $provinceData['city_id']]]);
  79. return $cityData['city_id'];
  80. }
  81. /**
  82. * @param $uid
  83. * @param $page
  84. * @param $limit
  85. * @return array
  86. * @author Qinii
  87. */
  88. public function getList($uid)
  89. {
  90. $list = $this->dao->getAll($uid)->order('is_default desc')->select();
  91. return compact('list');
  92. }
  93. /**
  94. * @param $id
  95. * @param $uid
  96. * @return array|\think\Model|null
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @author Qinii
  101. */
  102. public function get($id,$uid)
  103. {
  104. return $this->dao->getWhere(['address_id' => $id,'uid' => $uid]) ?? [] ;
  105. }
  106. }