NewCommonModel.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * NewCommonModel.php UTF-8
  4. * 新公共Model
  5. *
  6. * @date : 2020/3/23 11:16
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOUNION 8.5
  11. */
  12. namespace huomp\model\common;
  13. use huolib\constant\CacheConst;
  14. use huolib\constant\CommonConst;
  15. use think\Cache;
  16. use think\db\exception\BindParamException;
  17. use think\Exception;
  18. use think\exception\PDOException;
  19. use think\Model;
  20. class NewCommonModel extends Model {
  21. protected $pk = 'id';
  22. // 关联模型过滤
  23. protected $relationFilter = [];
  24. //protected $table_cache_key_prefix = CacheConst::CACHE_TABLE_FIELD_PREFIX;
  25. protected $table_cache_key_prefix = '';
  26. /**
  27. * 懒人函数
  28. *
  29. * @access public
  30. *
  31. * @param string $string 字符串
  32. *
  33. * @return
  34. */
  35. public function strToArr($string) {
  36. return is_string($string) ? explode(',', $string) : (array)$string;
  37. }
  38. /**
  39. * 懒人函数
  40. *
  41. * @access public
  42. *
  43. * @param array $arr 数组
  44. *
  45. * @return string
  46. */
  47. public function arrToStr($arr) {
  48. return is_array($arr) ? implode(',', $arr) : (string)$arr;
  49. }
  50. /**
  51. * 计算在哪张表
  52. *
  53. * @return \Think\Model
  54. */
  55. public function computeTable() {
  56. return $this;
  57. }
  58. /**
  59. * @param $data
  60. * @param bool $replace
  61. * @param bool $get_last_insert_id
  62. *
  63. * @return bool|int|string
  64. */
  65. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  66. if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
  67. return $_id;
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * 添加数据
  74. *
  75. * @param array $data 需要添加的数据
  76. *
  77. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  78. */
  79. public function addData($data) {
  80. $this->checkField();
  81. if (empty($data)) {
  82. return false;
  83. }
  84. $_data = $data;
  85. $_model = new static();
  86. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  87. if (false !== $_rs) {
  88. return $_model->getLastInsID();
  89. }
  90. return false;
  91. }
  92. /**
  93. * 通过ID获取信息
  94. *
  95. * @param int $id 主键ID
  96. *
  97. * @return array|false
  98. */
  99. public function getInfoById($id) {
  100. $this->checkField();
  101. $_map[$this->pk] = $id;
  102. $_info = $this->useGlobalScope(false)->where($_map)->find();
  103. if (false === $_info) {
  104. return false;
  105. }
  106. if (is_object($_info)) {
  107. return $_info->toArray();
  108. } else {
  109. return $_info;
  110. }
  111. }
  112. /**
  113. * 更新数据
  114. *
  115. * @param array $data 数据
  116. * @param int|array $id 主集ID
  117. *
  118. * @return bool
  119. */
  120. public function updateData($data, $id) {
  121. $this->checkField();
  122. if (is_array($id)) {
  123. $_map[$this->pk] = ['in', $id];
  124. } else {
  125. $_map[$this->pk] = $id;
  126. }
  127. $_data = $data;
  128. $_model = new static();
  129. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  130. if (false === $_rs) {
  131. return false;
  132. } else {
  133. return true;
  134. }
  135. }
  136. /**
  137. * 删除数据
  138. *
  139. * @param array|int $ids ID合集
  140. * @param bool $is_complete 是否完成删除
  141. *
  142. * @return bool|int
  143. */
  144. public function deleteData($ids, $is_complete = false) {
  145. $this->checkField();
  146. if (true == is_array($ids)) {
  147. $_ids = $ids;
  148. } else {
  149. $_ids = [$ids];
  150. }
  151. $_map[$this->pk] = ['in', $_ids];
  152. if (true == $is_complete) {
  153. /* 彻底删除 */
  154. return $this->useGlobalScope(false)->where($_map)->delete();
  155. } else {
  156. $_data['is_delete'] = CommonConst::CONST_DELETED;
  157. $_data['delete_time'] = time();
  158. foreach ($_ids as $_id) {
  159. $_rs = self::updateData($_data, $_id);
  160. if (false === $_rs) {
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. }
  167. /**当前模型名称(数据库表名,不带前缀)
  168. *
  169. * @return string
  170. */
  171. public function getName() {
  172. return $this->name;
  173. }
  174. /**
  175. * 通过条件获取数量
  176. *
  177. * @param array $where 条件
  178. *
  179. * @return int
  180. */
  181. public function getCnt($where = []) {
  182. $_map = $where;
  183. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  184. if (empty($_cnt)) {
  185. return 0;
  186. }
  187. return $_cnt;
  188. }
  189. /**
  190. * 通过条件获取汇总
  191. *
  192. * @param string $field 字段
  193. * @param array $where 条件
  194. *
  195. * @return double
  196. */
  197. public function getSum($field, $where = []) {
  198. $_map = $where;
  199. $_sum = $this->useGlobalScope(false)->where($_map)->sum($field);
  200. if (false == $_sum) {
  201. return false;
  202. }
  203. return $_sum;
  204. }
  205. /**
  206. * 判断表中字段是否存在
  207. *
  208. * @param string $table 表名
  209. * @param string $column 字段名
  210. *
  211. * @return bool
  212. */
  213. public function isColumnExist($table, $column) {
  214. $_cache_key = $this->table_cache_key_prefix.strtolower($table.$column);
  215. $_data = Cache::get($_cache_key);
  216. if (1 == $_data) {
  217. return true;
  218. }
  219. $_has_it = false;//是否存在该字段
  220. $_columns = db()->getTableFields($table);
  221. if (!empty($_columns)) {
  222. foreach ($_columns as $_k => $_v) {
  223. if ($_v == $column) {
  224. $_has_it = true;
  225. break;
  226. }
  227. }
  228. }
  229. if (true == $_has_it) {
  230. /* 永不过期 */
  231. Cache::set($_cache_key, 1, CommonConst::CONST_MAX_INT);
  232. }
  233. return $_has_it;
  234. }
  235. /**
  236. * 校验字段
  237. *
  238. * @return bool
  239. */
  240. protected function checkField() {
  241. return true;
  242. }
  243. /**
  244. * 获取表名缓存
  245. *
  246. * @param string $table_name
  247. *
  248. * @return string
  249. */
  250. public function getTableCacheKey($table_name) {
  251. return $this->table_cache_key_prefix.strtolower($table_name);
  252. }
  253. /**
  254. * @param Exception $exception
  255. *
  256. * @param object $class 类
  257. * @param string $method 方法
  258. * @param array $arguments 参数
  259. *
  260. * @return mixed
  261. * @throws Exception
  262. */
  263. public function exception($exception, $class, $method = '', $arguments = []) {
  264. if (false !== strpos($exception->getMessage(), 'Base table or view not found')) {
  265. $this->isTableExist();
  266. $this->genDefaultData();
  267. return call_user_func_array([$class, $method], $arguments);
  268. }
  269. switch ($exception->getCode()) {
  270. case 10501:
  271. /* PDO异常处理 */
  272. case 10500:
  273. /* Database相关异常处理类 */
  274. case 10502:
  275. /* PDO参数绑定异常 */
  276. default:
  277. }
  278. throw $exception;
  279. }
  280. /**
  281. * 判断表中字段是否存在
  282. *
  283. * @param string $table 表名
  284. *
  285. * @return bool
  286. */
  287. public function isTableExist($table = '') {
  288. $_table_name = $table;
  289. if (empty($_table_name)) {
  290. $_table_name = $this->table;
  291. if (!empty($this->partition_field)) {
  292. $_table_name = $this->getPartitionTableName(
  293. $this->partition_data, $this->partition_field, $this->partition_rule
  294. );
  295. }
  296. }
  297. $_cache_key = $this->getTableCacheKey($_table_name);
  298. $_data = Cache::get($_cache_key);
  299. if (1 == $_data) {
  300. return true;
  301. }
  302. /* 不存在表 */
  303. $_has_it = false;
  304. try {
  305. $_table_cnt = db()->query(
  306. "SELECT COUNT(*) as table_cnt FROM information_schema.tables WHERE table_schema = '"
  307. .$this->connection['database']."' AND table_name = '".$_table_name."';"
  308. );
  309. if (!empty($_table_cnt) && !empty($_table_cnt[0]) && !empty($_table_cnt[0]['table_cnt'])) {
  310. $_has_it = true;
  311. } else {
  312. $_rs = $this->checkTable();
  313. if (false !== $_rs) {
  314. $_has_it = true;
  315. }
  316. }
  317. } catch (BindParamException $e) {
  318. $_has_it = false;
  319. } catch (PDOException $e) {
  320. $_has_it = false;
  321. }
  322. if (true == $_has_it) {
  323. /* 永不过期 */
  324. Cache::set($_cache_key, 1, 0);
  325. }
  326. return $_has_it;
  327. }
  328. /**
  329. * 生成数据
  330. */
  331. public function genDefaultData() {
  332. return true;
  333. }
  334. /**
  335. * 通过条件获取主键
  336. *
  337. * @param array $where 条件
  338. *
  339. * @return int
  340. */
  341. public function getPkId($where = []) {
  342. $_map = $where;
  343. try {
  344. $_pk_value = $this->useGlobalScope(false)->where($_map)->value($this->pk);
  345. if (empty($_pk_value)) {
  346. return 0;
  347. }
  348. return $_pk_value;
  349. } catch (Exception $e) {
  350. return $this->exception($e, $this, __FUNCTION__, func_get_args());
  351. }
  352. }
  353. }