ActivityCreateSqlUtils.java 18.7 KB
package com.yoho.unions.shoudong;

import com.yoho.unions.shoudong.model.*;

import java.io.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by mingdan.ge on 2019/5/8.
 */
public class ActivityCreateSqlUtils {


    public static void main(String[] args) {
        BufferedWriter writer = null;

        /**
         * start配置参数
         * */
        String dateint="20191031";//TODO
        String unionDir = "E:\\union\\";
        String writerFile = unionDir + "activity"+dateint+".txt";
        String activityName = "双十一潮品高佣活动";//TODO
        int type = 2;//活动类型:1-特殊返,2-订单返利翻x倍,3-额外返,4-cps新人额外返//TODO
        int collage=2;//拼团订单处理:0-不做处理,1-剔除拼团订单,2-拼团参与//TODO

        //活动类型:2-订单返利翻x倍  ----字段
        BigDecimal amount=new BigDecimal(99999);//类型1、2-每单奖励xx元封顶--加倍返活动字段//TODO
        int percent=15;//类型1-返利比例万分点,类型2-倍数十分点,常用在加倍返回活动,例如活动配置加倍返1.3倍佣金,此次 配3(十分点)//TODO
        int priority=12;//优先级,越大越优先,只用于加倍返活动,同时间段多个加倍返可以参加,取此字段最大值的活动//TODO

        //活动类型:3-额外返---专用
        Map<Integer, BigDecimal> itemM = new HashMap<>();//TODO
//            itemM.put( 6,new BigDecimal(10));//满6单返10元
//            itemM.put( 10,new BigDecimal(15));
//            itemM.put( 15,new BigDecimal(30));
//            itemM.put( 30,new BigDecimal(40));
//            itemM.put( 1,new BigDecimal(5));
//            itemM.put( 2,new BigDecimal(12));
//            itemM.put( 3,new BigDecimal(21));

        int isNew=0;//是否首单专享:1-是,0-否//TODO

        int uidsType=2;//这组uid作用:0-不筛选uid 1-指定可参与活动的uid 2-剔除不可参与活动的uid//TODO
        String uidFile = unionDir + "uid" + dateint + ".txt";//文件内一行一个uid

        int needSkn=1;//这组skn作用:0-不筛选skn,1-指定可参与活动的skn,2-剔除不可参与活动的skn,3-订单只包含指定skn//TODO
        String sknFile = unionDir + "skn" + dateint + ".txt";//文件内一行一个skn

        int extraUidType=2;//满单返过滤维度:1-下单人满单,2-分享满单,一般配置的活动都是2//TODO
        int newDays=0;//注册有赚几天内可参与,这个字段一般不用,默认0就好//TODO
        BigDecimal orderAmount=new BigDecimal(99);//订单金额门槛,订单实付金额满多少可以参与活动//TODO

        String startTimeStr="2019-11-1 00:00:00";//活动开始时间//TODO
        String endTimeStr="2019-11-6 23:59:59";//活动结束时间2019/6/10 23:59:59//TODO
//        int startTime=1559959200;//活动开始时间戳,秒//TODO
//        int endTime=1560182399;//活动结束时间戳,秒//TODO
        int startTime= stringToDateInt(startTimeStr);
        int endTime= stringToDateInt(endTimeStr);

        //是否需要配置活动页面
        boolean needPage = true;//TODO
        //活动参与页面显示的banner图片
        String image = "http://img11.static.yhbimg.com/unionimg/2019/10/31/14/01dd3c21eff74f38e5c89ece2d26ff4eae.jpg";//TODO
        //活动参与页面显示的内容
        String contentFile = unionDir + "content" + dateint + ".txt";//需要把文件编码改成utf-8 无bom格式
//        String content = "奖励时间:5月9日-5月31日(共计22天)<br>奖励对象:首次带单有赚用户<br>奖励规则:活动期间,有赚新达人成功带新客订单,即可领取原本佣金之外的5元现金奖励!<br><br>注意事项:<br>特别说明:<br>1.以结算订单为准,出现取消或退货,不参与返现。<br>2.分享下单实付金额不低于50元,仅享受一次。<br>3. 拼团订单不计入活动<br>4.如出现批量注册,买号在同一个设备下单等违规行为,不进行奖励且取消其奖励资格。<br>奖励在活动结束后统一发放<br><br><br><br><br>";
        //报名成功跳转页面
        String url = "https://activity.yoho.cn/feature/6943.html?share_id=9649&title=高佣潮品汇集";//TODO
        /**
         * 配置参数end
         * */

        try {

            //创建活动
            ActivityBo activityBo = new ActivityBo();
            activityBo.setActivityName(activityName);
            activityBo.setAmount(amount);//类型1、2-每单奖励xx元封顶
            activityBo.setCollage(collage);//拼团订单处理:0-不做处理,1-剔除拼团订单,2-拼团参与
            activityBo.setType(type);//活动类型:1-特殊返,2-订单返利翻x倍,3-额外返,4-cps新人额外返
            activityBo.setStartTime(startTime);
            activityBo.setEndTime(endTime);
            activityBo.setIsNew((byte) isNew);//是否首单专享:1-是,0-否
            activityBo.setUidsType(uidsType);//这组uid作用:0-不筛选uid 1-指定可参与活动的uid 2-剔除可参与活动的uid
            activityBo.setNeedSkn(needSkn);//这组skn作用:0-不筛选skn,1-指定可参与活动的skn,2-剔除可参与活动的skn
            activityBo.setExtraUidType(extraUidType);//满单返过滤维度:1-下单人满单,2-分享满单
            activityBo.setNewDays(newDays);//注册有赚几天内可参与
            activityBo.setOrderAmount(orderAmount);//订单金额门槛
            activityBo.setPercent(percent);//类型1-返利比例万分点,类型2-倍数十分点
            activityBo.setPriority(priority);//优先级,越大越优先
            activityBo.setCreateTime((int)(System.currentTimeMillis()/1000));
            writer = new BufferedWriter(new FileWriter(writerFile));
            writer.write(createActivity(activityBo));//创建活动
            writer.newLine();//换行
            writer.newLine();//换行
            writer.write("#下面insert语句中的activityId 使用上面insert生成的id:");//创建活动
            writer.newLine();//换行

            //活动类型3-额外返,需要配置阶梯返额
            if (activityBo.getType() == 3) {
                activityBo.setAmount(new BigDecimal(0));

                ActivityItem item=getStartItem(itemM);
                if (item != null) {
                    do {
                        writer.write(createActivityItem(item));//创建活动阶梯
                        writer.newLine();//换行
                        item = item.getNext();
                    } while (item != null);
                }
                writer.newLine();//换行
            }

            //page
            if (needPage) {
                ActivityPageBo activityPageBo = new ActivityPageBo();
                String content = readFileForContent(contentFile);
                activityPageBo.setContent(content);
                activityPageBo.setTitle(activityBo.getActivityName());
                activityPageBo.setImage(image);
                activityPageBo.setCreateTime(activityBo.getCreateTime());
                activityPageBo.setUrl(url);
                writer.write(createActivityPage(activityPageBo));//创建活动
                writer.newLine();//换行
                writer.newLine();//换行
            }

            //skn
            if (activityBo.getNeedSkn() > 0) {
                Set<Integer> skns= readFileForNumSet (sknFile);
                writer.write("INSERT INTO yh_unions.union_share_activity_skn (`activity_id`, `skn`, `type`, `create_time`) VALUES");
                writer.newLine();//换行
                //IINSERT INTO `yh_unions`.`union_share_activity_skn`
                // (`activity_id`, `skn`, `type`, `create_time`) VALUES ('148', '51827678', '2', '1549004949');

                int j = 0;
                for (Integer skn : skns) {
                    writer.write(createActivitySkn(skn,activityBo.getNeedSkn(),activityBo.getCreateTime()));//创建活动
                    j++;
                    if (j < skns.size()) {
                        writer.write(",");
                    } else {
                        writer.write(";");
                    }

                }
                writer.newLine();//换行
                writer.newLine();//换行
            }

            //uid
            if (activityBo.getUidsType() > 0) {
                Set<Integer> uids= readFileForNumSet(uidFile);
                writer.write("INSERT INTO yh_unions.union_share_activity_uid (`activity_id`, `uid`, `type`, `create_time`) VALUES");
                writer.newLine();//换行
                //INSERT INTO `yh_unions`.`union_share_activity_uid`
                // (`activity_id`, `uid`, `type`, `create_time`) VALUES ('154', '9513433', '1', '1554967566');
                int i=0;
                for (Integer uid : uids) {
                    writer.write(createActivityUid(uid,activityBo.getUidsType(),activityBo.getCreateTime()));
                    i++;
                    if (i < uids.size()) {
                        writer.write(",");
                    } else {
                        writer.write(";");
                    }
                }
                writer.newLine();//换行
                writer.newLine();//换行
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static int stringToDateInt(String dateStr) {
        return stringToDateInt(dateStr,"yyyy-MM-dd HH:mm:ss");
    }
    public static int stringToDateInt(String dateStr, String simpleDateFormat) {
        return (int)(stringToDate(dateStr, simpleDateFormat).getTime() / 1000);
    }

    public static Date stringToDate(String dateStr, String simpleDateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
        Date d = null;
        if (dateStr==null||dateStr.trim().length()==0) {
            return null;
        } else {
            try {
                d = format.parse(dateStr);
            } catch (ParseException e) {
            }
            return d;
        }
    }

    public static ActivityItem getStartItem(Map<Integer, BigDecimal> itemM) {
        if (itemM.isEmpty()) {
            return null;
        }
        ActivityItem startItem = null;
        //排序
        for (Integer key : itemM.keySet()) {
            if (startItem == null) {
                startItem = new ActivityItem(itemM.get(key), key,1);
                startItem.setOrderMaxNum(100000);
            } else {
                ActivityItem nowItem = startItem;
                while (nowItem.hasNext()&&nowItem.getOrderMinNum() < key){
                    nowItem = nowItem.getNext();
                }

                ActivityItem newItem = new ActivityItem(itemM.get(key), key);
                if (nowItem.getOrderMinNum() > key) {
                    newItem.setOrderMaxNum(nowItem.getOrderMinNum() - 1);
                    newItem.setNext(nowItem);
                    newItem.setPre(nowItem.getPre());
                    newItem.setPriority(nowItem.getPriority());
                    if (newItem.getPre() != null) {
                        newItem.getPre().setNext(newItem);
                        newItem.getPre().setOrderMaxNum(newItem.getOrderMinNum() - 1);
                    }
                    nowItem.setPre(newItem);

                    while (nowItem != null) {
                        nowItem.setPriority(nowItem.getPriority() + 1);
                        nowItem = nowItem.getNext();
                    }
                } else {
                    newItem.setPre(nowItem);
                    newItem.setOrderMaxNum(100000);
                    newItem.setPriority(nowItem.getPriority()+1);
                    nowItem.setNext(newItem);
                    nowItem.setOrderMaxNum(key-1);
                }
                if (newItem.getPre() == null) {
                    startItem = newItem;
                }
            }
        }

        return startItem;
    }




    /**
     * 以行为单位读取文件
     */
    public static Set<Integer> readFileForNumSet(String readFile) {
        BufferedReader reader = null;
        Set<Integer> numSet = new HashSet<>();
        int line=0;
        try {
            reader = new BufferedReader(new FileReader(readFile));

            String tempString = null;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                if (tempString.trim().length()==0) {
                    System.out.println("此行为空:"+line+1);
                    continue;
                }
                line++;
                numSet.add(Integer.valueOf(tempString.trim()));//如果报错可能是文件里面含有非法字符,把文件编码改成“UTF-8无BOM格式”编码即可
            }
            reader.close();
        } catch (IOException e) {
            // 显示行号
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        System.out.println("line " + line  );
        return numSet;
    }/**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static String readFileForContent(String readFile) {
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            FileInputStream in = new FileInputStream(readFile);
            reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
//            reader = new BufferedReader(new FileReader(readFile));

            String tempString = null;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                if (tempString.trim().length() == 0) {
                    content.append("<br>");
                } else {
                    content.append(tempString.trim()).append("<br>");
                }
            }
            content.append("<br><br><br><br>");
            reader.close();
        } catch (IOException e) {
            // 显示行号
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return content.toString();
    }

    public static String createActivity(ActivityBo bo) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO yh_unions.union_share_orders_activity ");
        //INSERT INTO yh_unions.union_share_orders_activity
        // (`activity_name`, `type`, `extra_uid_type`, `need_skn`, `collage`, `is_new`, `uids_type`, `uids`, `amount`, `order_amount`, `percent`, `start_time`, `end_time`, `create_time`)
        // VALUES ('潮流T恤节', '2', '2', '1', '1', '1', '2', NULL, '10000.00', '99.00', '3', '1557021600', '1557676799', '1556526247');

        sql.append("(`activity_name`, `type`, `extra_uid_type`, `need_skn`, `collage`, `is_new`, `uids_type`, `amount`, `order_amount`, `percent`, `start_time`, `end_time`, `create_time`, `priority`)");
        sql.append(" VALUES (");
        sql.append("'"+bo.getActivityName()+"'").append(",").append(bo.getType()).append(",").append(bo.getExtraUidType()).append(",")
                .append(bo.getNeedSkn()).append(",").append(bo.getCollage()).append(",").append(bo.getIsNew()).append(",")
                .append(bo.getUidsType()).append(",").append(bo.getAmount()).append(",").append(bo.getOrderAmount()).append(",")
                .append(bo.getPercent()).append(",").append(bo.getStartTime()).append(",").append(bo.getEndTime()).append(",")
                .append(bo.getCreateTime()).append(",").append(bo.getPriority()).append(");");
        return sql.toString();
    }
    public static String createActivityItem(ActivityItem bo) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO yh_unions.union_share_orders_activity_item ");
        //INSERT INTO `yh_unions`.`union_share_orders_activity_item`
        // (`activity_id`, `amount`, `order_min_num`, `order_max_num`, `priority`)
        // VALUES ('150', '200.00', '100', '1000000', '5');
        sql.append("(`activity_id`, `amount`, `order_min_num`, `order_max_num`, `priority`)");
        sql.append(" VALUES ");
        sql.append("(activityId").append(",").append(bo.getAmount()).append(",")
                .append(bo.getOrderMinNum()).append(",").append(bo.getOrderMaxNum())
                .append(",").append(bo.getPriority()).append(");");
        return sql.toString();
    }
    public static String createActivityPage(ActivityPageBo bo) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO yh_unions.union_share_orders_activity_page ");
        //INSERT INTO `yh_unions`.`union_share_orders_activity_page`
        // (`activity_id`, `title`, `content`, `image`, `create_time`)
        // VALUES ('163', '匡威品牌日', '1.奖励活设备【有货有赚】公众号', 'http://img11.stat7.jpg', '1556173698');
        sql.append("(`activity_id`, `title`, `content`, `image`,`url`, `create_time`)");
        sql.append(" VALUES ");
        sql.append("(activityId").append(",'").append(bo.getTitle()).append("','")
                .append(bo.getContent()).append("','").append(bo.getImage()).append("','")
                .append(bo.getUrl()).append("',").append(bo.getCreateTime()).append(");");

        return sql.toString();
    }
    public static String createActivitySkn(Integer skn,int type,int createTime) {
        StringBuilder sql = new StringBuilder();
//        sql.append("INSERT INTO yh_unions.union_share_activity_skn ");
        //IINSERT INTO `yh_unions`.`union_share_activity_skn`
        // (`activity_id`, `skn`, `type`, `create_time`) VALUES ('148', '51827678', '2', '1549004949');

//        sql.append("(`activity_id`, `skn`, `type`, `create_time`)");
//        sql.append(" VALUES ");
        sql.append("(activityId").append(",").append(skn).append(",").append(type).append(",").append(createTime).append(")");
        return sql.toString();
    }
    public static String createActivityUid(Integer uid,int type,int createTime) {
//        ActivityUid bo=
        StringBuilder sql = new StringBuilder();
//        sql.append("INSERT INTO yh_unions.union_share_activity_uid ");
        //INSERT INTO `yh_unions`.`union_share_activity_uid`
        // (`activity_id`, `uid`, `type`, `create_time`) VALUES ('154', '9513433', '1', '1554967566');
//        sql.append("(`activity_id`, `uid`, `type`, `create_time`)");
//        sql.append(" VALUES ");
        sql.append("(activityId").append(",").append(uid).append(",").append(type).append(",").append(createTime).append(")");
        return sql.toString();
    }
}