<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\services\operate;

use app\model\operate\Ads;

class AdsService{

	/**
	 * @title 获取广告
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getDataList($request){
		$param = $request->param();
		$order = "id desc";
		$map = [];
		if(isset($param['title']) && $param['title'] != ''){
			$map[] = ['title', 'LIKE', '%' . $param['title'] . '%'];
		}
		if(isset($param['name']) && $param['name'] != ''){
			$map[] = ['name', '=', $param['name']];
		}

		$list = Ads::where($map)->order($order)->append(['status_text', 'photo'])->paginate($request->pageConfig);
		return $list;
	}

	/**
	 * @title 添加广告
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function create($request){
		$data = $request->param();

		$data['user_id'] = $request->user['uid'];
		return Ads::create($data);
	}

	/**
	 * @title 编辑广告
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function update($request){
		$data = $request->param();

		$ads = Ads::find($data['id']);
		return $ads->save($data);
	}

	/**
	 * @title 删除广告
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function delete($request){
		$id = $request->post('id', 0);
		if(!$id){
			throw new \think\Exception("非法操作！", 1);
		}
		
		return Ads::where('id', $id)->delete();
	}

	public function getAdsDetail($request){
		$name = $request->param('name', '');
		return Ads::where('name', $name)->append(['photo'])->find();
	}
}