package com.xf.mymark;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Controller
public class MarkController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @GetMapping("/")
    public String hello(){
        return "hello" ;
    }
    @RequestMapping("/showDatas")
    @ResponseBody
    public String showDatas(){
        initTables();
        String sql2 = "select id,pid,name, 'javascript:openMark('|| id ||');' url from markinfo order by pid ,id ";
        List<Map<String, Object>> datas = jdbcTemplate.queryForList(sql2);
        return JSONArray.toJSONString(datas);
    }
    @RequestMapping(value="/uploadImgs")
    @ResponseBody
    public JSONArray uploadImgs(@RequestParam(value="file[]")MultipartFile file[]) throws IOException {
        //创建JSON数组,返回一组url和name,告诉编辑器上传的图片路径
        JSONArray jsonArray = new JSONArray();
        try {
            //循环为编辑器传来的图片改名
            for(int i=0;i<file.length;i++) {
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                //获取文件后缀名,并拼接UUID和后缀名
                String suffix = file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf("."));
                File newFile = new File(uuid + suffix);
                byte[] bytes = file[i].getBytes();
                String contentType = file[i].getContentType();
                String imgbase64 = Base64.getEncoder().encodeToString(bytes);
                String base64EncoderImg = "data:" + contentType + ";base64," + imgbase64;
                //创建JSON对象并加入JSON数组
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", base64EncoderImg);
                jsonObject.put("name", file[i].getOriginalFilename());
                jsonObject.put("error", 0);
                jsonArray.add(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
    @RequestMapping("/updatePid")
    @ResponseBody
    public String updatePid(){
        String pid = request.getParameter("pid");
        String id = request.getParameter("id");
        if(id.equals(pid)) return "不能选择自身作为上级!";
        String sql2 = "update markinfo set pid = ? where id = ? ";
        jdbcTemplate.execute(sql2, new PreparedStatementCallback<Object>() {
            @Override
            public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                ps.setString(1,pid);
                ps.setString(2,id);
                return ps.execute();
            }
        });
        return "ok";
    }
    @RequestMapping("/del")
    @ResponseBody
    public String delMark(){
        String id = request.getParameter("id");
        String sql2 = "delete from markinfo where id = "+id ;
        jdbcTemplate.execute(sql2);
        return "ok";
    }
    @RequestMapping("/addMarkName")
    @ResponseBody
    public String addMarkName(){
        String name = request.getParameter("name");
        String pid = request.getParameter("pid");
        String sql = "select 1 from markinfo where pid = "+pid +" and name='"+name+"'";
        List<Map<String, Object>> datas = jdbcTemplate.queryForList(sql);
        if(datas.size()>0){
            return "名称:"+name+" 已存在,请勿重复创建!";
        }else{
            String sql2 = "insert into markinfo (pid,name)values (?,?)";
            jdbcTemplate.execute(sql2, new PreparedStatementCallback<Object>() {
                @Override
                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    ps.setString(1,pid);
                    ps.setString(2,name);
                    return ps.execute();
                }
            });
            return "ok";
        }
    }
    @RequestMapping("/saveDatas")
    @ResponseBody
    public String saveDatas(){
        String textInfo = request.getParameter("textInfo");
        String id = request.getParameter("id");
        String sql2 = "update markinfo set textInfo = ? where id = ? ";
        jdbcTemplate.execute(sql2, new PreparedStatementCallback<Object>() {
            @Override
            public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                ps.setString(1,textInfo);
                ps.setString(2,id);
                return ps.execute();
            }
        });
        return "ok";
    }
    @RequestMapping("/getData")
    @ResponseBody
    public String getData(){
        String id = request.getParameter("id");
        String sql2 = "select textinfo  from markinfo where id = "+id;
        List<Map<String,Object>> m = jdbcTemplate.queryForList(sql2);
        if(m.size()>0) {
            return (String) m.get(0).get("textinfo");
        }else{
            return "";
        }
    }
    private void initTables(){
        String sql = "create table if not exists markinfo(id INTEGER PRIMARY KEY , pid INTEGER ,name varchar(20),textInfo text)";
        jdbcTemplate.execute(sql);
        String sql2 = "insert into markinfo(pid,name) select '0','ROOT' where not exists (select 1 from markinfo)";
        jdbcTemplate.execute(sql2);
    }
}
 最近下载更多
最近下载更多
                
                7214978     LV2
                1月7日
            
            
        
                hanweinan6     LV13
                2024年10月9日
            
            
        
                Seaskye     LV14
                2023年11月4日
            
            
        
                笑得很美     LV11
                2023年10月12日
            
            
        
                DeanYao     LV2
                2023年10月11日
            
            
        
                1483259138     LV1
                2023年9月18日
            
            
        
                最代码官方     LV168
                2023年9月16日
            
            
         最近浏览更多
最近浏览更多
                
                1140717565     LV2
                7月18日
            
            
        
                潜心小白来到     LV3
                6月10日
            
            
        
                blanknb    
                3月18日
            
            
                    暂无贡献等级
            
        
                段池卿     LV5
                1月19日
            
            
        
                7214978     LV2
                1月7日
            
            
        
                yimaoermao     LV1
                2024年11月28日
            
            
        
                charleswang     LV7
                2024年10月21日
            
            
        
                hanweinan6     LV13
                2024年10月9日
            
            
        
                Peny_ZH     LV5
                2024年9月20日
            
            
        
                develop     LV10
                2024年6月2日
            
            
        
 
                 
                 
                 
                