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