123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Repositories;
- use App\Models\CmsContentTemplate;
- use App\Models\CmsContentTemplateSet;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- class CmsContentTemplateRepository {
- public function __construct(CmsContentTemplate $cmsContentTemplate) {
- $this->cmsContentTemplate = $cmsContentTemplate;
- }
- //模板列表
- public function index($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
- $where = [];
- if (isset($request['is_open'])){
- $where[] = ['is_open','=',$request['is_open']];
- }
- if (isset($request['city_id'])){
- $where[] = [
- 'city_id','=',$request['city_id'],
- ];
- }
- if (isset($request['status'])){
- $where[] = [
- 'status','=',$request['status'],
- ];
- }
- return $this->cmsContentTemplate->where($where)->where('status',1)->orderBy('id', 'asc')->paginate($perPage);
- }
- /**
- * 新建模板
- */
- public function create($request)
- {
- if (isset($request['city_id'])) {
- if ($this->cmsContentTemplate->where(['city_id' => $request['city_id'], 'apply_type' => $request['apply_type']])->exists()) {
- throw new HttpException(500, '当前城市该模版类型已存在');
- }
- }
- if ($request['apply_type'] == 2) {
- if ($this->cmsContentTemplate->where('apply_type', '=', 2)->exists()) {
- throw new HttpException(500, '兑换商城只能有一个');
- }
- }
- $subject = [
- 'title' => $request['title'],
- 'city_id' => isset($request['city_id']) ? $request['city_id'] : 0,
- 'city_name' => isset($request['city_name']) ? $request['city_name'] : '',
- 'apply_type' => $request['apply_type'],
- 'status' => 1,
- 'is_open' => 0,
- ];
- //默认开启,且不可手动关闭
- if (isset($request['city_id']) && $request['city_id'] == 610100){
- $subject['is_open'] = 1;
- }
- if ($request['apply_type'] == 2){
- $subject['is_open'] = 1;
- }
- if (!$this->cmsContentTemplate->create($subject)) {
- throw new HttpException(500, '添加失败');
- }
- }
- /**
- * 修改模板状态
- */
- public function edit($request)
- {
- $group_key = config('constants.CMS_GROUP');
- $market_key = config('constants.CMS_MARKET');
- $template_id = $this->cmsContentTemplate->find($request['id']);
- if ($request['city_id'] == 610100 && $request['is_open'] == 0){
- throw new HttpException(500, '城市为西安的模板不允许手动关闭');
- }
- if ($request['is_open'] == 1 && $template_id->status == 0){
- throw new HttpException(500, '请先配置内容并发布之后再开启');
- }
- $template = CmsContentTemplateSet::where('tpl_id',$request['id'])->get();
- if (count($template) == 0){
- throw new HttpException(500, '配置内容为空无法开启');
- }
- if ($template_id->apply_type == 2){
- throw new HttpException(500, '兑换商城不允许手动关闭');
- }
- $template_id->is_open = $request['is_open'];
- $template_id->updated_at = date('Y-m-d H:i:s');
- $res = $template_id->save();
- if (!$res) {
- throw new HttpException(500, '修改状态失败');
- }
- if ($template_id->apply_type == 0){
- if (Cache::has($group_key)) {
- Cache::forget($group_key);
- }
- }elseif ($template_id->apply_type == 1){
- if (Cache::has($market_key)) {
- Cache::forget($market_key);
- }
- }
- }
- /**
- * 修改模板名称
- * @param $data
- * @return mixed
- */
- public function updateTemplateName($data)
- {
- $template = $this->cmsContentTemplate->select('city_id','apply_type')->where('id',$data['id'])->first();
- $template_array = $this->cmsContentTemplate->where(['city_id' => $template->city_id, 'apply_type' => $template->apply_type])->get();
- if (count($template_array)>1){
- foreach ($template_array->toArray() as $value){
- $res = $this->cmsContentTemplate->where('id',$value['id'])->update(['title'=>$data['title']]);
- if (!$res){
- throw new HttpException(500, '修改失败,请重试');
- }
- }
- }
- return $this->cmsContentTemplate->where('id',$data['id'])->update(['title'=>$data['title']]);
- }
- }
|