// ROUTES FOR OUR API
// =============================================================================
var express = require('express');
var bcrypt = require('bcryptjs');
var router = express.Router();              // get an instance of the express Router
const querystring = require('querystring');
var mysql = require('./mysql');
const { json } = require('express');
// const _mysqlLabor = ["123.58.32.151", "LaborEdu"]; //edu數據庫信息
const _mysqlLabor = ["172.16.12.5", "LaborEdu"]; // edu數據庫信息 


//統壹處理區域
router.use(async function (req, res, next) {
    await asynnext(req, res, next);
    //// make sure we go to the next routes and don't stop here
    //res.end("");
});

/**
 * @des 參數解釋同上
 */
function asynnext(req, res, next) {
    //異步處理
    return new Promise(async (resolve, reject) => {
        next();
        //resolve(true);
    });
}

router.route('/register').post(function (req, res, next) {
    //mode的处理
    if (req.body[0]) {
        // 0 用户名 1 用户密码 2用户身份 3 第三方id（可有可无）
        p = Object.values(req.body[0]); //获取用户密码
        //没有第三方id的处理
        // if (p.length < 4) {
        //     p.push("")
        // }
        p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
        //密码加密
        // bcrypt.hash(p[1], 10, function (err, hash) {
        //     if (err) {
        //         return next(err);
        //     }
        //     p[1] = hash;
        // });
        //参数拼接，注册
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "add_register");
        //返回注册用户数据
        // mysql.usselect(p, function (ret) {
        //     res.end(ret);
        // });
        mysql.usselect(p, function (ret) {
            res.end(JSON.stringify(ret));
        });
    }
});

router.route('/registerStudent').post(function (req, res, next) {
    //mode的处理
    if (req.body[0]) {
        // 0 用户名 1 用户密码 2用户身份 3 第三方id（可有可无）
        p = Object.values(req.body[0]); //获取用户密码

        p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
        //参数拼接，注册
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], "add_registerStudent");
        mysql.usselect(p, function (ret) {
            res.end(JSON.stringify(ret));
        });
    }
});
/*登录接口*/
router.route('/login').all((req, res, next) => {
    //mode的处理
    if (req.body[0]) {
        // 0 用户名 1 用户密码
        c = Object.values(req.body[0]); //获取用户密码
        p = [c[0]]
        // p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
        var _password = [c[0]];
        _password.unshift(_mysqlLabor[0], _mysqlLabor[1], "select_password");

        mysql.usselect(_password, function (ret) {
            var _ret = ret
            var paJ = _ret[0][0].userpassword
            bcrypt.compare(c[1], paJ, function (err, result) {
                // console.log(result)
                if (result) {
                    //参数拼接，登录
                    p.unshift(_mysqlLabor[0], _mysqlLabor[1], "select_login");
                    //执行存储过程
                    mysql.usselect(p, function (ret) {
                        res.end(JSON.stringify({ status: "1", ...ret }));
                    });
                } else {
                    res.end(JSON.stringify([{ status: "0" }]));
                }
            })
        });

        // console.log(isEqual)
    }

});

//修改用户密码
router.route('/updateUserPassword').all((req, res, next) => {
    req.body[0].p = bcrypt.hashSync(req.body[0].p, bcrypt.genSaltSync(10));
    postmysql(req, res, "updateUserPassword");
});

//获取班级数据
router.route('/getClass').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_class", json['username'], json['cn'], json['page'], 10);
});

//获取申请修改班级的数量
router.route('/getClassC').all((req, res, next) => {
    getmysql(req, res, "select_updateClass");
});

//获取添加班级
router.route('/insertClass').all((req, res, next) => {
    postmysql(req, res, "insert_class");
});

//删除班级
router.route('/deleteClass').all((req, res, next) => {
    postmysql(req, res, "delete_class");
});

//删除班级
router.route('/deleteZone').all((req, res, next) => {
    postmysql(req, res, "delete_zone");
});

