SpecialistInfoFacadeRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Models\SpecialistInfo;
  4. use App\Models\SpecialistInfoLeaveMessage;
  5. use App\Models\SpecialistInfosAssociation;
  6. use App\Repositories\Contracts\SpecialistInfoInterface;
  7. use App\Repositories\Eloquent\BaseRepository;
  8. use Illuminate\Support\Facades\Auth;
  9. /**
  10. *
  11. * @author lilin
  12. *
  13. */
  14. class SpecialistInfoFacadeRepository extends BaseRepository implements SpecialistInfoInterface
  15. {
  16. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  17. {
  18. $result = SpecialistInfo::with(['firstCloumn'=>function($with){
  19. $with->select(['id','pid','name']);
  20. },'secondCloumn'=>function($with){
  21. $with->select(['id','pid','name']);
  22. }])->where(function($query) use($conditions){
  23. if (isset($conditions['first_id'])){
  24. $query->where('first_id', $conditions['first_id']);
  25. }
  26. if (isset($conditions['second_id'])){
  27. $second_id = (int)$conditions['second_id'];
  28. $query->where('second_id', $second_id);
  29. // $excludeData = array(12,13);
  30. // if (in_array($second_id,$excludeData)){
  31. // $query->where('second_id',$second_id);
  32. // }elseif ($second_id == 0){
  33. // $query->whereNotIn('second_id',$excludeData);
  34. // }else{
  35. // $query->where('second_id', $second_id);
  36. // }
  37. }
  38. if (isset($conditions['type'])){
  39. $query->where('type', $conditions['type']);
  40. }
  41. if (isset($conditions['tag_id']) && $conditions['tag_id']){
  42. $query->where('tag_id', $conditions['tag_id']);
  43. }
  44. if (isset($conditions['name']) && !empty($conditions['name'])){
  45. $query->where('name', 'like' , '%'.$conditions['name'].'%');
  46. }
  47. if (isset($conditions['comment_tag']) && $conditions['comment_tag']){
  48. $query->where(function($query) use($conditions){
  49. foreach ($conditions['comment_tag'] as $tag){
  50. $tag = str_replace(array("[","]","\""),"",$tag);
  51. $query->orWhereRaw("JSON_CONTAINS(comment_tag, '\"{$tag}\"', '$')");
  52. }
  53. });
  54. }
  55. })->orderByRaw($sort)->paginate($limit, $fields);
  56. $result->each(function($item){
  57. if ($item->cover){
  58. $newCover = [];
  59. foreach ($item->cover as $cover){
  60. $newCover[] = config('console.pic_path').$cover;
  61. }
  62. $item->cover = $newCover;
  63. }
  64. });
  65. return $this->response($result);
  66. }
  67. public function findBy(array $conditions, array $fields){
  68. $result = SpecialistInfo::with(
  69. [
  70. 'firstCloumn'=>function($with){$with->select(['id','pid','name']);},
  71. 'secondCloumn'=>function($with){$with->select(['id','pid','name']);},
  72. 'leaveMessage'=>function($with){$with->select(['id','user_id','specialist_infos_id','content','created_at']);},
  73. ]
  74. )->where(function($query) use($conditions){
  75. if (isset($conditions['id'])){
  76. $query->where('id', $conditions['id']);
  77. }
  78. })->first($fields);
  79. if ($result->cover){
  80. $newCover = [];
  81. foreach ($result->cover as $cover){
  82. $newCover[] = config('console.pic_path').$cover;
  83. }
  84. $result->cover = $newCover;
  85. }
  86. if ($result->video){
  87. $result->video = config('console.pic_path').$result->video;
  88. }
  89. if ($result){
  90. $result = $result->toArray();
  91. $user_id = Auth::id();
  92. $result["is_thumbs_up"] = SpecialistInfosAssociation::query()
  93. ->where(array(
  94. "user_id"=>$user_id,
  95. "association_id"=>$result["id"],
  96. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP,
  97. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM
  98. ))->count();
  99. $result["is_love"] = SpecialistInfosAssociation::query()
  100. ->where(array(
  101. "user_id"=>$user_id,
  102. "association_id"=>$result["id"],
  103. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE,
  104. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM
  105. ))->count();
  106. }
  107. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  108. }
  109. public function create(array $data)
  110. {
  111. $result = SpecialistInfo::create($data);
  112. return $result ? $this->response($result) : $this->error()->fail();
  113. }
  114. public function updateBy(array $conditions, array $data)
  115. {
  116. $result = SpecialistInfo::where(function($query) use($conditions){
  117. if (isset($conditions['id'])){
  118. $query->where('id', $conditions['id']);
  119. }
  120. })->update($data);
  121. return $result ? $this->response($result) : $this->error()->fail();
  122. }
  123. public function deleteBy(array $conditions)
  124. {
  125. $result = SpecialistInfo::where(function($query) use($conditions){
  126. if (isset($conditions['id'])){
  127. $query->where('id', $conditions['id']);
  128. }
  129. })->delete();
  130. return $result ? $this->response($result) : $this->error()->fail();
  131. }
  132. public function thumbsUpNum(int $id, int $userId)
  133. {
  134. $operateType = request('operateType', 0);
  135. $result = SpecialistInfo::query()->find($id);
  136. if (empty($result)){
  137. return $this->error()->fail("数据信息不存在");
  138. }
  139. if (empty($userId) || $userId <= 0){
  140. return $this->error()->fail("用户信息错误");
  141. }
  142. $data=array(
  143. "user_id"=>$userId,
  144. "association_id"=>$id,
  145. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP,
  146. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM
  147. );
  148. $checkCount = SpecialistInfosAssociation::query()->where($data)->count();
  149. if ($operateType){
  150. if ($checkCount <= 0){
  151. $addResult = SpecialistInfosAssociation::query()->create($data);
  152. if (empty($addResult)){
  153. return $this->error()->fail("新增点赞信息失败");
  154. }
  155. $result->increment('thumbs_up_num');
  156. }
  157. }else{
  158. if ($checkCount){
  159. $result->decrement('thumbs_up_num');
  160. SpecialistInfosAssociation::query()->where($data)->delete();
  161. }
  162. }
  163. return $this->response($result);
  164. }
  165. public function loveNum(int $id, int $userId)
  166. {
  167. $operateType = request('operateType', 0);
  168. $result = SpecialistInfo::query()->find($id);
  169. if (empty($result)){
  170. return $this->error()->fail("数据信息不存在");
  171. }
  172. if (empty($userId) || $userId <= 0){
  173. return $this->error()->fail("用户信息错误");
  174. }
  175. $data=array(
  176. "user_id"=>$userId,
  177. "association_id"=>$id,
  178. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE,
  179. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM
  180. );
  181. $checkCount = SpecialistInfosAssociation::query()->where($data)->count();
  182. if ($operateType){
  183. if ($checkCount <= 0){
  184. $addResult = SpecialistInfosAssociation::query()->create($data);
  185. if (empty($addResult)){
  186. return $this->error()->fail("新增喜欢信息失败");
  187. }
  188. $result->increment('love_num');
  189. }
  190. }else{
  191. if ($checkCount){
  192. $result->decrement('love_num');
  193. SpecialistInfosAssociation::query()->where($data)->delete();
  194. }
  195. }
  196. return $this->response($result);
  197. }
  198. public function leaveMessage(int $id, int $userId, string $message)
  199. {
  200. if (empty($userId) || $userId <= 0){
  201. return $this->error()->fail("用户信息错误");
  202. }
  203. if (empty($message)){
  204. return $this->error()->fail("留言信息不能为空");
  205. }
  206. $result = SpecialistInfo::query()->find($id);
  207. if (empty($result)){
  208. return $this->error()->fail("数据信息不存在");
  209. }
  210. $data=array(
  211. "user_id"=>$userId,
  212. "specialist_infos_id"=>$id,
  213. );
  214. $checkCount = SpecialistInfoLeaveMessage::query()->where($data)->count();
  215. if ($checkCount <= 0){
  216. $addResult = SpecialistInfoLeaveMessage::query()->create(
  217. array_merge(
  218. $data,
  219. array("content"=>$message)
  220. )
  221. );
  222. if (empty($addResult)){
  223. return $this->error()->fail("新增留言信息失败");
  224. }
  225. $result->increment('leave_num');
  226. }
  227. return $this->response($result);
  228. }
  229. public function specialistInfosAssociationList(int $userId, int $associationType,int $page, int $limit)
  230. {
  231. $operateType = request('operateType', 0);
  232. $base = array(
  233. "user_id"=>$userId,
  234. "association_type"=>$associationType,
  235. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_CLASS_ROOM
  236. );
  237. $result = SpecialistInfosAssociation::with(
  238. [
  239. "specialist"=>function($with){$with->select([
  240. 'id','first_id','second_id','name','subtitle','content',
  241. 'cover','video','type','speaker',
  242. 'source','author_id','number_of_studies','thumbs_up_num',
  243. 'love_num','leave_num'
  244. ]);}
  245. ]
  246. )->where(
  247. function($query) use($base,$operateType){
  248. $query->where($base);
  249. // if ($operateType==1){
  250. // $query->whereRaw("
  251. // association_id in ( select id from specialist_infos where second_id in (12,13) )
  252. //");
  253. // }else {
  254. // $query->whereRaw("
  255. // association_id not in ( select id from specialist_infos where second_id in (12,13) )
  256. //");
  257. // }
  258. }
  259. )
  260. ->orderByRaw("id desc")->paginate($limit, ['*'],"page",$page);
  261. $list = $result->items();
  262. foreach ($list as &$val){
  263. $val = $val->toArray();
  264. if ($val["specialist"]){
  265. $specialist = $val["specialist"];
  266. $specialist["video"] = config('console.pic_path').$specialist["video"];
  267. if ($specialist["cover"]){
  268. foreach ($specialist["cover"] as &$va){
  269. $va = config('console.pic_path').$va;
  270. }
  271. }
  272. $val["specialist"] = $specialist;
  273. }
  274. }
  275. $result->setCollection(collect($list));
  276. return $this->response($result);
  277. }
  278. }