{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os,re,json,time\n",
    "\n",
    "\"\"\"---设置原题目id与新题目id列表, 新id的数目不能小于旧id的数目---\"\"\"\n",
    "old_ids = \"603,604\"\n",
    "new_ids = \"50000:60000\"\n",
    "\"\"\"---设置完毕---\"\"\"\n",
    "\"\"\"---完成编辑后记得运行第二个单元格---\"\"\"\n",
    "\n",
    "# 题号转换\n",
    "def generate_number_set(string,dict):\n",
    "    string = re.sub(r\"[\\n\\s]\",\"\",string)\n",
    "    string_list = string.split(\",\")\n",
    "    numbers_list = []\n",
    "    for s in string_list:\n",
    "        if not \":\" in s:\n",
    "            numbers_list.append(s.zfill(6))\n",
    "        else:\n",
    "            start,end = s.split(\":\")\n",
    "            for ind in range(int(start),int(end)+1):\n",
    "                numbers_list.append(str(ind).zfill(6))\n",
    "    return numbers_list\n",
    "\n",
    "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "pro_dict = json.loads(database)\n",
    "\n",
    "old_id_list = generate_number_set(old_ids,pro_dict)\n",
    "new_id_list = generate_number_set(new_ids,pro_dict)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#判断是否有已占用的题号\n",
    "occupied_flag = False\n",
    "for new_id in new_id_list:\n",
    "    if new_id in pro_dict:\n",
    "        print(\"ID\",new_id,\"已被使用.\")\n",
    "        occupied_flag = True\n",
    "\n",
    "length_old = len(old_id_list)\n",
    "length_new = len(new_id_list)\n",
    "if length_old > length_new:\n",
    "    print(\"新ID的个数不够\")\n",
    "\n",
    "\n",
    "# 将未修订的题目添加入数据库, 并作好关联\n",
    "used_new_id_list = []\n",
    "if not occupied_flag and length_new >= length_old:\n",
    "    for pos in range(length_old):\n",
    "        old_id = old_id_list[pos]\n",
    "        new_id = new_id_list[pos]\n",
    "        used_new_id_list.append(new_id)\n",
    "        pro_dict[new_id] = {}\n",
    "        for field in pro_dict[old_id]:\n",
    "            if not field == \"id\":\n",
    "                pro_dict[new_id][field] = pro_dict[old_id][field] if not type(pro_dict[old_id][field]) == list else pro_dict[old_id][field].copy()\n",
    "            else:\n",
    "                pro_dict[new_id][field] = new_id\n",
    "        pro_dict[new_id][\"related\"].append(old_id)\n",
    "        pro_dict[new_id][\"same\"] = []\n",
    "        pro_dict[new_id][\"objs\"] = pro_dict[old_id][\"objs\"].copy()\n",
    "        pro_dict[new_id][\"usages\"] = []\n",
    "        pro_dict[new_id][\"edit\"].append(str(time.localtime().tm_year)+str(time.localtime().tm_mon).zfill(2)+str(time.localtime().tm_mday).zfill(2) + \"\\t\")\n",
    "        pro_dict[old_id][\"related\"].append(new_id)\n",
    "        pro_dict[new_id][\"origin\"] += \"-\" + str(time.localtime().tm_year)+str(time.localtime().tm_mon).zfill(2)+str(time.localtime().tm_mday).zfill(2) + \"修改\"\n",
    "    with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
    "        f.write(json.dumps(pro_dict,indent = 4,ensure_ascii= False))\n",
    "    output_list = {}\n",
    "    for id in used_new_id_list:\n",
    "        output_list[id] = pro_dict[id].copy()\n",
    "\n",
    "    with open(r\"临时文件/problem_edit.json\",\"w\",encoding=\"u8\") as f:\n",
    "        f.write(json.dumps(output_list,indent = 4,ensure_ascii=False))\n",
    "    os.system(r\"code -g 临时文件/problem_edit.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"编辑完成保存关闭后运行这个代码块\"\"\"\n",
    "with open(r\"临时文件/problem_edit.json\",\"r\",encoding=\"u8\") as f:\n",
    "    datanew = f.read()\n",
    "new_pro = json.loads(datanew)\n",
    "for id in new_pro:\n",
    "    pro_dict[id] = new_pro[id].copy()\n",
    "\n",
    "#按id排序生成字典\n",
    "sorted_dict_id = sorted(pro_dict)\n",
    "sorted_dict = {}\n",
    "for id in sorted_dict_id:\n",
    "    sorted_dict[id] = pro_dict[id]\n",
    "#将排序后的字典转为json\n",
    "\n",
    "with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(json.dumps(sorted_dict,indent = 4,ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pythontest",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