//查询所有学校
router.route('/getAllOrg').all((req, res, next) => {
    getmysql(req, res, "selectAll_org");
});

//查询课程
router.route('/getCourse').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_course", json['jid'], json['gid'], json['class'], json['cn'], json['page'], 10);
});



//根据课程id查询课程
router.route('/getCourseByid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_courseBycid", json['cid']);
});

//查询消息
router.route('/getNews').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_news", json['uid'], json['page'], 15);
});

//查询教师端消息
router.route('/getNewsTeacher').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_news", json['uid'], json['page'], 10);
});

//根据消息id查询消息
router.route('/getNewByid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_newsByid", json['nid']);
});
//获取家校区文章
router.route('/getHomeByid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_homecampusByid", json['id']);
});

//删除家校区文章
router.route('/deleteHome').all((req, res, next) => {
    postmysql(req, res, "delete_homecampus");
});

//查询课程详情
router.route('/getCourseDetail').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_courseDetail_teacher", json['cid']);
});


//根据家校区类型获取
router.route('/getHomeByPid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_homecampusByPid", json['pid'], json['page'], 10);
});

//获取家校区
router.route('/getHome').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_homecampus", json['page'], 10);
});


//提交作业
router.route('/submitWork').all((req, res, next) => {
    postmysql(req, res, "insert_works");
});

//查询这个老师底下的学生
router.route('/getStudent').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_student2", json['cuid'], json['cn'], json['class'], json['page'], 10);
});

//查询这个老师底下的学生提交的作业
router.route('/getWorks').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_works", json['bid'], json['isRate'], json['class'], json['name'], json['gid'], json['cid'], json['page'], 10);
});

//删除学生提交的作业
router.route('/deleteWork').all((req, res, next) => {
    postmysql(req, res, "delete_works");
});

router.route('/findWorkRepeat').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workRepeat", json['cid'], json['uid'], json['hid']);
});

//查看学生提交的作业
router.route('/getWork').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workByid", json['aid'], json['cid']);
});

//修改用户头像
router.route('/UpdateUserS').all((req, res, next) => {
    postmysql(req, res, "update_userS");
});

//修改用户名字
router.route('/UpdateUserName').all((req, res, next) => {
    postmysql(req, res, "update_userName");
});
//修改用户地区
router.route('/UpdateUserDest').all((req, res, next) => {
    postmysql(req, res, "update_userDest");
});

//修改用户家长姓名
router.route('/UpdateUserParent').all((req, res, next) => {
    postmysql(req, res, "update_userParent");
});
//修改用户家长姓名
router.route('/UpdateUserClassName').all((req, res, next) => {
    postmysql(req, res, "update_userClassName");
});
//查看前四个作业
router.route('/getWorkTopFour').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workTopFour", json['cid']);
});

//添加家校区文章
router.route('/addHome').all((req, res, next) => {
    if (!req.body[0].p) {
        req.body[0].p = ""
    }
    postmysql(req, res, "insert_homecampus");
});

//管理端查询家校区文章
router.route('/findHome').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_homecampus_admin", json['pid'], json['page'], 10);
});


//对学生提交的作业评分
router.route('/rate').all((req, res, next) => {
    postmysql(req, res, "rate_work");
});

//查看学生提交的作业
router.route('/getWorkByUid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workByUid", json['aid'], json['page'], 10);
});

//查出分组分类
router.route('/getGroup').all((req, res, next) => {
    getmysql(req, res, "select_group_teacher");
});

//查看此老师的所有权限
router.route('/getJuri').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_juri_teacher", json['cid']);
});

//根据作业id查看作业
router.route('/getWorkByWid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workByWid", json['aid']);
});
//根据作业id查看作业
router.route('/getWorkByWidT').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workByWid_teacher", json['aid']);
});
//添加课程
router.route('/addCourse').all((req, res, next) => {
    postmysql(req, res, "insert_course");
});

//查询老师底下的课程
router.route('/getCourseTeacher').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_course_teacher", json['uid'], json['gid'], json['page'], 10);
});

//查询是否有重复的账号
router.route('/findPhone').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_username", json['phone']);
});

//查询是否有重复的账号
router.route('/getUser').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_userByUid", json['uid']);
});
//查询所有课程专区
router.route('/getZone').all((req, res, next) => {
    getmysql(req, res, "select_zone");
});

//查询所有课程专区下的课程
router.route('/getZoneClass').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_zoneClass", json['bid'], json['page'], 10);
});

//查询所有课程专区下的课程
router.route('/getZoneCourse').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_zoneCourse", json['bid'], json['jid'], json['page'], 10);
});

//添加课程
router.route('/addZone').all((req, res, next) => {
    postmysql(req, res, "insert_classZone");
});

//课程专区搜索的课程 
router.route('/getSearchCourse').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "selectAll_courseSearch", json['t']);
});

//把课程添加进专区
router.route('/addZoneCourse').all((req, res, next) => {
    postmysql(req, res, "insert_app");
});

//删除课程专区底下的课程
router.route('/deleteZoneCourse').all((req, res, next) => {
    postmysql(req, res, "delete_app");
});

//添加banner
router.route('/addBanner').all((req, res, next) => {
    postmysql(req, res, "insert_banner");
});

//获取banner
router.route('/getBanner').all((req, res, next) => {
    getmysql(req, res, "select_banner");
});

//删除banner
router.route('/deleteBanner').all((req, res, next) => {
    postmysql(req, res, "delete_banner");
});

//查询发现
router.route('/getFind').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_find", json['page'], 7);
});

//查询在学课程数量和作品数量
router.route('/getNum').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workAndCourseNum", json['uid']);
});


//查询作品推荐
router.route('/getWrokI').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workRand", json['page'], 10);
});


//根据id查询发现文章
router.route('/getFindById').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_findById", json['id']);
});

//根据作业id查询此作业的学生评论
router.route('/findSComment').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_studentComment", json['pid']);
});

//根据id查询查询此作业的家长评论
router.route('/findPComment').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_parentComment", json['pid']);
});

//修改课程观看量
router.route('/addCourseCount').all((req, res, next) => {
    postmysql(req, res, "update_CourseVcount");
});

//把课程添加到我的课程
router.route('/addUserRate').all((req, res, next) => {
    postmysql(req, res, "insert_userRate");
});

//查看学生我的课程列表 select_userRate_student
router.route('/findMyClass').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_userRate_student", json['uid'], json['page'], 10);
});

//评论
router.route('/comment').all((req, res, next) => {
    postmysql(req, res, "insert_evaluate");
});

//修改老师
router.route('/UpdateTeacher').all((req, res, next) => {
    postmysql(req, res, "update_teacher");
});

//修改年级
router.route('/UpdateSClass').all((req, res, next) => {
    postmysql(req, res, "update_UserClass");
});

//提交修改年级申请
router.route('/UpdateSClassC').all((req, res, next) => {
    postmysql(req, res, "update_UserClassC");
});


//查询这个学校底下的老师
router.route('/findTeacher').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_teacher", json['oid']);
});

//查询这个老师底下的年级
router.route('/findClassByTeacher').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_classByCid", json['cid']);
});

//修改课程
router.route('/updateCourse').all((req, res, next) => {
    postmysql(req, res, "update_course");
});

//删除课程
router.route('/deleteCourse').all((req, res, next) => {
    postmysql(req, res, "delete_course");
});

//查询这个老师底下的作业的课程分类
router.route('/findWorkType').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workType2", json['uid']);
});
//查询这个老师底下的作业的课程分类
router.route('/findWorkType2').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_workCourseType", json['uid']);
});

//修改老师个人信息
router.route('/updateTeacherDetail').all((req, res, next) => {
    if (!req.body[0].mail) {
        req.body[0].mail = ""
    }
    if (!req.body[0].intro) {
        req.body[0].intro = ""
    }
    postmysql(req, res, "update_teacherDetail");
});

//查询这个老师的信息
router.route('/findteacherDeatail').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_teacherDetail", json['uid']);
});

//添加学生
router.route('/addStudent').all((req, res, next) => {
    // req.body[0].upassword = bcrypt.hashSync(req.body[0].upassword, bcrypt.genSaltSync(10));
    // postmysql(req, res, "add_student");
    var json = queryString(req.url);
    json['upassword'] = bcrypt.hashSync(json['upassword'], bcrypt.genSaltSync(10))
    getmysql(req, res, "add_student", json['uname'], json['upassword'], json['oid'], json['n'], json['cid'], json['tid']);
});

//查询这个手机号码有没有被注册
router.route('/findPhoneBoolean').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_usernameBoolean", json['un']);
});

//查询这个班级存不存在
router.route('/findClassBoolean').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_classBoolean", json['uid'], json['name'], json['bn']);
});

//删除这个学生
router.route('/deleteStudent').all((req, res, next) => {
    postmysql(req, res, "delete_user");
});


//后台添加分组
router.route('/addGroup').all((req, res, next) => {
    postmysql(req, res, "insert_group");
});

//后台添加年级
router.route('/addGroupGrade').all((req, res, next) => {
    postmysql(req, res, "insert_classGrade");
});

//后台查看分组下的年级
router.route('/getGroupGrade').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_classGrade", json['pid']);
});

//后台查看分组
router.route('/getGroupAdmin').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_group_admin", json['page'], 10);
});


//删除学校
router.route('/deleteSchool').all((req, res, next) => {
    postmysql(req, res, "delete_school");
});


//获取通知
router.route('/getNotice').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_notice_admin", json['page'], 10);
});
//删除通知
router.route('/deleteNotice').all((req, res, next) => {
    postmysql(req, res, "delete_notice");
});

//添加通知
router.route('/addNotice').all((req, res, next) => {
    postmysql(req, res, "insert_notice");
});

//获取通知文章
router.route('/getNoticeById').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_noticeByid", json['nid']);
});

//后台添加学校
router.route('/addSchool').all((req, res, next) => {
    postmysql(req, res, "insert_school");
});

//后台查看学校
router.route('/getSchoolAdmin').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_school_admin", json['page'], 10);
});

//后台添加发现文章
router.route('/addFind').all((req, res, next) => {
    if (!req.body[0].p) {
        req.body[0].p = ""
    }
    postmysql(req, res, "insert_find");
});


//后台删除发现文章
router.route('/deleteFind').all((req, res, next) => {
    postmysql(req, res, "delete_find");
});

//后台查看发现文章列表
router.route('/getFindAdmin').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_find_admin", json['page'], 10);
});

//后台查看所有课程
router.route('/getCourseAdmin').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_course_admin", json['cn'], json['page'], 10);
});

//后台查看专区下课程的数量
router.route('/getZoneClassNum').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_zoneClassNum", json['bid']);
});
//后台添加课程专区时查看课程是否在此专区重复
router.route('/getZoneClassRepeat').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_zoneClassRepeat", json['pid'], json['cid']);
});

//后台添加班级是否重复
router.route('/getClassRepeat').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_classRepeat", json['pid'], json['n']);
});
//后台专区上移下移
router.route('/zoneRank').all((req, res, next) => {
    postmysql(req, res, "update_classRank");
});

//修改分组名字
router.route('/updateClass').all((req, res, next) => {
    postmysql(req, res, "update_class");
});

//修改发现文章
router.route('/updateFind').all((req, res, next) => {
    if (!req.body[0].p) {
        req.body[0].p = ""
    }
    postmysql(req, res, "update_find");
});

