Create auto generated id like case_number

Hello Devs,
I have one custom module. I want to create a field which will auto-generated field like case_number.
I have written a code in view.edit.php which is working fine. but the problem is when I open a create window in different windows and click on create option at a same time then it will create same id.

You can use Before Save logic hook, so you can put your code there.

Or another option can be to use Auto Increment Field, but you cannot add prefix with that auto increment number.