Service.tpl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\{module}\Services;
  3. use App\Base\Exceptions\ApiException;
  4. use App\Base\Services\BaseService;
  5. use App\{module}\Models\{action}Model;
  6. use App\Log\Facades\LogAdminOperationFacade;
  7. class {action}Service extends BaseService
  8. {
  9. /**
  10. * {action}Service constructor.
  11. * @param {action}Model $model
  12. */
  13. public function __construct({action}Model $model)
  14. {
  15. $this->model = $model;
  16. }
  17. public function lists($request){
  18. $params = $request->all();
  19. $limit = $this->getPageSize($params);
  20. $page = $params['page'] ?? 1;
  21. $map = [];
  22. $OrWhere = [];
  23. $status = $params['status'] ?? '';
  24. if($status == '') {
  25. $map['a.status'] = [['in', [0, 1]]];
  26. }else{
  27. $map['a.status'] = $status;
  28. }
  29. $model = $this->model->newInstance()->alias('a')
  30. ->where(function($q)use($OrWhere){
  31. foreach ($OrWhere as $or){
  32. $q->OrWhere($or[0],$or[1],$or[2]);
  33. }
  34. })
  35. ->buildQuery($map);
  36. $counts = $model->count();
  37. $list = $model->forPage($page, $limit)
  38. ->orderBy('id', 'desc')
  39. ->get()->toArray();
  40. $data = $this->paginator($list, $counts);
  41. return $data;
  42. }
  43. public function saveInfo($request){
  44. $params = $request->all();
  45. $id = $params['id'];
  46. //无ID则为新增
  47. $params['update_id'] = $this->getAuthAdminId();
  48. $log = [];
  49. $log['admin_id'] = $params['update_id'];
  50. $log['ip'] = getClientIp();
  51. $log['url'] = $request->getRequestUri();
  52. if($id){
  53. $this->updateBy(['id' => $id],$params);
  54. $log['operation'] = '更新数据:' . json_encode($params, JSON_UNESCAPED_UNICODE);
  55. }else{
  56. $params['create_id'] = $params['update_id'];
  57. $id = $this->save($params)->id;
  58. $log['operation'] = '新增数据:id => '.$id.' =>'.json_encode($params, JSON_UNESCAPED_UNICODE);
  59. }
  60. LogAdminOperationFacade::addOperationLog($log);
  61. return $id;
  62. }
  63. public function changeStatus($request){
  64. $params = $request->all();
  65. $model = $this->findOneById($params['id']);
  66. if(!$model){
  67. throw new ApiException('common.no_records', '没有找到相关的记录');
  68. }
  69. $params['update_id'] = $this->getAuthAdminId();
  70. $ret = $this->updateBy(['id' => $params['id']],['status' => $params['status'],'update_id' => $params['update_id']]);
  71. $log = [];
  72. $log['admin_id'] = $params['update_id'];
  73. $log['ip'] = getClientIp();
  74. $log['url'] = $request->getRequestUri();
  75. $log['operation'] = '更新状态:' . json_encode($params, JSON_UNESCAPED_UNICODE);
  76. LogAdminOperationFacade::addOperationLog($log);
  77. return $ret;
  78. }
  79. public function deleteInfo($request){
  80. $ids = $request->input('ids', 0);
  81. if (!is_array($ids)) {
  82. $ids = explode(',', $ids);
  83. }
  84. $map = ['id' => [['in', $ids]]];
  85. $this->deleteBy($map, ['update_id' => $this->getAuthAdminId()]);
  86. $log = [];
  87. $log['admin_id'] = $this->getAuthAdminId();
  88. $log['ip'] = getClientIp();
  89. $log['url'] = $request->getRequestUri();
  90. $log['operation'] = '删除数据:ids => ' . implode(',',$ids);
  91. LogAdminOperationFacade::addOperationLog($log);
  92. return 1;
  93. }
  94. public function detail($request)
  95. {
  96. $id = $request->input('id', 0);
  97. $info = $this->findOneById($id);
  98. if ($info) {
  99. $info = $info->toArray();
  100. }
  101. $log = [];
  102. $log['admin_id'] = $this->getAuthAdminId();
  103. $log['ip'] = getClientIp();
  104. $log['url'] = $request->getRequestUri();
  105. $log['operation'] = '查看详情:id => ' . $id;
  106. LogAdminOperationFacade::addOperationLog($log);
  107. return $info;
  108. }
  109. }