码迷,mamicode.com
首页 > 其他好文 > 详细

12121212

时间:2020-05-11 01:28:41      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:记录操作   write   star   之间   add   receive   false   tin   clu   

<?php



public function task()
    {
        $list = M(‘SysTask‘)->where([‘status‘ => [‘neq‘,3]])->select();
        // 通过文件锁住操作执行完再执行下一个
        $fp = fopen("lock.txt", "w+");
        if (!flock($fp, LOCK_EX | LOCK_NB)) {
            echo "系统繁忙,请稍后再试";
            return false;
        }
        $result_num = count($list);
        $start      = time();
        if ($list) {
            $i = 0;
            $j = 0;
            $sum = 0;
            foreach ($list as $value) {
                $sum ++;
                //获取缓存,如果存在则跳过进行下一条记录操作
                if (($this->getTaskCache($value[‘id‘]))) {
                    $i++;
                    continue;
                }
                //设置缓存,防止重复操作同一条记录
                $this->setTaskCache($value[‘id‘]);
                switch ($value[‘type‘] == 1) {
                    case 1:
                        $j++;
                        $this->clickGiftNewPayTask($value);
                        break;
                    default:
                        break;
                }
            }
        }
        $end_time = time();

        //执行完成解锁
        flock($fp,LOCK_UN);
        //关闭文件
        fclose($fp);
        $str = ‘系统任务处理记录:数量‘.$result_num.‘,耗时‘.$end_time-$start.‘秒,执行‘.$j.‘条,跳出‘.$i.‘条‘;
        write_log($str,‘systemTaskLog‘);


        p(‘处理总数:‘.count($list));
        p(‘耗时:‘.$end_time-$start.‘秒‘);
        p(‘跳出:‘.$i);
        p(‘执行:‘.$j);
        return true;

    }




public function clickGiftNewPayTask($value)
    {
        $task[‘start_time‘] = time();
        $params = json_decode($value[‘params‘], true);
        if ( $params[‘pay_user_id‘] == $params[‘receive_user_id‘]) {
            return false;
        }
        //1.查找资金日志是否存在此记录
        $where = [
            ‘user_id‘     =>$params[‘receive_user_id‘],//领红包用户ID
            ‘operation_id‘=>$params[‘pay_user_id‘],//发红包用户ID                
            ‘gift_id‘     =>$params[‘gift_id‘],//主红包ID
        ];
        $amountLog = M(‘account_details‘)->where($where)->field(‘id‘)->find();
        if(empty($amountLog)) return false;

        //2.领取的红包是否存在
        $where2 = [
//            ‘id‘          =>$params[‘child_gift_id‘],//子红包id
            ‘user_id‘     =>$params[‘pay_user_id‘],//发红包用户ID
            ‘get_user_id‘ =>$params[‘receive_user_id‘],//领红包用户ID
            ‘gift_id‘     =>$params[‘gift_id‘],//主红包ID
            ‘gift_money‘  =>$params[‘gift_money‘],//红包金额

        ];
        $red_gift = M(‘gift_child‘)->where($where2)->field(‘status‘)->find();
        if(empty($red_gift)) return false;
        
        //3.查找该条记录是否成功
        $where3 = [
            ‘id‘     =>$value[‘id‘],//领红包用户ID
            ‘status‘ =>3,  //发红包用户ID
        ];
        $sys_task = M(‘sys_task‘)->where($where3)->field(‘id‘)->find();
        if($sys_task) return false;

        //进行新生商户之间转账操作
        $userModel = D(‘User‘);
        $payUser = $userModel->where(array(‘user_id‘ => $params[‘pay_user_id‘]))->field(‘mobile,nickname,uacount_user_id‘)->find();
        $receiveUserId = $userModel->where(array(‘user_id‘ => $params[‘receive_user_id‘]))->field(‘uacount_user_id‘)->find();
        //引入ncount库文件
        include_once("./Plugins/Ncount/Ncount.php");
        $NcountPay = new \NcountPay();
        $data = array(
            ‘tranAmount‘ => $params[‘gift_money‘],//金额
            ‘payUserId‘ => $payUser[‘uacount_user_id‘],//付款方用户编号
            ‘receiveUserId‘ => $receiveUserId[‘uacount_user_id‘], //收款方用户编号
        );
        $result = $NcountPay->transferAccounts($data);
        $task = [];
        $task[‘status‘] = 2;
        if (empty($result[‘resultCode‘]) || $result[‘resultCode‘] != ‘0000‘) {
            //如果失败清除缓存,进行下一次操作
            $this->delTaskCache($value[‘id‘]);
            $task[‘status‘] = 4;
            $task[‘result‘] = json_encode($result);

            //生成系统任务转账日志
            $logs = [];
            $logs[‘pay_user_id‘]    = $where[‘operation_id‘];//付款用户ID
            $logs[‘receive_user_id‘]= $where[‘user_id‘];//收款用户ID
            $logs[‘gift_id‘]        = $params[‘gift_id‘];
            $logs[‘amount‘]         = $data[‘tranAmount‘];
            $logs[‘pay_code‘]       = $data[‘payUserId‘];
            $logs[‘receive_code‘]   = $data[‘receiveUserId‘];
            $logs[‘add_time‘]       = time();
            $logs[‘result‘]         = json_encode($result);
            M(‘task_transfer_log‘)->add($logs);
        } else {
            $task[‘status‘] = 3;
            $task[‘result‘] = ‘succeed‘;
            //修改资金流水表的新生支付同步状态:0未同步;1已同步
            //更新新生转账状态
            M(‘account_details‘)->where(‘user_id‘,$amountLog[‘id‘])->save(array(‘is_state‘=>1));

            //生成系统任务转账日志
            $logs = [];
            $logs[‘pay_user_id‘]    = $where[‘operation_id‘];//付款用户ID
            $logs[‘receive_user_id‘]= $where[‘user_id‘];//收款用户ID
            $logs[‘gift_id‘]        = $params[‘gift_id‘];
            $logs[‘amount‘]         = $data[‘tranAmount‘];
            $logs[‘pay_code‘]       = $data[‘payUserId‘];
            $logs[‘receive_code‘]   = $data[‘receiveUserId‘];
            $logs[‘add_time‘]       = time();
            $logs[‘status‘]         = 1;
            $logs[‘result‘]         = json_encode($result);
            M(‘task_transfer_log‘)->add($logs);

        }
        $task[‘end_time‘] = time();
        $res = D(‘SysTask‘)->where([‘id‘ => $value[‘id‘]])->save($task);
        if ($res === false) {
            //如果失败清除缓存,进行下一次操作
            $this->delTaskCache($value[‘id‘]);
            write_log($value, ‘clickGiftNewPayTask‘, date(‘Y-m-d‘,time()));
        }



        //不能删除,删除的话在第二次查询的时候如果查到改id,则会进行下一次的转账执行(有可能会转多次账)
//        $this->delTaskCache($value[‘id‘]);
        return true;

    }

 

12121212

标签:记录操作   write   star   之间   add   receive   false   tin   clu   

原文地址:https://www.cnblogs.com/ccw869476711/p/12865984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!