CommonModel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace huo\model\common;
  10. use huolib\constant\CommonConst;
  11. use think\Loader;
  12. use think\Model;
  13. class CommonModel extends Model {
  14. // 关联模型过滤
  15. protected $relationFilter = [];
  16. public static function className() {
  17. return get_called_class();
  18. }
  19. /**
  20. * @param array $params
  21. *
  22. * @return array|false|\PDOStatement|string|\think\Collection|Model
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public function getDatas($params = []) {
  28. if (empty($params)) {
  29. return $this->select();
  30. }
  31. $this->setCondition($params);
  32. if (!empty($params['id'])) {
  33. $datas = $this->find();
  34. } else {
  35. $datas = $this->select();
  36. }
  37. if (!empty($params['relation'])) {
  38. $allowedRelations = $this->allowedRelations($params['relation']);
  39. if (!empty($allowedRelations)) {
  40. if (!empty($params['id'])) {
  41. if (!empty($datas)) {
  42. $datas->append($allowedRelations);
  43. }
  44. } else {
  45. if (count($datas) > 0) {
  46. $datas->load($allowedRelations);
  47. $datas->append($allowedRelations);
  48. }
  49. }
  50. }
  51. }
  52. return $datas;
  53. }
  54. /**
  55. * @access public
  56. *
  57. * @param array $params 过滤参数
  58. *
  59. * @return $this
  60. */
  61. public function setCondition($params) {
  62. if (empty($params)) {
  63. return $this;
  64. }
  65. if (!empty($params['relation'])) {
  66. $allowedRelations = $this->allowedRelations($params['relation']);
  67. if (!empty($allowedRelations)) {
  68. if (!empty($params['id']) && count($allowedRelations) == 1) {
  69. $this->paramsFilter($params);
  70. } else {
  71. $this->paramsFilter($params);//->with($allowedRelations);
  72. }
  73. }
  74. } else {
  75. $this->paramsFilter($params);
  76. }
  77. return $this;
  78. }
  79. public function orderFilter($order = []) {
  80. if (empty($order)) {
  81. return '';
  82. }
  83. $orderWhere = [];
  84. $_order = $this->strToArr($order);
  85. foreach ($_order as $key => $value) {
  86. $upDwn = substr($value, 0, 1);
  87. $orderType = $upDwn == '-' ? 'desc' : 'asc';
  88. $orderField = substr($value, 1);
  89. $orderWhere[$orderField] = $orderType;
  90. }
  91. return $orderWhere;
  92. }
  93. /**
  94. * @access public
  95. *
  96. * @param array $params 过滤参数
  97. * @param model $model 关联模型
  98. *
  99. * @return model|array $this|链式查询条件数组
  100. */
  101. public function paramsFilter($params, $model = null) {
  102. if (!empty($model)) {
  103. $_this = $model;
  104. } else {
  105. $_this = $this;
  106. }
  107. if (isset($_this->visible)) {
  108. $whiteParams = $_this->visible;
  109. }
  110. // 设置field字段过滤
  111. if (!empty($params['field'])) {
  112. $filterParams = $this->strToArr($params['field']);
  113. if (!empty($whiteParams)) {
  114. $mixedField = array_intersect($filterParams, $whiteParams);
  115. } else {
  116. $mixedField = $filterParams;
  117. }
  118. if (!empty($mixedField)) {
  119. $_this->field($mixedField);
  120. }
  121. }
  122. // 设置id,ids
  123. if (!empty($params['ids'])) {
  124. $ids = $this->strToArr($params['ids']);
  125. foreach ($ids as $key => $value) {
  126. $ids[$key] = intval($value);
  127. }
  128. }
  129. if (!empty($params['id'])) {
  130. $id = intval($params['id']);
  131. if (!empty($id)) {
  132. return $_this->where('id', $id);
  133. }
  134. } elseif (!empty($ids)) {
  135. $_this->where('id', 'in', $ids);
  136. }
  137. if (!empty($params['where'])) {
  138. if (empty($model)) {
  139. $_this->where($params['where']);
  140. }
  141. }
  142. // 设置分页
  143. if (!empty($params['page'])) {
  144. $pageArr = $this->strToArr($params['page']);
  145. $page = [];
  146. foreach ($pageArr as $value) {
  147. $page[] = intval($value);
  148. }
  149. if (count($page) == 1) {
  150. $_this->page($page[0]);
  151. } elseif (count($page) == 2) {
  152. $_this->page($page[0], $page[1]);
  153. }
  154. } elseif (!empty($params['limit'])) { // 设置limit查询
  155. $limitArr = $this->strToArr($params['limit']);
  156. $limit = [];
  157. foreach ($limitArr as $value) {
  158. $limit[] = intval($value);
  159. }
  160. if (count($limit) == 1) {
  161. $_this->limit($limit[0]);
  162. } elseif (count($limit) == 2) {
  163. $_this->limit($limit[0], $limit[1]);
  164. }
  165. } else {
  166. $_this->limit(10);
  167. }
  168. //设置排序
  169. if (!empty($params['order'])) {
  170. $order = $this->strToArr($params['order']);
  171. foreach ($order as $key => $value) {
  172. $upDwn = substr($value, 0, 1);
  173. $orderType = $upDwn == '-' ? 'desc' : 'asc';
  174. $orderField = substr($value, 1);
  175. if (!empty($whiteParams)) {
  176. if (in_array($orderField, $whiteParams)) {
  177. $orderWhere[$orderField] = $orderType;
  178. }
  179. } else {
  180. $orderWhere[$orderField] = $orderType;
  181. }
  182. }
  183. if (!empty($orderWhere)) {
  184. $_this->order($orderWhere);
  185. }
  186. }
  187. return $_this;
  188. }
  189. /**
  190. * 设置链式查询
  191. *
  192. * @access public
  193. *
  194. * @param array $params 链式查询条件
  195. * @param model $model 模型
  196. *
  197. * @return $this
  198. */
  199. public function setParamsQuery($params, $model = null) {
  200. if (!empty($model)) {
  201. $_this = $model;
  202. } else {
  203. $_this = $this;
  204. }
  205. $_this->alias('articles');
  206. if (!empty($params['field'])) {
  207. $_this->field($params['field']);
  208. }
  209. if (!empty($params['ids'])) {
  210. $_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
  211. }
  212. if (!empty($params['limit'])) {
  213. $_this->limit($params['limit']);
  214. }
  215. if (!empty($params['page'])) {
  216. $_this->page($params['page']);
  217. }
  218. if (!empty($params['order'])) {
  219. $_this->order($params['order']);
  220. }
  221. return $_this;
  222. }
  223. public function allowedRelations($relations) {
  224. if (is_string($relations)) {
  225. $relations = explode(',', $relations);
  226. }
  227. if (!is_array($relations)) {
  228. return false;
  229. }
  230. return array_intersect($this->relationFilter, $relations);
  231. }
  232. /**
  233. * 是否允许关联
  234. *
  235. * @access public
  236. *
  237. * @param string $relationName 模型关联方法名
  238. *
  239. * @return boolean
  240. */
  241. public function isWhite($relationName) {
  242. if (!is_string($relationName)) {
  243. return false;
  244. }
  245. $name = Loader::parseName($relationName, 1, false);
  246. if (in_array($name, $this->relationFilter)) {
  247. return true;
  248. } else {
  249. return false;
  250. }
  251. }
  252. /**
  253. * 懒人函数
  254. *
  255. * @access public
  256. *
  257. * @param string $string 字符串
  258. *
  259. * @return array
  260. */
  261. public function strToArr($string) {
  262. return is_string($string) ? explode(',', $string) : $string;
  263. }
  264. /**
  265. * 懒人函数
  266. *
  267. * @access public
  268. *
  269. * @param array $arr 数组
  270. *
  271. * @return string
  272. */
  273. public function arrToStr($arr) {
  274. return is_array($arr) ? implode(',', $arr) : $arr;
  275. }
  276. /**
  277. * @param array $data
  278. * @param string|int $key
  279. * @param string $default
  280. *
  281. * @return string
  282. */
  283. protected function getVal($data, $key, $default = '') {
  284. if (empty($key) || empty($data) || !isset($data[$key])) {
  285. return $default;
  286. }
  287. return $data[$key];
  288. }
  289. /**
  290. * 计算在哪张表
  291. *
  292. * @return \Think\Model
  293. */
  294. public function computeTable() {
  295. return $this;
  296. }
  297. /**
  298. * @param $data
  299. * @param bool $replace
  300. * @param bool $get_last_insert_id
  301. *
  302. * @return bool|int|string
  303. */
  304. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  305. if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
  306. return $_id;
  307. } else {
  308. return false;
  309. }
  310. }
  311. /**
  312. * 获取单条记录
  313. *
  314. * @param int $id
  315. *
  316. * @return array|false
  317. */
  318. public function getInfoById($id) {
  319. if (isset($this->pk)) {
  320. $_map[$this->pk] = $id;
  321. } else {
  322. $_map['id'] = $id;
  323. }
  324. $_info = $this->where($_map)->find();
  325. if (false === $_info) {
  326. return false;
  327. }
  328. if (is_object($_info)) {
  329. return $_info->toArray();
  330. } else {
  331. return $_info;
  332. }
  333. }
  334. /**当前模型名称(数据库表名,不带前缀)
  335. *
  336. * @return string
  337. */
  338. public function getName() {
  339. return $this->name;
  340. }
  341. /**
  342. * 校验字段
  343. *
  344. * @return bool
  345. */
  346. protected function checkField() {
  347. return true;
  348. }
  349. /**
  350. * 添加数据
  351. *
  352. * @param array $data 需要添加的数据
  353. *
  354. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  355. */
  356. public function parentAddData($data) {
  357. if (empty($data)) {
  358. return false;
  359. }
  360. $_data = $data;
  361. $_model = new static();
  362. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  363. if (false !== $_rs) {
  364. return $_model->getLastInsID();
  365. }
  366. return false;
  367. }
  368. /**
  369. * 通过ID获取信息
  370. *
  371. * @param int $id 主键ID
  372. *
  373. * @return array|false
  374. */
  375. public function parentGetInfoById($id) {
  376. $_map[$this->pk] = $id;
  377. $_info = $this->useGlobalScope(false)->where($_map)->find();
  378. if (false === $_info) {
  379. return false;
  380. }
  381. if (is_object($_info)) {
  382. return $_info->toArray();
  383. } else {
  384. return $_info;
  385. }
  386. }
  387. /**
  388. * 更新数据
  389. *
  390. * @param array $data 数据
  391. * @param int|array $id 主集ID
  392. *
  393. * @return bool
  394. */
  395. public function parentUpdateData($data, $id) {
  396. $this->checkField();
  397. if (is_array($id)) {
  398. $_map[$this->pk] = ['in', $id];
  399. } else {
  400. $_map[$this->pk] = $id;
  401. }
  402. $_data = $data;
  403. $_model = new static();
  404. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  405. if (false === $_rs) {
  406. return false;
  407. } else {
  408. return true;
  409. }
  410. }
  411. /**
  412. * 删除数据
  413. *
  414. * @param array|int $ids ID合集
  415. * @param bool $is_complete 是否完成删除
  416. *
  417. * @return bool|int
  418. */
  419. public function parentDeleteData($ids, $is_complete = false) {
  420. $this->checkField();
  421. if (true == is_array($ids)) {
  422. $_ids = $ids;
  423. } else {
  424. $_ids = [$ids];
  425. }
  426. $_map[$this->pk] = ['in', $_ids];
  427. if (true == $is_complete) {
  428. /* 彻底删除 */
  429. return $this->useGlobalScope(false)->where($_map)->delete();
  430. } else {
  431. $_data['is_delete'] = CommonConst::CONST_DELETED;
  432. $_data['delete_time'] = time();
  433. foreach ($_ids as $_id) {
  434. $_rs = self::parentUpdateData($_data, $_id);
  435. if (false === $_rs) {
  436. return false;
  437. }
  438. }
  439. return true;
  440. }
  441. }
  442. /**
  443. * 通过条件获取数量
  444. *
  445. * @param array $where 条件
  446. *
  447. * @return int
  448. */
  449. public function parentGetCnt($where = []) {
  450. $_map = $where;
  451. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  452. if (empty($_cnt)) {
  453. return 0;
  454. }
  455. return $_cnt;
  456. }
  457. /**
  458. * 通过条件获取汇总
  459. *
  460. * @param string $field 字段
  461. * @param array $where 条件
  462. *
  463. * @return double
  464. */
  465. public function parentGetSum($field, $where = []) {
  466. $_map = $where;
  467. $_sum = $this->useGlobalScope(false)->where($_map)->sum($field);
  468. if (false == $_sum) {
  469. return false;
  470. }
  471. return $_sum;
  472. }
  473. /**
  474. * 添加数据
  475. *
  476. * @param array $data 需要添加的数据
  477. *
  478. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  479. */
  480. public function addData($data) {
  481. if (empty($data)) {
  482. return false;
  483. }
  484. $_data = $data;
  485. $_model = new static();
  486. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  487. if (false !== $_rs) {
  488. return $_model->getLastInsID();
  489. }
  490. return false;
  491. }
  492. /**
  493. * 更新数据
  494. *
  495. * @param array $data 数据
  496. * @param int|array $id 主集ID
  497. *
  498. * @return bool
  499. */
  500. public function updateData($data, $id) {
  501. if (is_array($id)) {
  502. $_map[$this->pk] = ['in', $id];
  503. } else {
  504. $_map[$this->pk] = $id;
  505. }
  506. $_data = $data;
  507. $_model = new static();
  508. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  509. if (false === $_rs) {
  510. return false;
  511. } else {
  512. return true;
  513. }
  514. }
  515. /**
  516. * 删除数据
  517. *
  518. * @param array|int $ids ID合集
  519. * @param bool $is_complete 是否完成删除
  520. *
  521. * @return bool|int
  522. */
  523. public function deleteData($ids, $is_complete = false) {
  524. if (true == is_array($ids)) {
  525. $_ids = $ids;
  526. } else {
  527. $_ids = [$ids];
  528. }
  529. $_map[$this->pk] = ['in', $_ids];
  530. if (true == $is_complete) {
  531. /* 彻底删除 */
  532. return $this->useGlobalScope(false)->where($_map)->delete();
  533. } else {
  534. $_data['is_delete'] = CommonConst::CONST_DELETED;
  535. $_data['delete_time'] = time();
  536. foreach ($_ids as $_id) {
  537. $_rs = self::updateData($_data, $_id);
  538. if (false === $_rs) {
  539. return false;
  540. }
  541. }
  542. return true;
  543. }
  544. }
  545. }