MenuDao.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\common\dao\system\menu;
  11. use app\common\dao\BaseDao;
  12. use app\common\model\BaseModel;
  13. use app\common\model\system\auth\Menu;
  14. use think\db\BaseQuery;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\Model;
  19. /**
  20. * Class MenuDao
  21. * @package app\common\dao\system\menu
  22. * @author xaboy
  23. * @day 2020-04-08
  24. */
  25. class MenuDao extends BaseDao
  26. {
  27. /**
  28. * @return BaseModel
  29. * @author xaboy
  30. * @day 2020-03-30
  31. */
  32. protected function getModel(): string
  33. {
  34. return Menu::class;
  35. }
  36. /**
  37. * @param array $where
  38. * @param int $is_mer
  39. * @return BaseQuery
  40. * @author xaboy
  41. * @day 2020-04-08
  42. */
  43. public function search(array $where, int $is_mer = 0)
  44. {
  45. $query = Menu::getDB()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC');
  46. if (isset($where['pid'])) $query->where('pid', (int)$where['pid']);
  47. if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%");
  48. if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']);
  49. return $query;
  50. }
  51. /**
  52. * @param int $is_mer
  53. * @return array
  54. * @throws DataNotFoundException
  55. * @throws DbException
  56. * @throws ModelNotFoundException
  57. * @author xaboy
  58. * @day 2020-04-08
  59. */
  60. public function getAllMenu($is_mer = 0)
  61. {
  62. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->order('sort DESC,menu_id ASC')->select()->toArray();
  63. }
  64. /**
  65. * @param int $is_mer
  66. * @return array
  67. * @throws DataNotFoundException
  68. * @throws DbException
  69. * @throws ModelNotFoundException
  70. * @author xaboy
  71. * @day 2020-04-08
  72. */
  73. public function getAll($is_mer = 0)
  74. {
  75. return Menu::getInstance()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->select()->toArray();
  76. }
  77. /**
  78. * @param int $id
  79. * @param int $is_mer
  80. * @return bool
  81. * @author xaboy
  82. * @day 2020-04-08
  83. */
  84. public function menuExists(int $id, $is_mer = 0)
  85. {
  86. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 1)->where('is_mer', $is_mer)->count() > 0;
  87. }
  88. /**
  89. * @param int $id
  90. * @param int $is_mer
  91. * @return bool
  92. * @author xaboy
  93. * @day 2020-04-16
  94. */
  95. public function merExists(int $id, $is_mer = 0)
  96. {
  97. return Menu::getDB()->where($this->getPk(), $id)->where('is_mer', $is_mer)->count() > 0;
  98. }
  99. /**
  100. * @param int $id
  101. * @param int $is_mer
  102. * @return bool
  103. * @author xaboy
  104. * @day 2020-04-08
  105. */
  106. public function authExists(int $id, $is_mer = 0)
  107. {
  108. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  109. }
  110. /**
  111. * @param string $route
  112. * @param int $is_mer
  113. * @return bool
  114. * @author xaboy
  115. * @day 2020-04-08
  116. */
  117. public function routeExists(string $route, $is_mer = 0)
  118. {
  119. return Menu::getDB()->where('route', $route)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  120. }
  121. /**
  122. * @param int $is_mer
  123. * @return array
  124. * @author xaboy
  125. * @day 2020-04-08
  126. */
  127. public function getAllMenuOptions($is_mer = 0)
  128. {
  129. return Menu::getDB()->where('is_menu', 1)->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  130. }
  131. /**
  132. * @param array $rule
  133. * @param int $is_mer
  134. * @return array
  135. * @author xaboy
  136. * @day 2020-04-10
  137. */
  138. public function ruleByMenuList(array $rule, $is_mer = 0)
  139. {
  140. $paths = Menu::getDB()->whereIn($this->getPk(), $rule)->column('path', 'menu_id');
  141. $ids = [];
  142. foreach ($paths as $id => $path) {
  143. $ids = array_merge($ids, explode('/', trim($path, '/')));
  144. array_push($ids, $id);
  145. }
  146. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  147. ->whereIn('menu_id', array_unique(array_filter($ids)))
  148. ->column('menu_name,route,params,icon,pid,menu_id');
  149. }
  150. /**
  151. * @param int $is_mer
  152. * @return array
  153. * @author xaboy
  154. * @day 2020-04-10
  155. */
  156. public function getValidMenuList($is_mer = 0)
  157. {
  158. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  159. ->column('menu_name,route,params,icon,pid,menu_id');
  160. }
  161. /**
  162. * @param int $is_mer
  163. * @return array
  164. * @author xaboy
  165. * @day 2020-04-08
  166. */
  167. public function getAllOptions($is_mer = 0)
  168. {
  169. return Menu::getDB()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  170. }
  171. /**
  172. * @param $id
  173. * @param int $is_mer
  174. * @return mixed
  175. * @author xaboy
  176. * @day 2020-04-09
  177. */
  178. public function getPath($id, $is_mer = 0)
  179. {
  180. return Menu::getDB()->where('is_mer', $is_mer)->where('menu_id', $id)->value('path');
  181. }
  182. /**
  183. * @param int $id
  184. * @param int $is_mer
  185. * @return array|Model|null
  186. * @throws DataNotFoundException
  187. * @throws DbException
  188. * @throws ModelNotFoundException
  189. * @author xaboy
  190. * @day 2020-04-08
  191. */
  192. public function getMenu(int $id, $is_mer = 0)
  193. {
  194. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->where($this->getPk(), $id)->find();
  195. }
  196. /**
  197. * @param int $id
  198. * @param int $is_mer
  199. * @return array|Model|null
  200. * @throws DataNotFoundException
  201. * @throws DbException
  202. * @throws ModelNotFoundException
  203. * @author xaboy
  204. * @day 2020-04-08
  205. */
  206. public function getAuth(int $id, $is_mer = 0)
  207. {
  208. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 0)->where($this->getPk(), $id)->find();
  209. }
  210. /**
  211. * @param int $id
  212. * @param int $is_mer
  213. * @return int
  214. * @throws DbException
  215. * @author xaboy
  216. * @day 2020-04-08
  217. */
  218. public function delete(int $id, $is_mer = 0)
  219. {
  220. return Menu::getDB()->where('is_mer', $is_mer)->delete($id);
  221. }
  222. /**
  223. * @param int $id
  224. * @return bool
  225. * @author xaboy
  226. * @day 2020-04-08
  227. */
  228. public function pidExists(int $id)
  229. {
  230. return $this->fieldExists('pid', $id);
  231. }
  232. /**
  233. * @param array $ids
  234. * @return array
  235. * @author xaboy
  236. * @day 2020-04-10
  237. */
  238. public function idsByRoutes(array $ids)
  239. {
  240. return Menu::getDB()->where('is_menu', 0)->whereIn($this->getPk(), $ids)->column('params,route');
  241. }
  242. /**
  243. * @param string $oldPath
  244. * @param string $path
  245. * @throws DataNotFoundException
  246. * @throws DbException
  247. * @throws ModelNotFoundException
  248. * @author xaboy
  249. * @day 2020-04-30
  250. */
  251. public function updatePath(string $oldPath, string $path)
  252. {
  253. Menu::getDB()->whereLike('path', $oldPath . '%')->field('menu_id,path')->select()->each(function ($val) use ($oldPath, $path) {
  254. $newPath = str_replace($oldPath, $path, $val['path']);
  255. Menu::getDB()->where('menu_id', $val['menu_id'])->update(['path' => $newPath]);
  256. });
  257. }
  258. /**
  259. * @Author:Qinii
  260. * @Date: 2020/5/26
  261. * @param $field
  262. * @param $value
  263. * @return array|Model|null
  264. */
  265. public function getFieldExists($field,$value)
  266. {
  267. return (($this->getModel()::getDB())->where($field,$value)->find());
  268. }
  269. /**
  270. * @Author:Qinii
  271. * @Date: 2020/5/26
  272. * @param array $data
  273. * @return int
  274. */
  275. public function insertAll(array $data)
  276. {
  277. return ($this->getModel()::getDB())->insertAll($data);
  278. }
  279. public function deleteCommandMenu($where)
  280. {
  281. $this->getModel()::getDB()->where($where)->delete();
  282. }
  283. public function all()
  284. {
  285. return ($this->getModel()::getDB())->select();
  286. }
  287. }