MerchantAdminDao.php 6.2 KB

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