ModelTrait.php 10 KB

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