package com.lsw.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsw.utils.RSAUtils;
import com.lsw.utils.Security;

import java.net.URLEncoder;
import java.util.List;

public class AppController extends Controller {
    public void index() {
        Db.update("update " + getPara("table", "tlk_tool_borrowing") + " set parent =? where id=?", getPara("parent"), getPara("id"));
        renderJson(Kv.by("code", 200).set("msg", "操作成功"));
    }


    public void getUser() {
        List<Record> list = Db.find("select ID,NAME FROM T_USER");
        renderJson(Kv.by("code", 200).set("data", list));
    }

    public void decryptPassword() throws Exception {
        Record record = new Record();
        String token = getPara("userticket");
        if (StrKit.isBlank(token)) {
            record.set("errmsg", "token参数为空");
            renderJson(record);
            return;
        }
        //解密钉钉token
        String params = RSAUtils.decrypt(token);
        Record user = Db.findFirst("SELECT * FROM T_USER WHERE LOGINNO=?", params.split("\\|")[1].trim());
        System.out.println("解密前：" + user.getStr("LOGINPWD") + " 解密后：" + Security.decryptPassword(user.getStr("LOGINPWD")));
        redirect("https://tdm.liugong.com/h5/#/pages/login?loginNo=" + user.getStr("LOGINNO") + "&password=" + URLEncoder.encode(Security.decryptPassword(user.getStr("LOGINPWD")), "utf-8"));
    }
}