MerchantAdminDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-16
  7. *
  8. *
  9. */
  10. namespace app\common\dao\system\merchant;
  11. use app\common\dao\BaseDao;
  12. use app\common\model\system\merchant\MerchantAdmin;
  13. use think\db\BaseQuery;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\Model;
  18. /**
  19. * Class MerchantAdminDao
  20. * @package app\common\dao\system\merchant
  21. * @author xaboy
  22. * @day 2020-04-17
  23. */
  24. class MerchantAdminDao extends BaseDao
  25. {
  26. /**
  27. * @return string
  28. * @author xaboy
  29. * @day 2020-04-16
  30. */
  31. protected function getModel(): string
  32. {
  33. return MerchantAdmin::class;
  34. }
  35. /**
  36. * @param int $merId
  37. * @param array $where
  38. * @param int|null $level
  39. * @return BaseQuery
  40. * @author xaboy
  41. * @day 2020-04-18
  42. */
  43. public function search(int $merId, array $where = [], ?int $level = null)
  44. {
  45. $query = MerchantAdmin::getDB()->where('is_del', 0)->where('mer_id', $merId)
  46. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  47. getModelTime($query, $where['date']);
  48. });
  49. if (!is_null($level)) $query->where('level', $level);
  50. if (isset($where['keyword']) && $where['keyword'] !== '') {
  51. $query = $query->whereLike('real_name|account', '%' . $where['keyword'] . '%');
  52. }
  53. if (isset($where['status']) && $where['status'] !== '') {
  54. $query = $query->where('status', intval($where['status']));
  55. }
  56. return $query;
  57. }
  58. /**
  59. * @param int $merId
  60. * @return string
  61. * @author xaboy
  62. * @day 2020-04-16
  63. */
  64. public function merIdByAccount(int $merId): string
  65. {
  66. return MerchantAdmin::getDB()->where('mer_id', $merId)->where('level', 0)->value('account');
  67. }
  68. /**
  69. * @param int $merId
  70. * @return array|Model|null
  71. * @throws DataNotFoundException
  72. * @throws DbException
  73. * @throws ModelNotFoundException
  74. * @author xaboy
  75. * @day 2020/7/7
  76. */
  77. public function merIdByAdmin(int $merId)
  78. {
  79. return MerchantAdmin::getDB()->where('mer_id', $merId)->where('level', 0)->find();
  80. }
  81. /**
  82. * @param string $account
  83. * @param int $merId
  84. * @return array|Model|null
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. * @author xaboy
  89. * @day 2020-04-20
  90. */
  91. public function accountByAdmin(string $account, int $merId)
  92. {
  93. return MerchantAdmin::getInstance()->where('account', $account)
  94. ->where('is_del', 0)->where('mer_id', $merId)
  95. ->field(['account', 'pwd', 'real_name', 'login_count', 'merchant_admin_id', 'status', 'mer_id'])
  96. ->find();
  97. }
  98. /**
  99. * @param string $account
  100. * @return array|Model|null
  101. * @throws DataNotFoundException
  102. * @throws DbException
  103. * @throws ModelNotFoundException
  104. * @author xaboy
  105. * @day 2020-04-20
  106. */
  107. public function accountByTopAdmin(string $account)
  108. {
  109. return MerchantAdmin::getInstance()->where('account', $account)
  110. ->where('is_del', 0)->whereIn('level', [0,1])
  111. ->field(['account', 'pwd', 'real_name', 'login_count', 'merchant_admin_id', 'status', 'mer_id'])
  112. ->find();
  113. }
  114. /**
  115. * @param string $account
  116. * @return mixed
  117. * @author xaboy
  118. * @day 2020-04-20
  119. */
  120. public function accountByMerchantId(string $account)
  121. {
  122. return MerchantAdmin::getInstance()->where('account', $account)->value('mer_id');
  123. }
  124. /**
  125. * @param int $id
  126. * @return array|Model|null
  127. * @throws DataNotFoundException
  128. * @throws DbException
  129. * @throws ModelNotFoundException
  130. * @author xaboy
  131. * @day 2020-04-17
  132. */
  133. public function get( $id)
  134. {
  135. return MerchantAdmin::getInstance()->where('is_del', 0)->find($id);
  136. }
  137. /**
  138. * @param int $id
  139. * @param int $merId
  140. * @param int|null $level
  141. * @return bool
  142. * @author xaboy
  143. * @day 2020-04-18
  144. */
  145. public function exists(int $id, int $merId = 0, ?int $level = null)
  146. {
  147. $query = MerchantAdmin::getDB()->where($this->getPk(), $id)->where('is_del', 0);
  148. if ($merId) $query->where('mer_id', $merId);
  149. if (!is_null($level)) $query->where('level', $level);
  150. return $query->count() > 0;
  151. }
  152. /**
  153. * @param int $merId
  154. * @param $field
  155. * @param $value
  156. * @param int|null $except
  157. * @return bool
  158. * @author xaboy
  159. * @day 2020-04-18
  160. */
  161. public function merFieldExists(int $merId, $field, $value, ?int $except = null): bool
  162. {
  163. $query = MerchantAdmin::getDB()->where($field, $value)->where('mer_id', $merId);
  164. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  165. return $query->count() > 0;
  166. }
  167. /**
  168. * @param int $id
  169. * @return bool
  170. * @author xaboy
  171. * @day 2020-04-18
  172. */
  173. public function topExists(int $id)
  174. {
  175. $query = MerchantAdmin::getDB()->where($this->getPk(), $id)->where('is_del', 0)->where('level', 0);
  176. return $query->count() > 0;
  177. }
  178. /**
  179. * @param int $merId
  180. * @return mixed
  181. * @author xaboy
  182. * @day 2020-04-17
  183. */
  184. public function merchantIdByTopAdminId(int $merId)
  185. {
  186. return MerchantAdmin::getDB()->where('mer_id', $merId)->where('is_del', 0)->where('level', 0)->value('merchant_admin_id');
  187. }
  188. }