近期我們公司專案裡陸陸續續有很多為了招引新使用者的活動推出,砍價的活動由我來負責,我們的專案是在微信瀏覽器裡供使用者瀏覽訪問。
大概描述:進入砍價活動列表頁選擇有意向的商品,使用者點選商品圖片可以看到WEB商城中所賣的商品價格與詳細引數等資訊,點選列表中對應商品標識下的'馬上搶購'可以進入砍價頁面,該頁面有兩個主要按鈕,一個是'請土豪幫忙'(點選之後起引導分享作用)、'買買買'(砍到一定價位後可以購買),其次包括價格的進度條等資訊,使用者分享到朋友圈引來朋友幫忙砍價,在觸發砍價按鈕的同時,如果幫忙的朋友不是本站使用者,那麼幫忙的朋友會成為本站的會員,同時會是該分享使用者的下級,然後根據產品運營採購針對每個商品的討論,會給商品定義好要砍價的活動價和最低價,以及每砍一次所能砍掉的價格區間和要參與的人數做計算,比如(商品原價3000,最低價1000,那麼所能砍掉的價格是2000,規定參與的人數是500人,那麼平均一個人砍掉4塊錢,可以設定區間為1~7元,來設定砍價的起伏大小,來提高使用者的參與興趣),當使用者砍到規定的價格區間內購買時,跳轉到訂單確認頁面,然後下單支付一系列流程。
頁面截圖:
實現相關:
通過看到幾張圖片大概我想大家也會考慮到所涉及的相關資訊。
想要釋出參與活動的商品就有一個商品區分表示,在資料庫的設計當中,我沒有修改商品表來增加一個區分的欄位,而是新建立了一個資料表作為專門放置砍價活動的相關資訊。
來看一下後臺的前臺展示我是這麼設計的,沒有經過專業前端之手,僅僅在複製了後臺商品列表模組程式碼的基礎上自己簡單的做了一下佈局。
我的資料表設計:
#活動商品設定表:
CREATE TABLE `hp_activity_bargain` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`product_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品ID',
`product_name` varchar(200) COLLATE utf8_unicode_ci NOT NULL COMMENT '商品名稱',
`activity_money` decimal(7,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '活動價',
`bargain_section` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '砍價區間',
`bargain_section2` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '砍價區間2【使用者線上砍價(新使用者砍價區間)】', #忽略,此處是迭代後期地推而加上的
`join_count` smallint(3) unsigned NOT NULL DEFAULT '0' COMMENT '參與人數',
`product_desc` varchar(80) COLLATE utf8_unicode_ci NOT NULL COMMENT '活動商品描述',
`attr1_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'attr1屬性',
`attr2_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'attr2屬性',
`type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0是線上,1是地推',
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`),
KEY `attr2_id` (`attr2_id`),
KEY `attr1_id` (`attr1_id`),
KEY `type` (`type`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#使用者參與進度資訊表
CREATE TABLE `hp_activity_bargainirg` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
`activity_bargain_id` int(10) unsigned NOT NULL COMMENT 'activity_prodcuts主鍵id',
`product_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '參與活動的商品',
`attr1_id` smallint(5) unsigned NOT NULL COMMENT 'attr1屬性id',
`attr2_id` smallint(5) unsigned NOT NULL COMMENT 'attr2屬性id',
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '砍價商品發起的使用者ID',
`bargain_count` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '被砍價次數',
`deal_money` decimal(7,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '最終交易價格',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '發起時間',
`is_addorder` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否下單(0:未下單,1已下單)',
`type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0是線上,1是地推', #可以忽略,後期地推加上去的
PRIMARY KEY (`id`),
KEY `activity_bargain_id` (`activity_bargain_id`),
KEY `attr1_id` (`attr1_id`),
KEY `attr2_id` (`attr2_id`),
KEY `product_id` (`product_id`),
KEY `user_id` (`user_id`),
KEY `is_addorder` (`is_addorder`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#參與砍價詳情表
CREATE TABLE `hp_activity_bargain_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵ID',
`bargain_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'activity_bargainirg表主鍵id',
`assistor_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '幫助者ID',
`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '參與時間',
`bargain_money` decimal(5,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '砍掉價格',
PRIMARY KEY (`id`),
KEY `assistor_id` (`assistor_id`),
KEY `bargain_id` (`bargain_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
控制器幾個方法:
//線上砍價活動列表 public function bargainirgAction() { $type = 0; $way = input('param.way', '','string'); if (is_not_empty_string($way) && $way == 'live') { $type = 1; } $pageSize = 10; if (Request::isAjax()) { $page = input('post.page', 0, 'intval'); $product_list = Hmodel\Activity::getActivityBargainProducts($type, $pageSize, $page * $pageSize); if (is_not_empty_array($product_list)) { return json_encode(['status' => 1, 'info' => $product_list]); } else { return json_encode(['status' => 0]); } } $product_list = Hmodel\Activity::getActivityBargainProducts($type, 10, 0); $view = new view(); $view->assign('bargainirgList',$product_list); if ($type == 0) { return $view->fetch('bargainirg'); } else { return $view->fetch('bargainirg_live'); } } //砍價活動\商品詳情\檢視貢獻度\請幫忙 public function bargaindetailAction() { $this->checkUserLogin(); $uid = session('userinfo.uid'); // $uid = 3; $seting_id = input('param.id',0,'int'); $seting_info = Hmodel\Activity::getActivityProductsSeting($seting_id); //砍價活動商品設定 $bargain_section2 = !empty($seting_info['bargain_section2']) ? $seting_info['bargain_section2'] : 'no seting'; if (!is_not_empty_array($seting_info)) notFund(); $bargain_progress = Hmodel\Activity::returnProgressData( $seting_id, $seting_info['product_id'], $uid, $seting_info['attr1_id'],$seting_info['attr2_id'],
$seting_info['add_money'],$seting_info['type']); if (!is_not_empty_array($bargain_progress) || $bargain_progress['user_id'] != $uid) notFund(); $username = session('userinfo.username'); $user_info = Hmodel\User::getuser_info($uid); $invite_code = $user_info['invite_code']; $encrypt_code = encrypt_hopeband($bargain_progress['id'] . '(&)' .$bargain_progress['activity_bargain_id'] . '(&)' .$uid . '(&)' . $invite_code . '(&)'
. $seting_info['product_id'] . '(&)' . $seting_info['activity_money'] . '(&)' . $seting_info['bargain_section'] . '(&)' . $seting_info['bargain_section2'] .'(&)'
. $seting_info['join_count'] . '(&)'. $bargain_progress['type'], 'E', 'Hp_HopeBand_Bargainirg'); //是否已經下單 // $is_addorder = Hmodel\Activity::checkIsAddorder($bargain_progress['id']); $is_addorder = $bargain_progress['is_addorder']; //幫助列表 $assistor_list = Hmodel\Activity::getAssistorList($bargain_progress['id']); $view = new view(); $view->assign([ 'bar_code' => $encrypt_code, 'seting_info' => $seting_info, 'bargain_progress' => $bargain_progress, 'assistor_list' => $assistor_list, 'seting' => $seting_id, 'is_addorder' => $is_addorder ]); $view->assign(); return $view->fetch(); } //幫忙砍價\進度\底部砍價商品列表 public function bargainirgingAction () { $url = $_SERVER['REQUEST_URI']; $encrypt_code = substr(substr($url,29),0,strpos(substr($url,29), '?invite_code')); $bargain_param = self::retrunBargainCode($encrypt_code); $bargain_id = $bargain_param['bargain_id']; $bargainInfo = Hmodel\Activity::getBargainirgProgress($bargain_id); if ( !is_not_empty_array($bargain_param) || !is_not_empty_array($bargainInfo)) { notFund(); } $is_addorder = $bargainInfo['is_addorder'] == 1 ? true : false; $uid = session('userinfo.uid'); $activity_product_id = $bargain_param['activity_product_id']; if ($bargain_param['sponsor_uid'] == $uid) { $this->redirect('bargaindetail',['id' => $activity_product_id]); } $product_id = $bargain_param['product_id']; $bargain_list = Hmodel\Activity::getActivityBargainProducts($bargain_param['type'], 999); //所有參與砍價活動的商品 foreach ($bargain_list as $v) { if ( $v['id'] == $activity_product_id) { $product_info = $v; } } if (!is_not_empty_array($product_info)) notFund(); $type = $bargain_param['type']; $activity_bargain_url = url('activity/bargainirg') ; $view = new view(); $view->assign([ 'bar_code' => $encrypt_code, //邀請碼 'bargainInfo' => $bargainInfo, //當前砍價進度 'product_info' => $product_info, //商品詳情 'bargain_list' => $bargain_list, //底部相關推薦 'is_addorder' => $is_addorder, //是否入庫 'activity_bargain_url' => $activity_bargain_url ]); return $view->fetch(); } //ajax砍價 public function goBargainAction () { if (Request::isAjax()) { $uid = session('userinfo.uid'); $username = session('userinfo.username'); $encrypt_code = input('post.bar_code', '', 'string'); if (empty($uid) || empty($username)) { $this->checkUserLogin(); } $bargain_param = self::retrunBargainCode($encrypt_code); if (!is_not_empty_array($bargain_param)) { echo json_encode(array('status' => -3, 'info' => '不明錯誤,請聯絡客服'));die; } $seting_info = Hmodel\Activity::getActivityProductsSeting($bargain_param['activity_product_id']); //砍價活動商品設定 $stock = Hmodel\CategoryAttr::getproductstockbyidsonattr($seting_info['product_id'],$seting_info['attr1_id'],$seting_info['attr2_id']); if ($stock['category_sum'] < 1) { echo json_encode(['status' => -1, 'info' => '已搶光!']);die; } $userinfo = Hmodel\User::getuser_info($uid); $register_time = $userinfo['create_time']; $is_new_user = false; //使用者狀態[default:老使用者] if (($register_time + (60 * 60 * 8)) > time() && Hmodel\Activity::checkUserIsbargainEd($uid) === false) { $is_new_user = true; //是新使用者 } $sponsor_uid = $bargain_param['sponsor_uid']; //發起者id $bargain_id = $bargain_param['bargain_id']; //[activity_bargainirg]表主鍵id $join_count = $bargain_param['join_count']; //設定砍價次數 $section = $bargain_param['bargain_section']; //砍價區間(老使用者) $section2 = $bargain_param['bargain_section2']; //砍價區間(新使用者) $type = $bargain_param['type'] == $seting_info['type'] ? $bargain_param['type'] : ''; //0:線上; 1:地推 $activity_money= $bargain_param['activity_money']; //活動最低價 if (!is_not_empty_string($type)) { echo json_encode(array('status' => -3, 'info' => '不明錯誤,請聯絡客服'));die; } if ($uid == $sponsor_uid) { echo json_encode(array('status' => -1, 'info' => '不能給自己砍價'));die; } $state = Hmodel\Activity::checkPartBargain($bargain_id, $uid); //是否幫夥伴砍過當前參與的進度 if ( $state !== false) { echo json_encode(array('status' => -2, 'info' => '您已幫夥伴砍掉' . $state . '元啦,不要再砍啦!'));die; } if ($type == 1 && $is_new_user === false) { echo json_encode(array('status' => -4, 'info' => '抱歉,該活動僅限新使用者參加!'));die; } $state = Hmodel\Activity::givePartBargain($bargain_id, $sponsor_uid, $uid, $section, $section2, $join_count, $is_new_user, $activity_money, $type); if ($state == -1) { echo json_encode(array('status' => -3, 'info' => '已經最低價啦,不能再砍啦!'));die; } if ($state === false) { echo json_encode(array('status' => -3, 'info' => '哎呀,失敗了!稍後幫我砍一次!'));die; } else { if ($is_new_user === true) { echo json_encode(array('status' => 2, 'info' => '砍掉了' . $state .'元', 'deal_money' => $state));die; } else { echo json_encode(array('status' => 1, 'info' => '成功幫夥伴砍掉' . $state .'元!', 'deal_money' => $state));die; } } } } //返回砍價活動相關資料 public static function retrunBargainCode( $encrypt_str = '') { $data = []; $code_str = encrypt_hopeband($encrypt_str, 'D', 'Hp_HopeBand_Bargainirg'); $code_arr = explode('(&)', $code_str); if (is_not_empty_array($code_arr) && count($code_arr) == 10) { $data['bargain_id'] = $code_arr[0]; //砍價活動表主鍵id $data['activity_product_id'] = $code_arr[1]; $data['sponsor_uid'] = $code_arr[2]; //砍價活動發起者uid $data['sponsor_invite_code'] = $code_arr[3]; //砍價活動發起者邀請碼 $data['product_id'] = $code_arr[4]; //砍價活動發起的商品id $data['activity_money'] = $code_arr[5]; //活動最低價格 $data['bargain_section'] = $code_arr[6]; //老使用者砍價區間 $data['bargain_section2'] = $code_arr[7]; //新使用者砍價區間 $data['join_count'] = $code_arr[8]; //設定砍價次數 $data['type'] = $code_arr[9]; //設定砍價次數 } return $data; }
public function checkOrder2PayAction()
{
$this->checkUserLogin();
if (!Request::isAjax()) { notFund(); }
$seting_id = input('post.seting',0,'intval');
$user_id = session('userinfo.uid');
//拿付款的額度和商品id
$BargainPayData = Hmodel\Activity::getBargainResult2Pay($seting_id, $user_id);
$stock = Hmodel\CategoryAttr::getproductstockbyidsonattr($BargainPayData['product_id'],$BargainPayData['attr1_id'],$BargainPayData['attr2_id']);
if ($stock['category_sum'] < 1) {
return json_encode(['status' => -2, 'info' => '已搶光!']);die;
}
if ($BargainPayData['is_addorder'] == 1) {
return json_encode(['status' => -2, 'info' => '此商品已經購買過,不能重複購買!']);die;
}
if (!is_not_empty_array( $BargainPayData)) {
return json_encode(['status' => -1, 'info' => '不明錯誤,請聯絡客服!']);die;
}
$product_id = $BargainPayData['product_id'];
$attr1_name = '';
$attr2_name = '';
if (is_not_empty_array($attr1_info = Hmodel\Activity::getAttr1NameByAttrId($BargainPayData['attr1_id'], $product_id))){
$attr1_name = $attr1_info['attr'];
}
if (is_not_empty_array($attr2_info = Hmodel\Activity::getAttr2NameByAttrId($BargainPayData['attr2_id'], $product_id))){
$attr2_name = $attr2_info['attr'];
}
$data = [
'product_id' => $product_id,
'prodcut_num' => 1,
'attr1' => $attr1_name,
'attr2' => $attr2_name,
'seting_id' => $seting_id
];
return json_encode(['status' => 1, 'info' => $data]);
}
//檢視砍價後的預付款訂單資訊
public function createActivityOrderAction ()
{
$this->checkUserLogin();
$uid = session('userinfo.uid');
$product_num = 1;
$attr1 = input("param.attr1", "" , "trim,string");
$attr2 = input("param.attr2", "" , "trim,string");
$seting_id = intval(input('param.seting_id', 0, 'intval'));
$product_id = intval(input("param.product_id", "" , "intval"));
$pay_price_money = Hmodel\Activity::returnPayMoney($product_id, $seting_id, $uid);
$type = $pay_price_money['type'];
if (!is_not_empty_array($pay_price_money)) notFund();
Cookie::set('ready_finish_bargain', encrypt_hopeband($pay_price_money['id'] . '(&)', 'E', 'hp_ready_bargain_pay'));
$pay_info = $this->calculateFromProduct($product_id, $product_num, $attr1, $attr2, $uid , $pay_price_money['deal_money'], $type);
$def_address = Hmodel\UserAddress::getDefAddress($uid);
$view = new View();
$view->assign('def_address',$def_address);
$view->assign('product_carlist_bymerchantid',$pay_info['product_carlist_bymerchantid']);
$view->assign('total_price',sprintf("%.2f",$pay_info['total_price']));
$view->assign('total_delivery',$pay_info['total_delivery']);
$view->assign('seting_id',$seting_id);
return $view->fetch('createorder');
}
Model層部分方法:
//根據主鍵id查詢活動產品相關屬性設定
public static function getActivityProductsSeting( $id = 0) {
$data = [];
if (!is_positive_integer($id)) {
return $data;
}
$sql = "SELECT a.`id`,a.`product_id`,a.`activity_money`,a.`bargain_section`,a.`join_count`,a.`product_desc`,a.`product_name`,
a.`attr1_id`,a.`attr2_id`,
b.`give_score`,b.`category_img`,b.`add_money`,b.`category_sum`
FROM `hp_activity_bargain` AS a
LEFT JOIN `hp_category_attr` AS b
ON a.`attr1_id` = b.`attr1_son_id` AND a.`attr2_id` = b.`attr2_son_id` AND a.`product_id` = b.`category_id`
WHERE a.`id` = $id
LIMIT 1";
$res = Db::query($sql);
if (is_not_empty_array($res)) {
$data = $res[0];
}
return $data;
}
//通過主鍵id拿砍價活動表的相關資訊
public static function getBargainirgProgress($id = 0) {
$data = [];
$sql = "SELECT `id`,`activity_bargain_id`,`product_id`,`attr1_id`,`attr2_id`,`user_id`,`bargain_count`,`deal_money`,`is_addorder`
FROM `hp_activity_bargainirg`
WHERE `id` = $id LIMIT 1";
$res = self::query($sql);
if (is_not_empty_array($res)) {
$data = $res[0];
}
return $data;
}
//返回要砍
public static function returnProgressData($a_b_id = 0, $product_id = 0, $user_id = 0, $attr1_son_id = 0, $attr2_son_id = 0, $deal_money= 0){
$data = [];
$sql = "SELECT `id`,`activity_bargain_id`,`product_id`,`attr1_id`,`attr2_id`,`user_id`,`bargain_count`,`deal_money`,`is_addorder`
FROM `hp_activity_bargainirg`
WHERE `activity_bargain_id` = $a_b_id AND `attr1_id` = $attr1_son_id AND `attr2_id` = $attr2_son_id AND `product_id` = $product_id AND `user_id` = $user_id
LIMIT 1";
$res = self::query($sql);
if (is_not_empty_array($res)) {
$data = $res[0];
} else {
$data['activity_bargain_id'] = $a_b_id;
$data['product_id'] = $product_id;
$data['user_id'] = $user_id;
$data['deal_money'] = $deal_money;
$data['attr1_id'] = $attr1_son_id;
$data['attr2_id'] = $attr2_son_id;
$data['create_time'] = time();
$data['bargain_count'] = 0;
Db::name('activity_bargainirg')->insert($data);
$insertId = Db::name('activity_bargainirg')->getLastInsID();
$data['id'] = $insertId;
// $data = self::getBargainirgProgress($insert_id);
}
return $data;
}
//通過 表[activity_products] 主鍵id 和 user_id 拿到使用者購買前要付款的額度和購買商品
public static function getBargainResult2Pay($a_b_id = 0, $user_id = 0) {
$data = [];
if ( !is_positive_integer($a_b_id) || !is_positive_integer($user_id)) {
return $data;
}
$sql = "SELECT `id`,`deal_money`,`product_id`,`attr1_id`,`attr2_id` FROM `hp_activity_bargainirg`
WHERE `activity_bargain_id` = $a_b_id AND `user_id` = $user_id
LIMIT 1";
$res = Db::query($sql);
if (is_not_empty_array($res)) {
$data = $res[0];
}
return $data;
}
//檢測是否幫助夥伴砍價
//$bargain_id [activity_bargainirg]表主鍵id
//$assistor_id 幫助砍價者使用者id
public static function checkPartBargain ($bargain_id = 0, $assistor_id = 0) {
$state = false;
if (!is_positive_integer($bargain_id) || !is_positive_integer($assistor_id)) {
return $state;
}
$sql = "SELECT `bargain_money` FROM `hp_activity_bargain_list`
WHERE `bargain_id` = $bargain_id AND `assistor_id` = $assistor_id
LIMIT 1";
$res = self::query($sql);
if (is_not_empty_array($res)) {
return $res[0]['bargain_money'];
}
return $state;
}
//查詢幫忙砍價的夥伴列表
public static function getAssistorList ( $bargain_id = 0){
$data = [];
if (is_positive_integer($bargain_id) && is_positive_integer($assistor_id))
return $data;
$sql = "SELECT a.`create_time`, a.`bargain_money`, b.`nickname`, b.`headimgurl`
FROM `hp_activity_bargain_list` AS a
LEFT JOIN `hp_user_auths` AS b
ON a.`assistor_id` = b.`user_id`
WHERE a.`bargain_id` = $bargain_id
ORDER BY a.`id` DESC";
$data = self::query($sql);
return $data;
}
//拿到上次所砍掉的價格
public static function getBeforeMoney ( $bargain_id = 0, $limit = 1) {
$beforemoney_sum = 0;
$sql = "SELECT SUM(`bargain_money`) AS beforemoney_sum FROM
(SELECT `bargain_money` FROM `hp_activity_bargain_list`
WHERE `bargain_id` = $bargain_id
ORDER BY `id` DESC
LIMIT $limit) sum";
$res = Db::query($sql);
if (is_not_empty_array($res)) {
$beforemoney_sum = $res[0]['beforemoney_sum'];
}
return $beforemoney_sum;
}
/**
* 砍價相關資料操作
*$bargain_id [activity_bargainirg] 表主鍵id
*$sponsor_id 砍價發起者id
*$assistor_id 幫助砍價者id
*$min 最小值
*$max 最大值
*$join_count 設定要參與砍價的人數
*return bool
*/
public static function givePartBargain($bargain_id = 0, $sponsor_id = 0, $assistor_id = 0, $min = 0, $max = 0,$join_count = 0) {
$state = false;
if (is_positive_integer($assistor_id) && $bargain_id > 0 && is_positive_integer($sponsor_id)) {
$bargainirg_info = Db::name('activity_bargainirg')->find($bargain_id);
if ( !$bargainirg_info ) {
return $state;
}
$fp = fopen('./bargain_lock.txt','r');
$try = 5;
do {
$lock = flock($fp,LOCK_EX);
if(!$lock)
usleep(5000);
} while (!$lock && --$try >= 0) ;
if ($lock) {
Db::startTrans();
try {
$bargain_money = self::returnRandMoney($bargain_id, $min, $max, $join_count);
/*-------------*/
$id = 0;
$sql = "UPDATE `hp_activity_bargainirg`
SET `deal_money` = `deal_money` - $bargain_money,`bargain_count` = `bargain_count`+ 1
WHERE `id` = $bargain_id AND `user_id` = $sponsor_id AND `deal_money` > $bargain_money
AND `bargain_count` < $join_count";
$row = self::execute($sql);
if ( $row > 0) {
$insert_data = [];
$insert_data['bargain_id'] = $bargain_id;
$insert_data['assistor_id'] = $assistor_id;
$insert_data['bargain_money'] = $bargain_money;
$insert_data['create_time'] = time();
$id = Db::name('activity_bargain_list')->insert($insert_data);
}
/*-------------*/
if ($id > 0)
$state = true;
Db::commit();
}catch(\Exception $e){
$state = false;
Db::rollback();
}
flock($lock,LOCK_UN);
fclose($lock);
}
}
if ($state !== false ) {
return $bargain_money;
}
return $state;
}
//返回要砍的價格
public static function returnRandMoney ($bargain_id = 0, $min = 0 ,$max = 0, $join_count = 0 ){
$randMoney = self::randomFloat( $min, $max); //返回隨機價格
$prev_Progress = self::getBargainirgProgress($bargain_id);
$prev_bargain_count = $prev_Progress['bargain_count']; //返回已經被砍價的次數
$remainder = $prev_bargain_count % 3;
$bout_count = floor($join_count / 3) * 3; //最後一輪結束的刀數 39
$last_num = $join_count - $bout_count;
$avg = ($min + $max) / 2;
$before_sum = self::getBeforeMoney($bargain_id, $remainder);
if ($prev_bargain_count >= $bout_count) {
if ($last_num == 1){
return $avg;
} elseif ($last_num == 2) {
$end = $join_count - $prev_Progress['bargain_count'] ;
if ($end == 2) {
return $randMoney;
} elseif($end == 1) {
return $avg * 2 - $before_sum;
}
}
}
// $remainder_num = $join_count % 3; //總回合數的餘數
if ($remainder > 0) {
if ( $remainder == 1) {
$point = $max * 0.8; //最大額度的80%
$bout_sum = 3 * $avg;
if ($before_sum >= $point) {
$randMoney = self::randomFloat($min, ($bout_sum - $before_sum) / 2);
} else {
$randMoney = self::randomFloat(($bout_sum - $before_sum) / 2 , $point);
}
}
if ($remainder == 2) {
$round_sum_money = 3 * $avg;
$randMoney = $round_sum_money - $before_sum;
}
}
return $randMoney;
}
//拿隨機價格
public static function randomFloat($min = 0, $max = 1) {
return round($min + mt_rand() / mt_getrandmax() * ($max - $min),2);
}
//拿砍價活動下所有商品
public static function getActivityBargainProducts ( $limit = 0, $offset = 0) {
$data = [];
$sql = "SELECT a.`id`,a.`product_id`,a.`activity_money`,a.`bargain_section`,a.`join_count`,a.`product_desc`,a.`product_name`,
a.`attr1_id`,a.`attr2_id`,
b.`give_score`,b.`category_img`,b.`add_money`,b.`category_sum`
FROM `hp_activity_bargain` AS a
LEFT JOIN `hp_category_attr` AS b
ON a.`attr1_id` = b.`attr1_son_id` AND a.`attr2_id` = b.`attr2_son_id` AND a.`product_id` = b.`category_id`
WHERE 1 = 1
ORDER BY a.`id` DESC
LIMIT $limit OFFSET $offset";
$data = self::query($sql);
return $data;
}