CmsContentTemplateSetRepository.php 14 KB

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