MentalityDrillFacadeRepository.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Models\BrowseRecord;
  4. use App\Models\SpecialistInfoLeaveMessage;
  5. use App\Models\SpecialistInfosAssociation;
  6. use App\Repositories\Eloquent\BaseRepository;
  7. use App\Repositories\Contracts\MentalityDrillInterface;
  8. use App\Models\MentalityDrill;
  9. use App\Models\MentalityDrillExtend;
  10. use Illuminate\Support\Facades\Auth;
  11. /**
  12. *
  13. * @author lilin
  14. *
  15. */
  16. class MentalityDrillFacadeRepository extends BaseRepository implements MentalityDrillInterface
  17. {
  18. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  19. {
  20. $result = MentalityDrill::where(function($query) use($conditions){
  21. if (isset($conditions['category_id'])){
  22. $query->where('category_id', $conditions['category_id']);
  23. }
  24. if (isset($conditions['search'])){
  25. $query->where('title', 'like' , '%'.$conditions['search'].'%');
  26. }
  27. if (isset($conditions['comment_tag'])){
  28. $query->where(function($query) use($conditions){
  29. foreach ($conditions['comment_tag'] as $tag){
  30. $tag = str_replace(array("[","]","\""),"",$tag);
  31. $query->orWhereRaw("JSON_CONTAINS(comment_tag, '\"{$tag}\"', '$')");
  32. }
  33. });
  34. }
  35. })->orderByRaw($sort)->paginate($limit, $fields);
  36. return $this->response($result);
  37. }
  38. public function findBy(array $conditions, array $fields){
  39. $result = MentalityDrill::with([
  40. 'category'=>function($with){$with->select(['id','pic','name']);},
  41. 'extend'=>function($with){$with->select(['id','drill_id','name','time','src']);},
  42. 'subject'=>function($with){$with->select(['id','mentality_drills_id','name','time','src','type','pic']);},
  43. 'leaveMessage'=>function($with){$with->select(['id','user_id','mentality_drills_id','content','created_at']);},
  44. ])->where(function($query) use($conditions){
  45. if (isset($conditions['id'])){
  46. $query->where('id', $conditions['id']);
  47. }
  48. })->first($fields);
  49. MentalityDrill::where('id', $conditions['id'])->increment('number_of_studies');
  50. if ($result){
  51. $result = $result->toArray();
  52. $user_id = Auth::id();
  53. $result["is_thumbs_up"] = SpecialistInfosAssociation::query()
  54. ->where(array(
  55. "user_id"=>$user_id,
  56. "association_id"=>$result["id"],
  57. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP,
  58. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_WORKOUT
  59. ))->count();
  60. $result["is_love"] = SpecialistInfosAssociation::query()
  61. ->where(array(
  62. "user_id"=>$user_id,
  63. "association_id"=>$result["id"],
  64. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE,
  65. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_WORKOUT
  66. ))->count();
  67. $history = BrowseRecord::where('object_id', $result['id'])->where('column', BrowseRecord::COLUMN_4)->orderBy('id','desc')->first();
  68. $result['current_subject_id'] = $history?$history->subject_id: 0;
  69. }
  70. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  71. }
  72. public function create(array $data)
  73. {
  74. $result = MentalityDrill::create($data);
  75. return $result ? $this->response($result) : $this->error()->fail();
  76. }
  77. public function updateBy(array $conditions, array $data)
  78. {
  79. $result = MentalityDrill::where(function($query) use($conditions){
  80. if (isset($conditions['id'])){
  81. $query->where('id', $conditions['id']);
  82. }
  83. })->update($data);
  84. return $result ? $this->response($result) : $this->error()->fail();
  85. }
  86. public function deleteBy(array $conditions)
  87. {
  88. $result = MentalityDrill::where(function($query) use($conditions){
  89. if (isset($conditions['id'])){
  90. $query->where('id', $conditions['id']);
  91. }
  92. })->delete();
  93. return $result ? $this->response($result) : $this->error()->fail();
  94. }
  95. public function getDrillExtend(array $conditions, array $fields)
  96. {
  97. $result = MentalityDrillExtend::where(function($query) use($conditions){
  98. if (isset($conditions['id'])){
  99. $query->where('id', $conditions['id']);
  100. }
  101. })->first($fields);
  102. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  103. }
  104. public function thumbsUpNum(int $id, int $userId)
  105. {
  106. $operateType = request('operateType', 0);
  107. $result = MentalityDrill::query()->find($id);
  108. if (empty($result)){
  109. return $this->error()->fail("数据信息不存在");
  110. }
  111. if (empty($userId) || $userId <= 0){
  112. return $this->error()->fail("用户信息错误");
  113. }
  114. $data=array(
  115. "user_id"=>$userId,
  116. "association_id"=>$id,
  117. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_THUMBS_UP,
  118. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_WORKOUT
  119. );
  120. $checkCount = SpecialistInfosAssociation::query()->where($data)->count();
  121. if ($operateType){
  122. if ($checkCount <= 0){
  123. $addResult = SpecialistInfosAssociation::query()->create($data);
  124. if (empty($addResult)){
  125. return $this->error()->fail("新增点赞信息失败");
  126. }
  127. $result->increment('thumbs_up_num');
  128. }
  129. }else{
  130. if ($checkCount){
  131. $result->decrement('thumbs_up_num');
  132. SpecialistInfosAssociation::query()->where($data)->delete();
  133. }
  134. }
  135. return $this->response($result);
  136. }
  137. public function loveNum(int $id, int $userId)
  138. {
  139. $operateType = request('operateType', 0);
  140. $result = MentalityDrill::query()->find($id);
  141. if (empty($result)){
  142. return $this->error()->fail("数据信息不存在");
  143. }
  144. if (empty($userId) || $userId <= 0){
  145. return $this->error()->fail("用户信息错误");
  146. }
  147. $data=array(
  148. "user_id"=>$userId,
  149. "association_id"=>$id,
  150. "association_type"=>SpecialistInfosAssociation::ASSOCIATION_TYPE_LOVE,
  151. "operation_type"=>SpecialistInfosAssociation::OPERATION_TYPE_WORKOUT
  152. );
  153. $checkCount = SpecialistInfosAssociation::query()->where($data)->count();
  154. if ($operateType){
  155. if ($checkCount <= 0){
  156. $addResult = SpecialistInfosAssociation::query()->create($data);
  157. if (empty($addResult)){
  158. return $this->error()->fail("新增喜欢信息失败");
  159. }
  160. $result->increment('love_num');
  161. }
  162. }else{
  163. if ($checkCount){
  164. $result->decrement('love_num');
  165. SpecialistInfosAssociation::query()->where($data)->delete();
  166. }
  167. }
  168. return $this->response($result);
  169. }
  170. public function leaveMessage(int $id, int $userId, string $message)
  171. {
  172. if (empty($userId) || $userId <= 0){
  173. return $this->error()->fail("用户信息错误");
  174. }
  175. if (empty($message)){
  176. return $this->error()->fail("留言信息不能为空");
  177. }
  178. $result = MentalityDrill::query()->find($id);
  179. if (empty($result)){
  180. return $this->error()->fail("数据信息不存在");
  181. }
  182. $data=array(
  183. "user_id"=>$userId,
  184. "mentality_drills_id"=>$id,
  185. );
  186. $checkCount = SpecialistInfoLeaveMessage::query()->where($data)->count();
  187. if ($checkCount <= 0){
  188. $addResult = SpecialistInfoLeaveMessage::query()->create(
  189. array_merge(
  190. $data,
  191. array("content"=>$message)
  192. )
  193. );
  194. if (empty($addResult)){
  195. return $this->error()->fail("新增留言信息失败");
  196. }
  197. $result->increment('leave_num');
  198. }
  199. return $this->response($result);
  200. }
  201. }