CmsContentTemplateSetRepository.php 12 KB

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