/**
 * 用户管理控制器
 *
 * @author CaiAoLin
 * @date 2017/6/7
 * @version
 */
import BaseController from "../../common/base/base_controller";
import UserModel from "../models/user_model";
import Config from "../../../config/config";
import CompilationModel from "../models/compilation_model";
let config = require("../../../config/config.js");

class UserController extends BaseController {

    /**
     * 最近注册列表(最近登录前台用户列表)
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async normalUsers(request, response) {//取普通用户
        let userModel = new UserModel();
        let total = 0;
        let pageData = {};
        let userList = [];
        let compilationList =[];
        let filter = request.query;
        try {
            //获取编办列表
            let  compilationModel = new CompilationModel();
            compilationList = await compilationModel.getCompilationList();



            let condition = userModel.getFilterCondition(request);

            //获取注册时间
            let regtime = request.query.regtime;
            if(regtime !== '' && regtime !== undefined){
                filter.regtimeMsg = userModel.getDayMsg(regtime);
            }

            // 获取用户总数
            total = await userModel.count(condition);

            // 分页数据
            let page = request.query.page === undefined ? 1 : request.query.page;
            pageData = {
                current: page,
                total: Math.ceil(total / Config.pageSize),
                queryData: response.locals.urlQuery
            };

            // 获取用户列表
            userList = await userModel.getList(condition, page, Config.pageSize);
        } catch (error) {
            console.log(error);
        }

        // 渲染数据
        let renderData = {
            compilationList:compilationList,
            userList: userList,
            pages: pageData,
            total: total,
            filter: filter,
            model: userModel,
            layout: 'users/views/layout/layout',
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
        };
        response.render('users/views/user/index', renderData);
    }


    /**
     * 获取搜索用户json
     *
     * @param {object} request
     * @param {object} response
     * @return {string}
     */
    async search(request, response) {
        let userModel = new UserModel();
        let responseData = {
            error: 0,
            msg: '',
            data: null
        };
        try{
            let keyword = request.query.keyword;
            if(keyword === undefined || keyword === '') {
                throw { code: 1, err: '参数有误或为空'};
            }
            let condition = userModel.getFilterCondition(request);
            let userList = await userModel.getList(condition);
            responseData.data = userList;
        } catch (error) {
            console.log(error);
            responseData.error = error.code;
            responseData.msg = error.err;
        }

        response.json(responseData);
    }

    /**
     * 根据用户id列表获取用户信息列表 json
     *
     * @param {object} request
     * @param {object} response
     * @return {string}
     */
    async getUserList(request, response) {
        let params = JSON.parse(request.body.params),
            userIds = params.userIds;
        let userModel = new UserModel();
        let responseData = {
            error: 0,
            msg: '',
            data: null
        };
        try{
            if(userIds === undefined || userIds === '') {
                throw { code: 1, err: '参数有误或为空'};
            }
            let condition = {
                _id: {$in:userIds}
            };
            let userList = await userModel.getList(condition);
            responseData.data = userList;
        } catch (error) {
            console.log(error);
            responseData.error = error.code;
            responseData.msg = error.err;
        }

        response.json(responseData);
    }
}

export default UserController;
