ModelTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\traits;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-29 16:03
  12. // +----------------------------------------------------------------------
  13. use think\Model;
  14. trait ModelTrait
  15. {
  16. public static function get($where)
  17. {
  18. if (!is_array($where)) {
  19. return self::find($where);
  20. } else {
  21. return self::where($where)->find();
  22. }
  23. }
  24. public static function all($function)
  25. {
  26. $query = self::newQuery();
  27. $function($query);
  28. return $query->select();
  29. }
  30. /**
  31. * 查询一条数据是否存在
  32. * @param $map
  33. * @param string $field
  34. * @return bool 是否存在
  35. */
  36. public static function be($map, $field = '')
  37. {
  38. $model = (new self);
  39. if (!is_array($map) && empty($field)) $field = $model->getPk();
  40. $map = !is_array($map) ? [$field => $map] : $map;
  41. return 0 < $model->where($map)->count();
  42. }
  43. /**
  44. * 删除一条数据
  45. * @param $id
  46. * @return bool $type 返回成功失败
  47. */
  48. public static function del($id)
  49. {
  50. return false !== self::destroy($id);
  51. }
  52. /**
  53. * 获取去除html去除空格去除软回车,软换行,转换过后的字符串
  54. * @param string $str
  55. * @return string
  56. */
  57. public static function HtmlToMbStr($str)
  58. {
  59. return trim(strip_tags(str_replace(["\n", "\t", "\r", " ", "&nbsp;"], '', htmlspecialchars_decode($str))));
  60. }
  61. /**
  62. * 截取中文指定字节
  63. * @param string $str
  64. * @param int $utf8len
  65. * @param string $chaet
  66. * @param string $file
  67. * @return string
  68. */
  69. public static function getSubstrUTf8($str, $utf8len = 100, $chaet = 'UTF-8', $file = '....')
  70. {
  71. if (mb_strlen($str, $chaet) > $utf8len) {
  72. $str = mb_substr($str, 0, $utf8len, $chaet) . $file;
  73. }
  74. return $str;
  75. }
  76. /**
  77. * 获取本季度 time
  78. * @param int|string $time
  79. * @param string $ceil
  80. * @return array
  81. */
  82. public static function getMonth($time = '', $ceil = 0)
  83. {
  84. if ($ceil != 0)
  85. $season = ceil(date('n') / 3) - $ceil;
  86. else
  87. $season = ceil(date('n') / 3);
  88. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  89. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  90. return array($firstday, $lastday);
  91. }
  92. /**
  93. * 高精度 加法
  94. * @param int|string $uid id
  95. * @param string $decField 相加的字段
  96. * @param float|int $dec 加的值
  97. * @param string $keyField id的字段
  98. * @param int $acc 精度
  99. * @return bool
  100. */
  101. public static function bcInc($key, $incField, $inc, $keyField = null, $acc = 2)
  102. {
  103. if (!is_numeric($inc)) return false;
  104. $model = new self();
  105. if ($keyField === null) $keyField = $model->getPk();
  106. $result = self::where($keyField, $key)->find();
  107. if (!$result) return false;
  108. $new = bcadd($result[$incField], $inc, $acc);
  109. $result->$incField = $new;
  110. return false !== $result->save();
  111. // return false !== $model->where($keyField, $key)->update([$incField => $new]);
  112. }
  113. /**
  114. * 高精度 减法
  115. * @param int|string $uid id
  116. * @param string $decField 相减的字段
  117. * @param float|int $dec 减的值
  118. * @param string $keyField id的字段
  119. * @param bool $minus 是否可以为负数
  120. * @param int $acc 精度
  121. * @return bool
  122. */
  123. public static function bcDec($key, $decField, $dec, $keyField = null, $minus = false, $acc = 2)
  124. {
  125. if (!is_numeric($dec)) return false;
  126. $model = new self();
  127. if ($keyField === null) $keyField = $model->getPk();
  128. $result = self::where($keyField, $key)->find();
  129. if (!$result) return false;
  130. if (!$minus && $result[$decField] < $dec) return false;
  131. $new = bcsub($result[$decField], $dec, $acc);
  132. $result->$decField = $new;
  133. return false !== $result->save();
  134. }
  135. /**
  136. * @param null $model
  137. * @return Model
  138. */
  139. protected static function getSelfModel($model = null)
  140. {
  141. return $model == null ? (new self()) : $model;
  142. }
  143. /**
  144. * 修改一条数据
  145. * @param $data
  146. * @param $id
  147. * @param $field
  148. * @return bool $type 返回成功失败
  149. */
  150. public static function edit($data, $id, $field = null)
  151. {
  152. $model = new self;
  153. if (!$field) $field = $model->getPk();
  154. // return false !== $model->update($data,[$field=>$id]);
  155. // return 0 < $model->update($data,[$field=>$id])->result;
  156. $res = $model->update($data, [$field => $id]);
  157. if (isset($res->result))
  158. return 0 < $res->result;
  159. else if (isset($res['data']['result']))
  160. return 0 < $res['data']['result'];
  161. else
  162. return false !== $res;
  163. }
  164. }