com.base.service.AdminService.java Source code

Java tutorial

Introduction

Here is the source code for com.base.service.AdminService.java

Source

/**
 * $Id: CodeService.java May 30, 2015 2:23:26 PM hdp
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.base.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.sql.Update;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.base.dao.AdminDao;
import com.base.entity.Admin;
import com.base.mapper.AdminMapper;
import com.base.util.CreateResJosnUtil;
import com.base.util.security.MD5;
import com.core.dao.impl.AbstractBaseDaoImpl;
import com.core.dto.Order;
import com.core.dto.QueryResult;

/**
 * ?service
 * <p>
 * : May 30, 2015 2:23:26 PM
 * </p>
 * 
 * @author <a href="mailto:hongdanping@163.com">hdp</a>
 * @version V1.0
 * @since V1.0
 */
@Service
public class AdminService extends AbstractBaseService<Admin> {

    @Resource
    private AdminDao adminDao;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public AbstractBaseDaoImpl<Admin> getBaseDao() {
        return this.adminDao;
    }

    public boolean login(String account, String password) {
        List<Admin> list = this.adminDao.list("o.account=? ", account);
        if (list.size() == 0) {
            return false;
        }
        try {
            Admin admin = list.get(0);
            if (StringUtils.isEmpty(password) || !MD5.getMD5(password).equals(admin.getPassword())) {
                return false;
            }
        } catch (Exception e) {
            // TODO: handle exception
            return false;
        }
        return true;
    }

    public String login(String account, String password, String pageCode, String code, HttpSession session)
            throws Exception {
        JSONObject json = new JSONObject();
        try {
            if (!code.equals(pageCode.toUpperCase())) {
                CreateResJosnUtil.createjson(json, "err", "??");
                return json.toString();
            }
            List<Admin> list = this.adminDao.list("o.account=? ", account);
            if (list.size() == 0) {
                CreateResJosnUtil.createjson(json, "err", "???");
                return json.toString();
            }

            Admin admin = list.get(0);
            if (StringUtils.isEmpty(password) || !MD5.getMD5(password).equals(admin.getPassword())) {
                CreateResJosnUtil.createjson(json, "err", "???");
                return json.toString();
            }
            session.setAttribute("adminUser", admin);
            session.removeAttribute("code");
            CreateResJosnUtil.createjson(json, "success", "");
            return json.toString();
        } catch (Exception e) {
            throw e;
        }
    }

    public List<Admin> getList(HttpServletRequest request) {
        Order order = new Order();
        order.put("createTime", Order.DESC);
        QueryResult<Admin> res = this.adminDao.query(request, order, null);
        request.setAttribute("count", res.getCount());
        return res.getResults();
    }

    public List findByaccount(String account) {
        // TODO ?
        return this.adminDao.list("account=?", account);
    }

    public Admin getAdmin(String account) {
        Admin admin = adminDao.find("  account=? and isDeleted=false  ", account);
        return admin;
    }

    public void updateAdmin(String roleId) {
        String where = " and role.id='" + roleId + "' ";
        List<Admin> list = this.adminDao.queryByHql(where);
        if (list != null) {
            for (Admin admin : list) {
                admin.setRole(null);
                update(admin);
            }
        }

    }

    public Admin getAdminById(String id) {
        return adminMapper.getAdminById(id);
    }

}