com.flying.services.CarOwnerUserServices.java Source code

Java tutorial

Introduction

Here is the source code for com.flying.services.CarOwnerUserServices.java

Source

package com.flying.services;

import com.flying.CustomException.CarOwnerUserException;
import com.flying.dao.CarOwnerUserDao;
import com.flying.dao.OperationUserDao;
import com.flying.model.CarInfo;
import com.flying.model.CarOwnerUser;
import com.flying.shiro.UserHelper;
import com.flying.shiro.token.CarOwnerUserLoginToken;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by FlyingXu on 2016/4/11 0011.
 */
@Service("CarOwnerUserServices")
public class CarOwnerUserServices {
    @Autowired
    @Qualifier("CarOwnerUserDao")
    private CarOwnerUserDao userDao;

    @Autowired
    @Qualifier("OperationUserDao")
    private OperationUserDao operationUserDao;

    /**
     * user shiro
     * ?ID??????????
     *
     * @param user ?
     * @return ???true,??false
     */
    public boolean addUser(CarOwnerUser user) {
        if (user.getId() != null || StringUtils.isBlank(user.getPassword())) {
            return false;//
        }
        if (StringUtils.isBlank(user.getUsurllyAddress())) {
            return false;//
        }
        if (StringUtils.isBlank(user.getTrueName())) {
            return false;//
        }
        if (this.userDao.userNameIsExists(user.getUserName())) {
            //????
            return false;
        }
        if (this.operationUserDao.userNameIsExists(user.getUserName())) {
            //????
            return false;
        }
        if (user.getRoleId() == null || (user.getRoleId().intValue() != 3 && user.getRoleId().intValue() != 4)) {
            return false;
        }

        user.setCarInfoIsChecked(0);
        user.setUserIsChecked(0);
        user.setLocked(0);//?
        user.setOrderNum(0);
        user.setCreateDate(new Date());

        String salt = UserHelper.getSalt();
        user.setSalt(salt);
        user.setPassword(UserHelper.encryptPassword(user.getPassword(), user.getSalt()));
        this.userDao.add(user);//??User
        return true;
    }

    /**
     * ?session?
     *
     * @param session
     * @return
     */
    public CarOwnerUser getUserFromSession(HttpSession session) {
        String userName = (String) session.getAttribute("carOwnerUser");
        if (userName == null) {
            //?springmvc????
            throw (new CarOwnerUserException("carOwnerUserNotLogin"));
        }
        CarOwnerUser user = this.getUserByUserName(userName);
        return user;
    }

    /**
     * ????
     *
     * @param userName
     * @return
     */
    public CarOwnerUser getUserByUserName(String userName) {
        return this.userDao.findUserByUserName(userName);
    }

    /**
     * ??
     *
     * @param session
     * @param newPassword
     * @return
     */
    public boolean resetMyPassword(HttpSession session, String userName, String newPassword) {
        CarOwnerUser user = this.getUserByUserName(userName);
        this.userDao.resetMyPassword(user, newPassword);
        this.logout(session);
        return true;
    }

    /**
     * ??
     */
    public void reSetUserInfo(Integer id, CarOwnerUser user) {
        CarOwnerUser user_ = this.userDao.find(CarOwnerUser.class, id);
        if (user_ == null) {
            return;
        }
        user_.setQq(user.getQq());
        user_.setWeChat(user.getWeChat());
        user_.setCarCard(user.getCarCard());
        user_.setCarType(user.getCarType());
        user_.setTel(user.getTel());
        //user_.setCarInfo(user.getCarInfo());//?
        user_.setUsurllyAddress(user.getUsurllyAddress());//
        user_.setCompanyName(user.getCompanyName());//????
        user_.setCompanyAddress(user.getCompanyAddress());//??
        this.userDao.update(user_);
    }

    /**
     * ?????
     */
    public void resetUserCheckInfo(Integer id, CarOwnerUser user) {
        CarOwnerUser user_ = this.userDao.find(CarOwnerUser.class, id);
        if (user_ == null) {
            return;
        }
        user_.setIdCardNo(user.getIdCardNo());
        user_.setIdCardImagePath(user.getIdCardImagePath());
        user_.setCarLicenseImagePath(user.getCarLicenseImagePath());//?
        user_.setDrivingLicenseImagePath(user.getDrivingLicenseImagePath());//?
        user_.setScheduleLicenceImagePath(user.getScheduleLicenceImagePath());//?
        user_.setBusinessLicenceImagePath(user.getBusinessLicenceImagePath());//?

        user_.setCarInfoIsChecked(0);//?
        this.userDao.update(user_);
    }

