PatientController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers\Admin\TCM;
  3. use App\Exports\PatientExport;
  4. use App\Exports\StudentExport;
  5. use App\Http\Controllers\Controller;
  6. use App\Repositories\Criteria\TCM\PatientCriteria;
  7. use App\Repositories\Enums\ResponseCodeEnum;
  8. use App\Repositories\Models\TCM\Patient;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Log;
  11. use Jiannei\Response\Laravel\Support\Facades\Response;
  12. use Overtrue\LaravelPinyin\Facades\Pinyin;
  13. use Prettus\Validator\Contracts\ValidatorInterface;
  14. use Prettus\Validator\Exceptions\ValidatorException;
  15. use App\Contracts\Repositories\TCM\PatientRepository;
  16. use App\Repositories\Validators\TCM\PatientValidator;
  17. /**
  18. * Class PatientsController.
  19. *
  20. * @package namespace App\Http\Controllers\TCM;
  21. */
  22. class PatientController extends Controller
  23. {
  24. /**
  25. * @var PatientRepository
  26. */
  27. protected $repository;
  28. /**
  29. * @var PatientValidator
  30. */
  31. protected $validator;
  32. /**
  33. * PatientsController constructor.
  34. *
  35. * @param PatientRepository $repository
  36. * @param PatientValidator $validator
  37. */
  38. public function __construct(PatientRepository $repository, PatientValidator $validator)
  39. {
  40. $this->repository = $repository;
  41. $this->validator = $validator;
  42. }
  43. public function index()
  44. {
  45. $this->repository->pushCriteria(PatientCriteria::class);
  46. $lists = $this->repository->paginate(request('per_page', self::PAGE_NUM));
  47. return Response::success($lists);
  48. }
  49. public function store(Request $request)
  50. {
  51. $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_CREATE));
  52. try {
  53. $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_CREATE)));
  54. $data['admin_id'] = login_user_id();
  55. $patient = Patient::query()->create($data);
  56. return Response::success($patient);
  57. } catch (\Exception $e) {
  58. return $this->errorStore($e);
  59. }
  60. }
  61. public function show($id)
  62. {
  63. $patient = $this->repository->find($id);
  64. return Response::success($patient);
  65. }
  66. public function update(Request $request, $id)
  67. {
  68. $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_UPDATE));
  69. try {
  70. $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_UPDATE)));
  71. $patient = Patient::query()->where('id', $id)->first();
  72. $patient->fill($data);
  73. $patient->save();
  74. return Response::success($patient);
  75. } catch (\Exception $e) {
  76. $this->errorStore($e);
  77. }
  78. }
  79. public function destroy($id)
  80. {
  81. try {
  82. $re = $this->repository->delete($id);
  83. if ($re) {
  84. return Response::success(null, T('successfully delete.'));
  85. }
  86. return Response::fail(T('Delete failed.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  87. } catch (\Exception $exception) {
  88. return Response::fail(T('Delete failed.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  89. }
  90. }
  91. public function lists(Request $request)
  92. {
  93. $this->repository->pushCriteria(PatientCriteria::class);
  94. $lists = $this->repository->get();
  95. return Response::success($lists);
  96. }
  97. /**
  98. * 导出
  99. * Author: Mead
  100. */
  101. public function export(Request $request)
  102. {
  103. $this->validate($request, [
  104. 'ids' => 'required|array',
  105. 'fields' => 'sometimes|array',
  106. 'type' => 'required'
  107. ]);
  108. $ids = $request->get('ids');
  109. $name = time();
  110. return (new PatientExport($request->get('fields', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]), (int)$request->get('type', 0)))->forIds($ids)->download($name . '.xlsx', null, [
  111. 'Access-Control-Allow-Origin' => '*',
  112. 'Access-Control-Allow-Methods' => '*',
  113. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With',
  114. 'Access-Control-Expose-Headers' => 'Content-Disposition',
  115. 'Content-type' => 'application/octet-stream',
  116. 'Content-Disposition' => 'attachment; filename=' . $name . '.xlsx',
  117. ]);
  118. }
  119. }