package com.brframework.generate;

import com.brframework.generate.template.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xu
 * @date 2019/11/18 16:46
 */
public class GenerateClass {

    public static void main(String[] args) throws Exception{
/**
 * baseDir -> 基础路径
 * projectName -> 代码所属的项目名,例如 yigou_module
 * resourcesDir -> 资源目录,由于暂时需要生成xml,yml之类的文件,所以不需要
 * javaDir -> java类目录
 * packageDir -> 包名,在java类目录之下
 */
        String baseDir = "common-generate/src/build";
        String projectName = "";
        String javaDir = "src/main/java";
        String packageDir = "com/softwarebr/nunu";
        String route = baseDir + "/" + projectName + "/" + javaDir + "/" + packageDir;

        Map<String, String> noteMap = new HashMap<>();
        //文件注释
        noteMap.put("Author", "ljr");
        noteMap.put("Date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));

        /**
         * 实体map -> key:指定实体名称; value:功能模块
         * key是类名,小写即可,不要大写,不然serviceImpl注入的dao会变成大写
         */

        Map<String, List<FileDomain>> modelMap = new HashMap<>();
        List<FileDomain> infos = new ArrayList<>();

        FileDomain space = new FileDomain();
        //实体类名不需要首字母大写,也不需要在这里指定模块名
        space.setDoMain("information");
        space.setDoMainName("捡漏动态");
        space.setContent("//id\n" +
                "id:Long\n" +
                "//创建时间\n" +
                "createDate:LocalDateTime\n" +
                "//来源类型(1申请发布二手商品 2 已发布二手商品 3直播间下单成功4用户申请鉴定 5用户正在鉴定)\n" +
                "type:Integer\n" +
                "//发起的用户 \n" +
                "fromMemberId:Long\n" +
                "//涉及的用户\n" +
                "toMemberId:Long\n" +
                "//扩展\n" +
                "expand:String");
        infos.add(space);

        FileDomain comment = new FileDomain();
        //实体类名不需要首字母大写,也不需要在这里指定模块名
        comment.setDoMain("comment");
        comment.setDoMainName("评论记录");
        comment.setContent("//id\n" +
                "id:Long\n" +
                "//创建时间\n" +
                "createDate:LocalDateTime\n" +
                "//用户\n" +
                "memberId:Long\n" +
                "//评论内容\n" +
                "reviewContent:String\n" +
                "//评论图片\n" +
                "reviewUri:String\n" +
                "//对象类型 1.主播 2.鉴定师 3.商品 4.鉴定视频\n" +
                "targetType:Integer\n" +
                "//对象Id\n" +
                "targetId:Long\n" +
                "//订单号\n" +
                "orderNo:String\n" +
                "//星值1~10分\n" +
                "starValue:Integer\n" +
                "//评价标签\n" +
                "label:String\n" +
                "//点赞数\n" +
                "likeCount:Integer\n" +
                "//回复对象\n" +
                "replyId:Long\n" +
                "//审核状态 1未审核 2通过 3不通过\n" +
                "verify:Integer");
        infos.add(comment);
        //模块化,key是模块名和简称的组合,value是要生成的表属性的集合
        //user是生成的模块命,文件按模块来存放;u是数据库表名的简称;value是文件功能说明
        modelMap.put("app,app,app配置", infos);

        execute(modelMap, noteMap, route, packageDir);
    }

    private static void execute(Map<String, List<FileDomain>> modelMap, Map<String, String> noteMap, String route, String packageDir) throws Exception{
        for (String model : modelMap.keySet()) {
            String[] moduleInfo = model.split(",");
            //模块
            String function = moduleInfo[0];
            //缩写
            String abbreviation = moduleInfo[1];
            //模块名称
            String functionName = moduleInfo[2];
            List<FileDomain> fileDomains = modelMap.get(model);
            for (FileDomain fileDomain : fileDomains) {
                model = fileDomain.getDoMain();
                String content = fileDomain.getContent();
                //entity
                EntityClassFile entityClassFile =
                        new EntityClassFile(route, packageDir
                                , model, function,noteMap, abbreviation, content);
                GenerateUtil.generate(entityClassFile);

                //jsonParam
                JsonClassFile jsonParamFile =
                        new JsonClassFile(route, packageDir, model, function, Constant.JSON_NAME_CREATE, noteMap);
                GenerateUtil.generate(jsonParamFile);

                //jsonQuery
                JsonClassFile jsonQueryFile =
                        new JsonClassFile(route, packageDir, model, function, Constant.JSON_NAME_QUERY, noteMap);
                GenerateUtil.generate(jsonQueryFile);

                //jsonResult
                JsonClassFile jsonResultFile =
                        new JsonClassFile(route,packageDir, model, function, Constant.JSON_NAME_RESULT, noteMap);
                GenerateUtil.generate(jsonResultFile);

                //dao
                DaoClassFile daoClassFile =
                        new DaoClassFile(route, packageDir, model, function, noteMap);
                GenerateUtil.generate(daoClassFile);

                //service interface
                ServiceInterfaceFile serviceInterfaceFile =
                        new ServiceInterfaceFile(route, packageDir, model, function, noteMap);
                GenerateUtil.generate(serviceInterfaceFile);

                //serviceimpl
                ServiceImplFile serviceImplFile =
                        new ServiceImplFile(route, packageDir, model, function, noteMap);
                GenerateUtil.generate(serviceImplFile);

                //apicontroller
                ApiControllerClassFile apiControllerClassFile =
                        new ApiControllerClassFile(route, packageDir, model, function, noteMap);
                GenerateUtil.generate(apiControllerClassFile);
            }
            //admincontroller
            AdminControllerFile adminControllerFile =
                    new AdminControllerFile(route , packageDir, function, noteMap, fileDomains, functionName);
            GenerateUtil.generate(adminControllerFile);

        }
    }

}
