StoreCategoryDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\StoreCategory as model;
  14. use crmeb\traits\CategoresDao;
  15. class StoreCategoryDao extends BaseDao
  16. {
  17. use CategoresDao;
  18. protected function getModel(): string
  19. {
  20. return model::class;
  21. }
  22. public function findChildrenId($id)
  23. {
  24. return model::getDB()->whereLike('path', '%/'. $id . '/%')->column('store_category_id');
  25. }
  26. public function selectChildrenId(array $ids)
  27. {
  28. if (!is_array($ids) || empty($ids)) return [];
  29. $query = model::getDB()->where(function($query) use($ids){
  30. foreach ($ids as $id) {
  31. $query->whereOr('path', 'like','%/'. $id . '/%');
  32. }
  33. });
  34. return $query->column('store_category_id');
  35. }
  36. public function fieldExistsList(?int $merId,$field,$value,$except = null)
  37. {
  38. return ($this->getModel()::getDB())->when($except ,function($query)use($field,$except){
  39. $query->where($field,'<>',$except);
  40. })->when(($merId !== null) ,function($query)use($merId){
  41. $query->where('mer_id',$merId);
  42. })->where($field,$value);
  43. }
  44. public function getTwoLevel($merId = 0)
  45. {
  46. $pid = model::getDB()->where('pid', 0)->where('is_show',1)->where('mer_id', $merId)->order('sort DESC')->column('store_category_id');
  47. return model::getDB()->whereIn('pid', $pid)->where('is_show', 1)->where('mer_id', $merId)->limit(20)->order('sort DESC')->column('store_category_id,cate_name,pid');
  48. }
  49. public function children($pid, $merId = 0)
  50. {
  51. return model::getDB()->where('pid', $pid)->where('mer_id', $merId)->where('is_show', 1)->order('sort DESC')->column('store_category_id,cate_name,pic');
  52. }
  53. public function allChildren($id)
  54. {
  55. $path = model::getDB()->where('store_category_id', is_array($id) ? 'IN' : '=', $id)->where('mer_id', 0)->column('path', 'store_category_id');
  56. if (!count($path)) return [];
  57. return model::getDB()->where(function ($query) use ($path) {
  58. foreach ($path as $k => $v) {
  59. $query->whereOr('path', 'LIKE', "$v$k/%");
  60. }
  61. })->where('mer_id', 0)->order('sort DESC')->column('store_category_id');
  62. }
  63. public function idsByAllChildren(array $ids)
  64. {
  65. $paths = model::getDB()->whereIn('store_category_id', $ids)->where('mer_id', 0)->column('path');
  66. if (!count($paths)) return [];
  67. return model::getDB()->where(function ($query) use ($paths) {
  68. foreach ($paths as $path) {
  69. $query->whereOr('path', 'LIKE', "$path%");
  70. }
  71. })->where('mer_id', 0)->order('sort DESC')->column('store_category_id');
  72. }
  73. public function getMaxLevel($merId = null)
  74. {
  75. if($merId) return 2;
  76. return 3;
  77. }
  78. public function searchLevelAttr($query, $value)
  79. {
  80. $query->where('level', $value);
  81. }
  82. }