示例#1
0
def main():

    # get the fasta file from stdin and return the sequences (prots.fa)
    sequences= hf.Get_sequences()

    # if the file is not empty proceed
    if sequences:

        # define the motif in Prosite notation
        pattern = 'KL[EI]{2,}K'

        # collect the KLEEK sequences
        filtered_seqs = hf.Filter_sequences(sequences, pattern)
        # write the filtered sequences to file
        hf.WriteFasta(filtered_seqs)

        # Call muscle and do multisequence allignment
        subprocess.call(["muscle", "-in", "infile", "-out", "multiseq"])

        # Call the alignment viewer to inspect the results
        subprocess.call(["seaview", "multiseq"])

        # remove temp files
        os.remove('infile')
        os.remove('multiseq')
示例#2
0
def main():
    # get filenames from stdin (for n inputs)
    for filename in sys.argv[1:]:
        # get the genome sequence out of the file
        genome = hf.ReadFasta(filename)
        if not genome:
            print('File ' + filename + ' is empty. Try again!')
            return
        # get the GC content for th[is file
        GC = hf.GC_content(genome)
        # print the GC content (with accuracy ~e-3)
        print("%.3f" % GC)
def main():
    # get filenames from stdin (for n inputs)
    for filename in sys.argv[1:]:
        # get the name of the spiece
        spiece = hf.GetName(filename)
        if not spiece:
            print('File ' + filename + ' is empty. Try again!')
            return
        # get the genome sequence out of the file
        genome = hf.ReadFasta(filename)
        # print the length of the sequence
        print("The Genome size for the " + spiece + " is: " + str(len(genome)))
示例#4
0
def main():

    # get the fasta file from stdin and return the sequences (translationtest.dna)
    sequences = hf.Get_sequences()

    # if the file is not empty proceed
    if sequences:

        # do translation and replace the seq
        translated_seq = hf.TranslateSeq(sequences)

        # print out the translation
        hf.PrintFasta(translated_seq, 'std')
示例#5
0
    def audit(self, params):
        """代理申请审核"""

        required_params = ("auditor_id", "agent_id")
        if not HelpFun.check_required_params(required_params, params):
            return '{"code":1,"errmsg":"缺少必要参数"}'

        auditor_id = params['auditor_id'].strip()
        # sql = "select id from agent where id='%s'" % auditor_id
        # auditor = self.cache_mysql.get_one("default", sql)

        if auditor_id not in login.background_user:
            return '{"code":1,"errmsg":"找不到审核人"}'

        agent_id = params['agent_id'].strip()
        sql = "select id, apply_status,invitecode,channelcode from agent where id='%s'" % agent_id
        agent = self.cache_mysql.get_one("default", sql)
        if not agent:
            return '{"code":1,"errmsg":"找不到代理"}'

        if agent['apply_status'] == 1:
            return '{"code":1,"errmsg":"该玩家已经是代理了"}'

        sql = "update agent set apply_status=1, auditor='%s', audit_time='%s' where id='%s'" % (
            auditor_id, int(time.time()), agent_id)
        self.cache_mysql.execute("default", sql)
        DEBUG_MSG("### 代理申请审核 agent_id:'%s'" % agent_id)
        return '{"code":0, "msg":"代理申请审核成功!"}'
示例#6
0
    def signin(self, params):
        """代理用户登录"""

        mobile = params.get('mobile', '').strip()
        self.password = params.get('password', '').strip()
        current_time = int(time.time())

        if not mobile:
            return '{"code":1,"errmsg":"请输入手机号码"}'

        if not HelpFun.ismobile(mobile):
            return '{"code":1, "errmsg":"请输入正确的手机号码"}'

        if not self.password:
            return '{"code":1,"errmsg":"请输入密码"}'

        if len(self.password) > 64:
            return '{"code":1,"errmsg":"密码长度不能太长"}'

        sql = "select id, channelcode from agent where mobile='%s'" % mobile
        agent = self.cache_mysql.get_one('default', sql)
        if not agent:
            return '{"code":1,"errmsg":"用户不存在"}'

        name_hash = NameHash.NameHash(mobile)
        dbname = name_hash.crcHash()
        sql = "select accountName from kbe_accountinfos where accountName='%s' and password='******'" % (
            mobile, self.password)
        ka = self.cache_mysql.get_one(dbname, sql)
        if not ka:
            return '{"code":1,"errmsg":"密码错误"}'
        token = manageToken.manageToken.generate_token(mobile)
        return '{"code":0,"msg":"登录成功!","token":"%s","channelcode":"%s"}' % (
            token, agent['channelcode'])
