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




import com.qiuguo.iot.data.entity.device.DeviceInfoEntity;
import com.qiuguo.iot.data.resp.device.DeviceInfoResp;
import org.hswebframework.web.api.crud.entity.PagerResult;

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 MysqlUtilTable2Contoller {
    public static String TAB = "	";
    /**
     * 打印entity的信息
     */
    public static void printController(TablesBean tableBean) {

        boolean hasDate = false;


        List<FieldBean> list = tableBean.getFieldList();

        StringBuffer bf = new StringBuffer();

        String realName = MysqlMain.pre + tableBean.getSpaceName() + "";
        String fileName = MysqlMain.save_path + "/" + realName + "Controller.java";

        try {
            StringBuilder content = new StringBuilder();
            content.append("package com.api.admin.controller;\n");
            content.append("\n");
            content.append("\n");
            content.append("\n");
            content.append("\n");

            content.append("/**\n");
            content.append("* <p>\n");
            //content += "* 菜单权限表\n";
            //content += "* </p>";
            content.append("*" + tableBean.getComment() + "\n");
            content.append("* @author wulin\n");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            content.append("* @since " + format.format(new Date()) + "\n");
            content.append("*/\n");

            content.append("\n");
            content.append("\n");

            content.append("@RestController\n");
            content.append("@RequestMapping(\"/" + tableBean.getJavaName() + "\")\n");
            content.append("public class " + tableBean.getSpaceName() + "Controller {\n");
            content.append("\n");
            content.append("\n");
            content.append("    @Autowired\n");
            content.append("    private " + tableBean.getSpaceName() + "Service " + tableBean.getJavaName() + "Service;\n");
            content.append("\n");
            content.append("\n");
            content.append("    @RequestMapping(\"/list\")\n");
            content.append("     public R list(@RequestBody String body) {\n");
            content.append("        Map<String,Object> params = new HashMap<>();\n");
            content.append("        if(StringUtil.isNotBlank(body)){\n");
            content.append("            params = JSONObject.parseObject(body,Map.class);\n");
            content.append("        }\n");
            content.append("        PageUtils page = " + tableBean.getJavaName() + "Service.queryPage(params);\n");
            content.append("        return R.ok().put(\"page\", page);\n");
            content.append("     }\n");
            content.append("\n");
            content.append("\n");


            content.append("     @RequestMapping(\"/getById\")\n");
            content.append("     public R getById(@RequestBody " + tableBean.getSpaceName() + " " + tableBean.getJavaName() + ") {\n");
            content.append("        " + tableBean.getJavaName() + " = " + tableBean.getJavaName() + "Service.select" + tableBean.getSpaceName() + "ById(" + tableBean.getJavaName() + ".getId());\n");
            content.append("        return R.ok().put(\"" + tableBean.getJavaName() + "\"," + tableBean.getJavaName() + ");\n");

            content.append("     }\n");


            content.append("\n");
            content.append("\n");
            content.append("     @RequestMapping(\"/update\")\n");
            content.append("     public R update(@RequestBody " + tableBean.getSpaceName() + " " + tableBean.getJavaName() + ") {\n");
            content.append("        " + tableBean.getJavaName() + "Service.update" + tableBean.getSpaceName() + "ById(" + tableBean.getJavaName() + ");\n");
            content.append("        return R.ok();\n");
            content.append("     }\n");
            content.append("\n");
            content.append("\n");
            content.append("     @RequestMapping(\"/save\")\n");
            content.append("        public R save(@RequestBody " + tableBean.getSpaceName() + " " + tableBean.getJavaName() + ") {\n");
            content.append("        " + tableBean.getJavaName() + "Service.insert" + tableBean.getSpaceName() + "(" + tableBean.getJavaName() + ");\n");
            content.append("        return R.ok();\n");
            content.append("     }\n");
            content.append("\n");
            content.append("\n");
            content.append("    @RequestMapping(\"/delete\")\n");
            content.append("    public R delete(@RequestBody Long id) {\n");
            content.append("        " + tableBean.getJavaName() + "Service.delete" + tableBean.getSpaceName() + "ById(id);\n");
            content.append("        return R.ok();\n");
            content.append("     }\n");
            content.append("}\n");

            FileOutputStream fos = new FileOutputStream(fileName);

            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(content.toString());
            out.close();
            fos.close();
            System.out.println("===" + realName + ".java" + "生成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    public static void printReactorController(TablesBean tableBean) {
        String realName = MysqlMain.pre + tableBean.getSpaceName();
        String fileName = MysqlMain.save_path + "/" + realName + "Controller.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("表", "") + "Controller类\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 += "@RestController\n@Slf4j\n@RequestMapping(\"/" + realName + "\")\n";
            content += "public class " + tableBean.getSpaceName() + "Controller{\n";
            content += "\n";
            content += "\n";

            content += TAB + "@Autowired\n";
            content += TAB + "private " + realName + "Service " + MysqlUtil.getFirstToLower(realName) + "Service;\n";

            content += TAB + "@PostMapping(\"/info\")\n";
            content += TAB + "public Mono<" + realName + "Resp> select" + realName + "ByRequest(@RequestBody " + realName + "Request request){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.select"
                    + realName + "ByRequest(request).map(d -> {return new " + realName + "Resp(d);});\n";

            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@PostMapping(\"/list\")\n";
            content += TAB + "public Mono<PagerResult<" + realName + "Resp>> select" + realName + "sByRequest(@RequestBody " + realName + "Request request){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.selectDeviceInfosByRequest(request).map(d -> {\n" +
                    "            PagerResult<" + realName + "Resp> result = new PagerResult<>();\n" +
                    "            result.setPageIndex(d.getPageIndex());\n" +
                    "            result.setPageSize(d.getPageSize());\n" +
                    "            result.setTotal(d.getTotal());\n" +
                    "            List<" + realName + "Resp> ds = d.getData().stream().map(new Function<" + realName + "Entity, " + realName + "Resp>() {\n" +
                    "                    @Override\n" +
                    "                    public DeviceInfoResp apply(" + realName + "Entity entity) {\n" +
                    "                        return new " + realName + "Resp(entity);\n" +
                    "                    }\n" +
                    "                }\n" +
                    "\n" +
                    "            ).collect(Collectors.toList());\n" +
                    "            result.setData(ds);\n" +
                    "            return result;\n" +
                    "        });\n";

            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@GetMapping(\"/id\")\n";
            content += TAB + "public Mono<" + realName + "Resp> select" + realName + "ById(@RequestParam Long id){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.select"
                    + realName + "ById(id).map(d -> {return new " + realName + "Resp(d);});\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@PostMapping(\"/save\")\n";
            content += TAB + "public Mono<Integer> insert" + realName + "(@RequestBody " + realName + "Entity entity){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.insert"
                    + realName + "(entity);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@PostMapping(\"/update\")\n";
            content += TAB + "public Mono<Integer> update" + realName + "ById(@RequestBody " + realName + "Entity entity){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.update"
                    + realName + "ById(entity);\n";
            content += TAB + "}\n";

            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@PostMapping(\"/updateCover\")\n";
            content += TAB + "public Mono<Integer> updateCover" + realName + "ById(@RequestBody " + realName + "Entity entity){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.updateCover"
                    + realName + "ById(entity);\n";
            content += TAB + "}\n";
            content += "\n";
            content += "\n";
            content += "\n";
            content += TAB + "@PostMapping(\"/delete\")\n";
            content += TAB + "public Mono<Integer> delete" + tableBean.getSpaceName() + "ById(@RequestParam Long id){\n";
            content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.delete"
                    + realName + "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 + "Service.java" + "生成");
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            // FileWriter writer = new FileWriter(fileName, false);
            // writer.write(content);
            // writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
