CmsSubjectRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsSubject;
  4. use App\Models\CmsSubjectProduct;
  5. use Illuminate\Support\Facades\Log;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. use Dingo\Api\Http\Response;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Database\QueryException;
  10. class CmsSubjectRepository {
  11. public function __construct(CmsSubject $cmsSubject,CmsSubjectProduct $cmsSubjectProduct) {
  12. $this->cmsSubject = $cmsSubject;
  13. $this->cmsSubjectProduct = $cmsSubjectProduct;
  14. }
  15. //专题列表
  16. public function index($request)
  17. {
  18. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  19. $where = [];
  20. if(isset($request['id'])){
  21. $where[] = ['id', '=', $request['id']];
  22. }
  23. if(isset($request['city_id'])){
  24. $where[] = ['city_id', '=', $request['city_id']];
  25. }
  26. if(isset($request['is_open'])){
  27. $where[] = ['is_open', '=', $request['is_open']];
  28. }
  29. if(isset($request['used_mall'])){
  30. $where[] = ['used_mall', '=', $request['used_mall']];
  31. }
  32. return $this->cmsSubject->where($where)->orderBy('id', 'asc')->paginate($perPage);
  33. }
  34. public function detail($request)
  35. {
  36. return $this->cmsSubject->find($request['id']);
  37. }
  38. /**
  39. * 添加专题
  40. */
  41. public function create($request)
  42. {
  43. if($this->cmsSubject->where('title', trim($request['title']))->exists()){
  44. throw new HttpException(500, '该专题已经存在');
  45. }
  46. $subject = [
  47. 'title' => $request['title'],
  48. 'city_id' => isset($request['city_id']) ? $request['city_id'] : 0,
  49. 'city_name' => isset($request['city_name']) ? $request['city_name'] : '',
  50. 'show_type' => $request['show_type'],
  51. 'is_open' => $request['is_open'],
  52. 'used_count' => 0,
  53. 'used_mall' => isset($request['used_mall']) ? $request['used_mall'] : 1,
  54. 'subject_img' => isset($request['subject_img']) ? $request['subject_img'] : ''
  55. ];
  56. $date = date('Y-m-d H:i:s');
  57. DB::beginTransaction();
  58. try{
  59. $res = $this->cmsSubject->create($subject);
  60. if ($res && $request['product_id']){
  61. $products = explode(',',$request['product_id']);
  62. $sort = explode(',',$request['sort']);
  63. $product_num = count($products);
  64. $sort_num = count($sort);
  65. if ($sort_num != $product_num){
  66. throw new HttpException(500, '请检查商品与排序数目是否对应');
  67. }
  68. $resert_data = [];
  69. for ($i = 0;$i < $product_num;$i++){
  70. $resert_data[] =[
  71. 'product_id' => $products[$i],
  72. 'sort' => $sort[$i],
  73. 'subject_id' => $res['id'],
  74. 'created_at' => $date,
  75. 'updated_at' => $date,
  76. ];
  77. }
  78. $result = $this->cmsSubjectProduct->insert($resert_data);
  79. if (!$result){
  80. throw new HttpException(500, '专题商品添加失败');
  81. }
  82. }
  83. DB::commit();
  84. return Response::create();
  85. }catch (QueryException $exception){
  86. DB::rollBack();
  87. return Response::create([
  88. 'message' => '添加失败,请重试',
  89. 'error' => $exception->getMessage(),
  90. 'status_code' => 500
  91. ]);
  92. }
  93. }
  94. public function edit($request)
  95. {
  96. $subject = $this->cmsSubject->find($request['id']);
  97. $subject->title = $request['title'];
  98. $subject->city_id = isset($request['city_id']) ? $request['city_id'] : 0;
  99. $subject->city_name = isset($request['city_name']) ? $request['city_name'] : '';
  100. $subject->show_type = $request['show_type'];
  101. $subject->is_open = $request['is_open'];
  102. $subject->used_count = 0;
  103. $subject->used_mall = isset($request['used_mall']) ? $request['used_mall'] : 1;
  104. $subject->subject_img = isset($request['subject_img']) ? $request['subject_img'] : '';
  105. $date = date('Y-m-d H:i:s');
  106. DB::beginTransaction();
  107. try{
  108. $res = $subject->save();
  109. if($res && $request['product_id']) {
  110. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  111. $products = explode(',', $request['product_id']);
  112. $sort = explode(',', $request['sort']);
  113. $product_num = count($products);
  114. $sort_num = count($sort);
  115. if ($sort_num != $product_num) {
  116. throw new HttpException(500, '请检查商品与排序数目是否对应');
  117. }
  118. $update_data = [];
  119. for ($i = 0;$i < $product_num;$i++){
  120. $update_data[] =[
  121. 'product_id' => $products[$i],
  122. 'sort' => $sort[$i],
  123. 'subject_id' => $subject->id,
  124. 'created_at' => $date,
  125. 'updated_at' => $date,
  126. ];
  127. }
  128. $result = $this->cmsSubjectProduct->insert($update_data);
  129. if (!$result){
  130. throw new HttpException(500, '专题商品添加失败');
  131. }
  132. }
  133. DB::commit();
  134. return Response::create();
  135. }catch (QueryException $exception){
  136. DB::rollBack();
  137. return Response::create([
  138. 'message' => '编辑失败,请重试',
  139. 'error' => $exception->getMessage(),
  140. 'status_code' => 500
  141. ]);
  142. }
  143. }
  144. public function delete($request)
  145. {
  146. $subject = $this->cmsSubject->find($request['id']);
  147. if ($subject->used_count != 0){
  148. throw new HttpException(500, '该专题正在使用不能被删除');
  149. }
  150. DB::beginTransaction();
  151. try{
  152. $res = $subject->delete();
  153. if ($res){
  154. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  155. }
  156. DB::commit();
  157. return Response::create();
  158. }catch (QueryException $exception){
  159. DB::rollBack();
  160. return Response::create([
  161. 'message' => '删除失败,请重试',
  162. 'error' => $exception->getMessage(),
  163. 'status_code' => 500
  164. ]);
  165. }
  166. }
  167. public function editStatus($request)
  168. {
  169. $subject = $this->cmsSubject->find($request['id']);
  170. $subject->is_open = $request['is_open'];
  171. $subject->updated_at = date('Y-m-d H:i:s');
  172. if ($subject->used_count != 0 && $request['is_open'] == 0){
  173. throw new HttpException(500, '该专题正在使用不能被关闭');
  174. }
  175. $res = $subject->save();
  176. if (!$res) {
  177. throw new HttpException(500, '修改状态失败');
  178. }
  179. }
  180. }