package com.hac.util; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import com.hac.service.HacService; /** * 优惠券 * @author zhaoxueyuan * */ public class CreateYHQUtil { private static HacService codeService = new HacService(); public static void main(String[] args) { createYHQ("CS", 5, 0, 1000, 0, "2015-12-30 00:00:00", 0); } /** * * @param str 设定起始字符 * @param length 设定优惠券长度 * @param type 设定优惠券类型 * type为0,promotion是多少价格减去多少(总价减去promotion) type为1,promotion是多少价格是多少 (1元清洗) type为2,(打折) * @param sum 设定生成总券数 * @param money 设定优惠券金额 * @param endTime 过期时间 * @param available 是否是重复使用券 0为否 1为是 */ public static void createYHQ(String str, int length, int type, int sum, int money, String endTime, int available) { StringBuffer buf = new StringBuffer(); buf.append("A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"); buf.append(",1,2,3,4,5,6,7,8,9,0"); String[] arr = buf.toString().split(","); // 库里的优惠券 List<String> codeList = codeService.getAllCode(); Map<String, String> map = new HashMap<String, String>(); String YHQ = str; Random random = new Random(); //生成总个数 boolean isfull = true; //计数器 int count = 0; while (isfull) { //优惠券长度 for (int i = 0; i < length - str.length(); i++) { YHQ += arr[random.nextInt(arr.length)]; } //不重复 插库 if (!codeList.contains(YHQ.toUpperCase())) { map.put("code", YHQ.toUpperCase()); map.put("promotion", money + ""); map.put("useFlag", "0"); map.put("type", type + ""); map.put("available", available + ""); map.put("endTime", endTime); //插库 codeService.insertCode(map); System.out.println(YHQ); //新生成的加到codeList中,防止新生成的优惠码是重复的 codeList.add(YHQ); count++; } YHQ = str; if (count == sum) { isfull = false; } } } } 优惠码数据库设计
时间: 2024-10-14 03:19:22