示例#7
0
def main():

    # get the XML file from stdin and return the Blast record (test1.xml, test2.xml, cst_blast.xml)
    # and the pattern to match
    BlastRec, outfile= hf.Get_files()

    # pdb.set_trace()
    # if the file is not empty proceed
    if BlastRec:
        # get scores for the first Blast result
        FirstScores = hf.Get_First_Blast(BlastRec)

        # plot the score's histogram
        plt.hist(FirstScores, round(max(FirstScores)), normed=1, facecolor='blue')
        # save histogram as pdf
        plt.savefig(outfile + '.pdf')
示例#8
0
def main():

    # get the fasta file from stdin and return the sequences (prots.fa)
    sequences = hf.Get_sequences()

    # if the file is not empty proceed
    if sequences:

        # define the motif in Prosite notation
        pattern = 'KL[EI]{2,}K'

        # collect the KLEEK sequences
        filtered_seqs = hf.Filter_sequences(sequences, pattern)

        # print the KLEEKs in stdout
        hf.PrintFasta(filtered_seqs, 'KLEEK')
        # print the number of KLEEK sequences
        # pdb.set_trace()
        print('\n' + 'There are ' + str(len(filtered_seqs)) +
              ' KLEEK sequences in the file')
示例#9
0
def main():
    files = []
    distances = []
    # get filenames from stdin and append them to list
    for filename in sys.argv[1:]:
        files.append(filename)

    # for all pairs in list
    for x_file in files:
        # create lattent list (one element per time to create row)
        _ = []
        # appent the spiece's name
        _.append(hf.GetFaNames(x_file))
        for y_file in files:

            # get the genome sequences out of the file
            genome1 = hf.ReadFasta(x_file)
            genome2 = hf.ReadFasta(y_file)
            if not genome1 or not genome2:
                print('File ' + filename + ' is empty. Try again!')
                return

            # get the related differences of genes
            diff = hf.dist(genome1, genome2)

            # append difference in lattent list
            _.append(diff)
        # appent in distaces (one column per time)
        distances.append(_)

    # just leaving a blank line here fot the matrix to be in better position in the text
    print()
    # Print the number of spieces
    addSpace = ''.join([' '] * 4)
    print(addSpace + str(len(files)))
    # print the distance matrix (with accuracy ~e-3)
    hf.PrintList(distances)

    # just leaving a blank line here fot the matrix to be in better position in the text
    print()
示例#10
0
def main():
    files = []
    distances = []
    # get filenames from stdin and append them to list
    for filename in sys.argv[1:]:
        files.append(filename)

    # for all pairs in list
    for x_file in files:
        # create lattent list (one element per time to create row)
        _ = []
        # appent the spiece's name
        _.append(hf.GetFaNames(x_file))
        for y_file in files:

            # get the genome sequences out of the file
            genome1 = hf.ReadFasta(x_file)
            genome2 = hf.ReadFasta(y_file)
            if not genome1 or not genome2:
                print('File ' + filename + ' is empty. Try again!')
                return
            # get the related differences of genes
            diff = hf.dist(genome1, genome2)

            # append difference in lattent list
            _.append(diff)
        # appent in distaces (one column per time)
        distances.append(_)


    in_file = open("infile", "w")
    # Print the number of spieces
    in_file.write("%s\n" % len(files))

    # print the distance matrix (with accuracy ~e-3)
    hf.PrintList_file(distances, in_file)

    in_file.close()
