CommonModel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * CommonModel.php UTF-8
  4. * 公共Model
  5. *
  6. * @date : 2020/9/14 17:03
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : H5IOS 1.0
  11. */
  12. namespace huosdk\h5ios\core\model;
  13. use huolib\constant\CommonConst;
  14. use huosdk\h5ios\core\constant\CacheConst;
  15. use think\Cache;
  16. use think\Model;
  17. class CommonModel extends Model {
  18. protected $pk = 'id';
  19. // 关联模型过滤
  20. protected $relationFilter = [];
  21. protected $table_cache_key_prefix = CacheConst::CACHE_TABLE_FIELD_PREFIX;
  22. /**
  23. * 懒人函数
  24. *
  25. * @access public
  26. *
  27. * @param string $string 字符串
  28. *
  29. * @return
  30. */
  31. public function strToArr($string) {
  32. return is_string($string) ? explode(',', $string) : (array)$string;
  33. }
  34. /**
  35. * 懒人函数
  36. *
  37. * @access public
  38. *
  39. * @param array $arr 数组
  40. *
  41. * @return string
  42. */
  43. public function arrToStr($arr) {
  44. return is_array($arr) ? implode(',', $arr) : (string)$arr;
  45. }
  46. /**
  47. * 计算在哪张表
  48. *
  49. * @return \Think\Model
  50. */
  51. public function computeTable() {
  52. return $this;
  53. }
  54. /**
  55. * @param $data
  56. * @param bool $replace
  57. * @param bool $get_last_insert_id
  58. *
  59. * @return bool|int|string
  60. */
  61. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  62. if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
  63. return $_id;
  64. } else {
  65. return false;
  66. }
  67. }
  68. /**
  69. * 添加数据
  70. *
  71. * @param array $data 需要添加的数据
  72. *
  73. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  74. */
  75. public function addData($data) {
  76. $this->checkField();
  77. if (empty($data)) {
  78. return false;
  79. }
  80. $_data = $data;
  81. $_model = new static();
  82. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  83. if (false !== $_rs) {
  84. return $_model->getLastInsID();
  85. }
  86. return false;
  87. }
  88. /**
  89. * 通过ID获取信息
  90. *
  91. * @param int $id 主键ID
  92. *
  93. * @return array|false
  94. */
  95. public function getInfoById($id) {
  96. $this->checkField();
  97. $_map[$this->pk] = $id;
  98. $_info = $this->useGlobalScope(false)->where($_map)->find();
  99. if (false === $_info) {
  100. return false;
  101. }
  102. if (is_object($_info)) {
  103. return $_info->toArray();
  104. } else {
  105. return $_info;
  106. }
  107. }
  108. /**
  109. * 更新数据
  110. *
  111. * @param array $data 数据
  112. * @param int|array $id 主集ID
  113. *
  114. * @return bool
  115. */
  116. public function updateData($data, $id) {
  117. $this->checkField();
  118. if (is_array($id)) {
  119. $_map[$this->pk] = ['in', $id];
  120. } else {
  121. $_map[$this->pk] = $id;
  122. }
  123. $_data = $data;
  124. $_model = new static();
  125. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  126. if (false === $_rs) {
  127. return false;
  128. } else {
  129. return true;
  130. }
  131. }
  132. /**
  133. * 删除数据
  134. *
  135. * @param array|int $ids ID合集
  136. * @param bool $is_complete 是否完成删除
  137. *
  138. * @return bool|int
  139. */
  140. public function deleteData($ids, $is_complete = false) {
  141. $this->checkField();
  142. if (true == is_array($ids)) {
  143. $_ids = $ids;
  144. } else {
  145. $_ids = [$ids];
  146. }
  147. $_map[$this->pk] = ['in', $_ids];
  148. if (true == $is_complete) {
  149. /* 彻底删除 */
  150. return $this->useGlobalScope(false)->where($_map)->delete();
  151. } else {
  152. $_data['is_delete'] = CommonConst::CONST_DELETED;
  153. $_data['delete_time'] = time();
  154. foreach ($_ids as $_id) {
  155. $_rs = self::updateData($_data, $_id);
  156. if (false === $_rs) {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. }
  163. /**当前模型名称(数据库表名,不带前缀)
  164. *
  165. * @return string
  166. */
  167. public function getName() {
  168. return $this->name;
  169. }
  170. /**
  171. * 通过条件获取数量
  172. *
  173. * @param array $where 条件
  174. *
  175. * @return int
  176. */
  177. public function getCnt($where = []) {
  178. $_map = $where;
  179. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  180. if (empty($_cnt)) {
  181. return 0;
  182. }
  183. return $_cnt;
  184. }
  185. /**
  186. * 通过条件获取汇总
  187. *
  188. * @param string $field 字段
  189. * @param array $where 条件
  190. *
  191. * @return double
  192. */
  193. public function getSum($field, $where = []) {
  194. $_map = $where;
  195. $_sum = $this->useGlobalScope(false)->where($_map)->sum($field);
  196. if (false == $_sum) {
  197. return false;
  198. }
  199. return $_sum;
  200. }
  201. /**
  202. * 判断表中字段是否存在
  203. *
  204. * @param string $table 表名
  205. * @param string $column 字段名
  206. *
  207. * @return bool
  208. */
  209. public function isColumnExist($table, $column) {
  210. $_cache_key = $this->table_cache_key_prefix.strtolower($table.$column);
  211. $_data = Cache::get($_cache_key);
  212. if (1 == $_data) {
  213. return true;
  214. }
  215. $_has_it = false;//是否存在该字段
  216. $_columns = db()->getTableFields($table);
  217. if (!empty($_columns)) {
  218. foreach ($_columns as $_k => $_v) {
  219. if ($_v == $column) {
  220. $_has_it = true;
  221. break;
  222. }
  223. }
  224. }
  225. if (true == $_has_it) {
  226. /* 永不过期 */
  227. Cache::set($_cache_key, 1, CommonConst::CONST_MAX_INT);
  228. }
  229. return $_has_it;
  230. }
  231. /**
  232. * 校验字段
  233. *
  234. * @return bool
  235. */
  236. protected function checkField() {
  237. return true;
  238. }
  239. }