V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
freshlhy
V2EX  ›  PHP

求教!开发 phpwind 插件,事务只能用 lockProcess 吗?

  •  
  •   freshlhy · 2015-12-10 08:41:42 +08:00 · 2203 次点击
    这是一个创建于 3297 天前的主题,其中的信息可能已经有所发展或是发生改变。

    阿里云市场有个phpwind9.x 积分兑换商城插件,看其源码,事务用 lockProcess 搞,感觉哪不对,对 php 和 wind 不熟啊,感觉会有性能问题吧,如果搞个正儿八经的积分商城的话。

    /**
         * 兑换奖品
         */
        public function doAwardAction() {
            $info = array();
            $info['uid'] = $this->loginUser->uid;
            $info['username'] = $this->loginUser->username;
            $info['prize_id'] = $this->getInput('prize_id', 'post');
            !$info['prize_id'] && $this->showError('奖品不存在!');
            $info['num'] = $this->getInput('num', 'post');
            $info['name'] = $this->getInput('name', 'post');
            $info['address'] = $this->getInput('address', 'post');
            if(!$info['address']){
                $this->showError('详细地址不能为空');
            }
            $info['zip'] = $this->getInput('zip', 'post');
            $info['tel'] = $this->getInput('tel', 'post');
            if (!is_numeric($info['num']) || $info['num'] <= 0) {
                $this->showError('请输入正确的兑换数量!');
            }
            //读取应用配置
            $config = Wekit::C('weibojifen');
            //获取流通货币的名字,单位,以及该用户的值
            Wind::import('SRV:credit.bo.PwCreditBo');
            $creditBo = PwCreditBo::getInstance();
            $credit_num = $config['prizeCredit'];
            $credit_name = $creditBo->cType[$credit_num];
            $credit_unit = $creditBo->cUnit[$credit_num];
            $credit_value = $this->loginUser->info['credit'.$credit_num];
            //得到奖品信息
            $prize_info = $this->_getPrizeDs()->getPrizeInfo($info['prize_id']);
            $needpay = $prize_info['price'] * $info['num'];
            $needpay = intval($needpay);
            if (empty($prize_info)){
                $this->showError('非法 id');
            }
            if ($prize_info['num'] < 1){
                $this->showError('该奖品暂时缺货!');
            }
            if ($prize_info['num'] < $info['num']) {
                $this->showError("该奖品只剩下存货".$prize_info['num']."件!");
            }
            if ($credit_value < $needpay) {
                $this->showError('您没有足够的积分参与该奖品的兑换!');
            }
            //生成订单
            $info['price'] = $prize_info['price'];
            $info['state'] = 0;
            $info['create_time'] = Pw::getTime();
            $info['logistics'] = '';
            $info['logistics_id'] = '';
            $info['send_time'] = 0;
    
            $_flag = 'add_order';
            $_time = 300;
            $servce = Wekit::load('process.srv.PwProcessService');
            if ($servce->lockProcess($_flag, $_time)){
                //积分操作
                $creditBo->set($this->loginUser->uid, $config['prizeCredit'], -$needpay);
                $creditBo->addLog('积分兑换', array($credit_num => -$needpay), new PwUserBo($this->loginUser->uid));
                $creditBo->writeLog();
                //增加订单
                $this->_getOrderDs()->addOrderInfo($info);
                //减少奖品数目
                $this->_getPrizeDs()->updatePrizeNum($info['prize_id'], array('num'=>num-$info['num'],'sellnum'=>sellnum+$info['num']));
                $servce->unlockProcess($_flag);
                $this->showMessage("积分兑换奖品成功!");
            }else{
                $this->showError('未解锁状态');
            }
        }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5363 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 08:54 · PVG 16:54 · LAX 00:54 · JFK 03:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.