示例#11
0
def main():

    # get fasta file and # of bootsrtaps from stdin
    var = sys.argv[1:]
    if len(var) < 2:
        print('Error: You have to specify the number of bootstraps.')
        print('\n' + 'Usage: \n' +
              '\t bootstrap <filename> <number of boostraps>')
        return
    filename = var[0]
    BootNum = var[1]

    # get sequences from the fasta file and asign them to sequence objects
    with open(filename, 'r') as f:
        sequence = list(AlignIO.parse(f, 'fasta'))
    if not sequence:
        print('File ' + filename + ' is empty')
        return
    sequence = sequence[0]

    # get short names and return the dictionary
    names = hf.get_name(sequence)

    # create input file for bootstrap analysis
    # with open('infile', 'w') as infile:
    #     AlignIO.write(sequence, infile, 'phylip')
    pdb.set_trace()
    with tempfile.NamedTemporaryFile() as temp:
        # temp.write(sequence)

        temp = open('infile', 'w')
        temp = AlignIO.write(sequence, temp, 'phylip')

    # run bootstrap analysis
    hf.bootstrap_analysis(BootNum)

    # return the original names to the ids and print (stdout) the consensus tree
    hf.retIDandPrint(names)
def main():

    # get the XML file from stdin and return the Blast record (test1.xml, test2.xml, cst_blast.xml)
    # and the pattern to match
    pattern, BlastRec= hf.Get_XML()

    # if the file is not empty proceed
    if BlastRec:

        # set E-value upper limit
        E_value = 10**-20

        # get the blast matches basted on the pattern and the E-value
        MatchResults, BestResult = hf.Get_BlastMatch(BlastRec, pattern, E_value)

        # print out in stdout the matches
        hf.PrintBlastresults(MatchResults)
        if len(MatchResults) < 2:
            return
        print()
        # print out in stdout the best match
        print('The Best result (Smaller E-value) is:')
        hf.PrintBlastresults(BestResult)
示例#13
0
    def generate_token(cls, userid):
        key = HelpFun.get_random_string(10)
        userid_byte = userid.encode("utf-8")
        sha1_tshexstr = hmac.new(key.encode("utf-8"), userid_byte,
                                 'sha1').hexdigest()

        token = userid + ':' + sha1_tshexstr
        b64_token = base64.urlsafe_b64encode(token.encode("utf-8"))
        token = b64_token.decode("utf-8")
        cls.user_key[userid] = {
            "token": sha1_tshexstr,
            "expire": int(time.time())
        }
        DEBUG_MSG('token=%s' % token)
        return token
示例#14
0
    def freeze_modify(self, params):
        """
        代理冻结修改
        """
        required_params = ("agent_id", 'freeze_type', "remark", "operator_id")
        if not HelpFun.check_required_params(required_params, params):
            return '{"code":1,"errmsg":"缺少必要参数"}'

        freeze_type = int(params['freeze_type'])
        if freeze_type not in (0, 1):
            return '{"code":1,"errmsg":"类型不存在"}'

        operator_id = params['operator_id'].strip()
        # sql = "select mobile from agent where id='%s'" % operator_id
        # operator = self.cache_mysql.get_one("default", sql)
        if operator_id not in login.background_user:
            return '{"code":1,"errmsg":"找不到操作人"}'

        agent_id = params['agent_id'].strip()
        sql = "select id from agent where id='%s'" % agent_id
        agent = self.cache_mysql.get_one("default", sql)
        if not agent:
            return '{"code":1,"errmsg":"找不到代理"}'

        remark = params['remark'].strip()
        modify_time = int(time.time())
        # 冻结
        if freeze_type == 1:
            sql = "update agent set is_freezed=1 where id='%s'" % agent_id
            self.cache_mysql.execute("default", sql)
            sql = "insert into `agent_freeze` (`agent_id`,`freeze_status`, `remark`, `modify_time`, `operator_id`, `operator`) \
                   VALUES('{agent_id}', '{freeze_status}', '{remark}', {modify_time}, '{operator_id}', '{operator}') ;"\
                .format(agent_id=agent_id, freeze_status=freeze_type, remark=remark, modify_time=modify_time, operator_id=operator_id, operator=operator_id)
            self.cache_mysql.execute("default", sql)
            return '{"code":0, "msg":"代理冻结成功!"}'

        # 解冻
        sql = "update agent set is_freezed=0 where id='%s'" % agent_id
        self.cache_mysql.execute("default", sql)
        sql = "insert into `agent_freeze` (`agent_id` , `freeze_status`, `remark`, `modify_time`, `operator_id`, `operator`) \
               VALUES('{agent_id}', '{freeze_status}', '{remark}', {modify_time}, '{operator_id}', '{operator}') ;"                                                                                                                   .\
               format(agent_id=agent_id, freeze_status=freeze_type, remark=remark, modify_time=modify_time, operator_id=operator_id, operator=operator_id)

        self.cache_mysql.execute("default", sql)
        DEBUG_MSG("### 代理冻结修改 operator_id:%s, agent_id:%s, freeze_type:'%s'" %
                  (operator_id, agent_id, freeze_type))
        return '{"code":0, "msg":"代理解冻成功!"}'
