123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- declare (strict_types=1);
- namespace library\traits;
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // | Author: TABLE ME
- // +----------------------------------------------------------------------
- // | Date: 2020-08-29 16:03
- // +----------------------------------------------------------------------
- use think\Model;
- trait ModelTrait
- {
- public static function get($where)
- {
- if (!is_array($where)) {
- return self::find($where);
- } else {
- return self::where($where)->find();
- }
- }
- public static function all($function)
- {
- $query = self::newQuery();
- $function($query);
- return $query->select();
- }
- /**
- * 查询一条数据是否存在
- * @param $map
- * @param string $field
- * @return bool 是否存在
- */
- public static function be($map, $field = '')
- {
- $model = (new self);
- if (!is_array($map) && empty($field)) $field = $model->getPk();
- $map = !is_array($map) ? [$field => $map] : $map;
- return 0 < $model->where($map)->count();
- }
- /**
- * 删除一条数据
- * @param $id
- * @return bool $type 返回成功失败
- */
- public static function del($id)
- {
- return false !== self::destroy($id);
- }
- /**
- * 获取去除html去除空格去除软回车,软换行,转换过后的字符串
- * @param string $str
- * @return string
- */
- public static function HtmlToMbStr($str)
- {
- return trim(strip_tags(str_replace(["\n", "\t", "\r", " ", " "], '', htmlspecialchars_decode($str))));
- }
- /**
- * 截取中文指定字节
- * @param string $str
- * @param int $utf8len
- * @param string $chaet
- * @param string $file
- * @return string
- */
- public static function getSubstrUTf8($str, $utf8len = 100, $chaet = 'UTF-8', $file = '....')
- {
- if (mb_strlen($str, $chaet) > $utf8len) {
- $str = mb_substr($str, 0, $utf8len, $chaet) . $file;
- }
- return $str;
- }
- /**
- * 获取本季度 time
- * @param int|string $time
- * @param string $ceil
- * @return array
- */
- public static function getMonth($time = '', $ceil = 0)
- {
- if ($ceil != 0)
- $season = ceil(date('n') / 3) - $ceil;
- else
- $season = ceil(date('n') / 3);
- $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
- $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
- return array($firstday, $lastday);
- }
- /**
- * 高精度 加法
- * @param int|string $uid id
- * @param string $decField 相加的字段
- * @param float|int $dec 加的值
- * @param string $keyField id的字段
- * @param int $acc 精度
- * @return bool
- */
- public static function bcInc($key, $incField, $inc, $keyField = null, $acc = 2)
- {
- if (!is_numeric($inc)) return false;
- $model = new self();
- if ($keyField === null) $keyField = $model->getPk();
- $result = self::where($keyField, $key)->find();
- if (!$result) return false;
- $new = bcadd($result[$incField], $inc, $acc);
- $result->$incField = $new;
- return false !== $result->save();
- // return false !== $model->where($keyField, $key)->update([$incField => $new]);
- }
- /**
- * 高精度 减法
- * @param int|string $uid id
- * @param string $decField 相减的字段
- * @param float|int $dec 减的值
- * @param string $keyField id的字段
- * @param bool $minus 是否可以为负数
- * @param int $acc 精度
- * @return bool
- */
- public static function bcDec($key, $decField, $dec, $keyField = null, $minus = false, $acc = 2)
- {
- if (!is_numeric($dec)) return false;
- $model = new self();
- if ($keyField === null) $keyField = $model->getPk();
- $result = self::where($keyField, $key)->find();
- if (!$result) return false;
- if (!$minus && $result[$decField] < $dec) return false;
- $new = bcsub($result[$decField], $dec, $acc);
- $result->$decField = $new;
- return false !== $result->save();
- }
- /**
- * @param null $model
- * @return Model
- */
- protected static function getSelfModel($model = null)
- {
- return $model == null ? (new self()) : $model;
- }
- }
|