BaseMongoModel.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\DB;
  4. use Jenssegers\Mongodb\Eloquent\Model;
  5. class BaseMongoModel extends Model
  6. {
  7. protected $connection = 'mongodb';
  8. protected $db = null;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->db = DB::connection('mongodb');
  13. }
  14. /**
  15. * @function 公用查询model
  16. * @param array $param
  17. * @param array $fields 返回字段名数组['_id', 'res_count'],一维数组
  18. * @param array $sort
  19. * @param string $pageNo
  20. * @param string $perPage
  21. * @param array $group
  22. * @param array $lookup
  23. * @param array $unwind
  24. * @param array $match
  25. * @return array totalCount表示返回如果有分页总条数,data返回查询后的数据
  26. */
  27. public function getListByCondition(
  28. $param = [],
  29. $fields = [],
  30. $sort = [],
  31. $pageNo = '',
  32. $perPage = '',
  33. $group = [],
  34. $lookup = [],
  35. $unwind = [],
  36. $match = []
  37. ) {
  38. $result = [];
  39. $condition = [];
  40. //过滤条件,过滤字段
  41. if ($param) {
  42. $condition [] = $param;
  43. }
  44. //连表查询
  45. if ($lookup && is_array($lookup)) {
  46. $condition[] = ['$lookup' => $lookup];
  47. }
  48. if ($unwind && is_array($unwind)) {
  49. $condition[] = ['$unwind' => $unwind];
  50. }
  51. if ($match && is_array($match)) {
  52. $condition[] = ['$match' => $match];
  53. }
  54. //是否有分组过滤
  55. if ($group && is_array($group)) {
  56. $condition[] = ['$group' => $group];
  57. }
  58. //是否有排序
  59. if ($sort && is_array($sort)) {
  60. $condition[] = ['$sort' => $sort];
  61. }
  62. //是否有分页
  63. if ($pageNo) {
  64. //有分页需要获取总条数
  65. $totalCount = $this->statCountByCondition($condition);
  66. $result['totalCount'] = $totalCount;
  67. $perPage = $this->getPerPage($perPage);
  68. $pageNo = $this->getPage($pageNo);
  69. $condition[] = ['$skip' => (int)(($pageNo - 1) * $perPage)];
  70. $condition[] = ['$limit' => $perPage];
  71. }
  72. //字段过滤
  73. if ($fields && is_array($fields)) {
  74. $project = [];
  75. foreach ($fields as $value) {
  76. $project[$value] = 1;
  77. }
  78. $condition[] = ['$project' => $project];
  79. }
  80. $respData = $this->db->selectCollection($this->collection)->aggregate($condition)->toArray();
  81. $respData = $respData ? $respData : [];
  82. $result['data'] = $respData;
  83. return $result;
  84. }
  85. /**
  86. * @function 根据条件来获取相应的数据
  87. * @param array $param
  88. * @param array $fields
  89. * @param bool $is_one
  90. * @param string $sortField
  91. * @param string $sort
  92. * @return bool|mixed
  93. */
  94. public function getInfoByCondition($param = [], $fields = [], $is_one = false, $sortField = '', $sort = 'desc')
  95. {
  96. if (empty($param)) {
  97. return false;
  98. }
  99. if ($is_one) {
  100. $respData = $this->whereRaw($param)->first($fields);
  101. } else {
  102. if ($sortField) {
  103. $respData = $this->whereRaw($param)->orderBy($sortField, $sort)->get($fields);
  104. } else {
  105. $respData = $this->whereRaw($param)->get($fields);
  106. }
  107. }
  108. return $respData ? json_decode($respData, true) : [];
  109. }
  110. /**
  111. * @function 根据条件统计条数
  112. * @param array $param
  113. * @param array $field
  114. * @return int
  115. */
  116. public function statCountByCondition($param = [], $field = ['$count' => 'id'])
  117. {
  118. $condition = $param;
  119. $condition[] = $field;
  120. $result = $this->db->selectCollection($this->collection)->aggregate($condition)->toArray();
  121. $resCount = 0;
  122. if ($result) {
  123. foreach ($result as $value) {
  124. foreach ($value as $item) {
  125. $resCount = $item;
  126. }
  127. }
  128. }
  129. return $resCount;
  130. }
  131. /**
  132. * @function 更新数据updateData
  133. * @param array $condition
  134. * @param array $updateData
  135. * @param bool $isOne
  136. * @return bool
  137. */
  138. public function updateData($condition = [], $updateData = [], $isOne = true)
  139. {
  140. if (!$updateData || !is_array($updateData)) {
  141. return false;
  142. }
  143. if ($isOne) {
  144. return $this->db->selectCollection($this->collection)->updateOne($condition, $updateData);
  145. } else {
  146. return $this->db->selectCollection($this->collection)->updateMany($condition, $updateData);
  147. }
  148. }
  149. /**
  150. * @function 插入数据
  151. * @param array $insertData 要插入的数据
  152. * @param bool $isOne 是否插入单条数据
  153. * @param bool $getId 是否返回插入ID(单条有效)
  154. * @return bool 返回数据
  155. */
  156. public function insertData($insertData = [], $isOne = true, $getId = false)
  157. {
  158. if (!$insertData || !is_array($insertData)) {
  159. return false;
  160. }
  161. //插入一条数据
  162. if ($isOne) {
  163. $result = $this->db->selectCollection($this->collection)->insertOne($insertData);
  164. if ($getId) {
  165. $result = $result->getInsertedId();
  166. }
  167. } else {
  168. $result = $this->db->selectCollection($this->collection)->insertMany($insertData);
  169. }
  170. return $result;
  171. }
  172. /**
  173. * 获取分页行数
  174. * @param $data
  175. * @return int
  176. */
  177. protected function getPageSize($data)
  178. {
  179. return $data['page_size'] ?? config('app.app_rows');
  180. }
  181. /**
  182. * 获取页码
  183. * @param $data
  184. * @return int
  185. */
  186. protected function getPageNo($data)
  187. {
  188. return $data['page'] ?? config('app.app_page_no');
  189. }
  190. /**
  191. * 获取查询偏移量
  192. * @param $data
  193. * @return int
  194. */
  195. protected function getSkip($page,$pageSize)
  196. {
  197. return ($page - 1) * $pageSize;;
  198. }
  199. /**
  200. * 获取分页参数
  201. * */
  202. protected function getPaginatorParams($params){
  203. $pageSize = $this->getPageSize($params);
  204. $page = $this->getPageNo($params);
  205. $skip = $this->getSkip($page,$pageSize);
  206. return array($pageSize, $page,$skip);
  207. }
  208. /**
  209. * @function 获取页数
  210. * @author lin
  211. * @param $pageNo
  212. * @return int
  213. */
  214. public function getPage($pageNo)
  215. {
  216. $pageNo = intval($pageNo);
  217. return $pageNo ? $pageNo : 1;
  218. }
  219. /**
  220. * @function 获取分页
  221. *
  222. * @author lin
  223. * @param string $perPage
  224. * @return int|mixed
  225. */
  226. public function getPerPage($perPage = '')
  227. {
  228. $perPage = intval($perPage);
  229. return $perPage ? $perPage : config('app.app_rows');
  230. }
  231. /**
  232. * 构造返回页面数据
  233. * @param $data //数据
  234. * @param $formNb //开始数量
  235. * @param $page //页数
  236. * @param $perPageSize //每页数量
  237. * @param $total //总数量
  238. * @return mixed
  239. */
  240. public function buildPaginator($data = [], $formNb = 0, $page = 1, $perPageSize = 10, $total = 0)
  241. {
  242. $lastPage = ceil($total / $perPageSize);
  243. $toNb = $page * $perPageSize;
  244. if (empty($formNb)) {
  245. $formNb = $toNb - $perPageSize;
  246. }
  247. if ($toNb > $total) {
  248. $toNb = $total;
  249. }
  250. $resultDdata = array(
  251. 'current_page' => $page,
  252. 'data' => $data,
  253. 'from' => $formNb,
  254. 'last_page' => $lastPage,
  255. 'per_page' => $perPageSize,
  256. 'to' => $toNb,
  257. 'total' => $total,
  258. );
  259. return $resultDdata;
  260. }
  261. }