def main():

    # get the fasta file from stdin and return the sequence (cst3.fa)
    sequence = hf.Get_sequences()[0]

    # if the file is not empty proceed
    if sequence:
        # Do Blast search of a given protein sequence against the nr database at NCBI
        # https://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastp&PAGE_TYPE=BlastSearch&LINK_LOC=blasthome
        # see NCBIWWW documentation at http://biopython.org/DIST/docs/tutorial/Tutorial.html (chapter 7.1)

        # invoke the NCBI BLAST server over the internet
        # The first argument is the blast program to use for the search ('blastp' in our case)
        # The second argument specifies the databases to search against ('nr' in our case)
        # The third argument is a string containing your query sequence
        result_handle = NCBIWWW.qblast("blastp", "nr", sequence.seq)
        # print out the results (stdout)
        print(result_handle.read())
示例#16
0
    def modify_agent_info(self, request: dict):
        """修改代理信息"""
        agent_id = request.get('agent_id', '')
        if not agent_id and len(request.keys()) != 2:
            return {"code": 1, "errmsg": "参数错误"}
        agent_sql = "select id from agent where id='%s'" % agent_id
        agent = self.cache_mysql.get_one('default', agent_sql)
        if not agent:
            return {"code": 1, "errmsg": "该代理不存在"}
        mobile = request.get('mobile', '')
        level = request.get('level', '')
        comment = request.get('comment', '')
        sql = "update agent set "
        if mobile:
            if not HelpFun.ismobile(mobile):
                return {"code": 1, "errmsg": "电话号码错误"}
            sql += " mobile='%s'" % mobile
        if comment:
            sql += " comment='%s'" % comment

        if level:
            if not level.isdigit():
                return {"code": 1, "errmsg": "等级不是整数"}
            level = int(level)
            if level not in (1, 2, 3):
                return {"code": 1, "errmsg": "等级错误"}
            AgentAmountEum = {
                1: AgentLevelAmount.Level1.value,
                2: AgentLevelAmount.Level2.value,
                3: AgentLevelAmount.Level3.value
            }
            AgentGiftGoldEum = {
                1: AgentGiftGold.Level1.value,
                2: AgentGiftGold.Level2.value,
                3: AgentGiftGold.Level3.value
            }
            sql += " level=%d ,amount=%d,gift_gold=%d " % (
                level, AgentAmountEum.get(level), AgentGiftGoldEum.get(level))
        sql += " where id='%s'" % agent_id
        ask = self.cache_mysql.execute('default', sql)
        if not ask:
            return {"code": 1, "errmsg": "修改不成功!"}
        return {"code": 0, "errmsg": "修改成功!"}
示例#17
0
    def add_user(self, accountName, passwd, invitation_code, channelCode):
        DEBUG_MSG("accountName=%s, invitecode=%s, channelCode=%s" %
                  (accountName, invitation_code, channelCode))
        name_hash = NameHash.NameHash(accountName)
        dbname = name_hash.crcHash()
        db = self._cache_mysql.conn_mysql(dbname)
        try:
            cur = db.cursor()
            sql_select = "select id from tbl_Account where sm_accountName='%s';" % accountName
            cur.execute(sql_select)
            results = cur.fetchall()
            if len(results) != 0:
                db.close()  # 关闭连接
                return 1
            uuid, nickname, selfInvitationCode = HelpFun.get_accountname_uuid_nickname(
            )
            sql = "insert into tbl_Account(sm_accountName,sm_uuid,sm_nickname,sm_selfInvitationCode,sm_invitationCode,sm_channelCode)values('%s','%s','%s','%s','%s','%s');" % (
                accountName, uuid, nickname, selfInvitationCode,
                invitation_code, channelCode)
            cur.execute(sql)  # 执行sql语句
            db.commit()

            cur.execute(sql_select)
            results = cur.fetchall()
            row = results[0]
            entityDBID = int(row[0])
            md5 = hashlib.md5()
            md5.update(passwd.encode('utf-8'))
            sign = md5.hexdigest()
            sql = "insert into kbe_accountinfos(accountName,password,email,entityDBID,regtime)values('%s','%s','%s',%d,%i);" % (
                accountName, sign, accountName + "@0.0", entityDBID,
                int(time.time()))
            cur.execute(sql)  # 执行sql语句
            db.commit()
            db.close()  # 关闭连接
            return 0
        except Exception as e:
            ERROR_MSG("background.add_user:error=%s" % str(e))
            return 2
