MerchantAdminDao.php 5.6 KB

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