    /**
     * ?
     *
     * @param uid
     */
    public void setUserInfoChecked(Integer uid, Integer isChecked) {
        CarOwnerUser user = this.userDao.find(CarOwnerUser.class, uid);
        user.setUserIsChecked(isChecked);
        this.userDao.update(user);
    }

    public void setUserCarInfoChecked(Integer uid, Integer isChecked) {
        CarOwnerUser user = this.userDao.find(CarOwnerUser.class, uid);
        user.setCarInfoIsChecked(isChecked);
        this.userDao.update(user);
    }

    /**
     * ?
     *
     * @param uid
     */
    public void lockedUser(Integer uid) {
        CarOwnerUser user = this.userDao.find(CarOwnerUser.class, uid);
        user.setLocked(1);
        this.userDao.update(user);
    }

    /**
     * ?
     *
     * @param uid
     */
    public void unLockedUser(Integer uid) {
        CarOwnerUser user = this.userDao.find(CarOwnerUser.class, uid);
        user.setLocked(0);
        this.userDao.update(user);
    }

    /**
     * ??
     *
     * @param userId
     */
    public void addOrderNum(Integer userId) {
        CarOwnerUser user = this.userDao.find(CarOwnerUser.class, userId);
        if (user.getOrderNum() == 0) {
            user.setOrderNum(0);
        }
        user.setOrderNum(user.getOrderNum() + 1);
        this.userDao.update(user);
    }

    /**
     * 
     */
    public void logout(HttpSession session) {
        session.removeAttribute("carOwnerUser");
        //shiro?
        try {
            SecurityUtils.getSubject().logout();//
        } catch (Exception e) {

        }
    }

    //?

    /**
     * ??true??false
     *
     * @param openid
     * @return
     */
    public boolean isBindWX(String openid) {
        List<CarOwnerUser> userList = this.userDao.query(CarOwnerUser.class, "o.openid = ?",
                new Object[] { openid });
        if (userList.size() == 0) {
            return false;
        }
        return true;
    }

    /**
     * openid
     *
     * @param userName
     * @param openid
     */
    public void bindOpenid(String userName, String openid) {
        CarOwnerUser user = this.getUserByUserName(userName);
        if (StringUtils.isBlank(user.getOpenid())) {
            user.setOpenid(openid);
            this.userDao.update(user);
        }
    }

    /**
     * ?openid?User
     *
     * @param openid
     * @return
     */
    public CarOwnerUser getUserByOpneid(String openid) {
        if (StringUtils.isBlank(openid)) {
            return null;
        }
        List<CarOwnerUser> userList = this.userDao.query(CarOwnerUser.class, "o.openid = ?",
                new Object[] { openid });
        if (userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    public List<CarOwnerUser> getCarOwnerUserShowIndex() {
        Map<Integer, Integer> userIdMap = new HashMap<Integer, Integer>();
        for (CarInfo ci : this.userDao.<CarInfo>query(CarInfo.class)) {
            Integer num = userIdMap.get(ci.getUserId());
            if (num != null) {
                userIdMap.put(ci.getUserId(), num + 1);
            } else {
                userIdMap.put(ci.getUserId(), 1);
            }
        }
        List<CarOwnerUser> carOwnerUserList = new ArrayList<CarOwnerUser>(userIdMap.size());
        for (Map.Entry<Integer, Integer> e : userIdMap.entrySet()) {
            CarOwnerUser user = this.userDao.find(CarOwnerUser.class, e.getKey());
            if (user != null) {
                user.setCarNum(e.getValue());
                carOwnerUserList.add(user);
            }
        }
        Collections.sort(carOwnerUserList, new Comparator<CarOwnerUser>() {
            @Override
            public int compare(CarOwnerUser o1, CarOwnerUser o2) {
                return o2.getCarNum() - o1.getCarNum();//??
            }
        });
        int length = carOwnerUserList.size();
        if (length > 30) {
            length = 30;
        }
        return carOwnerUserList.subList(0, length);
    }

    public void resetCommon(Integer id, String commont) {
        CarOwnerUser carOwnerUser = this.userDao.find(CarOwnerUser.class, id);
        carOwnerUser.setCommon(commont);
        this.userDao.update(carOwnerUser);
    }
}