
router.route('/updateStudioWorkNew4').all((req, res, next) => { //学生
    if (req.body[0]) {
        let p = [req.body[0].cid];
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "select_studio_course_detail2"); //執行存儲過程
        mysql.usselect(p, function(ret) {
            let _unitIndex = parseInt(req.body[0].unitIndex);
            let _chapters = JSON.parse(req.body[0].chapters);
            let _unitJson = JSON.parse(ret[0][0].chapters)
            if ((_unitJson.length - 1) < _unitIndex) {
                let _i = _unitIndex - (_unitJson.length - 1)
                for (let i = 0; i < _i; i++) {
                    _unitJson.push({
                        dyName: "", //单元标题
                        chapterInfo: [{
                            isread: false,
                            chapterid: guid(),
                            title: "",
                            courseName: "",
                            taskJson: [{
                                task: "",
                                taskDetail: "",
                                chapterData: [],
                                toolText: "",
                                toolChoose: [{
                                    tool: [],
                                    toolDetail: "",
                                    toolType: 0,
                                    askCount: 1,
                                    askTitle: "",
                                    askJson: [{ askstitle: "", askItem: 1, checkList: [] }],
                                }, ],
                                isShowTools: false,
                                askCount: 1,
                                isFold: 0,
                                askTitle: "",
                                askJson: [{ askstitle: "", askItem: 1, checkList: [] }],
                                checkJson: [{ checkCount: [], checkPerent: [] }],
                                homeworkList: [],
                            }, ],
                            itemCount: 1,
                            fileList1: [],
                            video: [],
                            testData: [],
                            pData: [],
                            templateArray: [],
                        }, ],
                    })
                }
                _unitJson[_unitIndex] = _chapters[_unitIndex]
            } else {
                _unitJson[_unitIndex] = _chapters[_unitIndex]
            }
            // console.log(_unitJson);
            let p2 = [req.body[0].cid, JSON.stringify(_unitJson).replaceAll(/%/g, "%25")];
            p2.unshift(_mysqlLabor[0], _mysqlLabor[1], "updateStudioWorkNew4"); //執行存儲過程
            mysql.usselect(p2, function(ret) {
                res.end(JSON.stringify({ success: 1 }));
            });
            // res.end(JSON.stringify({ success: 1 }));
        });
    }
});
router.route('/updateStudioWorkNew2').all((req, res, next) => { //学生
    if (req.body[0]) {
        p = [req.body[0].cid, req.body[0].title, req.body[0].brief, req.body[0].cover, req.body[0].evaId, req.body[0].astudent, req.body[0].see, req.body[0].chapters, req.body[0].template, req.body[0].ateacher];
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "updateStudioWorkNew2"); //執行存儲過程
        mysql.usselect(p, function(ret) {
            let course = req.body[0].courseType;
            let courseId = req.body[0].cid;
            let userid = req.body[0].uid;
            delStudioCourseTypeS(course, courseId, userid);
            res.end(JSON.stringify({ success: 1 }));
        });
    }
});
router.route('/addStudioCourseWorkNew2').all((req, res, next) => { //学生
    if (req.body[0]) {
        p = [req.body[0].uid, req.body[0].title, req.body[0].brief, req.body[0].cover, req.body[0].evaId, req.body[0].astudent, req.body[0].see, req.body[0].chapters, req.body[0].template, req.body[0].ateacher];
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "addStudioCourseWorkNew2"); //執行存儲過程
        mysql.usselect(p, function(ret) {
            let course = req.body[0].courseType;
            let courseId = ret[0][0].courseId;
            let userid = req.body[0].uid;
            delStudioCourseTypeS(course, courseId, userid);
            res.end(JSON.stringify({ ordernumber: ret[0][0].ordernumber, courseId: ret[0][0].courseId, success: 1 }));
        });
    }
});

//teacher 查询项目模板
router.route('/getStudioTemplateTs').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "getStudioTemplateTs", json['oid']);
});
//查看课程
router.route('/select_studio_course_detail').all((req, res, next) => { //学生
    var json = queryString(req.url);
    getmysql(req, res, "select_studio_course_detail", json['cid']);
});
//查看课程
router.route('/select_studio_course_detail2').all((req, res, next) => { //学生
    var json = queryString(req.url);
    getmysql(req, res, "select_studio_course_detail2", json['cid']);
});
//协同恢复删除的阶段
router.route('/restoreStudioWork').all((req, res, next) => { //学生
    if (req.body[0]) {
        let p = [req.body[0].cid];
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "select_studio_course_detail2"); //執行存儲過程
        mysql.usselect(p, function(ret) {
            let _chapid = req.body[0].chapid;
            // console.log(_chapid);
            let _chapters = JSON.parse(req.body[0].chapters);
            let _unitJson = JSON.parse(ret[0][0].chapters)
            let _unitJson2 = []
            let chpaindex;
            for (var i = 0; i < _chapters.length; i++) {
                if (_chapters[i].chapterInfo[0].chapterid == _chapid) {
                    chpaindex = i
                }
            }
            for (var i = 0; i < _unitJson.length; i++) {
                if (_unitJson[i].chapterInfo[0].chapterid != _chapid) {
                    // console.log(1);
                    // _unitJson.splice(i, -1)
                    _unitJson2.push(_unitJson[i])
                        // break;
                }
            }
            _unitJson2.splice(chpaindex, 0, _chapters[chpaindex])
                // console.log(_unitJson2);
            let p2 = [req.body[0].cid, JSON.stringify(_unitJson2).replaceAll(/%/g, "%25")];
            p2.unshift(_mysqlLabor[0], _mysqlLabor[1], "updateStudioWorkNew4"); //執行存儲過程
            mysql.usselect(p2, function(ret) {
                res.end(JSON.stringify({ success: 1 }));
            });
            // res.end(JSON.stringify({ success: 1 }));
        });
    }
});
//查询分类及所有子分类
router.route('/selectAllTypeStudio').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "selectAllTypeStudio", json['org'], json['oid']);
});
//上传文件
router.route('/addStudioCourseWorksS').all((req, res, next) => {
    postmysql(req, res, "addStudioCourseWorksS");
});
//删除提交的作业
router.route('/deleteStudioCourseWorkS').all((req, res, next) => {
    postmysql(req, res, "deleteStudioCourseWorkS");
});
router.route('/selectStudioCourseNew222').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "selectStudioCourseNew222", json['type'], json['uid'], json['oid'], json['org'], json['typea'], json['typeb'], json['typec'], json['typed'], json['typef'], json['typeE'], json['cu'], json['cn'], json['page'], json['pageSize']);
});
//删除课程
router.route('/deleteStudioCourse').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "deleteStudioCourse", json['cid']);
});


function delStudioCourseTypeS(course, courseId, userid) {
    let _courseId = courseId;
    var _userdata = [
        _courseId
    ];
    _userdata.unshift(_mysqlLabor[0], _mysqlLabor[1], "delete_studioCourseTypeS");
    mysql.usselect(_userdata, function(ret) {
        // console.log(ret);
        addStudioCourseTypeS(userid, course, _courseId)

    });

    // setTimeout(() => {
    // }, 1000);
}
function addStudioCourseTypeS(userid, course, _courseId) {
    var _uid = userid;
    var _courses = _courseId
    var _tid = JSON.parse(course)
    for (let i = 0; i < _tid.length; i++) {
        if (_tid[i] == '') {

            continue;

        }
        var _userdata = [
            _courses,
            _tid[i],
            _uid,
        ];
        _userdata.unshift(_mysqlLabor[0], _mysqlLabor[1], "add_studioCourseTypeS");
        mysql.usselect(_userdata, function(ret) {
            // console.log(ret);
        });
    }
}