Search in sources :

Example 1 with AddNewStaffResponse

use of beans.response.AddNewStaffResponse in project MSEC by Tencent.

the class AddNewStaff method exec.

public AddNewStaffResponse exec(AddNewStaffRequest request) {
    Logger logger = Logger.getLogger("AddNewStaff");
    AddNewStaffResponse response = new AddNewStaffResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    //检查用户身份
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getStaff_name() == null || request.getStaff_name().equals("") || request.getStaff_phone() == null || request.getStaff_phone().equals("") || request.getPassword() == null || request.getPassword().equals("")) {
        response.setMessage("input should NOT be empty.");
        response.setStatus(100);
        return response;
    }
    //连接并插入数据库
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    try {
        String salt = AddNewStaff.newSalt();
        logger.info(String.format("p1:%s\nsalt:%s", request.getPassword(), salt));
        String saltedPwd = AddNewStaff.geneSaltedPwd(request.getPassword(), salt);
        if (saltedPwd.equals("")) {
            response.setMessage("geneSaltedPwd() failed.");
            response.setStatus(100);
            return response;
        }
        logger.info(String.format("p2:%s", saltedPwd));
        String sql = "insert into t_staff(staff_name, staff_phone, password, salt) values(?,?,?, ?)";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(request.getStaff_name());
        params.add(request.getStaff_phone());
        params.add(saltedPwd);
        params.add(salt);
        try {
            int addNum = util.updateByPreparedStatement(sql, params);
            if (addNum >= 0) {
                response.setMessage("success");
                response.setStatus(0);
                return response;
            }
        } catch (SQLException e) {
            response.setMessage("add record failed:" + e.toString());
            response.setStatus(100);
            e.printStackTrace();
            return response;
        }
        return response;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(msec.org.DBUtil) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) AddNewStaffResponse(beans.response.AddNewStaffResponse)

Aggregations

AddNewStaffResponse (beans.response.AddNewStaffResponse)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DBUtil (msec.org.DBUtil)1 Logger (org.apache.log4j.Logger)1