/**
|
# __----~~~~~~~~~~~------___
|
# . . ~~//====...... __--~ ~~
|
# -. \_|// |||\\ ~~~~~~::::... /~
|
# ___-==_ _-~o~ \/ ||| \\ _/~~-
|
# __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
|
# _-~~ .=~ | \\-_ '-~7 /- / || \ /
|
# .~ .~ | \\ -_ / /- / || \ /
|
# / ____ / | \\ ~-_/ /|- _/ .|| \ /
|
# |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
|
# ' ~-| /| |-~\~~ __--~~
|
# |-~~-_/ | | ~\_ _-~ /\
|
# / \ \__ \/~ \__
|
# _--~ _/ | .-~~____--~-/ ~~==.
|
# ((->/~ '.|||' -_| ~~-/ , . _||
|
# -_ ~\ ~~---l__i__i__i--~~_/
|
# _-~-__ ~) \--______________--~~
|
# //.-~~~-~_--~- |-------~~~~~~~~
|
# //.-~~~--\
|
# 神兽保佑
|
# 永无BUG!
|
*/
|
package com.lunhan.water.service;
|
|
import com.lunhan.water.common.*;
|
import com.lunhan.water.common.enums.*;
|
import com.lunhan.water.common.jwt.JWTUtil;
|
import com.lunhan.water.common.jwt.LoginUserDTO;
|
import com.lunhan.water.common.model.Tuple;
|
import com.lunhan.water.common.security.MD5Util;
|
import com.lunhan.water.common.util.*;
|
import com.lunhan.water.entity.enums.EState;
|
import com.lunhan.water.entity.enums.EUserType;
|
import com.lunhan.water.entity.request.ReqChangePassword;
|
import com.lunhan.water.entity.request.ReqUserLogin;
|
import com.lunhan.water.repository.impl.PaymentRecordsMapperImpl;
|
import com.lunhan.water.repository.impl.RechargeRecordsMapperImpl;
|
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.ibatis.annotations.Select;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
import com.lunhan.water.repository.impl.UserLoginMapperImpl;
|
import com.lunhan.water.repository.po.UserLoginPO;
|
import com.lunhan.water.entity.request.userlogin.ReqCreateUserLogin;
|
import com.lunhan.water.entity.request.userlogin.ReqModifyUserLogin;
|
import com.lunhan.water.entity.search.SearchUserLogin;
|
import com.lunhan.water.repository.vo.UserLoginVO;
|
import com.lunhan.water.service.convert.UserLoginConvert;
|
|
/**
|
* UserLogin
|
* @author lin.liu
|
*/
|
@Service
|
public class UserLoginService extends BaseService {
|
@Autowired
|
private UserLoginMapperImpl mapper;
|
@Autowired
|
private RechargeRecordsMapperImpl rechargeRecordsMapper;
|
@Autowired
|
private PaymentRecordsMapperImpl paymentRecordsMapper;
|
|
public ExecutedResult<Long> create(ReqCreateUserLogin request) {
|
// 转换po
|
UserLoginPO item = UserLoginConvert.INSTANCE.toCreate(request);
|
// 设置状态
|
//item.setStatus(EState.NORMAL.getValue());
|
// 设置记录创建时间
|
item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
|
// 是否删除(逻辑删除)初始值
|
item.setIsDelete(EYesOrNo.NO.getValue());
|
|
int rowCount = mapper.insert(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("创建[null]失败。");
|
}
|
return ExecutedResult.success(item.getId());
|
}
|
|
public ExecutedResult<String> modify(ReqModifyUserLogin request) {
|
// 验证记录是否存在
|
ExecutedResult<UserLoginPO> checkExists = this.check4Id(request.getId());
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
//UserLoginPO data = checkExists.getData();
|
// 转换po
|
UserLoginPO item =new UserLoginPO();
|
item.setId( checkExists.getData().getId());
|
item.setNickName(request.getNickName());
|
item.setHeadImg(request.getHeadImg());
|
int rowCount = mapper.updateById(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("编辑用户信息失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<UserLoginVO> get(Long id) {
|
UserLoginVO result = new UserLoginVO();
|
|
UserLoginPO find = mapper.get(id);
|
if (null != find) {
|
// 转换vo
|
result = UserLoginConvert.INSTANCE.toVo(find);
|
}
|
return ExecutedResult.success(result);
|
}
|
public ExecutedResult<UserLoginVO> getUser(LoginUserDTO loginUser){
|
UserLoginPO userLoginPO = mapper.get4Openid(loginUser.getUserId());
|
if(Objects.isNull(userLoginPO)){
|
return ExecutedResult.failed("未获取到用户信息!");
|
}
|
UserLoginVO userLoginVO = CopierUtil.mapTo(userLoginPO, UserLoginVO.class);
|
//查询总够水量
|
BigDecimal sumBuyCount = rechargeRecordsMapper.getSumBuyCount(userLoginPO.getId());
|
//查询总用水量
|
BigDecimal sumUseCount = paymentRecordsMapper.getSumUseCount(userLoginPO.getId());
|
userLoginVO.setSumBuyCount(sumBuyCount);
|
userLoginVO.setSumUseCount(sumUseCount);
|
|
|
return ExecutedResult.success(userLoginVO);
|
}
|
// public ExecutedResult<String> stop(Long id) {
|
// // 验证记录是否存在
|
// ExecutedResult<UserLoginPO> checkExists = this.check4Id(id);
|
// if (checkExists.isFailed()) {
|
// return ExecutedResult.failed(checkExists.getMsg());
|
// }
|
// UserLoginPO item = new UserLoginPO();
|
// item.setId(id);
|
// item.setStatus(EState.DISABLED.getValue());
|
//
|
// int rowCount = mapper.updateById(item);
|
// if (rowCount != 1) {
|
// return ExecutedResult.failed("停用[null]失败。");
|
// }
|
// return ExecutedResult.success();
|
// }
|
//
|
// public ExecutedResult<String> enable(Long id) {
|
// // 验证记录是否存在
|
// ExecutedResult<UserLoginPO> checkExists = this.check4Id(id);
|
// if (checkExists.isFailed()) {
|
// return ExecutedResult.failed(checkExists.getMsg());
|
// }
|
// UserLoginPO item = new UserLoginPO();
|
// item.setId(id);
|
// item.setStatus(EState.NORMAL.getValue());
|
//
|
// int rowCount = mapper.updateById(item);
|
// if (rowCount != 1) {
|
// return ExecutedResult.failed("启用[null]失败。");
|
// }
|
// return ExecutedResult.success();
|
// }
|
//
|
// public ExecutedResult<String> setSort(ReqSetSort request) {
|
// // 验证记录是否存在
|
// ExecutedResult<UserLoginPO> checkExists = this.check4Id(request.getId());
|
// if (checkExists.isFailed()) {
|
// return ExecutedResult.failed(checkExists.getMsg());
|
// }
|
// UserLoginPO item = new UserLoginPO();
|
// item.setId(request.getId());
|
// item.setSort(request.getSort());
|
//
|
// int rowCount = mapper.updateById(item);
|
// if (rowCount != 1) {
|
// return ExecutedResult.failed("设置[null]排序值失败。");
|
// }
|
// return ExecutedResult.success();
|
// }
|
//
|
// public ExecutedResult<String> listSetSort(ReqListSetSort request) {
|
// // id列表
|
// List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList());
|
// // 验证记录是否存在
|
// ExecutedResult<List<UserLoginPO>> checkExists = this.check4Id(listId);
|
// if (checkExists.isFailed()) {
|
// return ExecutedResult.failed(checkExists.getMsg());
|
// }
|
//
|
// List<UserLoginPO> listUpdate = request.getList().stream()
|
// .map(c -> {
|
// UserLoginPO item = new UserLoginPO();
|
// item.setId(c.getId());
|
// item.setSort(c.getSort());
|
// return item;
|
// })
|
// .collect(Collectors.toList());
|
// Boolean result = mapper.modifyList(listUpdate);
|
// if (result) {
|
// return ExecutedResult.success();
|
// }
|
// return ExecutedResult.failed("[null]设置排序值失败");
|
// }
|
//
|
// public ExecutedResult<String> remove(Long id) {
|
// Boolean result = mapper.deleteLogic(id);
|
// if (BooleanUtils.isFalse(result)) {
|
// return ExecutedResult.failed("删除[null]失败。");
|
// }
|
// return ExecutedResult.success();
|
// }
|
//
|
// public ExecutedResult<String> removeList(List<Long> ids) {
|
// Boolean result = mapper.deleteLogic(ids);
|
// if (BooleanUtils.isFalse(result)) {
|
// return ExecutedResult.failed("删除[null]失败。");
|
// }
|
// return ExecutedResult.success();
|
// }
|
|
public ExecutedResult<List<UserLoginVO>> getList(List<Long> listId) {
|
List<UserLoginVO> result = new ArrayList<>();
|
|
List<UserLoginPO> list = mapper.getList(listId);
|
if (ListUtil.isNotNullOrEmpty(list)) {
|
// 转换vo
|
result = UserLoginConvert.INSTANCE.toVo(list);
|
}
|
return ExecutedResult.success(result);
|
}
|
|
public ExecutedResult<PagerResult<UserLoginVO>> search(SearchUserLogin search) {
|
// 处理创建时间范围-查询参数
|
Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());
|
if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {
|
search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());
|
}
|
if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {
|
search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());
|
}
|
|
PagerResult<UserLoginPO> pageList = mapper.search(search);
|
List<UserLoginVO> listVo = new ArrayList<>();
|
List<UserLoginPO> list = pageList.getList();
|
if (ListUtil.isNotNullOrEmpty(list)) {
|
pageList.setLastId(list.get(list.size() - 1).getId());
|
// 转换vo
|
listVo = UserLoginConvert.INSTANCE.toVo(list);
|
}
|
PagerResult<UserLoginVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
|
result.setLastId(pageList.getLastId());
|
return ExecutedResult.success(result);
|
}
|
|
protected ExecutedResult<UserLoginPO> check4Id(Long id) {
|
UserLoginPO exists = mapper.get(id);
|
if (Objects.isNull(exists)) {
|
return ExecutedResult.failed("[null]不存在:" + id);
|
}
|
return ExecutedResult.success(exists);
|
}
|
protected ExecutedResult<List<UserLoginPO>> check4Id(List<Long> listId) {
|
// 从数据库查找null
|
List<UserLoginPO> list = mapper.getList(listId);
|
if (ListUtil.isNullOrEmpty(list)) {
|
return ExecutedResult.failed("[null]不存在." + listId);
|
}
|
// 数据库找到的id列表
|
List<Long> listIdFind = list.stream().map(UserLoginPO::getId).collect(Collectors.toList());
|
// 数量不一致
|
if (listId.size() != listIdFind.size()) {
|
// 筛选数据库不存在的null
|
List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList());
|
if (ListUtil.isNullOrEmpty(list)) {
|
return ExecutedResult.failed("[null]不存在." + listIdNotFound);
|
}
|
}
|
return ExecutedResult.success(list);
|
}
|
|
public UserLoginPO get4Openid(String openId) {
|
return mapper.get4Openid(openId);
|
}
|
// 修改密码
|
public ExecutedResult<String> changePassword(LoginUserDTO user, ReqChangePassword request) {
|
String openId = user.getUserId();
|
// 验证记录是否存在
|
ExecutedResult<UserLoginPO> checkExists = this.check4Openid(openId);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
String oldPass = MD5Util.encrypt(request.getOldPassword() + ConstantFactory.KEY_PASSWORD);
|
if (BooleanUtils.isFalse(oldPass.equals(checkExists.getData().getPassword()))) {
|
return ExecutedResult.failed("密码校验失败。");
|
}
|
return this.changePasswordDo(checkExists.getData().getId(), request.getNewPassword());
|
}
|
private ExecutedResult<String> changePasswordDo(Long userLoginId, String password) {
|
UserLoginPO item = new UserLoginPO();
|
item.setId(userLoginId);
|
// 密码加密
|
item.setPassword(MD5Util.encrypt(password + ConstantFactory.KEY_PASSWORD));
|
int modify = mapper.updateById(item);
|
if (modify>0) {
|
return ExecutedResult.success();
|
}
|
return ExecutedResult.failed("密码修改失败。");
|
}
|
|
// 设置登录帐号
|
public ExecutedResult<String> setUserName(String openId, String userCode) {
|
// 验证记录是否存在
|
ExecutedResult<UserLoginPO> checkExists = this.check4Openid(openId);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
// 转换po
|
UserLoginPO item = new UserLoginPO();
|
item.setId(checkExists.getData().getId());
|
item.setUserName(userCode);
|
// 密码加密
|
item.setPassword(MD5Util.encrypt("88888888" + ConstantFactory.KEY_PASSWORD));
|
|
int result = this.mapper.updateById(item);
|
if (result<1) {
|
return ExecutedResult.failed("设置登录帐号失败。");
|
}
|
return ExecutedResult.success();
|
}
|
protected ExecutedResult<UserLoginPO> check4Openid(String openId) {
|
UserLoginPO exists = mapper.get4Openid(openId);
|
if (Objects.isNull(exists)) {
|
return ExecutedResult.failed("[用户登录]不存在:" + openId);
|
}
|
return ExecutedResult.success(exists);
|
}
|
protected ExecutedResult<UserLoginPO> check4UserName(String userName) {
|
UserLoginPO exists = mapper.get4UserName(userName);
|
if (Objects.isNull(exists)) {
|
return ExecutedResult.failed("[用户登录]不存在:" + userName);
|
}
|
return ExecutedResult.success(exists);
|
}
|
public ExecutedResult<LoginUserDTO> login(ReqUserLogin request) {
|
// 验证记录是否存在
|
ExecutedResult<UserLoginPO> checkExists = this.check4UserName(request.getUserName());
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed("帐号或密码错误");
|
}
|
UserLoginPO user = checkExists.getData();
|
String password = MD5Util.encrypt(request.getPassword() + ConstantFactory.KEY_PASSWORD);
|
if (!Objects.equals(user.getPassword(), password)) {
|
return ExecutedResult.failed("帐号或密码错误");
|
}
|
if (!Objects.equals(user.getStatus(), EState.NORMAL.getValue())) {
|
return ExecutedResult.failed("账号异常, 请联系管理员!");
|
}
|
|
// 构建登录用户
|
LoginUserDTO loginUser = new LoginUserDTO();
|
loginUser.setUserType(EUserType.CUSTOMER_USER.getValue());
|
loginUser.setUserId(user.getWxOpenId());
|
loginUser.setUserName(user.getUserName());
|
loginUser.setNickName(user.getNickName());
|
loginUser.setHeadImg(user.getHeadImg());
|
loginUser.setPhone(user.getPhone());
|
loginUser.setListRole(new ArrayList<>());
|
loginUser.setListRoleName(new ArrayList<>());
|
|
// 保存用户信息到jwt
|
String token = JWTUtil.getToken(loginUser);
|
loginUser.setToken(token);
|
return ExecutedResult.success(loginUser);
|
}
|
}
|