ModelTrait.php 9.5 KB

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