package com.qiuguo.iot.customer.http.api.mysql;







import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MysqlUtilTable2Service {


    public static String TAB = "	";

    public static void printReactorService(TablesBean tableBean) {
        String realName = MysqlMain.pre + tableBean.getSpaceName();

        String realName2 = MysqlMain.pre + tableBean.getSpaceName() + "";

        String javaName = "" + captureName(tableBean.getJavaName());
        String fileName = MysqlMain.save_path + "/" + realName + "Service.java";
        List<FieldBean> list = tableBean.getFieldList();
        try {
            String content = "package com.admin.service.impl;\n\n\n\n" +
                    "import org.apache.commons.lang3.StringUtils;\n" +
                    "import java.util.Date;\n";


            content += "/**\n";
            content += "* <p>\n";
            content += "* " + tableBean.getComment().replace("表", "") + "服务类\n";
            content += "* </p>\n";
            content += "*\n";
            content += "* @author wulin\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";

            content += "\n";
            content += "@Service\n@Slf4j\n";
            content += "public class " + tableBean.getSpaceName() + "Service extends GenericReactiveCrudService<" + tableBean.getSpaceName() + "Entity, Long>  {\n";
            content += "\n";
            content += "\n";

            content += TAB + "public Mono<" + realName + "Entity> select" + realName + "ByRequest(" + realName + "Request request){\n";
            content += TAB + TAB + "ReactiveQuery<" + realName + "Entity> reactiveQuery = createQuery();\n";
            content += TAB + TAB + "reactiveQuery = reactiveQuery.and(\"is_delete\", 0);\n";
            for (FieldBean tb : list) {
               if(tb.getJavaType().equals("String")){
                    content += TAB + TAB + "if(StringUtils.isNotEmpty(request." + tb.getJavaCodeForGet() + "())){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.and(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }else{
                    content += TAB + TAB + "if(request." + tb.getJavaCodeForGet() + "() != null){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.and(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }
            }
            content += TAB + TAB + "SortOrder sortOrder = null;\n";
            content += TAB + TAB + "if(StringUtils.isNotEmpty(request.getOrder())){\n";
            content += TAB + TAB + TAB + "if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo(\"0\") == 0){\n";
            content += TAB + TAB + TAB + TAB + "sortOrder = SortOrder.desc(request.getOrder());\n";
            content += TAB + TAB + TAB + "}else{\n";
            content += TAB + TAB + TAB + TAB + "sortOrder = SortOrder.asc(request.getOrder());\n";
            content += TAB + TAB  + TAB + "}\n";
            content += TAB + TAB  + TAB + "reactiveQuery = reactiveQuery.orderBy(sortOrder);\n";
            content += TAB + TAB + "}\n";
            content += TAB + TAB + "return reactiveQuery.fetchOne();\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<PagerResult<" + realName + "Entity>> select" + realName + "sByRequest(" + realName + "Request request){\n";
            content += TAB + TAB + "ReactiveQuery<" + realName + "Entity> reactiveQuery = createQuery();\n";
            content += TAB + TAB + "reactiveQuery = reactiveQuery.and(\"is_delete\", 0);\n";
            for (FieldBean tb : list) {
                if(tb.getJavaType().equals("Date") || tb.getField().equals("modify_time") || tb.getField().equals("create_time")){
                    content += TAB + TAB + "if(request." + tb.getJavaCodeForGet() + "Start() != null){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.gte(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "Start());\n";
                    content += TAB + TAB + "}\n";
                    content += TAB + TAB + "if(request." + tb.getJavaCodeForGet() + "End() != null){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.lte(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "End());\n";
                    content += TAB + TAB + "}\n";
                }else if(tb.getJavaType().equals("String")){
                    content += TAB + TAB + "if(StringUtils.isNotEmpty(request." + tb.getJavaCodeForGet() + "())){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.$like$(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }else{
                    content += TAB + TAB + "if(request." + tb.getJavaCodeForGet() + "() != null){\n";
                    content += TAB + TAB + TAB + "reactiveQuery = reactiveQuery.and(" + realName + "Request::" + tb.getJavaCodeForGet() + ", request." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }
            }
            content += TAB + TAB + "SortOrder sortOrder = null;\n";
            content += TAB + TAB + "if(StringUtils.isNotEmpty(request.getOrder())){\n";
            content += TAB + TAB + TAB + "if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo(\"0\") == 0){\n";
            content += TAB + TAB + TAB + TAB + "sortOrder = SortOrder.desc(request.getOrder());\n";
            content += TAB + TAB + TAB + "}else{\n";
            content += TAB + TAB + TAB + TAB + "sortOrder = SortOrder.asc(request.getOrder());\n";
            content += TAB + TAB  + TAB + "}\n";
            content += TAB + TAB  + TAB + "reactiveQuery = reactiveQuery.orderBy(sortOrder);\n";
            content += TAB + TAB + "}\n";
            content += TAB + TAB +"QueryParamEntity param = QueryParamEntity.of(reactiveQuery.getParam());\n" +
                    "\t\tparam.setPageIndex(request.getCurrPage());\n" +
                    "\t\tparam.setPageSize(request.getPageSize());\n" +
                    "\t\tparam.setPaging(true);\n" +
                    "\t\tparam.setFirstPageIndex(1);\n";
            content += TAB + TAB + "return queryPager(param);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<" + realName + "Entity> select" + realName + "ById(Long id){\n";
            content += TAB + TAB + "return createQuery()\n" +
                    "\t\t\t\t.and(\"is_delete\", 0)\n" +
                    "\t\t\t\t.and(\"id\", id)\n" +
                    "\t\t\t\t.fetchOne();\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<Integer> insert" + realName + "(" + realName + "Entity entity){\n";
            content += TAB + TAB + "entity.setId(null);\n";
            content += TAB + TAB + "entity.setCreateTime(null);\n";
            content += TAB + TAB + "entity.setModifyTime(null);\n";
            content += TAB + TAB + "return insert(entity);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<Integer> update" + realName + "ById(" + realName + "Entity entity){\n";
            content += TAB + TAB + "ReactiveUpdate<" + realName + "Entity> update = createUpdate()\n" +
                    "                .set(" + realName + "Entity::getModifyTime, new Date());\n";
            for (FieldBean tb : list) {
                if(tb.getField().equals("id") || tb.getField().equals("modify_time") || tb.getField().equals("create_time")){

                }else if(tb.getJavaType().equals("String")){
                    content += TAB + TAB + "if(StringUtils.isNotEmpty(entity." + tb.getJavaCodeForGet() + "())){\n";
                    content += TAB + TAB + TAB + "update = update.set(" + realName + "Entity::" + tb.getJavaCodeForGet() + ", entity." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }else{
                    content += TAB + TAB + "if(entity." + tb.getJavaCodeForGet() + "() != null){\n";
                    content += TAB + TAB + TAB + "update = update.set(" + realName + "Entity::" + tb.getJavaCodeForGet() + ", entity." + tb.getJavaCodeForGet() + "());\n";
                    content += TAB + TAB + "}\n";
                }
            }
            content += TAB + TAB + "return update.where(" + realName + "Entity::getId, entity.getId()).and(\"is_delete\", 0).execute();\n";
            content += TAB + "}\n";

            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<Integer> updateCover" + realName + "ById(" + realName + "Entity entity){\n";
            content += TAB + TAB + "ReactiveUpdate<" + realName + "Entity> update = createUpdate()\n" +
                    "                .set(" + realName + "Entity::getModifyTime, new Date());\n";
            for (FieldBean tb : list) {
                if(tb.getField().equals("id") || tb.getField().equals("modify_time") || tb.getField().equals("create_time")){

                }else{
                    content += TAB + TAB + "update = update.set(" + realName + "Entity::" + tb.getJavaCodeForGet() + ", entity." + tb.getJavaCodeForGet() + "());\n";

                }
            }
            content += TAB + TAB + "return update.where(" + realName + "Entity::getId, entity.getId()).and(\"is_delete\", 0).execute();\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "public Mono<Integer> delete" + tableBean.getSpaceName() + "ById(Long id){\n";
            content += TAB + TAB + "return createUpdate()\n" +
                    "\t\t\t\t.set(\"is_delete\", 1)\n" +
                    "\t\t\t\t.set(\"modify_time\", new Date())\n" +
                    "\t\t\t\t.where(\"id\", id)\n" +
                    "\t\t\t\t.execute();\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += "}" + "\n";
            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();

            System.out.println("===" + realName + "Service.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void printService(TablesBean tableBean) {
        String realName = MysqlMain.pre + tableBean.getSpaceName();
        String realName2 = MysqlMain.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain.save_path + "/" + realName + "Service.java";

        try {
            String content = "package com.api.business.service;\n";


            content += "/**\n";
            content += "* <p>\n";
            content += "* " + tableBean.getComment() + " 服务类\n";
            content += "* </p>\n";
            content += "*\n";
            content += "* @author wulin\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";
            content += "public interface " + tableBean.getSpaceName() + "Service extends IService<" + tableBean.getSpaceName() + "> {\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += TAB + realName + " select" + realName + "ById(Long id);";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "Long insert" + realName + "(" + realName + " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int update" + realName + "ById(" + realName + " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int updateCover" + realName + "ById(" + realName + " " + tableBean.getJavaName() + ");";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "int delete" + tableBean.getSpaceName() + "ById(Long id);";
            content += "\n";
            content += "\n";
            content += "\n";


            content += "}";

            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();

            System.out.println("===" + realName + "Service.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String captureName(String name) {
        if (StringUtil.isNotBlank(name) && name.length() > 0) {
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        return name;

    }

    public static void printServiceImpl(TablesBean tableBean) {
        String realName = MysqlMain.pre + tableBean.getSpaceName();

        String realName2 = MysqlMain.pre + tableBean.getSpaceName() + "";

        String javaName = "" + captureName(tableBean.getJavaName());
        String fileName = MysqlMain.save_path + "/" + realName + "ServiceImpl.java";

        try {
            String content = "package com.admin.service.impl;\n";


            content += "/**\n";
            content += "* <p>\n";
            content += "* " + tableBean.getComment() + " 服务类\n";
            content += "* </p>\n";
            content += "*\n";
            content += "* @author wulin\n";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content += "* @since " + format.format(new Date()) + "\n";
            content += "*/\n";

            content += "\n";
            content += "@Service\n";
            content += "public class " + tableBean.getSpaceName() + "ServiceImpl extends ServiceImpl<" + tableBean.getSpaceName() + "Mapper, " + tableBean.getSpaceName() + "> implements " + tableBean.getSpaceName() + "Service {\n";
            content += "\n";
            content += "\n";
            content += "    @Autowired\n";
            content += "\tprivate " + realName + "Mapper " + MysqlUtil.getFirstToLower(javaName) + "Mapper;" + "\n";
            content += "\n";
            content += "\n";
            content += "\n";

            content += TAB + "@Override\n";
            content += TAB + "public " + realName + " select" + realName + "ById(Long id){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Mapper." + "select" + realName + "ById(id);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public Long insert" + realName + "(" + realName + " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Mapper." + "insert" + realName + "(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public int update" + realName + "ById(" + realName + " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Mapper." + "update" + realName + "ById(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";

            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public int updateCover" + realName + "ById(" + realName + " " + tableBean.getJavaName() + "){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Mapper." + "updateCover" + realName + "ById(" + tableBean.getJavaName() + ");\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@Override\n";
            content += TAB + "public int delete" + tableBean.getSpaceName() + "ById(Long id){\n";
            content += TAB + TAB + "return " + tableBean.getJavaName() + "Mapper." + "delete" + tableBean.getSpaceName() + "ById(id);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";


            content += "}" + "\n";
            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content);
            out.close();
            fos.close();

            System.out.println("===" + realName + "ServiceImpl.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