示例#18
0
    def audit_list(self, params):
        """代理审核、汇总列表"""
        required_params = ("page_no", "page_size", "sort", "start", "end",
                           "level", "time_type", "mobile", "kind")
        if not HelpFun.check_required_params(required_params, params):
            return '{"code":1,"errmsg":"缺少必要参数"}'

        kind = params.get('kind')
        if kind not in ('bg', 'oc'):
            return '{"code":1,"errmsg":"参数错误"}'
        page_no = int(params.get('page_no', '1'))
        if page_no <= 0:
            return '{"code":1,"errmsg":"页码不正确"}'

        page_size = int(params.get('page_size', '20'))
        if page_size <= 0:
            return '{"code":1,"errmsg":"列表尺寸不正确"}'

        sort = params.get('sort', 'desc').strip()  # asc 升序排序; desc 降序排序;
        if sort not in ("asc", "desc"):
            return '{"code":1,"errmsg":"排序类型不正确"}'

        if not (params["start"].isdigit() and params["end"].isdigit() and
                params['level'].isdigit() and params['time_type'].isdigit()):
            return '{"code":1,"errmsg":"时间参数出错"}'

        level = int(params.get('level', 1))  # 0.全部; 1.一级代理; 2.二级代理; 3.三级代理;
        if level not in (0, 1, 2, 3):
            return '{"code":1,"errmsg":"代理级别不正确"}'

        time_type = int(params.get('time_type', 0))  # 0.全部; 1.注册时间; 2.晋升代理时间;
        if time_type not in (0, 1, 2):
            return '{"code":1,"errmsg":"时间类型不正确"}'

        start = int(params.get('start'))
        end = int(params.get('end'))
        mobile = params.get('mobile').strip()

        sql = "select id,mobile, invitecode,pid,ppid, apply_time, amount, gift_gold, apply_status, auditor, comment,audit_time, is_freezed, level, channelcode\
              from agent where id<>''"

        if level > 0:
            sql += " and level=%d" % level

        if time_type == 1:
            sql += " and regtime between %d and %d" % (start, end)

        if time_type == 2:
            sql += " and audit_time between %d and %d" % (start, end)

        if mobile:
            sql += " and mobile='%s' or id='%s'" % (mobile, mobile)
        if kind == 'oc':
            sql += " and apply_status=1"

        sql += " group by apply_time order by apply_time %s limit %d, %d;" % (
            sort, (page_no - 1) * page_size, page_size)

        # TAX_RATE = {1:AgentRate.ChildLevel1TaxRate.value, 2:AgentRate.ChildLevel2TaxRate.value, 3:AgentRate.ChildLevel3TaxRate.value}
        RateStr = {
            1: AgentRate.ChildLevel1TaxRateStr.value,
            2: AgentRate.ChildLevel2TaxRateStr.value,
            3: AgentRate.ChildLevel3TaxRateStr.value
        }

        _agent_list = self.cache_mysql.get_list("default", sql)
        agent_list = []
        for a in _agent_list:
            PLAYER = dict()
            PLAYER['id'] = a['id']  # 玩家ID
            PLAYER['mobile'] = a['mobile']  # 玩家手机号
            name_hash = NameHash.NameHash(a['mobile'])
            dbname = name_hash.crcHash()
            regtime_sql = "select regtime from kbe_accountinfos where accountName='%s';" % a[
                'mobile']
            p = self.cache_mysql.get_one(dbname, regtime_sql)
            PLAYER['regtime'] = p['regtime']  # 注册时间
            PLAYER['amount'] = int(a['amount'])  # 代理费用
            PLAYER[
                'child_charge_rate_str'] = AgentRate.ChildChargeRateStr.value  # 旗下玩家充值返佣基数(RMB)
            PLAYER['child_tax_rate_str'] = RateStr.get(
                a['level'])  # 旗下玩家税金返币基数(反币)
            PLAYER['gift_gold'] = a['gift_gold']  # 赠送骑士币(万)
            PLAYER['audit_time'] = a['audit_time']  # 修改时间
            PLAYER['child_tax_agent_rate_str'] = '10%'  # 直推代理费返佣基数(RMB)
            PLAYER['child_tax_user_rate_str'] = '10%'  # 直接推荐人税金返佣基数(币)
            PLAYER['child_tax_cuser_rate_str'] = '5%'  # 间接推荐人税金返佣基数(币)
            PLAYER['comment'] = a['comment']  # 备注
            if kind == 'bg':
                PLAYER['invitecode'] = a['invitecode']  # 邀请码
                PLAYER['apply_time'] = a['apply_time']  # 申请时间(添加时间)
                PLAYER['apply_status'] = a['apply_status']  # 申请状态
                PLAYER['auditor'] = a['auditor']  # 修改操作ID
                PLAYER['is_freezed'] = a['is_freezed']  # 是否冻结 0.否; 1.是;
                PLAYER['level'] = a['level']  # 代理级别 0.默认; 1.等级一; 2.等级二; 3.等级三;
                PLAYER['channelcode'] = a['channelcode']  # 代理人包体标识号
                #后台冻结/解冻代理信息
                freeze_sql = "select freeze_status,modify_time,operator_id from agent_freeze where agent_id='%s';" % a[
                    'id']
                log_list = self.cache_mysql.get_list("default", freeze_sql)
                PLAYER['freeze_log'] = log_list
            else:
                dictAgentInfo = self.profit.statistics_one(
                    a['channelcode'], a['audit_time'], getNow())
                PLAYER[
                    'player_count'] = self.profit.statistics_agent_register_count(
                        a['mobile'], a['channelcode'], 0, getNow())  #旗下用户数
                PLAYER['player_recharge'] = int(
                    dictAgentInfo.get('charge_gold', 0) / 10)  #旗下用户充值总数
                PLAYER['player_tax'] = abs(int(dictAgentInfo.get(
                    'tax_fee', 0)))  #旗下用户税金汇总
                PLAYER['direct_pushback_rmb'] = int(
                    dictAgentInfo.get('agent_amount', 0) * 0.1)  #直推返佣金额
                PLAYER['direct_pushback_tax'] = abs(
                    int(
                        dictAgentInfo.get('agent_tax_fee', 0) * 0.1 +
                        dictAgentInfo.get('agent_tax_fee', 0) *
                        0.05))  #直推税金返佣总额

                VBC_INFO = self.profit.statistics_one_return_info(a['id'])
                PLAYER['returned_quantity'] = VBC_INFO['re_num']  #已返还数量(万)
                PLAYER['not_return_num'] = VBC_INFO['not_re_count']  #剩余未返期数
                PLAYER['not_return_vbc'] = VBC_INFO['not_re_num']  #未返骑士币总量(万)
                PLAYER[
                    'rmb_return_count'] = PLAYER['direct_pushback_rmb'] + int(
                        PLAYER['player_recharge'] * AgentRate.ChildChargeRate.
                        value)  #现金返佣收益汇总(元) 直推返佣+充值返佣
                PLAYER['vbc_return_count'] = PLAYER[
                    'direct_pushback_tax'] + PLAYER['player_tax']  #骑士币返佣汇总(币)

            agent_list.append(PLAYER)
        return '{"code":0,"data":%s}' % json.dumps(agent_list,
                                                   ensure_ascii=False)
示例#19
0
    def agent_apply(self, params):
        """代理申请"""

        mobile = params.get('mobile', '').strip()
        level = int(params.get('level', 0))  # 申请级别 0.默认; 1.等级一; 2.等级二; 3.等级三;
        current_time = int(time.time())

        if level not in (1, 2, 3) or not mobile:
            return '{"code":1,"errmsg":"参数出错"}'

        if not HelpFun.ismobile(mobile):
            return '{"code":1, "errmsg":"手机号码不正确"}'

        player = self.background.select_user(mobile)
        if not player:
            return '{"code":1,"errmsg":"玩家不存在"}'

        sql = "select id from agent where mobile='%s' and apply_status=0" % mobile
        is_apply = self.cache_mysql.get_one("default", sql)
        if is_apply:
            return '{"code":1,"errmsg":"您的申请已经提交过了, 待审核中..."}'

        sql = "select id, mobile, invitecode, channelcode from agent where mobile='%s' and apply_status=1" % mobile
        is_agent = self.cache_mysql.get_one("default", sql)
        if is_agent:
            return '{"code":1,"errmsg":"您已经是代理了"}'

        # 如果玩家有上层邀请码,就查找上层代理人信息
        if player['sm_invitationCode']:
            sql = "select id, pid, mobile, pmobile, amount, level, plevel, apply_status from agent where invitecode='%s'" % \
                  player['sm_invitationCode']
            inviter = self.cache_mysql.get_one("default", sql)
            if not inviter:
                return '{"code":1,"errmsg":"找不到邀请人"}'

            if inviter['apply_status'] == 0:
                return '{"code":1,"errmsg":"邀请人还不是代理"}'

            # 如果邀请人有上层邀请码, 查找上层代理的上层代理(即是自己的上上代理)
            if inviter['pid']:
                sql = "select id, apply_status from agent where pid='%s'" % inviter[
                    'pid']
                pinviter = self.cache_mysql.get_one("default", sql)
                if not pinviter:
                    return '{"code":1,"errmsg":"找不到上上层代理"}'

                if pinviter['apply_status'] == 0:
                    return '{"code":1,"errmsg":"上上层邀请人还不是代理"}'
        else:
            inviter = {
                "id": "",
                "pid": "",
                "mobile": "",
                "pmobile": "",
                "amount": 0.00,
                "level": 0,
                "plevel": 0
            }

        channelcode = self.get_random_id()
        id = '20' + str(level) + channelcode  # 如 201DNFHERO

        AGENT_AMOUNT = {
            1: AgentLevelAmount.Level1.value,
            2: AgentLevelAmount.Level2.value,
            3: AgentLevelAmount.Level3.value
        }
        TAX_RATE = {
            1: AgentRate.ChildLevel1TaxRate.value,
            2: AgentRate.ChildLevel2TaxRate.value,
            3: AgentRate.ChildLevel3TaxRate.value
        }
        GIFT_GOLD = {
            1: AgentGiftGold.Level1.value,
            2: AgentGiftGold.Level2.value,
            3: AgentGiftGold.Level3.value
        }

        sql = "insert into agent (`id`, `mobile`, `level`, `invitecode`, `amount`, `tax_rate`, `gold_rate`, `apply_time`,\
              `pid`, `ppid`, `pmobile`, `ppmobile`, `channelcode`,`gift_gold`, `pamount`, `plevel`, `pplevel`) \
              value('%s','%s', '%s', '%s', '%s', '%s','%s', '%s', '%s','%s', '%s', '%s','%s', '%s', '%s', '%s', '%s')"                                                                                                                       % \
              (id, mobile, level, player['sm_selfInvitationCode'], AGENT_AMOUNT.get(level), TAX_RATE.get(level),
               AgentRate.ChildChargeRate.value,
               current_time, inviter['id'], inviter['pid'], inviter['mobile'], inviter['pmobile'], channelcode,GIFT_GOLD.get(level),
               inviter['amount'], inviter['level'],
               inviter['plevel'])

        result = self.cache_mysql.execute("default", sql)
        if not result:
            return '{"code":1,"errmsg":"申请失败!"}'

        self.background.mdify_player_channel_code(
            mobile, player['sm_selfInvitationCode'], channelcode)
        self.background.modifyChannelCode(channelcode, mobile)
        return '{"code":0, "msg":"申请成功!"}'