123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- /**
- * 服务基类
- * @author laravel开发员
- * @since 2020/11/10
- * Class BaseService
- * @package App\Services
- */
- class BaseService
- {
- // 模型
- protected $model;
- // 验证类
- protected $validate;
- // 错误信息
- protected $error = '1003';
- // 静态对象
- protected static $instance = null;
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获得错误信息
- * @return string
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * 获取数据列表
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function getList()
- {
- // 初始化变量
- $map = [];
- $sort = [['id', 'desc']];
- $is_sql = 0;
- // 获取参数
- $argList = func_get_args();
- if (!empty($argList)) {
- // 查询条件
- $map = (isset($argList[0]) && !empty($argList[0])) ? $argList[0] : [];
- // 排序
- $sort = (isset($argList[1]) && !empty($argList[1])) ? $argList[1] : [['id', 'desc']];
- // 是否打印SQL
- $is_sql = isset($argList[2]) ? isset($argList[2]) : 0;
- }
- // $is_sql = 1;
- // 打印SQL
- if ($is_sql) {
- $this->model->getLastSql(1);
- }
- // 常规查询条件
- $param = request()->input();
- if ($param) {
- // 筛选名称
- if (isset($param['name']) && $param['name']) {
- $map[] = ['name', 'like', "%{$param['name']}%"];
- }
- // 筛选账号
- if (isset($param['username']) && $param['username']) {
- $map[] = ['username', 'like', "%{$param['username']}%"];
- }
- // 筛选标题
- if (isset($param['title']) && $param['title']) {
- $map[] = ['title', 'like', "%{$param['title']}%"];
- }
- // 筛选类型
- if (isset($param['type']) && $param['type']) {
- $map[] = ['type', '=', $param['type']];
- }
- // 筛选类型
- if (isset($param['user_type']) && $param['user_type']) {
- $map[] = ['user_type', '=', $param['user_type']];
- }
- // 筛选身份认证
- if (isset($param['idcard_check']) && $param['idcard_check']) {
- $map[] = ['idcard_check', '=', $param['idcard_check']];
- }
- // 筛选平台
- if (isset($param['api_id']) && $param['api_id']) {
- $map[] = ['api_id', '=', $param['api_id']];
- }
- // 筛选状态
- if (isset($param['status']) && $param['status']) {
- $map[] = ['status', '=', $param['status']];
- }
- // 筛选交易状态
- if (isset($param['trade_status']) && $param['trade_status']) {
- $map[] = ['trade_status', '=', $param['trade_status']];
- }
- // 手机号码
- if (isset($param['mobile']) && $param['mobile']) {
- $map[] = ['mobile', '=', $param['mobile']];
- }
- // 位置
- if (isset($param['position']) && $param['position']) {
- $map[] = ['position', '=', $param['position']];
- }
- // 单号
- if (isset($param['order_no']) && $param['order_no']) {
- $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
- }
- // 待处理
- if (isset($param['catch']) && $param['catch']) {
- $map[] = ['order_no', 'like', "%".trim($param['order_no'])."%"];
- }
- }
- // 设置查询条件
- if (is_array($map)) {
- $map[] = ['mark', '=', 1];
- } elseif ($map) {
- $map .= " AND mark=1 ";
- } else {
- $map .= " mark=1 ";
- }
- // 排序(支持多重排序)
- $query = $this->model->where($map)->where(function($query) use($param){
- $businessId = isset($param['bsid'])? intval($param['bsid']) : 0;
- if($businessId>0){
- $query->where(['business_id'=> $businessId]);
- }
- $userType = isset($param['user_type'])? $param['user_type'] : 0;
- if($userType){
- $userType = explode(',', $userType);
- $query->whereIn('user_type', $userType);
- }
- })->when($sort, function ($query, $sort) {
- foreach ($sort as $v) {
- $query->orderBy($v[0], $v[1]);
- }
- });
- // 分页条件
- $offset = (PAGE - 1) * PERPAGE;
- $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
- $result = $result ? $result->toArray() : [];
- $list = [];
- if (is_array($result)) {
- foreach ($result as $val) {
- $info = $this->model->getInfo($val['id']);
- $list[] = $info;
- }
- }
- //获取数据总数
- $count = $this->model->where($map)->count();
- //返回结果
- $message = array(
- "msg" => '操作成功',
- "code" => 0,
- "data" => $list,
- "count" => $count,
- );
- return $message;
- }
- /**
- * 获取记录详情
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function info()
- {
- // 记录ID
- $id = request()->input("id", 0);
- $info = [];
- if ($id) {
- $info = $this->model->getInfo($id);
- }
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 添加或编辑记录
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 获取参数
- $argList = func_get_args();
- // 查询条件
- $data = isset($argList[0]) ? $argList[0] : [];
- // 是否打印SQL
- $is_sql = isset($argList[1]) ? $argList[1] : false;
- if (!$data) {
- $data = request()->all();
- }
- $error = '';
- $rowId = $this->model->edit($data, $error, $is_sql);
- if ($rowId) {
- return message();
- }
- return message($error, false);
- }
- /**
- * 删除记录
- * @return array
- * @since 2020/11/12
- * @author laravel开发员
- */
- public function delete()
- {
- // 参数
- $param = request()->all();
- // 记录ID
- $ids = getter($param, "id");
- if (empty($ids)) {
- return message("记录ID不能为空", false);
- }
- $this->model->where(['mark'=>0])->where('update_time','<=',time() - 3600)->delete();
- if (is_array($ids)) {
- // 批量删除
- $result = $this->model->deleteAll($ids);
- if (!$result) {
- return message("删除失败", false);
- }
- return message("删除成功");
- } else {
- // 单个删除
- $info = $this->model->getInfo($ids);
- if ($info) {
- $result = $this->model->drop($ids);
- if ($result !== false) {
- return message();
- }
- }
- return message($this->model->getError(), false);
- }
- }
- /**
- * 验证数据是否已经存在
- * @param $field 字段名
- * @param $value 字段值
- * @param string $pk 键名
- * @return mixed
- */
- public function checkExists($field, $value, $pk='id',$status=1)
- {
- $where = [$field=> $value,'mark'=>1];
- if($status>0){
- $where['status'] = $status;
- }
- return $this->model->where($where)->value($pk);
- }
- /**
- * 设置记录状态
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function status()
- {
- $data = request()->all();
- if (!$data['id']) {
- return message('记录ID不能为空', false);
- }
- if (!$data['status']) {
- return message('记录状态不能为空', false);
- }
- $error = '';
- $item = [
- 'id' => $data['id'],
- 'status' => $data['status']
- ];
- $rowId = $this->model->edit($item, $error);
- if (!$rowId) {
- return message($error, false);
- }
- return message();
- }
- }
|