ExpressDao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\store\shipping;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\shipping\Express as model;
  14. class ExpressDao extends BaseDao
  15. {
  16. /**
  17. * @Author:Qinii
  18. * @Date: 2020/5/13
  19. * @return string
  20. */
  21. protected function getModel(): string
  22. {
  23. return model::class;
  24. }
  25. /**
  26. * 检查指定字段的值是否已存在,可排除特定的值或ID。
  27. *
  28. * 此函数用于查询数据库中是否存在指定字段的值,同时允许排除特定的值或ID,
  29. * 以及限定只查询显示状态为是的记录。这在处理数据唯一性验证或存在性检查时非常有用。
  30. *
  31. * @param string $field 要检查的字段名。
  32. * @param mixed $value 要检查的字段值。
  33. * @param mixed $except 排除的值,如果不为空,则查询时不包括此值。
  34. * @param mixed $id 排除的ID,如果不为空,则查询时不包括此ID的记录。
  35. * @param bool $isUser 是否只查询显示状态为是的记录,true表示只查询显示状态为是的记录,false或null表示不作此限制。
  36. * @return bool 如果存在返回true,否则返回false。
  37. */
  38. public function merFieldExists($field, $value, $except = null, $id = null, $isUser = null)
  39. {
  40. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  41. $query->where($field, '<>', $except);
  42. })->when($id, function ($query) use ($id) {
  43. $query->where($this->getPk(), '<>', $id);
  44. })->when($isUser, function ($query) {
  45. $query->where('is_show', 1);
  46. })->where($field, $value)->count() > 0;
  47. }
  48. /**
  49. * 根据条件搜索数据。
  50. *
  51. * 本函数用于根据提供的条件数组搜索相应的数据。支持的条件包括关键字、代码、显示状态和ID。
  52. * 搜索条件是可选的,只有在数组中提供了相应的键值对时,才会应用相应的过滤条件。
  53. *
  54. * @param array $where 搜索条件数组,包含可能的键:keyword(关键字)、code(代码)、is_show(显示状态)、id(ID)。
  55. * @return \Illuminate\Database\Eloquent\Builder|static 返回一个构建器对象,已应用搜索条件和排序。
  56. */
  57. public function search(array $where)
  58. {
  59. // 从模型获取数据库实例,并根据提供的条件应用相应的查询约束。
  60. $query = ($this->getModel()::getDB())
  61. // 如果提供了关键字,则在名称或代码字段中进行模糊搜索。
  62. ->when(isset($where['keyword']) && $where['keyword'], function ($query) use ($where) {
  63. $query->where('name|code', 'like', '%'.$where['keyword'].'%');
  64. })
  65. // 如果提供了代码,则精确匹配代码字段。
  66. ->where(isset($where['code']) && $where['code'], function ($query) use ($where) {
  67. $query->where('code', $where['code']);
  68. })
  69. // 如果提供了显示状态,则精确匹配显示状态字段。
  70. ->where(isset($where['is_show']) && $where['is_show'], function ($query) use ($where) {
  71. $query->where('is_show', $where['is_show']);
  72. })
  73. // 如果提供了ID,则精确匹配ID字段。
  74. ->where(isset($where['id']) && $where['id'], function ($query) use ($where) {
  75. $query->where('id', $where['id']);
  76. });
  77. // 返回应用了排序(按排序降序)的查询构建器。
  78. return $query->order('sort DESC');
  79. }
  80. public function options($merId, $where, $field = 'id,name')
  81. {
  82. $query = $this->getModel()::getDB()->where($where)->field($field);
  83. if($merId) {
  84. $query->whereFindInSet('open_mer', $merId);
  85. }
  86. return $query->select()->toArray();
  87. }
  88. }