//修改家校区文章
router.route('/updateHomecampus').all((req, res, next) => {
    if (!req.body[0].p) {
        req.body[0].p = ""
    }
    postmysql(req, res, "update_homecampus");
});

//获取所有用户
router.route('/getAllUser').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_user_admin2", json['cn'], json['cclass'], json['school'], json['class'], json['type'], json['page'], 10);
});

//获取所有作业
router.route('/getAllWorks').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_works_admin", json['cn'], json['page'], 10);
});

//后台删除作业
router.route('/deleteWorkAdmin').all((req, res, next) => {
    postmysql(req, res, "delect_work_admin");
});

//上移作业
router.route('/upWorks').all((req, res, next) => {
    postmysql(req, res, "up_works");
});

//下移作业
router.route('/downWorks').all((req, res, next) => {
    postmysql(req, res, "down_works");
});

//置顶作业
router.route('/firstWorks').all((req, res, next) => {
    postmysql(req, res, "first_works");
});

//修改用户角色
router.route('/updateUserType').all((req, res, next) => {
    postmysql(req, res, "update_userType");
});

//创建调查问卷
router.route('/addAsk').all((req, res, next) => {
    postmysql(req, res, "insert_ask");
});

//获取调查问卷
router.route('/getAsk').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_ask", json['uid'], json['page'], 10);
});

//获取调查问卷
router.route('/getAskApp').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_ask_app", json['page'], 15);
});


//根据id获取调查问卷
router.route('/getAskById').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_askById", json['id']);
});

//学生提交问卷
router.route('/addAskList').all((req, res, next) => {
    postmysql(req, res, "insert_askList");
});

//根据id获取调查问卷
router.route('/getAskList').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_askList", json['uid'], json['page'], 10);
});

//删除学生提交问卷
router.route('/deleteAskList').all((req, res, next) => {
    postmysql(req, res, "update_askList_delete");
});

//问卷调查数据统计
router.route('/getAskListByAskid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_askListByAskid", json['aid']);
});

//查看未读作业的数量
router.route('/getNcount').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_worksCount", json['bid']);
});

//查看作业改成已读
router.route('/updateWorkRead').all((req, res, next) => {
    postmysql(req, res, "update_workRead");
});

//查看消息的数量
router.route('/getNewsCount').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_newsCount", json['uid']);
});

//消息改成已读
router.route('/updateNewsRead').all((req, res, next) => {
    postmysql(req, res, "update_newsRead");
});

//查看学生评价得信息
router.route('/getStudentE').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_studentE2", json['cn'], json['page'], 10);
});
//查看学生评价得信息通过学生id
router.route('/getStudentEByUid').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "select_studentEByuid", json['uid']);
});


//导入用户
router.route('/addUser').all((req, res, next) => {
    // req.body[0].upassword = bcrypt.hashSync(req.body[0].upassword, bcrypt.genSaltSync(10));
    // postmysql(req, res, "add_user");
    var json = queryString(req.url);
    json['upassword'] = bcrypt.hashSync(json['upassword'], bcrypt.genSaltSync(10))
    getmysql(req, res, "add_user", json['uname'], json['upassword'], json['t'], json['oid'], json['n']);
});

//查找有无学校
router.route('/findSchool').all((req, res, next) => {
    var json = queryString(req.url);
    getmysql(req, res, "find_school", json['n']);
});

queryString = function (url) {
    var a = url.split("?");
    var json = querystring.parse(a[1])
    return json;
}

//post存儲過程處理
postmysql = function (req, res, functionname) {
    //mode的處理
    if (req.body[0]) {
        p = Object.values(req.body[0]);
        p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
        //執行存儲過程
        mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); });
    }
}

//get獲取數據庫信息
getmysql = function (req, res, functionname, ...p) {
    p = p || [];
    p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
    //執行存儲過程
    mysql.usselect(p, function (ret) {
        res.end(JSON.stringify(ret));
    });  //
}


module.exports = router;

