基於微擎 砍價活動

功能需求:

1、添加砍價活動(這裏是教育項目,砍價活動爲課程中的一個分類)

2、用戶第一次砍價,成爲砍主

3、用戶可以邀請好友幫自己砍價(一個好友只能幫一個用戶砍價一次)

接口:

用戶成爲砍主

//用戶砍價(成爲砍主)
    public function doPagebeBargainer()
    {
        global $_W,$_GPC;
        $uniacid = $_W['uniacid'];
        $id = $_GPC['id'];  //砍價活動id
        $uid = $_GPC['uid'];  //當前用戶(砍主)
        //根據uid查詢當前用戶(砍主)信息(用戶是否存在)
        $userinfo = pdo_fetch("select * from " . tablename('yzpx_sun_user') . " where id='$uid'");
        if (!$userinfo) {
            return $this->result(1, 'error', '未找到該用戶');
        }
        //判斷重複提交
        $kanzhu = pdo_fetch("select * from " . tablename('yzpx_sun_bargain') . " where uid='$uid' and cid='$id' and help_uid=0 ");
        if ($kanzhu) {
            return $this->result(1, 'error', '你已經是該活動砍主,可邀請好友幫砍');
        }
        //獲取當前砍價課程  這裏要獲取當前課程的砍價範圍,砍價次數
        $bargainInfo = pdo_get('yzpx_sun_course', array('uniacid' => $_W['uniacid'], 'id' => $_GPC['id']));
        $kanjia_num = $bargainInfo['bargain_num'];  //砍價次數
        $cut = $bargainInfo['money'] - $bargainInfo['nowmoney'];  //砍價的範圍  原價-底價
        // echo $cut;exit();
        $cutmoney = rand(0, $cut)*0.5;
        // echo $cutmoney;exit();
        $now_money =  $bargainInfo['money'] - $cutmoney;
        // 蒐集數據
        $data = array();
        $data['cid'] = $id;  //活動id
        $data['uid'] = $uid;   //砍主(當前用戶id)
        $data['now_money'] = $now_money;
        $data['kj_money'] = $cutmoney;
        $data['help_uid'] = 0;//爲砍主
        $data['createtime'] = time();
        $data['uniacid'] = $_W['uniacid'];
        $data['username'] = $userinfo['user_name'];
        $data['headurl'] = $userinfo['headimg'];
        $data['help_num'] = 1;
        $data['isbuy'] = 0; //0未購買  1已購買
        $res = pdo_insert('yzpx_sun_bargain', $data);
        if($res){
            //該課程參與人數
            $num=1;
            pdo_update('yzpx_sun_course', array('signnum +=' => $num), array('id' => $id));
            return $this->result(0, 'success', $cutmoney);
        }else{
            return $this->result(1, 'error', '操作失敗');
        }
    }

邀請好友幫砍

//好友幫砍
    public function doPagebarginHelper()
    {
        global $_GPC, $_W;
        //接受參數
        $uniacid = $_W['uniacid'];
        $id = $_GPC['id'];  //砍價活動id
        $uid = $_GPC['uid'];
        //獲取當前砍價課程  這裏要獲取當前課程的砍價範圍,砍價次數
        $bargainInfo = pdo_get('yzpx_sun_course', array('uniacid' => $_W['uniacid'], 'id' => $_GPC['id']));
        //查詢已經砍價的次數
        $num = pdo_fetchcolumn("select count(*) from " . tablename("yzpx_sun_bargain")  ."where uniacid=" . $uniacid . " and uid=" . $uid . " and cid=" . $id . "");
        //剩餘砍價次數
        $sy_num = $bargainInfo['bargain_num'] - $num;  //如果剩餘砍價次數爲0   禁止砍價
        //先查詢砍主  
        $master = pdo_get('yzpx_sun_bargain', array('help_uid' => '0', 'cid' => $_GPC['id'], 'uid' => $_GPC['uid'],'uniacid' => $_W['uniacid']));
        //判斷一個好友只能幫助一次
        $helper = pdo_get('yzpx_sun_bargain', array('help_uid' => $_GPC['help_uid'], 'cid' => $_GPC['id'], 'uid' => $_GPC['uid'],'uniacid' => $_W['uniacid']));
        if ($helper) {
            return $this->result(1, 'error', '不能重複幫砍');
        }
        //查詢當前活動幫砍的最後一位好友(包含砍主)
        $sql3 = "SELECT * FROM ims_yzpx_sun_bargain WHERE uid = " . $uid . " and cid=" . $id . " Order BY id desc";
        $helperList = pdo_fetchall($sql3);
        //獲取當前砍價課程  這裏要獲取當前課程的砍價範圍,砍價次數
        $bargainInfo = pdo_get('yzpx_sun_course', array('uniacid' => $_W['uniacid'], 'id' => $_GPC['id']));
        $cutMax = $helperList[0]['now_money'] - $bargainInfo['nowmoney'] ;  //隨機數的上限 
        //如果當前是最後一位
        if($sy_num <= 1){
            $cutmoney = $cutMax;
        }else{
            $cutmoney = rand(0, $cutMax)*0.5;
        }
        $now_money = $cutMax - $cutmoney; 
        $nowprice = $now_money + $bargainInfo['nowmoney'];
        // 蒐集數據
        $data = array();
        $data['cid'] = $id;  //活動id
        $data['uid'] = $uid;   //砍主(當前用戶id)
        $data['now_money'] = $nowprice;
        $data['kj_money'] = $cutmoney;  //砍掉的金額
        $data['help_uid'] = $_GPC['help_uid'];  //幫砍好友的id
        $data['createtime'] = time();
        $data['uniacid'] = $_W['uniacid'];
        $data['username'] = $_GPC['user_name'];
        $data['headurl'] = $_GPC['headimg'];
        $data['help_num'] = 1;
        $data['isbuy'] = 0; //0未購買  1已購買
        $res1 = pdo_insert('yzpx_sun_bargain', $data);
        //修改砍主的信息(當前金額  幫砍次數)
        $num = pdo_fetchcolumn("select count(*) from " . tablename("yzpx_sun_bargain")  ."where uniacid=" . $uniacid . " and uid=" . $uid . " and cid=" . $id . "");
        $datas['now_money'] = $nowprice;
        $datas['help_num'] = $num-1;  //減去自己的一次
        $res2 = pdo_update('yzpx_sun_bargain', $datas, array('id' => $master['id'],'uniacid' => $_W['uniacid']));
        if($res2){
            return $this->result(0, 'success', $cutmoney);
        }else{
            return $this->result(1, 'error', '幫砍失敗');
        }   
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章