ScaleFacadeRepository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Models\Scale;
  4. use App\Models\Collect;
  5. use App\Repositories\Contracts\ScaleInterface;
  6. use App\Facades\ThirdScaleFacade;
  7. use App\Models\ThirdScale;
  8. use App\Models\User;
  9. use App\Models\BrowseRecord;
  10. use App\Facades\BrowseRecordFacade;
  11. /**
  12. * 量表
  13. * @author lilin
  14. *
  15. */
  16. class ScaleFacadeRepository extends BaseRepository implements ScaleInterface
  17. {
  18. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  19. {
  20. $result = Scale::where(function($query) use($conditions){
  21. if (isset($conditions['category_id']) && $conditions['category_id']){
  22. $query->where('category_id', $conditions['category_id']);
  23. }
  24. if (isset($conditions['second_id']) && $conditions['second_id']){
  25. $query->where('second_id', $conditions['second_id']);
  26. }
  27. if (isset($conditions['search']) && $conditions['search']){
  28. $query->where('title', 'like' , '%'.$conditions['search'].'%');
  29. }
  30. if (isset($conditions['comment_tag']) && $conditions['comment_tag']){
  31. $query->where(function($query) use($conditions){
  32. foreach ($conditions['comment_tag'] as $tag){
  33. $tag = str_replace(array("[","]","\""),"",$tag);
  34. $query->orWhereRaw("JSON_CONTAINS(comment_tag, '\"{$tag}\"', '$')");
  35. }
  36. });
  37. }
  38. })->orderByRaw($sort)->paginate($limit, $fields);
  39. return $this->response($result);
  40. }
  41. public function findBy(array $conditions, array $fields){
  42. $result = Scale::where(function($query) use($conditions){
  43. if (isset($conditions['id'])){
  44. $query->where('id', $conditions['id']);
  45. }
  46. })->first($fields);
  47. if ($result){
  48. $result->collect = Collect::where('uid', $conditions['uid'])->where('object_id', $conditions['id'])->exists();
  49. }
  50. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  51. }
  52. public function create(array $data)
  53. {
  54. $result = Scale::create($data);
  55. return $result ? $this->response($result) : $this->error()->fail();
  56. }
  57. public function updateBy(array $conditions, array $data)
  58. {
  59. $result = Scale::where(function($query) use($conditions){
  60. if (isset($conditions['id'])){
  61. $query->where('id', $conditions['id']);
  62. }
  63. if (isset($conditions['uid'])){
  64. $query->where('uid', $conditions['uid']);
  65. }
  66. })->update($data);
  67. return $result ? $this->response($result) : $this->error()->fail();
  68. }
  69. public function deleteBy(array $conditions)
  70. {
  71. $result = Scale::where(function($query) use($conditions){
  72. if (isset($conditions['id'])){
  73. $query->where('id', $conditions['id']);
  74. }
  75. if (isset($conditions['uid'])){
  76. $query->where('uid', $conditions['uid']);
  77. }
  78. })->delete();
  79. return $result ? $this->response($result) : $this->error()->fail();
  80. }
  81. public function createThirdScale(int $start, int $count, string $keyword)
  82. {
  83. $scales = ThirdScaleFacade::getScale($start, $count, $keyword);
  84. $lists = $scales->getData()->data->data->lists;
  85. $saveArr = [];
  86. foreach ($lists as $item){
  87. $item = json_decode(json_encode($item),true);
  88. unset($item['sort']);
  89. unset($item['ask_count']);
  90. unset($item['is_doctor_report']);
  91. unset($item['is_other_doctor']);
  92. unset($item['is_other_kin']);
  93. unset($item['is_other_nurse']);
  94. unset($item['is_outside_report']);
  95. unset($item['is_self_assess']);
  96. unset($item['is_other_assess']);
  97. // unset($item['b_image_icon']);
  98. // unset($item['instructions']);
  99. // unset($item['s_image_icon']);
  100. $saveArr[] = $item;
  101. }
  102. ThirdScale::insert($saveArr);
  103. }
  104. public function startTest($uid, $scaleId)
  105. {
  106. try {
  107. //获取远程open_id
  108. $user = User::find($uid);
  109. //获取远程量表id
  110. $scale = Scale::withTrashed()->with(['thirdScale'])->find($scaleId);
  111. //获取pushid
  112. $pushId = ThirdScaleFacade::createPush($user->third_openid, $scale->thirdScale->mid);
  113. //得到跳转的URL
  114. $url = ThirdScaleFacade::appLogin($user->third_openid, $pushId, '');
  115. $record = [
  116. 'uid' => $uid,
  117. 'column' => BrowseRecord::COLUMN_1,
  118. 'object_id' => $scaleId,
  119. 'repeat' => 'uid:'.$uid.' column:'.BrowseRecord::COLUMN_1.' object_id:'.$scaleId.'ext:'.md5($url),
  120. 'ext' => str_replace('http', 'https', $url)
  121. ];
  122. BrowseRecordFacade::create($record);
  123. return $this->response(str_replace('http', 'https', $url));
  124. } catch (\Exception $e) {
  125. $this->error()->fail($e->getMessage());
  126. }
  127. }
  128. }