CmsContentTemplateSetRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsSubject;
  4. use Dingo\Api\Http\Response;
  5. use App\Models\CmsContentTemplate;
  6. use App\Models\CmsContentTemplateSet;
  7. use App\Models\CmsSubjectProduct;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. class CmsContentTemplateSetRepository {
  10. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate,CmsSubjectProduct $cmsSubjectProduct,CmsSubject $cmsSubject) {
  11. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  12. $this->cmsContentTemplate = $cmsContentTemplate;
  13. $this->cmsSubjectProduct = $cmsSubjectProduct;
  14. $this->cmsSubject = $cmsSubject;
  15. }
  16. /**
  17. * 点击配置
  18. */
  19. /**
  20. * banner配置
  21. */
  22. public function bannerSet($request)
  23. {
  24. $banner_id = $request['id']??'';
  25. // $rules = json_decode($request['rule'],true);
  26. $rules = $request['rule'];
  27. if (is_array($rules)){
  28. if (count($rules)>10) {
  29. throw new HttpException(500, '最多只能添加10个banner海报');
  30. }
  31. $subject = [
  32. 'rule' => json_encode($rules),
  33. 'tpl_id' => $request['tpl_id'],
  34. 'area_type' => $request['area_type'],
  35. 'status' => 0,
  36. ];
  37. if ($banner_id){
  38. $banners = $this->cmsContentTemplateSet->where('id',$banner_id)->update($subject);
  39. if (!$banners) {
  40. throw new HttpException(500, '更新失败');
  41. }
  42. }else{
  43. if (!$this->cmsContentTemplateSet->create($subject)) {
  44. throw new HttpException(500, '添加失败');
  45. }
  46. }
  47. }else{
  48. throw new HttpException(500, '参数格式有误');
  49. }
  50. }
  51. /**
  52. * 专题广告配置
  53. */
  54. public function advertisementSet($request)
  55. {
  56. $advertisement_id = $request['id']??'';
  57. $subject = [
  58. 'tpl_id' => $request['tpl_id'],
  59. 'rule' => $request['rule'],
  60. 'area_type' => $request['area_type'],
  61. 'status' => 0,
  62. ];
  63. if ($advertisement_id){
  64. $advertisement_ids = $this->cmsContentTemplateSet->where('id',$advertisement_id)->update($subject);
  65. if (!$advertisement_ids) {
  66. throw new HttpException(500, '更新失败');
  67. }
  68. }else{
  69. if (!$this->cmsContentTemplateSet->create($subject)) {
  70. throw new HttpException(500, '添加失败');
  71. }
  72. }
  73. }
  74. /**
  75. * 商品楼层配置
  76. */
  77. public function floorSet($request)
  78. {
  79. $floor_id = $request['id']??'';
  80. $subject = [
  81. 'tpl_id' => $request['tpl_id'],
  82. 'rule' => $request['rule'],
  83. 'area_type' => $request['area_type'],
  84. 'status' => 0,
  85. ];
  86. if ($floor_id){
  87. $floor_ids = $this->cmsContentTemplateSet->where('id',$floor_id)->update($subject);
  88. if (!$floor_ids) {
  89. throw new HttpException(500, '更新失败');
  90. }
  91. }else {
  92. if (!$this->cmsContentTemplateSet->create($subject)) {
  93. throw new HttpException(500, '添加失败');
  94. }
  95. }
  96. }
  97. /**
  98. * 分类专题配置
  99. */
  100. public function categorySet($request)
  101. {
  102. $category_id = $request['id']??'';
  103. $subject = [
  104. 'tpl_id' => $request['tpl_id'],
  105. 'rule' => $request['rule'],
  106. 'area_type' => $request['area_type'],
  107. 'status' => 0,
  108. ];
  109. if ($category_id){
  110. $category_ids = $this->cmsContentTemplateSet->where('id',$category_id)->update($subject);
  111. if (!$category_ids) {
  112. throw new HttpException(500, '更新失败');
  113. }
  114. }else {
  115. if (!$this->cmsContentTemplateSet->create($subject)) {
  116. throw new HttpException(500, '添加失败');
  117. }
  118. }
  119. }
  120. /**
  121. * 内容发布
  122. */
  123. public function release($request)
  124. {
  125. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  126. $templateSet = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->select('id','status')->get();
  127. $tem_array = $templateSet->toArray();
  128. foreach ($tem_array as $k=>$v) {
  129. if ($v['status'] == 1) {
  130. $result = $this->cmsContentTemplateSet->where('id', $v['id'])->delete();
  131. if (!$result) {
  132. return Response::create([
  133. 'message' => '删除失败,请重试',
  134. 'status_code' => 500
  135. ]);
  136. }
  137. }
  138. }
  139. $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]);
  140. if (!$res){
  141. return Response::create([
  142. 'message' => '修改失败,请重试',
  143. 'status_code' => 500
  144. ]);
  145. }
  146. }
  147. /**
  148. * 内容预览
  149. */
  150. public function preview($request)
  151. {
  152. $temalates = $this->cmsContentTemplate->select('title')->where(['id'=>$request['tpl_id']])->first();
  153. if (!$temalates){
  154. throw new HttpException(500, '没有找到对应模板');
  155. }
  156. //团购首页
  157. $group_array = [];
  158. if ($request['type'] == 0){
  159. $group_array['apply_type'] = 0;
  160. $group_array['title'] = $temalates->title;
  161. $group_array['content'] =[];
  162. }else{//菜市场首页
  163. $group_array['apply_type'] = 1;
  164. $group_array['title'] = $temalates->title;
  165. $group_array['content'] =[];
  166. }
  167. $group_array['content'][0]['area_type'] = 0;
  168. $banner_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>0])->get();
  169. $new_rule = [];
  170. foreach ($banner_rule->toArray() as $k=>$v){
  171. $new_rule[$k]['id'] = $v['id'];
  172. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  173. }
  174. $group_array['content'][0]['rule'] = $new_rule;
  175. $group_array['content'][1]['area_type'] = 1;
  176. $subject_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>1])->get();
  177. $new_rule = [];
  178. foreach ($subject_rule->toArray() as $k=>$v){
  179. $new_rule[$k]['id'] = $v['id'];
  180. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  181. }
  182. $group_array['content'][1]['rule'] = $new_rule;
  183. $group_array['content'][2]['area_type'] = 2;
  184. $floor_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>2])->get();
  185. $new_rule = [];
  186. foreach ($floor_rule->toArray() as $k=>$v){
  187. $new_rule[$k]['id'] = $v['id'];
  188. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  189. }
  190. foreach ($new_rule as $k=>$v){
  191. if ($v){
  192. $rules = $v['rule'];
  193. $show_num = intval($rules['show_num']);
  194. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  195. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  196. $pro_array = $product->toArray();
  197. $res_id = implode(",", array_column($pro_array, 'product_id'));
  198. $new_rule[$k]['product_id'] = $res_id;
  199. $new_rule[$k]['subject_id'] = $rules['link_url'];
  200. $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
  201. unset($v['url']);
  202. unset($v['show_num']);
  203. unset($rules['link_url']);
  204. unset($v['link_type']);
  205. }
  206. }
  207. $group_array['content'][2]['rule'] = $new_rule;
  208. if ($request['type'] == 1){
  209. $group_array['content'][3]['area_type'] = 3;
  210. $category_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>3])->get();
  211. $new_rule = [];
  212. foreach ($category_rule->toArray() as $k=>$v){
  213. $new_rule[$k]['id'] = $v['id'];
  214. $new_rule[$k]['rule'] = json_decode($v['rule'],true);
  215. }
  216. $group_array['content'][3]['rule'] = $new_rule;
  217. }
  218. return $group_array;
  219. }
  220. public function getTemplate($cityId)
  221. {
  222. return $this->cmsContentTemplate->select('id','title','apply_type')->where(['city_id'=>$cityId,'is_open'=>1])->orderBy('apply_type','asc')->get();
  223. }
  224. }