BaseService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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['user_type']) && $param['user_type']) {
  96. $map[] = ['user_type', '=', $param['user_type']];
  97. }
  98. // 筛选身份认证
  99. if (isset($param['idcard_check']) && $param['idcard_check']) {
  100. $map[] = ['idcard_check', '=', $param['idcard_check']];
  101. }
  102. // 筛选状态
  103. if (isset($param['status']) && $param['status']) {
  104. $map[] = ['status', '=', $param['status']];
  105. }
  106. // 筛选状态
  107. if (isset($param['trade_status']) && $param['trade_status']) {
  108. $map[] = ['trade_status', '=', $param['trade_status']];
  109. }
  110. // 手机号码
  111. if (isset($param['mobile']) && $param['mobile']) {
  112. $map[] = ['mobile', '=', $param['mobile']];
  113. }
  114. // 位置
  115. if (isset($param['position']) && $param['position']) {
  116. $map[] = ['position', '=', $param['position']];
  117. }
  118. // 单号
  119. if (isset($param['order_no']) && $param['order_no']) {
  120. $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
  121. }
  122. // 待处理
  123. if (isset($param['catch']) && $param['catch']) {
  124. $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
  125. }
  126. }
  127. // 设置查询条件
  128. if (is_array($map)) {
  129. $map[] = ['mark', '=', 1];
  130. } elseif ($map) {
  131. $map .= " AND mark=1 ";
  132. } else {
  133. $map .= " mark=1 ";
  134. }
  135. // 排序(支持多重排序)
  136. $query = $this->model->where($map)->where(function($query) use($param){
  137. $businessId = isset($param['bsid'])? intval($param['bsid']) : 0;
  138. if($businessId>0){
  139. $query->where(['business_id'=> $businessId]);
  140. }
  141. $userType = isset($param['user_type'])? $param['user_type'] : 0;
  142. if($userType){
  143. $userType = explode(',', $userType);
  144. $query->whereIn('user_type', $userType);
  145. }
  146. })->when($sort, function ($query, $sort) {
  147. foreach ($sort as $v) {
  148. $query->orderBy($v[0], $v[1]);
  149. }
  150. });
  151. // 分页条件
  152. $offset = (PAGE - 1) * PERPAGE;
  153. $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
  154. $result = $result ? $result->toArray() : [];
  155. $list = [];
  156. if (is_array($result)) {
  157. foreach ($result as $val) {
  158. $info = $this->model->getInfo($val['id']);
  159. $list[] = $info;
  160. }
  161. }
  162. //获取数据总数
  163. $count = $this->model->where($map)->count();
  164. //返回结果
  165. $message = array(
  166. "msg" => '操作成功',
  167. "code" => 0,
  168. "data" => $list,
  169. "count" => $count,
  170. );
  171. return $message;
  172. }
  173. /**
  174. * 获取记录详情
  175. * @return array
  176. * @since 2020/11/11
  177. * @author laravel开发员
  178. */
  179. public function info()
  180. {
  181. // 记录ID
  182. $id = request()->input("id", 0);
  183. $info = [];
  184. if ($id) {
  185. $info = $this->model->getInfo($id);
  186. }
  187. return message(MESSAGE_OK, true, $info);
  188. }
  189. /**
  190. * 添加或编辑记录
  191. * @return array
  192. * @since 2020/11/11
  193. * @author laravel开发员
  194. */
  195. public function edit()
  196. {
  197. // 获取参数
  198. $argList = func_get_args();
  199. // 查询条件
  200. $data = isset($argList[0]) ? $argList[0] : [];
  201. // 是否打印SQL
  202. $is_sql = isset($argList[1]) ? $argList[1] : false;
  203. if (!$data) {
  204. $data = request()->all();
  205. }
  206. $error = '';
  207. $rowId = $this->model->edit($data, $error, $is_sql);
  208. if ($rowId) {
  209. return message();
  210. }
  211. return message($error, false);
  212. }
  213. /**
  214. * 删除记录
  215. * @return array
  216. * @since 2020/11/12
  217. * @author laravel开发员
  218. */
  219. public function delete()
  220. {
  221. // 参数
  222. $param = request()->all();
  223. // 记录ID
  224. $ids = getter($param, "id");
  225. if (empty($ids)) {
  226. return message("记录ID不能为空", false);
  227. }
  228. if (is_array($ids)) {
  229. // 批量删除
  230. $result = $this->model->deleteAll($ids);
  231. if (!$result) {
  232. return message("删除失败", false);
  233. }
  234. return message("删除成功");
  235. } else {
  236. // 单个删除
  237. $info = $this->model->getInfo($ids);
  238. if ($info) {
  239. $result = $this->model->drop($ids);
  240. if ($result !== false) {
  241. return message();
  242. }
  243. }
  244. return message($this->model->getError(), false);
  245. }
  246. }
  247. /**
  248. * 验证数据是否已经存在
  249. * @param $field 字段名
  250. * @param $value 字段值
  251. * @param string $pk 键名
  252. * @return mixed
  253. */
  254. public function checkExists($field, $value, $pk='id',$status=1)
  255. {
  256. $where = [$field=> $value,'mark'=>1];
  257. if($status>0){
  258. $where['status'] = $status;
  259. }
  260. return $this->model->where($where)->value($pk);
  261. }
  262. /**
  263. * 设置记录状态
  264. * @return array
  265. * @since 2020/11/11
  266. * @author laravel开发员
  267. */
  268. public function status()
  269. {
  270. $data = request()->all();
  271. if (!$data['id']) {
  272. return message('记录ID不能为空', false);
  273. }
  274. if (!$data['status']) {
  275. return message('记录状态不能为空', false);
  276. }
  277. $error = '';
  278. $item = [
  279. 'id' => $data['id'],
  280. 'status' => $data['status']
  281. ];
  282. $rowId = $this->model->edit($item, $error);
  283. if (!$rowId) {
  284. return message($error, false);
  285. }
  286. return message();
  287. }
  288. }