123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- <?php
- namespace App\Repositories;
- use App\Models\CmsSubject;
- use Dingo\Api\Http\Response;
- use App\Models\CmsContentTemplate;
- use App\Models\CmsContentTemplateSet;
- use App\Models\CmsSubjectProduct;
- use Illuminate\Support\Facades\Cache;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\QueryException;
- class CmsContentTemplateSetRepository
- {
- public function __construct(CmsContentTemplateSet $cmsContentTemplateSet, CmsContentTemplate $cmsContentTemplate, CmsSubjectProduct $cmsSubjectProduct, CmsSubject $cmsSubject)
- {
- $this->cmsContentTemplateSet = $cmsContentTemplateSet;
- $this->cmsContentTemplate = $cmsContentTemplate;
- $this->cmsSubjectProduct = $cmsSubjectProduct;
- $this->cmsSubject = $cmsSubject;
- }
- /**
- * 点击内容配置
- */
- public function set($request)
- {
- $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
- if (!$template) {
- throw new HttpException(500, '没有找到对应模板');
- }
- $templates = $template->toArray();
- $where = [
- 'city_id' => $templates['city_id'],
- 'apply_type' => $templates['apply_type'],
- ];
- $copys_template = $this->cmsContentTemplate->where($where)->get();
- $request = [];
- $need_create = true;
- foreach ($copys_template as $k => $v) {
- if ($v['status'] == 0) {
- $need_create = false;
- $request['type'] = $v['apply_type'];
- $request['tpl_id'] = $v['id'];
- $request['city_id'] = $v['city_id'];
- if ($v['apply_type'] == 2) {
- return $this->exchangeMall($request);
- } else {
- return $this->preview($request);
- }
- }
- }
- if ($need_create) {
- $new_template = [
- 'title' => $templates['title'],
- 'city_id' => $templates['city_id'],
- 'city_name' => $templates['city_name'],
- 'apply_type' => $templates['apply_type'],
- 'status' => 0,
- 'is_open' => $templates['is_open'],
- ];
- DB::beginTransaction();
- try {
- $res = $this->cmsContentTemplate->create($new_template);
- if (!$res) {
- throw new HttpException(500, '添加草稿模板失败');
- }
- $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
- if (count($template_set) > 0) {
- foreach ($template_set->toArray() as $key => $val) {
- $copy_template = [
- 'rule' => $val['rule'],
- 'tpl_id' => $res->id,
- 'area_type' => $val['area_type'],
- 'status' => $val['status'],
- 'sort' => $val['sort'],
- 'name' => $val['name'],
- ];
- $result = $this->cmsContentTemplateSet->create($copy_template);
- if (!$result) {
- throw new HttpException(500, '生成模板内容失败');
- }
- }
- }
- $request['type'] = $res->apply_type;
- $request['tpl_id'] = $res->id;
- $request['city_id'] = $res->city_id;
- DB::commit();
- if ($res->apply_type == 2) {
- return $this->exchangeMall($request);
- } else {
- return $this->preview($request);
- }
- //return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- return Response::create([
- 'message' => '生成模板内容失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
- //banner、左一右二、上一下三配置-数据处理
- function request_data($request, $id, $rules)
- {
- foreach ($rules as $k => $v) {
- if (isset($v['link_type'])) {
- $rules[$k]['link_type'] = intval($v['link_type']);
- }
- }
- $templateSet = [
- 'rule' => json_encode($rules),
- 'tpl_id' => $request['tpl_id'],
- 'area_type' => $request['area_type'],
- 'status' => 0,
- 'sort' => isset($request['sort']) ? $request['sort'] : 999,
- 'name' => isset($request['name']) ? $request['name'] : '',
- 'floor_img' => isset($request['floor_img']) ? $request['floor_img'] : '',
- ];
- if ($id) {
- $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($id);
- $subject_id_array = json_decode($old_subject_id['rule'], true);
- $request_rule = $request['rule'];
- if (count($request_rule) > 0) {
- foreach ($request_rule as $k => $v) {
- if (isset($v['link_type']) && $v['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- }
- }
- if (count($subject_id_array) > 0) {
- foreach ($subject_id_array as $val) {
- if (isset($val['link_type']) && $val['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
- $update_template = [
- $templates->used_count -= 1
- ];
- $templates->update($update_template);
- }
- }
- }
- $update_templates = $this->cmsContentTemplateSet->where('id', $id)->update($templateSet);
- if (!$update_templates) {
- throw new HttpException(500, '更新失败');
- }
- } else {
- if (count($request['rule']) > 0) {
- foreach ($request['rule'] as $v) {
- if (isset($v['link_type']) && $v['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- }
- }
- if (!$this->cmsContentTemplateSet->create($templateSet)) {
- throw new HttpException(500, '添加失败');
- }
- }
- }
- //专题被使用计数
- function update_subject($request)
- {
- $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($request['id']);
- $subject_id_array = json_decode($old_subject_id['rule'], true);
- if ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] == 1) {//链接方式都为专题
- if ($subject_id_array['link_url'] != $request['rule']['link_url']) {
- $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
- $update_template = [
- $templates->used_count -= 1
- ];
- $templates->update($update_template);
- $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- } elseif ($subject_id_array['link_type'] == 1 && $request['rule']['link_type'] != 1) {
- $templates = $this->cmsSubject->where('id', intval($subject_id_array['link_url']))->first();
- $update_template = [
- $templates->used_count -= 1
- ];
- $templates->update($update_template);
- } elseif ($subject_id_array['link_type'] != 1 && $request['rule']['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- }
- /**
- * banner配置
- */
- public function bannerSet($request)
- {
- $id = $request['id'] ?? '';
- $rules = $request['rule'];
- if (is_array($rules)) {
- if (count($rules) > 10) {
- throw new HttpException(500, '最多只能添加10个banner海报');
- }
- $this->request_data($request, $id, $rules);
- } else {
- throw new HttpException(500, '参数格式有误');
- }
- }
- /**
- * 专题广告配置
- */
- public function advertisementSet($request)
- {
- $advertisement_id = $request['id'] ?? '';
- $subject = [
- 'tpl_id' => $request['tpl_id'],
- 'rule' => json_encode($request['rule']),
- 'area_type' => $request['area_type'],
- 'status' => 0,
- ];
- if (empty($advertisement_id)) {
- if ($request['rule']['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- if (!$this->cmsContentTemplateSet->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- } else {
- $this->update_subject($request);
- $advertisement_ids = $this->cmsContentTemplateSet->where('id', $advertisement_id)->update($subject);
- if (!$advertisement_ids) {
- throw new HttpException(500, '更新失败');
- }
- }
- }
- /**
- * 商品楼层配置
- */
- public function floorSet($request)
- {
- $floor_id = $request['id'] ?? '';
- if (isset($request['rule']['link_type'])) {
- $request['rule']['link_type'] = intval($request['rule']['link_type']);
- }
- if (isset($request['rule']['show_num'])) {
- $request['rule']['show_num'] = intval($request['rule']['show_num']);
- }
- $subject = [
- 'tpl_id' => $request['tpl_id'],
- 'rule' => json_encode($request['rule']),
- 'area_type' => $request['area_type'],
- 'status' => 0,
- 'sort' => isset($request['sort']) ? $request['sort'] : 999,
- 'name' => isset($request['name']) ? $request['name'] : '',
- 'floor_img' => isset($request['floor_img']) ? $request['floor_img'] : '',
- ];
- if (empty($floor_id)) {
- if ($request['rule']['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- if (!$this->cmsContentTemplateSet->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- } else {
- $this->update_subject($request);
- $floor_ids = $this->cmsContentTemplateSet->where('id', $floor_id)->update($subject);
- if (!$floor_ids) {
- throw new HttpException(500, '更新失败');
- }
- }
- }
- /**
- * 分类专题配置
- */
- public function categorySet($request)
- {
- $category_id = $request['id'] ?? '';
- $subject = [
- 'tpl_id' => $request['tpl_id'],
- 'rule' => json_encode($request['rule']),
- 'area_type' => $request['area_type'],
- 'status' => 0,
- ];
- if (empty($category_id)) {
- if ($request['rule']['link_type'] == 1) {
- $templates = $this->cmsSubject->where('id', intval($request['rule']['link_url']))->first();
- $templates->used_count += 1;
- $templates->save();
- }
- if (!$this->cmsContentTemplateSet->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- } else {
- $this->update_subject($request);
- $category_ids = $this->cmsContentTemplateSet->where('id', $category_id)->update($subject);
- if (!$category_ids) {
- throw new HttpException(500, '更新失败');
- }
- }
- }
- //左一右二配置
- public function subjectOne($request)
- {
- $id = $request['id'] ?? '';
- $rules = $request['rule'];
- $this->request_data($request, $id, $rules);
- }
- //上一下三配置
- public function subjectTwo($request)
- {
- $id = $request['id'] ?? '';
- $rules = $request['rule'];
- $this->request_data($request, $id, $rules);
- }
- /**
- * 内容发布
- */
- public function release($request)
- {
- $group_key = config('constants.CMS_GROUP');
- $market_key = config('constants.CMS_MARKET');
- //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
- $template = $this->cmsContentTemplate->select('id', 'city_name', 'city_id', 'title', 'apply_type', 'is_open', 'status')->where('id', $request['tpl_id'])->first();
- $where = [
- 'city_id' => $template->city_id,
- 'title' => $template->title,
- 'apply_type' => $template->apply_type,
- ];
- $templates = $this->cmsContentTemplate->where($where)->get();
- $update_is_open_ids = [];
- foreach ($templates->toArray() as $k => $v) {
- if ($v['status'] == 1) {
- $update_is_open_ids[] = $v['id'];
- }
- }
- if ($update_is_open_ids) {
- $delete_open_ids = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->get();
- if (count($delete_open_ids) > 0) {
- $result = $this->cmsContentTemplate->whereIn('id', $update_is_open_ids)->delete();
- if (!$result) {
- throw new HttpException(500, '删除失败,请重试');
- }
- }
- $update_open_ids = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->get();
- if (count($update_open_ids) > 0) {
- $res = $this->cmsContentTemplateSet->whereIn('tpl_id', $update_is_open_ids)->update(['status' => 0]);
- if (!$res) {
- throw new HttpException(500, '修改失败,请重试');
- }
- }
- }
- $update_tpl = $this->cmsContentTemplate->where('id', $request['tpl_id'])->first();
- if ($update_tpl) {
- $result = $this->cmsContentTemplate->where('id', $request['tpl_id'])->update(['status' => 1]);
- if (!$result) {
- throw new HttpException(500, '修改失败,请重试');
- }
- }
- $template_id = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->get();
- if (count($template_id) > 0) {
- $res = $this->cmsContentTemplateSet->where('tpl_id', $request['tpl_id'])->update(['status' => 1]);
- if (!$res) {
- throw new HttpException(500, '修改失败,请重试');
- }
- }
- foreach ($templates->toArray() as $k => $v) {
- $result = $this->cmsContentTemplateSet->where(['status' => 0, 'tpl_id' => $v['id']])->delete();
- if ($result) {
- return Response::create();
- }
- }
- if ($template->apply_type == 0) {
- if (Cache::has($group_key)) {
- Cache::forget($group_key);
- }
- } elseif ($template->apply_type == 1) {
- if (Cache::has($market_key)) {
- Cache::forget($market_key);
- }
- }
- }
- /**
- * 内容预览
- */
- public function preview($request)
- {
- $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
- if (!$temalates) {
- throw new HttpException(500, '没有找到对应模板');
- }
- //团购首页
- $group_array = [];
- if ($request['type'] == 0) {
- $group_array['apply_type'] = 0;
- $group_array['tpl_id'] = $request['tpl_id'];
- $group_array['title'] = $temalates->title;
- $group_array['content'] = [];
- } else {//菜市场首页
- $group_array['apply_type'] = 1;
- $group_array['tpl_id'] = $request['tpl_id'];
- $group_array['title'] = $temalates->title;
- $group_array['content'] = [];
- }
- $group_array['content'][0]['area_type'] = 0;
- $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
- $new_rule = [];
- foreach ($banner_rule->toArray() as $k => $v) {
- if (count(json_decode($v['rule'])) > 0) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- }
- }
- $group_array['content'][0]['rule'] = $new_rule;
- $group_array['content'][1]['area_type'] = 1;
- $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 1])->get();
- $new_rule = [];
- foreach ($subject_rule->toArray() as $k => $v) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- }
- $group_array['content'][1]['rule'] = $new_rule;
- $group_array['content'][2]['area_type'] = 2;
- $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->get();
- $new_rule = [];
- foreach ($floor_rule->toArray() as $k => $v) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- }
- foreach ($new_rule as $k => $v) {
- if ($v) {
- $rules = $v['rule'];
- $show_num = intval($rules['show_num']);
- $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
- $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
- $pro_array = $product->toArray();
- $res_id = implode(",", array_column($pro_array, 'product_id'));
- $new_rule[$k]['product_id'] = $res_id;
- $new_rule[$k]['subject_id'] = $rules['link_url'];
- $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
- unset($v['url']);
- unset($v['show_num']);
- unset($rules['link_url']);
- unset($v['link_type']);
- }
- }
- $group_array['content'][2]['rule'] = $new_rule;
- if ($request['type'] == 1) {
- $group_array['content'][3]['area_type'] = 3;
- $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 3])->get();
- $new_rule = [];
- foreach ($category_rule->toArray() as $k => $v) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- }
- $group_array['content'][3]['rule'] = $new_rule;
- }
- return $group_array;
- }
- public function getTemplate($cityId)
- {
- $group = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 0])->get();
- $groups = $group->toArray();
- if (count($groups) > 1) {
- foreach ($groups as $key => $val) {
- if ($val['status'] == 1) {
- unset($groups[$key]);
- }
- }
- }
- $market = $this->cmsContentTemplate->select('id', 'status', 'title', 'apply_type')->where(['city_id' => $cityId, 'deleted_at' => null, 'apply_type' => 1])->get();
- $markets = $market->toArray();
- if (count($markets) > 1) {
- foreach ($markets as $key => $val) {
- if ($val['status'] == 1) {
- unset($markets[$key]);
- }
- }
- }
- $templates['cms_content_templates'] = array_merge($groups, $markets);
- return $templates;
- }
- public function templateSetDelete($request)
- {
- $advertisement = $this->cmsContentTemplateSet->where('id', $request['id'])->first();
- $res = $advertisement->delete();
- if (!$res) {
- return Response::create([
- 'message' => '删除失败,请重试',
- 'status_code' => 500
- ]);
- }
- }
- /**
- * v0.3兑换商城内容预览
- */
- public function exchangeMall($request)
- {
- $temalates = $this->cmsContentTemplate->select('title')->where(['id' => $request['tpl_id']])->first();
- if (!$temalates) {
- throw new HttpException(500, '没有找到对应模板');
- }
- $group_array = [];
- $group_array['apply_type'] = 2;
- $group_array['tpl_id'] = intval($request['tpl_id']);
- $group_array['title'] = $temalates->title;
- $group_array['content'] = [];
- $group_array['content'][0]['area_type'] = 0;
- $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 0])->orderBy('id', 'desc')->limit(1)->get();
- $new_rule = [];
- foreach ($banner_rule->toArray() as $k => $v) {
- // if (count(json_decode($v['rule'])) > 0) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- // }
- }
- $group_array['content'][0]['rule'] = $new_rule;
- $all_subject = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name', 'floor_img', 'area_type')->where(['tpl_id' => $request['tpl_id']])->whereIn('area_type', [4, 5])->orderBy('sort', 'asc')->get();
- $all_subject = $all_subject->toArray();
- $count = count($all_subject);
- $new_rule = [];
- foreach ($all_subject as $k => $v) {
- // if (count(json_decode($v['rule'])) > 0) {
- $new_rule[$k]['area_type'] = $v['area_type'];
- $new_rule[$k]['rule']['id'] = $v['id'];
- // $new_rule[$k]['rule']['name'] = $v['name'];
- $new_rule[$k]['rule']['sort'] = $v['sort'];
- $new_rule[$k]['rule']['floor_img'] = $v['floor_img'];
- $new_rule[$k]['rule']['rule'] = json_decode($v['rule'], true);
- // }
- }
- foreach ($new_rule as $k => $v) {
- $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
- $group_array['content'][$k + 1]['rule'] = array($new_rule[$k]['rule']);
- }
- $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule', 'floor_img')->where(['tpl_id' => $request['tpl_id'], 'area_type' => 2])->orderBy('id', 'asc')->get();
- $new_rule = [];
- foreach ($floor_rule->toArray() as $k => $v) {
- $new_rule[$k]['id'] = $v['id'];
- $new_rule[$k]['floor_img'] = $v['floor_img'];
- $new_rule[$k]['rule'] = json_decode($v['rule'], true);
- }
- foreach ($new_rule as $k => $v) {
- if ($v) {
- $rules = $v['rule'];
- $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
- $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->get();
- $pro_array = $product->toArray();
- $res_id = implode(",", array_column($pro_array, 'product_id'));
- $new_rule[$k]['product_id'] = $res_id;
- $new_rule[$k]['subject_id'] = intval($rules['link_url']);
- $new_rule[$k]['show_type'] = $show_type->show_type ?? '';
- unset($v['url']);
- unset($rules['link_url']);
- unset($v['link_type']);
- }
- }
- $group_array['content'][$count + 1]['area_type'] = 2;
- $group_array['content'][$count + 1]['rule'] = $new_rule;
- return $group_array;
- }
- }
|