PHP 秒杀商品库存同步

<?php
/**
 * 任务计划 -  秒杀商品库存同步
 *
 * 
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
PHP_SAPI == 'cli' or exit ( 'Cli Access Invalid!' );
class rush_goods_storageControl {

    public $exception = true;
   public $title_msg = '';
    /**
     * 初始化对象
     */
    public function __construct(){
        register_shutdown_function(array($this,"shutdown"));
    }

    /**
     * 乐县域商品分类
     */
    public function indexOp() {
        try{
            $res = $this->get_rush_list();
        }catch (Exception $e){
            $this->exception = false;
        }
    }

   /**
     * 秒杀商品库存同步
     */
    public function get_rush_list() {
        //秒杀商品
       $model_rush = Model('rush');
       $rush_condition = array(
          'rush_activity.status'  => array('neq', '4'),
          /*'rush_goods.rush_start_time'  => array('lt', time()),
          'rush_goods.rush_end_time'  => array('gt', time()),*/
       );
       $rush_info = $model_rush->getRushGoodsList($rush_condition);
       if(!empty($rush_info)){
          foreach ($rush_info as $v){
              if($v['rush_goods_storage']>0){
                 $rush_start_time = date('YmdHis',$v['rush_start_time']);
                 $rush_end_time =   date('YmdHis',$v['rush_end_time']);
                $k='rush_'.$v['rush_id'].'_quantity_spu_'.$v['common_id'].'_'.$rush_start_time.'_'.$rush_end_time;
                 $redis = new Redis();
                 $redis->connect(C('rush_redis_host'),C('rush_redis_port'));
                 $storage = $redis->get($k);
                if($storage){
                   if($storage!=$v['rush_goods_storage']){
                      $condition = [];
                      $data = [];
                      $condition['rush_id'] =$v['rush_id'];
                      $data['rush_goods_storage'] = $storage ;
                      $request = Model()->table('rush_goods')->where($condition)->update($data);
                      if(!$request){
                         LogManager::log_error(array('logKey'=>'update_rush_goods','message'=>'更新秒杀库存失败','condition'=>$condition,'data'=>$data),'get_rush_storage');
                      }
                   }
                }else{
                   LogManager::log_error(array('logKey'=>'get_rush_goods_key','message'=>'秒杀商品查询key失败','KEY'=>$k),'get_rush_storage');
                }
             }
          }
       }else{
          $this->title_msg = '没有查询到秒杀信息';
       }
   }
    /**
     * 执行完成提示信息
     *
     */
    public function shutdown()
    {
        if($this->exception) {
            $caption = '秒杀库存更新成功--';
            if($this->title_msg){
               $title = $this->title_msg;
            }else {
               $title = '秒杀库存更新成功';
            }
            $this->lxy_sendmail($title,$caption);
            exit("success at ". date('Y-m-d H:i:s', TIMESTAMP)."\n");
        }else{
            $caption = '秒杀库存更新失败--';
           $title = '秒杀库存更新失败';
            $this->lxy_sendmail($title,$caption);
            exit("failure at" . date('Y-m-d H:i:s', TIMESTAMP)."\n");
        }
   }

    //sendmail
    public function lxy_sendmail($title,$caption){
        $content = nl2br($caption);
        //发送邮件
        $to = 'hejp@lecuntao.com';
        sendmail($to, 'update_rush_goods@163.com', $title, $content);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值