123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Models\Scale;
- use App\Models\Collect;
- use App\Repositories\Contracts\ScaleInterface;
- use App\Facades\ThirdScaleFacade;
- use App\Models\ThirdScale;
- use App\Models\User;
- use App\Models\BrowseRecord;
- use App\Facades\BrowseRecordFacade;
- /**
- * 量表
- * @author lilin
- *
- */
- class ScaleFacadeRepository extends BaseRepository implements ScaleInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = Scale::where(function($query) use($conditions){
- if (isset($conditions['category_id']) && $conditions['category_id']){
- $query->where('category_id', $conditions['category_id']);
- }
- if (isset($conditions['second_id']) && $conditions['second_id']){
- $query->where('second_id', $conditions['second_id']);
- }
- if (isset($conditions['search']) && $conditions['search']){
- $query->where('title', 'like' , '%'.$conditions['search'].'%');
- }
- if (isset($conditions['comment_tag']) && $conditions['comment_tag']){
- $query->where(function($query) use($conditions){
- foreach ($conditions['comment_tag'] as $tag){
- $tag = str_replace(array("[","]","\""),"",$tag);
- $query->orWhereRaw("JSON_CONTAINS(comment_tag, '\"{$tag}\"', '$')");
- }
- });
- }
- })->orderByRaw($sort)->paginate($limit, $fields);
- return $this->response($result);
- }
- public function findBy(array $conditions, array $fields){
- $result = Scale::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- })->first($fields);
- if ($result){
- $result->collect = Collect::where('uid', $conditions['uid'])->where('object_id', $conditions['id'])->exists();
- }
- return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
- }
- public function create(array $data)
- {
- $result = Scale::create($data);
- return $result ? $this->response($result) : $this->error()->fail();
- }
- public function updateBy(array $conditions, array $data)
- {
- $result = Scale::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->update($data);
- return $result ? $this->response($result) : $this->error()->fail();
- }
- public function deleteBy(array $conditions)
- {
- $result = Scale::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->delete();
- return $result ? $this->response($result) : $this->error()->fail();
- }
- public function createThirdScale(int $start, int $count, string $keyword)
- {
- $scales = ThirdScaleFacade::getScale($start, $count, $keyword);
- $lists = $scales->getData()->data->data->lists;
- $saveArr = [];
- foreach ($lists as $item){
- $item = json_decode(json_encode($item),true);
- unset($item['sort']);
- unset($item['ask_count']);
- unset($item['is_doctor_report']);
- unset($item['is_other_doctor']);
- unset($item['is_other_kin']);
- unset($item['is_other_nurse']);
- unset($item['is_outside_report']);
- unset($item['is_self_assess']);
- unset($item['is_other_assess']);
- // unset($item['b_image_icon']);
- // unset($item['instructions']);
- // unset($item['s_image_icon']);
- $saveArr[] = $item;
- }
- ThirdScale::insert($saveArr);
- }
- public function startTest($uid, $scaleId)
- {
- try {
- //获取远程open_id
- $user = User::find($uid);
- //获取远程量表id
- $scale = Scale::withTrashed()->with(['thirdScale'])->find($scaleId);
- //获取pushid
- $pushId = ThirdScaleFacade::createPush($user->third_openid, $scale->thirdScale->mid);
- //得到跳转的URL
- $url = ThirdScaleFacade::appLogin($user->third_openid, $pushId, '');
- $record = [
- 'uid' => $uid,
- 'column' => BrowseRecord::COLUMN_1,
- 'object_id' => $scaleId,
- 'repeat' => 'uid:'.$uid.' column:'.BrowseRecord::COLUMN_1.' object_id:'.$scaleId.'ext:'.md5($url),
- 'ext' => str_replace('http', 'https', $url)
- ];
- BrowseRecordFacade::create($record);
- return $this->response(str_replace('http', 'https', $url));
- } catch (\Exception $e) {
- $this->error()->fail($e->getMessage());
- }
- }
- }
|