<?php
/**
* Created by PhpStorm.
* User: lcz
* Date: 2018/7/19
* Time: 9:40
* 商家
*/
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\BusinessCoupon;
use think\Db;
class Seller extends Api{
protected $noNeedRight = ['*'];
protected $noNeedLogin = ['index'];
/**
* 店铺首页
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(){
//商家ID
$busId = input('post.id');
!$busId && $this->error('参数错误');
$returnData = [
'banner' => [],
];
$time = time();
$where = [
'ai.position' => 10,
'ai.begin_time' => ['elt', $time],
'ai.end_time' => ['egt', $time],
'ai.business_id' => $busId,
];
//顶部轮播图
$returnData['banner'] = Db::name('appIndex')->alias('ai')
->join('appClass ac', 'ai.app_class_id = ac.id', 'left')
->where($where)
->field('ai.position, ai.app_image, ai.name, ai.url, ai.param_id, ai.login, ac.ios_class, ac.android_class')
->order(['ai.sort' => 'ASC'])
->select();
$goodsModel = new \app\common\model\Goods();
//推荐商品
$recommendGoods = Db::name('goods')
->where([
'is_on_sale' => $goodsModel::IS_ON_SALE_Y,
'is_recommend' => $goodsModel::RECOMMEND_Y,
'business_id' => $busId,
'effective_time' => ['elt', time()],
])
->order(['sort' => 'ASC'])
->field('id, goods_name, original_image, shop_price, market_price, sale_number, exchange_integral')
->limit(20)
->select();
$returnData['recommend'] = $recommendGoods;
$this->success('请求成功', $returnData);
}
/**
* 店铺优惠券列表
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function coupon(){
$id = input('post.id', '', 'intval');
!$id && $this->error('参数错误');
$model = new BusinessCoupon();
$rows = Db::name('businessCoupon')
->where([
'business_id' => $id,
'receive_start_time' => ['elt', time()],
'receive_end_time' => ['egt', time()],
'status' => $model::STATUS_Y
])
->field('id, \'店铺优惠券\' as name , amount, min_amount, from_unixtime(use_start_time) as start_time, from_unixtime(use_end_time) as end_time, 0 as receive_status')
->order(['sort' => 'ASC'])
->select();
if($rows) {
$ids = [];
foreach($rows as $row) {
$ids[] = $row['id'];
}
$userCoupon = Db::name('userCoupon')
->where(['user_id' => $this->auth->id, 'coupon_id' => ['in', $ids]])
->column('coupon_id', 'id');
if($userCoupon) {
foreach($rows as $k => $v) {
if(in_array($v['id'], $userCoupon)) {
$rows[$k]['receive_status'] = 1;
}
}
}
}
$this->success('查询成功', $rows);
}
/**
* 领取优惠券
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function receiveCoupon(){
$id = input('post.id', '', 'intval');
!$id && $this->error('请选择要领取的优惠券');
$userId = $this->auth->id;
$model = new BusinessCoupon();
$coupon = Db::name('businessCoupon')
->where([
'id' => $id,
'status' => $model::STATUS_Y,
'receive_start_time' => ['elt', time()],
'receive_end_time' => ['egt', time()],
])->find();
!$coupon && $this->error('优惠券不存在');
$row = Db::name('userCoupon')
->where(['coupon_id' => $id, 'user_id' => $userId])
->find();
$row && $this->error('你已经领取过了');
$res = $model->receiveCoupon($coupon, $userId);
if($res) {
$this->success('领取成功');
} else {
$this->error('领取出错:'.$model->getError());
}
}
}