All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.haoxuer.discover.user.rest.resource.UserInfoResource Maven / Gradle / Ivy

There is a newer version: 3.3.18-20230117
Show newest version
package com.haoxuer.discover.user.rest.resource;

import com.haoxuer.discover.rest.base.ResponseObject;
import com.haoxuer.discover.user.api.apis.UserInfoApi;
import com.haoxuer.discover.user.api.domain.list.UserInfoList;
import com.haoxuer.discover.user.api.domain.page.UserInfoPage;
import com.haoxuer.discover.user.api.domain.request.*;
import com.haoxuer.discover.user.api.domain.response.UserInfoResponse;
import com.haoxuer.discover.user.data.dao.UserInfoDao;
import com.haoxuer.discover.user.data.entity.UserInfo;
import com.haoxuer.discover.user.data.enums.SecurityType;
import com.haoxuer.discover.user.data.request.ResetPasswordRequest;
import com.haoxuer.discover.user.data.request.UpdatePasswordRequest;
import com.haoxuer.discover.user.rest.convert.UserInfoResponseConvert;
import com.haoxuer.discover.user.rest.convert.UserInfoSimpleConvert;
import com.haoxuer.discover.config.utils.ConverResourceUtils;
import com.haoxuer.discover.data.page.Filter;
import com.haoxuer.discover.data.page.Order;
import com.haoxuer.discover.data.page.Page;
import com.haoxuer.discover.data.page.Pageable;
import com.haoxuer.discover.data.utils.FilterUtils;
import jodd.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.haoxuer.discover.user.rest.conver.PageableConver;
import com.haoxuer.discover.data.utils.BeanDataUtils;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Component
public class UserInfoResource implements UserInfoApi {

    @Autowired
    private UserInfoDao dataDao;


    @Override
    public UserInfoResponse create(UserInfoDataRequest request) {
        UserInfoResponse result = new UserInfoResponse();

        UserInfo bean = new UserInfo();
        handleData(request, bean);
        dataDao.save(bean);
        result = new UserInfoResponseConvert().conver(bean);
        return result;
    }

    @Override
    public UserInfoResponse update(UserInfoDataRequest request) {
        UserInfoResponse result = new UserInfoResponse();
        if (request.getId() == null) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        UserInfo bean = dataDao.findById(request.getId());
        if (bean == null) {
            result.setCode(502);
            result.setMsg("无效id");
            return result;
        }
        handleData(request, bean);
        result = new UserInfoResponseConvert().conver(bean);
        return result;
    }

    private void handleData(UserInfoDataRequest request, UserInfo bean) {
        BeanDataUtils.copyProperties(request, bean);

    }

    @Override
    public UserInfoResponse delete(UserInfoDataRequest request) {
        UserInfoResponse result = new UserInfoResponse();
        if (request.getId() == null) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        dataDao.deleteById(request.getId());
        return result;
    }

    @Override
    public UserInfoResponse view(UserInfoDataRequest request) {
        UserInfoResponse result = new UserInfoResponse();
        UserInfo bean = dataDao.findById(request.getId());
        if (bean == null) {
            result.setCode(1000);
            result.setMsg("无效id");
            return result;
        }
        result = new UserInfoResponseConvert().conver(bean);
        return result;
    }

    @Override
    public UserInfoList list(UserInfoSearchRequest request) {
        UserInfoList result = new UserInfoList();

        List filters = new ArrayList<>();
        filters.addAll(FilterUtils.getFilters(request));
        List orders = new ArrayList<>();
        if ("asc".equals(request.getSortMethod())) {
            orders.add(Order.asc("" + request.getSortField()));
        } else if ("desc".equals(request.getSortMethod())) {
            orders.add(Order.desc("" + request.getSortField()));
        } else {
            orders.add(Order.desc("id"));
        }
        List organizations = dataDao.list(0, request.getSize(), filters, orders);
        UserInfoSimpleConvert convert = new UserInfoSimpleConvert();
        ConverResourceUtils.converList(result, organizations, convert);
        return result;
    }

    @Override
    public UserInfoResponse login(UserInfoDataRequest request) {
        UserInfoResponse result = new UserInfoResponse();
        UsernamePasswordToken token = new UsernamePasswordToken(request.getUsername(), request.getPassword());
        try {
            SecurityUtils.getSubject().login(token);
            result.setSessionId("" + SecurityUtils.getSubject().getSession().getId());
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            result.setCode(501);
            result.setMsg("该账号不存在");
        } catch (LockedAccountException e) {
            e.printStackTrace();
            result.setCode(502);
            result.setMsg("该账号被禁用");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            result.setCode(503);
            result.setMsg("密码错误");
        } catch (Exception e) {
            result.setCode(501);
            result.setMsg("账号或者密码错误");
        }

        return result;
    }

    @Override
    public UserInfoPage search(UserInfoSearchRequest request) {
        UserInfoPage result = new UserInfoPage();
        Pageable pageable = new PageableConver().conver(request);
        pageable.getFilters().addAll(FilterUtils.getFilters(request));
        if ("asc".equals(request.getSortMethod())) {
            pageable.getOrders().add(Order.asc("" + request.getSortField()));
        } else if ("desc".equals(request.getSortMethod())) {
            pageable.getOrders().add(Order.desc("" + request.getSortField()));
        } else {
            pageable.getOrders().add(Order.desc("id"));
        }
        Page page = dataDao.page(pageable);
        UserInfoSimpleConvert convert = new UserInfoSimpleConvert();
        ConverResourceUtils.converPage(result, page, convert);
        return result;
    }

    @Override
    public UserInfoPage changePassword(UpdatePasswordRequest request) {
        UserInfoPage result = new UserInfoPage();
        if (request.getId() == null) {
            result.setCode(501);
            result.setMsg("用户id为空");
            return result;
        }
        UserInfo info = dataDao.findById(request.getId());
        if (info == null) {
            result.setCode(502);
            result.setMsg("用户为空");
            return result;
        }
        ResponseObject res = dataDao.updatePassword(request);
        if (res.getCode() != 0) {
            result.setCode(res.getCode());
            result.setMsg(res.getMsg());
            return result;
        }
        return result;
    }

    @Override
    public UserInfoPage restPassword(ResetPasswordRequest request) {
        UserInfoPage result = new UserInfoPage();
        if (request.getId() == null) {
            result.setCode(501);
            result.setMsg("用户id为空");
            return result;
        }
        UserInfo info = dataDao.findById(request.getId());
        if (info == null) {
            result.setCode(502);
            result.setMsg("用户为空");
            return result;
        }
        ResponseObject res = dataDao.resetPassword(request);
        if (res.getCode() != 0) {
            result.setCode(res.getCode());
            result.setMsg(res.getMsg());
            return result;
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy