· 7 years ago · Jan 12, 2019, 09:04 AM
1duplicate error
2def generate_loanid(leadid='0', counter=0):
3 """ Locks the customer_loanid table retrieves maximum loanid from it.
4 Generates a new loanid and puts it back in the table.
5 Returns the new loanid.
6 """
7 def gen_id(max_loanid=0, count=0):
8 """
9 Increments the counter if an id exists for the day.
10 Else resets the counter and creates a fresh one.
11 """
12 timestamp_now = time.localtime()
13 if max_loanid:
14 logger.debug("""Current Maximum Loan id :
15 (Year(YY)+Julian Day(DDD)+Counter(CCCC) %d,
16 Current timestamp : %s""" %(max_loanid, timestamp_now))
17 julian_day = (max_loanid/10000) % 1000
18 if julian_day == timestamp_now[7]:
19 count = max_loanid % 10000
20 return (str(timestamp_now[0])[2:] +
21 str(timestamp_now[7]).rjust(3, '0') +
22 str(count+1).rjust(4, '0')
23 )
24
25 logger.debug("Leadid:%s - Counter:%s"%(leadid, counter))
26 db_obj = dbLis(pooling=False)
27 try:
28 try:
29 db_obj.query("lock tables customer_loanid write")
30 max_loanid = db_obj.query("select max(loanid) as loanid from customer_loanid")
31 curr_loanid = gen_id(max_loanid = max_loanid.__len__() and max_loanid[0].loanid)
32 db_obj.insert('customer_loanid', loanid=curr_loanid)
33 except (MySQLdb.IntegrityError,MySQLdb.OperationalError):
34 logger.warning(traceback.format_exc())
35 #There is no logical backing for this piece of code.
36 if counter < 3:
37 db_obj.query("unlock tables")
38 return generate_loanid(counter=counter+1)
39 else:
40 raise
41 finally:
42 try:
43 db_obj.query("unlock tables")
44 logger.debug("Unlocked All Tables")
45 db_obj.ctx.db.close()
46 except MySQLdb.OperationalError:
47 logger.info("Table unlocked already")
48 logger.debug(traceback.format_exc())
49 logger.info("Generated Loanid : %s"%(curr_loanid))
50 return curr_loanid
51
52CREATE TABLE `customer_loanid` (
53 `loanid` int(15) NOT NULL DEFAULT '0',
54 PRIMARY KEY (`loanid`)
55)
56
57timestamp_now = time.localtime()
58day_part_of_id = str(timestamp_now[0])[2:] + str(timestamp_now[7]).rjust(3, '0')
59min_id_of_day = day_part_of_id + '0001'
60
61sql = "INSERT INTO customer_loanid (SELECT "+min_id_of_day+" + count(*) FROM customer_loanid WHERE loanid LIKE '"+day_part_of_id%+"'"