BaseService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. /**
  13. * 服务基类
  14. * @author laravel开发员
  15. * @since 2020/11/10
  16. * Class BaseService
  17. * @package App\Services
  18. */
  19. class BaseService
  20. {
  21. // 模型
  22. protected $model;
  23. // 验证类
  24. protected $validate;
  25. // 错误信息
  26. protected $error = '1003';
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 静态入口
  31. * @return static|null
  32. */
  33. public static function make()
  34. {
  35. if(!self::$instance){
  36. self::$instance = (new static());
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * 获得错误信息
  42. * @return string
  43. */
  44. public function getError()
  45. {
  46. return $this->error;
  47. }
  48. /**
  49. * 获取数据列表
  50. * @return array
  51. * @since 2020/11/11
  52. * @author laravel开发员
  53. */
  54. public function getList()
  55. {
  56. // 初始化变量
  57. $map = [];
  58. $sort = [['id', 'desc']];
  59. $is_sql = 0;
  60. // 获取参数
  61. $argList = func_get_args();
  62. if (!empty($argList)) {
  63. // 查询条件
  64. $map = (isset($argList[0]) && !empty($argList[0])) ? $argList[0] : [];
  65. // 排序
  66. $sort = (isset($argList[1]) && !empty($argList[1])) ? $argList[1] : [['id', 'desc']];
  67. // 是否打印SQL
  68. $is_sql = isset($argList[2]) ? isset($argList[2]) : 0;
  69. }
  70. // $is_sql = 1;
  71. // 打印SQL
  72. if ($is_sql) {
  73. $this->model->getLastSql(1);
  74. }
  75. // 常规查询条件
  76. $param = request()->input();
  77. if ($param) {
  78. // 筛选名称
  79. if (isset($param['name']) && $param['name']) {
  80. $map[] = ['name', 'like', "%{$param['name']}%"];
  81. }
  82. // 筛选账号
  83. if (isset($param['username']) && $param['username']) {
  84. $map[] = ['username', 'like', "%{$param['username']}%"];
  85. }
  86. // 筛选标题
  87. if (isset($param['title']) && $param['title']) {
  88. $map[] = ['title', 'like', "%{$param['title']}%"];
  89. }
  90. // 筛选类型
  91. if (isset($param['type']) && $param['type']) {
  92. $map[] = ['type', '=', $param['type']];
  93. }
  94. // 筛选状态
  95. if (isset($param['status']) && $param['status']) {
  96. $map[] = ['status', '=', $param['status']];
  97. }
  98. // 手机号码
  99. if (isset($param['mobile']) && $param['mobile']) {
  100. $map[] = ['mobile', '=', $param['mobile']];
  101. }
  102. // 位置
  103. if (isset($param['position']) && $param['position']) {
  104. $map[] = ['position', '=', $param['position']];
  105. }
  106. // 单号
  107. if (isset($param['order_no']) && $param['order_no']) {
  108. $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
  109. }
  110. // 待处理
  111. if (isset($param['catch']) && $param['catch']) {
  112. $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
  113. }
  114. }
  115. // 设置查询条件
  116. if (is_array($map)) {
  117. $map[] = ['mark', '=', 1];
  118. } elseif ($map) {
  119. $map .= " AND mark=1 ";
  120. } else {
  121. $map .= " mark=1 ";
  122. }
  123. // 排序(支持多重排序)
  124. $query = $this->model->where($map)->where(function($query) use($param){
  125. $businessId = isset($param['bsid'])? intval($param['bsid']) : 0;
  126. if($businessId>0){
  127. $query->where(['business_id'=> $businessId]);
  128. }
  129. })->when($sort, function ($query, $sort) {
  130. foreach ($sort as $v) {
  131. $query->orderBy($v[0], $v[1]);
  132. }
  133. });
  134. // 分页条件
  135. $offset = (PAGE - 1) * PERPAGE;
  136. $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
  137. $result = $result ? $result->toArray() : [];
  138. $list = [];
  139. if (is_array($result)) {
  140. foreach ($result as $val) {
  141. $info = $this->model->getInfo($val['id']);
  142. $list[] = $info;
  143. }
  144. }
  145. //获取数据总数
  146. $count = $this->model->where($map)->count();
  147. //返回结果
  148. $message = array(
  149. "msg" => '操作成功',
  150. "code" => 0,
  151. "data" => $list,
  152. "count" => $count,
  153. );
  154. return $message;
  155. }
  156. /**
  157. * 获取记录详情
  158. * @return array
  159. * @since 2020/11/11
  160. * @author laravel开发员
  161. */
  162. public function info()
  163. {
  164. // 记录ID
  165. $id = request()->input("id", 0);
  166. $info = [];
  167. if ($id) {
  168. $info = $this->model->getInfo($id);
  169. }
  170. return message(MESSAGE_OK, true, $info);
  171. }
  172. /**
  173. * 添加或编辑记录
  174. * @return array
  175. * @since 2020/11/11
  176. * @author laravel开发员
  177. */
  178. public function edit()
  179. {
  180. // 获取参数
  181. $argList = func_get_args();
  182. // 查询条件
  183. $data = isset($argList[0]) ? $argList[0] : [];
  184. // 是否打印SQL
  185. $is_sql = isset($argList[1]) ? $argList[1] : false;
  186. if (!$data) {
  187. $data = request()->all();
  188. }
  189. $error = '';
  190. $rowId = $this->model->edit($data, $error, $is_sql);
  191. if ($rowId) {
  192. return message();
  193. }
  194. return message($error, false);
  195. }
  196. /**
  197. * 删除记录
  198. * @return array
  199. * @since 2020/11/12
  200. * @author laravel开发员
  201. */
  202. public function delete()
  203. {
  204. // 参数
  205. $param = request()->all();
  206. // 记录ID
  207. $ids = getter($param, "id");
  208. if (empty($ids)) {
  209. return message("记录ID不能为空", false);
  210. }
  211. if (is_array($ids)) {
  212. // 批量删除
  213. $result = $this->model->deleteAll($ids);
  214. if (!$result) {
  215. return message("删除失败", false);
  216. }
  217. return message("删除成功");
  218. } else {
  219. // 单个删除
  220. $info = $this->model->getInfo($ids);
  221. if ($info) {
  222. $result = $this->model->drop($ids);
  223. if ($result !== false) {
  224. return message();
  225. }
  226. }
  227. return message($this->model->getError(), false);
  228. }
  229. }
  230. /**
  231. * 验证数据是否已经存在
  232. * @param $field 字段名
  233. * @param $value 字段值
  234. * @param string $pk 键名
  235. * @return mixed
  236. */
  237. public function checkExists($field, $value, $pk='id')
  238. {
  239. return $this->model->where([$field=> $value,'status'=>1,'mark'=>1])->value($pk);
  240. }
  241. /**
  242. * 设置记录状态
  243. * @return array
  244. * @since 2020/11/11
  245. * @author laravel开发员
  246. */
  247. public function status()
  248. {
  249. $data = request()->all();
  250. if (!$data['id']) {
  251. return message('记录ID不能为空', false);
  252. }
  253. if (!$data['status']) {
  254. return message('记录状态不能为空', false);
  255. }
  256. $error = '';
  257. $item = [
  258. 'id' => $data['id'],
  259. 'status' => $data['status']
  260. ];
  261. $rowId = $this->model->edit($item, $error);
  262. if (!$rowId) {
  263. return message($error, false);
  264. }
  265. return message();
  266. }
  267. }