CmsContentTemplateSetRepository.php 9.5 KB

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