package com.tofly.codegen.controller;

import com.tofly.codegen.entity.GenConfig;
import com.tofly.codegen.service.GeneratorService;
import com.tofly.codegen.util.DataSourceUtil;
import com.tofly.codegen.util.IOUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/9/26  10:55
 */
@Controller
@Slf4j
@CrossOrigin
public class GeneratorController {

    @Autowired
    private GeneratorService generatorService;
    @Autowired
    private StringEncryptor stringEncryptor;

    private Map<String,Object> datasources;

    private DataSource dataSource;


    @PostMapping("/connectData")
    @ResponseBody
    public String connect(@RequestBody  Map map){
        DataSourceUtil.setDataSource(map);
        return "数据源加载成功";
    }

    @RequestMapping("/test")
    @ResponseBody
    public List mytest(){
        JdbcTemplate jdbcTemplate=DataSourceUtil.getJdbcTemplate();
        List list = jdbcTemplate.queryForList("select * from tf_smpn_user_x");
        return list;
    }

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(Model model) {
        model.addAttribute("name", "word");
        return "hello";
    }



    @ResponseBody
    @RequestMapping("/gettables")
    public Map tables() {
        JdbcTemplate jdbcTemplate=DataSourceUtil.getJdbcTemplate();
        if(jdbcTemplate==null){
            return null;
        }
        List list=generatorService.getTables();
        Map jsonObject=new HashMap();
        jsonObject.put("total",list.size());
        jsonObject.put("data",list);
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping("/gettablecols")
    public Map gettableCols(String tablename) {
        List<Map> list=generatorService.queryColumns(tablename);
        Map jsonObject=new HashMap();
        jsonObject.put("total",list.size());
        jsonObject.put("data",list);
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping("/codegen")
    @SneakyThrows
    public void codegen(GenConfig genConfig, HttpServletResponse response){
        byte[] data = generatorService.generatorCode(genConfig);
        String fileName= StringUtils.isEmpty(genConfig.getTableName())?genConfig.getModuleName():genConfig.getTableName();
        response.reset();
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s.zip", fileName));
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtil.write(response.getOutputStream(), Boolean.TRUE, data);
    }

    @GetMapping("/code")
    public String uiCodeGen(){
        return "codegen";
    }

    @GetMapping("/tablegen")
    public String tablegen(Model model,@RequestParam Map row){
        System.out.println(row);
        model.addAllAttributes(row);
        return "tablegen";
    }

    @RequestMapping("/encrypt")
    @ResponseBody
    public String encrypt(String word){
        return stringEncryptor.encrypt(word);
    }

    @RequestMapping("/decrypt")
    @ResponseBody
    public String decrypt(String word) {
        String encrypt = stringEncryptor.decrypt(word);
        return encrypt;
    }
}
