UserLabelDao.php 2.5 KB

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