CmsContentTemplateSetRepository.php 14 KB

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