{"size": 6358, "ext": "js", "max_stars_repo_path": "server/models/DbOpt.js", "max_stars_repo_name": "kao2kao/wjsam", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/models/DbOpt.js", "max_issues_repo_name": "kao2kao/wjsam", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/models/DbOpt.js", "max_forks_repo_name": "kao2kao/wjsam", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.5673076923, "max_line_length": 92, "alphanum_fraction": 0.4834853728}
/**
 * Created by Administrator on 2015/09/28.
 */
var url = require('url');
//加密类
var crypto = require("crypto");
var mongoose = require('mongoose');
//站点配置
var settings = require("./common/settings");
var db = mongoose.connect(settings.DBURL, settings.DBOptions);

//信息删除操作
var DbOpt = {
    del: function (obj, req, res, logMsg) {
        var params = url.parse(req.url, true);
        obj.remove({_id: params.query.uid}, function (err, result) {
            if (err) {
                res.end(err);
            } else {
                console.log(logMsg + " success!");
                res.end("success");
            }
        })
    },
    findAll: function (obj, req, res, logMsg) {//查找指定对象所有记录
        obj.find({}, function (err, result) {
            if (err) {

            } else {
                console.log(logMsg + " success!");
                return res.json(result);
            }
        })
    },
    findOne: function (obj, req, res, logMsg, key) { //根据ID查找单条记录
        var params = url.parse(req.url, true);
        var currentId = (params.query.uid).split('.')[0];
        obj.findOne({_id: currentId}, function (err, result) {
            if (err) {

            } else {
                console.log(logMsg + " success!");
                return res.json(result);
            }
        })
    },
    updateOneByID: function (obj, req, res, logMsg) {
        var params = url.parse(req.url, true);
        var conditions = {_id: params.query.uid};
        req.body.updateDate = new Date();
        var update = {$set: req.body};
        obj.update(conditions, update, function (err, result) {
            if (err) {

            } else {
                console.log(logMsg + " success!");
                res.end("success");
            }
        })
    },
    addOne: function (obj, req, res, logMsg) {
        var newObj = new obj(req.body);
        newObj.save();
        console.log(logMsg + " success!");
        res.end("success");
    },

    pagination: function (obj, req, res, conditions, callback) {

        var params = url.parse(req.url, true);
        var startNum = (params.query.currentPage - 1) * params.query.limit + 1;
        var currentPage = Number(params.query.currentPage);
        var limit = Number(params.query.limit);
        var pageInfo;

        //根据条件查询记录(如果有条件传递，则按条件查询)
        var query;
        if (conditions && conditions.length > 1) {
            query = obj.find().or(conditions);
        }
        else if (conditions) {
            query = obj.find(conditions);
        }
        else {
            query = obj.find({});
        }

        query.sort({'date': -1});
        query.exec(function (err, docs) {
            if (err) {
                console.log(err)

            } else {
                pageInfo = {
                    "totalItems": docs.length,
                    "currentPage": currentPage,
                    "limit": limit,
                    "startNum": Number(startNum)
                };

                return callback + "(" + res.json({
                        docs: docs.slice(startNum - 1, startNum + limit - 1),
                        pageInfo: pageInfo
                    }) + ")";
            }
        })
    },


    getPaginationResult: function (obj, req, res, q, filed) {// 通用查询，带分页，注意参数传递格式,filed为指定字段
        var searchKey = req.query.searchKey;
        var page = parseInt(req.query.currentPage);
        var limit = parseInt(req.query.limit);
        if (!page) page = 1;
        if (!limit) limit = 20;
        var order = req.query.order;
        var sq = {}, Str, A = 'problemID', B = 'asc';
        if (order) {    //是否有排序请求
            Str = order.split('_');
            A = Str[0];
            B = Str[1];
            sq[A] = B;    //关联数组增加查询条件，更加灵活，因为A是变量
        } else {
            sq.gmtCreated = -1;    //默认排序查询条件：创建时间倒序
        }

        var startNum = (page - 1) * limit;
        var resultList;
        var resultNum;
        if (q && q.length > 1) { // 多条件只要其中一条符合
            resultList = obj.find().or(q, filed).sort(sq).skip(startNum).limit(limit);
            resultNum = obj.find().or(q, filed).count();
        } else {
            resultList = obj.find(q).sort(sq).skip(startNum).limit(limit);
            resultNum = obj.find(q).count();
        }
        //        分页参数
        var pageInfo = {
            "totalItems": resultNum,
            "currentPage": page,
            "limit": limit,
            "startNum": startNum + 1,
            "searchKey": searchKey
        };
        var datasInfo = {
            docs: resultList,
            pageInfo: pageInfo
        };

        return datasInfo;
    },

    getDatasByParam: function (obj, req, res, q) {
    // 通用查询list不带分页，注意参数传递格式,通过express-promise去掉了回调方式返回数据
    // 默认查询所有记录，有条件顺带排序和查询部分记录
        var order = req.query.order;
        var limit = parseInt(req.query.limit);
        var sq = {}, Str, A = 'problemID', B = 'asc';
        if (order) {    //是否有排序请求
            Str = order.split('_');
            A = Str[0];
            B = Str[1];
            sq[A] = B;    //关联数组增加查询条件，更加灵活，因为A是变量
        } else {
            sq.gmt_created = -1;    //默认排序查询条件
        }
        if (!limit) {
            return obj.find(q).sort(sq);
        } else {
            return obj.find(q).sort(sq).skip(0).limit(limit);
        }
    },

    getKeyArrByTokenId: function (tokenId) {
        var newLink = DbOpt.decrypt(tokenId, settings.encrypt_key);
        var keyArr = newLink.split('$');
        return keyArr;
    },

    getCount: function (obj, req, res, conditions) { // 查询指定对象的数量
        obj.count(conditions, function (err, count) {
            if (err) {
                console.log(err);
            } else {
                return res.json({
                    count: count
                });
            }

        });
    },
    encrypt: function (data, key) { // 密码加密
        var cipher = crypto.createCipher("bf", key);
        var newPsd = "";
        newPsd += cipher.update(data, "utf8", "hex");
        newPsd += cipher.final("hex");
        return newPsd;
    },
    decrypt: function (data, key) { //密码解密
        var decipher = crypto.createDecipher("bf", key);
        var oldPsd = "";
        oldPsd += decipher.update(data, "hex", "utf8");
        oldPsd += decipher.final("utf8");
        return oldPsd;
    }
};

module.exports = DbOpt;