WorkClientDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\dao\work;
  12. use app\dao\BaseDao;
  13. use app\model\work\WorkClient;
  14. use crmeb\basic\BaseAuth;
  15. use crmeb\traits\SearchDaoTrait;
  16. /**
  17. * 企业微信客户
  18. * Class WorkClientDao
  19. * @package app\dao\work
  20. */
  21. class WorkClientDao extends BaseDao
  22. {
  23. use SearchDaoTrait;
  24. /**
  25. * @return string
  26. */
  27. protected function setModel(): string
  28. {
  29. return WorkClient::class;
  30. }
  31. /**
  32. * @param array $where
  33. * @param bool $authWhere
  34. * @return \crmeb\basic\BaseModel
  35. */
  36. public function searchWhere(array $where, bool $authWhere = true)
  37. {
  38. [$with, $whereKey] = app()->make(BaseAuth::class)->________(array_keys($where), $this->setModel());
  39. $whereData = [];
  40. foreach ($whereKey as $key) {
  41. if (isset($where[$key])) {
  42. $whereData[$key] = $where[$key];
  43. }
  44. }
  45. return $this->getModel()->withSearch($with, $where)->when(!empty($where['label']) || !empty($where['notLabel']), function ($query) use ($where) {
  46. $query->whereIn('id', function ($query) use ($where) {
  47. $query->name('work_client_follow')->whereIn('id', function ($query) use ($where) {
  48. $query->name('work_client_follow_tags')->when(!empty($where['label']), function ($query) use ($where) {
  49. $query->whereIn('tag_id', $where['label']);
  50. })->when(!empty($where['notLabel']), function ($query) use ($where) {
  51. $query->whereNotIn('tag_id', $where['notLabel']);
  52. })->field('follow_id');
  53. })->field('client_id');
  54. });
  55. })->when(!empty($where['userid']), function ($query) use ($where) {
  56. $query->whereIn('id', function ($query) use ($where) {
  57. $query->name('work_client_follow')->when(!empty($where['state']), function ($query) use ($where) {
  58. $query->where('state', '<>', '');
  59. })->whereIn('userid', $where['userid'])->field('client_id');
  60. });
  61. })->when(isset($where['name']) && '' !== $where['name'], function ($query) use ($where) {
  62. $query->whereLike('name', '%' . $where['name'] . '%');
  63. })->when(!empty($where['corp_id']), function ($query) use ($where) {
  64. $query->where('corp_id', $where['corp_id']);
  65. })->when(!empty($where['gender']), function ($query) use ($where) {
  66. $query->where('gender', $where['gender']);
  67. })->when(!empty($where['state']) && empty($where['userid']), function ($query) use ($where) {
  68. $query->whereIn('id', function ($query) use ($where) {
  69. $query->name('work_client_follow')->where('state', '<>', '')->field('client_id');
  70. });
  71. })->when(!empty($where['status']), function ($query) use ($where) {
  72. $query->whereIn('id', function ($query) use ($where) {
  73. $query->name('work_client_follow')->where('is_del_user', $where['status'])->field('client_id');
  74. });
  75. })->when(!empty($where['notUserid']), function ($query) use ($where) {
  76. $query->whereNotIn('external_userid', $where['notUserid']);
  77. });
  78. }
  79. /**
  80. * @param array $where
  81. * @return int
  82. */
  83. public function getClientCount(array $where)
  84. {
  85. return $this->searchWhere($where)->count();
  86. }
  87. /**
  88. * 获取客户userid
  89. * @param array $where
  90. * @return array
  91. */
  92. public function getClientUserIds(array $where)
  93. {
  94. return $this->searchWhere($where)->column('external_userid');
  95. }
  96. }