<?php
namespace Service\Controller;
use Base\Webbase;
class SeckillController extends Webbase
{
/**
* 预约操作
*/
public function order()
{
$cond1 = array(
'starttime' => array('ELT',time()),
'endtime' => array('EGT',time())
);
M()->startTrans();//开启事务
$plan = M('maskplans')->lock(true)->where($cond1)->find();
// echo M('maskplans')->getLastSql();
// dump($plan);die();
if (!$plan || $plan['plan_num']<=$plan['real_num'])
{
$this->weberror(self::THIS_ACTION_ERROR,"当前场次已结束!");
}
$cond2 = array(
'idcard'=> $_POST['idcard'],
'add_time' => array("EGT",strtotime("-7 day"))
);
$order = M('maskorders')->where($cond2)->find();
if ($order)
{
$this->weberror(self::THIS_ACTION_ERROR,"每人7天只能预约1次,请勿重复预约!");
}
$postData = array(
// 'name'=>$_POST['name'],
// 'idcard'=>$_POST['idcard'],
// 'lat'=>$_POST['lat'] ,
// 'lon'=>$_POST['lon'] ,
// 'sku'=>rand(100000,999999),
// 'add_time'=>time()
'name'=>"a".rand(100,999),
'idcard'=>rand(1000000,9999999),
'lat'=>1,
'lon'=>2,
'sku'=>rand(100000,999999),
'add_time'=>time()
);
if (!$postData['name'] || !$postData['idcard' || !$postData['lon'] || !$postData['lat'])
{
$this->weberror(self::THIS_ACTION_ERROR,"信息填写错误!");
}
$status =M('maskplans')->lock(true)->where(array('id'=>$plan['id']))->setInc('real_num');
if ($status)
{
$re = M('maskorders')->add($postData);
if ($re)
{
$data = array(
'sku'=>$postData['sku']
);
M()->commit();//事务提交
$this->websuccess(self::OUTPUT_SUCCESS,"预约成功",$data);
}
}
M()->rollback();//回滚
$this->weberror(self::THIS_ACTION_ERROR,"预约失败!");
}
/**
* 我的预约
*/
public function myAppointment()
{
$idcard = $_POST['idcard'];
$re = M('maskorders')->where(array('idcard'=>$idcard))->order('add_time desc')->select();
if ($re)
{
foreach ($re as &$item)
{
$item['add_time'] = date("Y-m-d H:i:s",$item['add_time']);
}
$this->websuccess(self::OUTPUT_SUCCESS,"成功!",$re);
}
$this->websuccess(self::OUTPUT_SUCCESS,"暂无数据!");
}
/**
* 预约计划列表
*/
public function appointList()
{
$cond = array(
'end_time' =>array('GT',time())
);
$lists = M('maskplans')->where($cond)->select();
if ($lists)
{
foreach ($lists as &$item)
{
if ($item['starttime']<time() && $item['endtime'] >time())
{
$item['show'] = 1;
}else{
$item['show'] = 0;
}
$item['starttime'] = date("Y-m-d H:i:s",$item['starttime']);
$item['endtime'] = date("Y-m-d H:i:s",$item['endtime']);
}
$this->websuccess(self::OUTPUT_SUCCESS,"成功!",$lists);
}
$this->websuccess(self::OUTPUT_SUCCESS,"暂无数据!");
}
}
/*
create table yixiang_maskorders(
`id` int(11) NOT NULL AUTO_INCREMENT primary key,
`name` varchar(255) NOT NULL COMMENT "姓名",
`idcard` varchar(255) NOT NULL COMMENT "身份证号",
`lat` varchar(255) NOT NULL COMMENT "经度",
`lon` varchar(255) NOT NULL COMMENT "纬度",
`sku` varchar(255) NOT NULL COMMENT "编号",
`add_time` varchar(255) NOT NULL COMMENT "抢购时间",
);
create table yixiang_maskplans(
`id` int(11) NOT NULL AUTO_INCREMENT primary key,
`starttime` int(11) NOT NULL COMMENT "计划投放开始时间",
`endtime` int(11) NOT NULL COMMENT "计划投放结束时间",
`plan_locale` varchar(255) NOT NULL COMMENT "地点",
`plan_num` int(11) NOT NULL COMMENT "计划发放量",
`real_num` int(11) NOT NULL COMMENT "实际发放量"
);
使用ab测试:
效果应该如图所示