ModelTrait.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace traits;
  12. use think\db\Query;
  13. use think\Model;
  14. trait ModelTrait
  15. {
  16. /**
  17. * 添加一条数据
  18. * @param $data
  19. * @return object $model 数据对象
  20. */
  21. public static function set($data)
  22. {
  23. return self::create($data);
  24. }
  25. /**
  26. * 添加多条数据
  27. * @param $group
  28. * @param bool $replace
  29. * @return mixed
  30. */
  31. public static function setAll($group, $replace = false)
  32. {
  33. return self::insertAll($group,$replace);
  34. }
  35. /**
  36. * 修改一条数据
  37. * @param $data
  38. * @param $id
  39. * @param $field
  40. * @return bool $type 返回成功失败
  41. */
  42. public static function edit($data,$id,$field = null)
  43. {
  44. $model = new self;
  45. if(!$field) $field = $model->getPk();
  46. return false !== $model->update($data,[$field=>$id]);
  47. }
  48. /**
  49. * 查询一条数据是否存在
  50. * @param $map
  51. * @param string $field
  52. * @return bool 是否存在
  53. */
  54. public static function be($map, $field = '')
  55. {
  56. $model = (new self);
  57. if(!is_array($map) && empty($field)) $field = $model->getPk();
  58. $map = !is_array($map) ? [$field=>$map] : $map;
  59. return 0 < $model->where($map)->count();
  60. }
  61. /**
  62. * 删除一条数据
  63. * @param $id
  64. * @return bool $type 返回成功失败
  65. */
  66. public static function del($id)
  67. {
  68. return false !== self::destroy($id);
  69. }
  70. /**
  71. * 分页
  72. * @param null $model 模型
  73. * @param null $eachFn 处理结果函数
  74. * @param array $params 分页参数
  75. * @param int $limit 分页数
  76. * @return array
  77. */
  78. public static function page($model = null, $eachFn = null, $params = [], $limit = 20)
  79. {
  80. if(is_numeric($eachFn) && is_numeric($model)){
  81. return parent::page($model,$eachFn);
  82. }
  83. if(is_numeric($eachFn)){
  84. $limit = $eachFn;
  85. $eachFn = null;
  86. }else if(is_array($eachFn)){
  87. $params = $eachFn;
  88. $eachFn = null;
  89. }
  90. if(is_callable($model)){
  91. $eachFn = $model;
  92. $model = null;
  93. }elseif(is_numeric($model)){
  94. $limit = $model;
  95. $model = null;
  96. }elseif(is_array($model)){
  97. $params = $model;
  98. $model = null;
  99. }
  100. if(is_numeric($params)){
  101. $limit = $params;
  102. $params = [];
  103. }
  104. $paginate = $model === null ? self::paginate($limit,false,['query'=>$params]) : $model->paginate($limit,false,['query'=>$params]);
  105. $list = is_callable($eachFn) ? $paginate->each($eachFn) : $paginate;
  106. $page = $list->render();
  107. $total = $list->total();
  108. return compact('list','page','total');
  109. }
  110. /**
  111. * 获取分页 生成where 条件和 whereOr 支持多表查询生成条件
  112. * @param object $model 模型对象
  113. * @param array $where 需要检索的数组
  114. * @param array $field where字段名
  115. * @param array $fieldOr whereOr字段名
  116. * @param array $fun 闭包函数
  117. * @param string $like 模糊查找 关键字
  118. * @return array
  119. */
  120. public static function setWherePage($model=null,$where=[],$field=[],$fieldOr=[],$fun=null,$like='LIKE'){
  121. if(!is_array($where) || !is_array($field)) return false;
  122. if($model===null) $model=new self();
  123. //处理等于行查询
  124. foreach ($field as $key=>$item){
  125. if(($count=strpos($item,'.'))===false){
  126. if(isset($where[$item]) && $where[$item]!='') {
  127. $model=$model->where($item,$where[$item]);
  128. }
  129. }else{
  130. $item_l=substr($item,$count+1);
  131. if(isset($where[$item_l]) && $where[$item_l]!=''){
  132. $model=$model->where($item,$where[$item_l]);
  133. }
  134. }
  135. }
  136. //回收变量
  137. unset($count,$key,$item,$item_l);
  138. //处理模糊查询
  139. if(!empty($fieldOr) && is_array($fieldOr) && isset($fieldOr[0])){
  140. if(($count=strpos($fieldOr[0],'.'))===false){
  141. if(isset($where[$fieldOr[0]]) && $where[$fieldOr[0]]!='') {
  142. $model=$model->where(self::get_field($fieldOr),$like,"%".$where[$fieldOr[0]]."%");
  143. }
  144. }else{
  145. $item_l = substr($fieldOr[0],$count+1);
  146. if(isset($where[$item_l]) && $where[$item_l]!='') {
  147. $model=$model->where(self::get_field($fieldOr),$like,"%".$where[$item_l]."%");
  148. }
  149. }
  150. }
  151. unset($count,$key,$item,$item_l);
  152. return $model;
  153. }
  154. /**
  155. * 字符串拼接
  156. * @param int|array $id
  157. * @param string $str
  158. * @return string
  159. */
  160. private static function get_field($id,$str='|'){
  161. if(is_array($id)){
  162. $sql="";
  163. $i=0;
  164. foreach($id as $val){
  165. $i++;
  166. if($i<count($id)){
  167. $sql.=$val.$str;
  168. }else{
  169. $sql.=$val;
  170. }
  171. }
  172. return $sql;
  173. }else{
  174. return $id;
  175. }
  176. }
  177. /**
  178. * 条件切割
  179. * @param string $order
  180. * @param string $file
  181. * @return string
  182. */
  183. public static function setOrder($order,$file='-'){
  184. if(empty($order)) return '';
  185. return str_replace($file,' ',$order);
  186. }
  187. /**
  188. * 获取时间段之间的model
  189. * @param int|string $time
  190. * @param string $ceil
  191. * @return array
  192. */
  193. public static function getModelTime($where,$model=null,$prefix='add_time',$data='data',$field=' - '){
  194. if ($model == null) $model = new self;
  195. if(!isset($where[$data])) return $model;
  196. switch ($where[$data]){
  197. case 'today':case 'week':case 'month':case 'year':case 'yesterday':
  198. $model=$model->whereTime($prefix,$where[$data]);
  199. break;
  200. case 'quarter':
  201. list($startTime,$endTime)=self::getMonth();
  202. $model = $model->where($prefix, '>', strtotime($startTime));
  203. $model = $model->where($prefix, '<', strtotime($endTime));
  204. break;
  205. default:
  206. if(strstr($where[$data],$field)!==false){
  207. list($startTime, $endTime) = explode($field, $where[$data]);
  208. if($startTime && $endTime) {
  209. $model = $model->where($prefix, '>', strtotime($startTime));
  210. $model = $model->where($prefix, '<', strtotime($endTime));
  211. }
  212. }
  213. break;
  214. }
  215. return $model;
  216. }
  217. /**
  218. * 获取去除html去除空格去除软回车,软换行,转换过后的字符串
  219. * @param string $str
  220. * @return string
  221. */
  222. public static function HtmlToMbStr($str){
  223. return trim(strip_tags(str_replace(["\n","\t","\r"," ","&nbsp;"],'',htmlspecialchars_decode($str))));
  224. }
  225. /**
  226. * 截取中文指定字节
  227. * @param string $str
  228. * @param int $utf8len
  229. * @param string $chaet
  230. * @param string $file
  231. * @return string
  232. */
  233. public static function getSubstrUTf8($str,$utf8len=100,$chaet='UTF-8',$file='....'){
  234. if(mb_strlen($str,$chaet)>$utf8len){
  235. $str=mb_substr($str,0,$utf8len,$chaet).$file;
  236. }
  237. return $str;
  238. }
  239. /**
  240. * 获取本季度 time
  241. * @param int|string $time
  242. * @param string $ceil
  243. * @return array
  244. */
  245. public static function getMonth($time='',$ceil=0){
  246. if($ceil!=0)
  247. $season = ceil(date('n') /3)-$ceil;
  248. else
  249. $season = ceil(date('n') /3);
  250. $firstday=date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
  251. $lastday=date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));
  252. return array($firstday,$lastday);
  253. }
  254. /**
  255. * 高精度 加法
  256. * @param int|string $uid id
  257. * @param string $decField 相加的字段
  258. * @param float|int $dec 加的值
  259. * @param string $keyField id的字段
  260. * @param int $acc 精度
  261. * @return bool
  262. */
  263. public static function bcInc($key, $incField, $inc, $keyField = null, $acc=2)
  264. {
  265. if(!is_numeric($inc)) return false;
  266. $model = new self();
  267. if($keyField === null) $keyField = $model->getPk();
  268. $result = self::where($keyField,$key)->find();
  269. if(!$result) return false;
  270. $new = bcadd($result[$incField],$inc,$acc);
  271. return false !== $model->where($keyField,$key)->update([$incField=>$new]);
  272. }
  273. /**
  274. * 高精度 减法
  275. * @param int|string $uid id
  276. * @param string $decField 相减的字段
  277. * @param float|int $dec 减的值
  278. * @param string $keyField id的字段
  279. * @param bool $minus 是否可以为负数
  280. * @param int $acc 精度
  281. * @return bool
  282. */
  283. public static function bcDec($key, $decField, $dec, $keyField = null, $minus = false, $acc=2)
  284. {
  285. if(!is_numeric($dec)) return false;
  286. $model = new self();
  287. if($keyField === null) $keyField = $model->getPk();
  288. $result = self::where($keyField,$key)->find();
  289. if(!$result) return false;
  290. if(!$minus && $result[$decField] < $dec) return false;
  291. $new = bcsub($result[$decField],$dec,$acc);
  292. return false !== $model->where($keyField,$key)->update([$decField=>$new]);
  293. }
  294. /**
  295. * @param null $model
  296. * @return Model
  297. */
  298. protected static function getSelfModel($model = null)
  299. {
  300. return $model == null ? (new self()) : $model;
  301. }
  302. }