<?php

namespace wstmart\admin\model;

use think\Db;

/**

 * ============================================================================

 * 广告业务处理

 */

class adgoods extends Base{

	/**

	 * 分页

	 */

	public function pageQuery(){

		$where = [];

		$apId = (int)input('adId');

		if($apId>0)$where['a.adId'] = $apId;

		return Db::name('adgoods')->alias('a')

		            ->join('ads ap','a.adId=ap.adId','left')

					->join('goods g','g.goodsId=a.goodsId')

					->field('a.adId,adName,g.goodsId,goodsName,a.startDate,a.endDate,adGoodsImg,a.adGoodsStatus,a.adGoodsId')

		            ->where($where)->order('a.adId desc')

		            ->order('a.goodsId','asc')

		            ->paginate(input('limit/d'));

	}

	public function getById($id){

		$result= $this->get(['adGoodsId'=>$id]);

		//$result['adName']=db('ads')->where('adId',$result['adId'])->value('adName');

		return $result;

	}

	/**

	 * 新增

	 */

	public function add(){

		$data = input('post.');

		$data['createTime'] = time();

		WSTUnset($data,'adGoodsId');

		Db::startTrans();

		try{

			$goods=db('goods')->where(['goodsId'=>$data['goodsId'],'dataFlag'=>1,'isSale'=>1,'goodsStatus'=>1])->find();

			if(!$goods) return WSTReturn('无此商品,请重新选择商品');

			$result = $this->validate('adgoods.add')->allowField(true)->save($data);

        	if(false !== $result){

        		WSTClearAllCache();

				$id = $this->adId;

        	    //启用上传图片

			    WSTUseImages(1, $id, $data['adGoodsImg']);

        		Db::commit();

        	    return WSTReturn("新增成功", 1);

        	}else{

        		return WSTReturn($this->getError(),-1); 

        	}

		}catch (\Exception $e) {

            Db::rollback();errLog($e);

        }

        return WSTReturn('新增失败',-1); 

	} 

    /**

	 * 编辑

	 */

	public function edit(){

		$data = input('post.');

		WSTUnset($data,'createTime');

		Db::startTrans();

		try{

			//WSTUseImages(1, (int)$data['adId'], $data['adFile'], 'ads-pic', 'adFile');

		    $result = $this->validate('adgoods.edit')->allowField(true)->save($data,['adGoodsId'=>(int)$data['adGoodsId']]);

	        if(false !== $result){

	        	WSTClearAllCache();

	        	Db::commit();

	        	return WSTReturn("编辑成功", 1);

	        }else{

        		return WSTReturn($this->getError(),-1); 

        	}

		}catch (\Exception $e) {

            Db::rollback();errLog($e);

        }

        return WSTReturn('编辑失败',-1); 

	}

	/**

	 * 删除

	 */

    public function del(){

	    $id = (int)input('post.id/d');

	    Db::startTrans();

		try{

		    $result = $this->where(['adGoodsId'=>$id])->delete();

		    WSTUnuseImage('adgoods','adGoodsImg',$id);

	        if($result){

	        	WSTClearAllCache();

	        	Db::commit();

	        	return WSTReturn("删除成功", 1);

	        }

		}catch (\Exception $e) {

            Db::rollback();errLog($e);

            return WSTReturn('删除失败',-1);

        }

	}

	/**

	* 修改广告排序

	*/

	public function changeSort(){

		$id = (int)input('id');

		$adSort = (int)input('adSort');

		$result = $this->setField(['adId'=>$id,'adSort'=>$adSort]);

		if(false !== $result){

			WSTClearAllCache();

        	return WSTReturn("操作成功", 1);

        }else{

        	return WSTReturn($this->getError(),-1);

        }

	}

	

}