A way to generate order numbers in Java (Generate a friendly order number)

Source: Internet
Author: User

For example, the order number required 20-bit string, in addition to the 8-bit YYYYMMDD, there are 12 more can do their own.

Provides a tool class that generates order numbers based on a number. result str = YYYYMMDD + The format of this int, format is 0, for example today is 16 12.30 The first order number is 1 so the complete order number is 20161230000000000001


Suppose today is the first time to order number, I need 100.



A service class is then provided to record and obtain the maximum order number generated today.

Need num represents how many order numbers the business will occupy

public int getmaxordernum (int neednum)

{

int maxnum = Get today's largest ordrnum go to the database (the database has dates and maxNum2 fields), if today no record returns 0;

if (Maxnum ==0)

{

Insert a maxnum to the database for today's 0 record.

}

int updatedbmaxnum = Maxnum + neednum;

Update database today, Updatedbmaxnum.


Return maxnum back.

}

The following is the implementation of our company, we get to an int value by the Getmaxordernumberformdate method, then formart the order number.

public class Ordernumberdubbboserviceimpl implements Ordernumberdubbboservice {/** * redis lock Operation Service * *
    
    @Resource (name = "Redislockserviceimpl") private Redislockservice Redislockservice;
    
    @Autowired private Ordernumberdao DAO; @Override public int getmaxordernumberfromdate (map<string, object> parammap) {//Lock table (used to determine if an order can now be created)
            An hour timeout if (!redislockservice.addredislock (Constantkey.redis_lock_max_order_number, 2000, 2)) {
        return 0;
        int number = Converterutils.toint (Parammap.get ("number"));
        int nowdate = Converterutils.toint (Parammap.get ("date"));
        Get the latest order information for this type from the library ordernumber ordernumber = Dao.getordernumber (Parammap);
        OrderNumber newordernumber = new OrderNumber ();
        Newordernumber.settype (Converterutils.toint (Parammap.get ("type")));
        Newordernumber.setdate (nowdate);
        int startnum = 1; if (! Checkutils.isnullorempty (OrderNumber) && Nowdate = = Ordernumber.getdate ()) {//library has and is today, get the data in the library Startnum = Ordernumbe
            R.getnumber () + 1;
            Newordernumber.setnumber (Ordernumber.getnumber () + number);
            Dao.updateordernumber (Newordernumber);
            Redislockservice.delredislock (Constantkey.redis_lock_max_order_number);
        return startnum;
        } newordernumber.setnumber (number);
        Dao.addordernumber (Newordernumber);
        Unlock Redislockservice.delredislock (Constantkey.redis_lock_max_order_number);
    return startnum; }
}


If you think this article helps you, give the author some coffee money.



The business layer gets the maximum num is 0, and then he can occupy 1-100.


When the next business in need 100, get is 100, he occupies 101-200;


The business layer then invokes the tool class to format the number as the final order number.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.