CmsContentTemplateSetRepository.php 9.2 KB

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