const { CloudQuery, CloudObject } = require("../lib/ncloud");
const { FilmUserList } = require("./data.js");
inportFilmUser()

DataMap = {
   FilmUser:{}
}

async function inportFilmUser(){
    // 导入用户数据
    let filmuserList =FilmUserList
    for (let index = 0; index <filmuserList.length; index++) {
        let filmuser =filmuserList[index];
       filmuser = await importObject("FilmUser",filmuser)
    }
    console.log(DataMap["FilmUser"])
}

async function importObject(className,data){

    // 查重 username 数据源列表中的objectId并非数据库生成的唯一ID，因此需要有一个username字段进行记录，并查重
    let query = new CloudQuery(className)
    let username = data.objectId
    query.equalTo("username",username)
    let importObj = await query.first()
    console.log(importObj)

    // 导入
    // 导入前批量处理Pointer类型数据，进行重定向
    Object.keys(data)?.forEach(key=>{
        let field = data[key]
        let username = field?.objectId
        if(username){ // 是数组字段
            if(key=="depart"){
                data[key] = DataMap?.["Department"]?.[username]?.toPointer();
            }
        }
    })
    // 若未添加，则创建新对象并保存
    if(!importObj?.id){
        importObj = new CloudObject(className)
    }

      // 保存或更新数据
      data.username = username;
      importObj.set(data);
      importObj = await importObj.save();
      DataMap[className][username] = importObj
}
































// 测试

// const { CloudQuery, CloudObject } = require("../lib/ncloud");
// testUser()
// // testCRUD()
// // testQuery()

// async function testQuery(){
//     let query = new CloudQuery("FilmRole")
//     // query.equalTo("gender","女")
//     // query.greaterThanAndEqualTo("age",40)
//     // query.lessThan("age",41)
//     let list = await query.find();
//     console.log(list)
// }

// async function testCRUD(){
//     // 基本的增删查改测试
//     let query = new CloudQuery("FilmRole")
//     let FilmRoleList = await query.find();
//     console.log("FilmRolelist count",FilmRoleList?.length)

//     let newFilmRole = new CloudObject("FilmRole")
//     newFilmRole.set({"name":"123"})
    
//     newFilmRole = await newFilmRole.save(newFilmRole)
//     console.log("newFilmRole",newFilmRole)

//     await newFilmRole.destory()
//     console.log("newFilmRole 已删除",newFilmRole)
// }

// async function testUser(){
//     //基本的增删查改测试
//     let query = new CloudQuery("FilmUser")
//     query.equalTo("name","qwq")
//     let List =await query.find();

//     List.forEach(obj => {
//         let newFilmUser = new CloudObject("FilmUser")
//         console.log(`${obj.objectId}`);
//         newFilmUser.id=`${obj.objectId}`
//         newFilmUser.destory()
//         console.log("newFilmUser 已删除",newFilmUser)
//       });
    

//     // let FilmUserList = await query.find();
//     // console.log("FilmUserList count",FilmUserList?.length)

//     let query1 = new CloudQuery("FilmUser")
//     let FilmUserList1 = await query1.find();
//     console.log(FilmUserList1)
//     console.log("FilmUserList count",FilmUserList1?.length)
// }