ModelTrait.php 9.5 KB

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