UserLabelDao.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\UserLabel;
  15. use think\db\BaseQuery;
  16. /**
  17. * Class UserLabelDao
  18. * @package app\common\dao\user
  19. * @author xaboy
  20. * @day 2020-05-07
  21. */
  22. class UserLabelDao extends BaseDao
  23. {
  24. /**
  25. * @return BaseModel
  26. * @author xaboy
  27. * @day 2020-03-30
  28. */
  29. protected function getModel(): string
  30. {
  31. return UserLabel::class;
  32. }
  33. /**
  34. * @param array $where
  35. * @return BaseQuery
  36. * @author xaboy
  37. * @day 2020-05-06
  38. */
  39. public function search(array $where = [])
  40. {
  41. return UserLabel::getDB()->when(!isset($where['all']) || $where['all'] === '', function ($query) use ($where) {
  42. $query->where('type', 0);
  43. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  44. $query->where('mer_id', $where['mer_id']);
  45. }, function ($query) {
  46. $query->where('mer_id', 0);
  47. });
  48. }
  49. /**
  50. * @param int $merId
  51. * @return array
  52. * @author xaboy
  53. * @day 2020/10/20
  54. */
  55. public function allOptions($merId = 0)
  56. {
  57. return UserLabel::getDB()->where('mer_id', $merId)->where('type', 0)->column('label_name', 'label_id');
  58. }
  59. /**
  60. * @param array $ids
  61. * @param int $merId
  62. * @return array
  63. * @author xaboy
  64. * @day 2020/10/20
  65. */
  66. public function labels(array $ids, $merId = 0)
  67. {
  68. return UserLabel::getDB()->where('mer_id', $merId)->whereIn('label_id', $ids)->column('label_name');
  69. }
  70. /**
  71. * @param int $id
  72. * @param int $mer_id
  73. * @return bool
  74. * @author xaboy
  75. * @day 2020/10/20
  76. */
  77. public function exists(int $id, $mer_id = 0)
  78. {
  79. return $this->existsWhere(['label_id' => $id, 'type' => 0, 'mer_id' => $mer_id]);
  80. }
  81. public function existsName($name, $mer_id = 0, $type = 0, $except = null)
  82. {
  83. return UserLabel::where('label_name', $name)->where('mer_id', $mer_id)->where('type', $type)
  84. ->when($except, function ($query, $except) {
  85. $query->where($this->getPk(), '<>', $except);
  86. })->count() > 0;
  87. }
  88. public function intersection(array $ids, $merId, $type)
  89. {
  90. return UserLabel::getDB()->whereIn('label_id', $ids)->where('mer_id', $merId)->when(!is_null($type), function ($query) use ($type) {
  91. $query->where('type', $type);
  92. })->column('label_id');
  93. }
  94. }