forked from gzzfw/backEnd/gz-dyh

dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtAccountService.java
@@ -1,12 +1,23 @@
package cn.huge.module.ctuser.service;
import cn.huge.base.common.bo.ReturnBO;
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.common.utils.*;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.ctuser.dao.mapper.CtAccountMapper;
import cn.huge.module.ctuser.domain.po.CtAccount;
import cn.huge.module.ctuser.domain.po.CtUser;
import cn.huge.module.ctuser.domain.po.CtUserole;
import cn.huge.module.ctuser.dto.CtAccountLoginDTO;
import cn.huge.module.ctuser.dto.CtCipherDTO;
import cn.huge.module.ctuser.dto.CtUserAddrDTO;
import cn.huge.module.ctuser.dto.UserLoginDTO;
import cn.huge.module.cust.constant.UserBaseConsts;
import cn.huge.module.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@@ -15,8 +26,10 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -26,7 +39,7 @@
 * @Description 客户用户账号表业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-08-17 15:30:56
 * @Time 2024-08-19 20:04:18
 * @version 1.0.0
 */
@Slf4j
@@ -39,6 +52,10 @@
    @Autowired
    private UtilsClientImpl utilsClient;
    @Autowired
    private CtUserService ctUserService;
    @Autowired
    private CtUseroleService ctUseroleService;
    /**
    * 更新对象
@@ -116,7 +133,7 @@
    */
    public void saveCtAccount(CtAccount ctAccount){
        try{
            Date nowDate = DateUtils.getMowDate();
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(ctAccount.getId())){
                ctAccount.setId(utilsClient.getNewTimeId());
@@ -130,4 +147,155 @@
        }
    }
    /**
     * 根据账号和类型查询
     * @param acc 账号
     * @param accType 账号类型
     * @return
     */
    public CtAccount getByAccAndType(String acc, String accType){
        QueryWrapper<CtAccount> accountWrapper = new QueryWrapper<>();
        accountWrapper.eq("acc", acc)
                .eq("acc_type", accType);
        CtAccount ctAccount = this.getOne(accountWrapper);
        return ctAccount;
    }
    /**
     * 判断密码是否正确
     * @param cipher 密码
     * @param ctAccount 账号信息
     * @return Boolean
     */
    public Boolean checkCredential(String cipher, CtAccount ctAccount){
        String credentialMd5 = DigestUtils.md5DigestAsHex(cipher.getBytes());
        if (StringUtils.equals(credentialMd5, ctAccount.getCipher())) {
            return true;
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String createTime = sdf.format(ctAccount.getCreateTime());
            String credentialTime = sdf.format(ctAccount.getCreateTime());
            //是否修改过密码,未修改过可以用默认密码登录
            if (createTime.equals(credentialTime)) {
                if (cipher.equals(UserBaseConsts.MR_CIPHER)) {
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }
    /**
     * web端-工作人员用户登录
     * @param ctAccountLoginDTO 登录账号信息
     * @return UserLoginDTO
     */
    public ReturnBO webLogin(CtAccountLoginDTO ctAccountLoginDTO) {
        try{
            // 判断账号密码是否为空
            if (StringUtils.isBlank(ctAccountLoginDTO.getAcc()) || StringUtils.isBlank(ctAccountLoginDTO.getCipher())) {
                return ReturnSucUtils.getRepInfo("账号或密码不能为空", null);
            }
            // 判断账号是否存在
            CtAccount ctAccount = this.getByAccAndType(ctAccountLoginDTO.getAcc(), UserBaseConsts.ACC_TYPE_1);
            if (ObjectUtils.isNotEmpty(ctAccount)){
                // 判断账号是否被锁定
                if (ObjectUtils.isNotEmpty(ctAccount.getLimitTime())) {
                    Date nowDate = new Date();
                    if (nowDate.before(ctAccount.getLimitTime())) {
                        SimpleDateFormat sdf = new SimpleDateFormat("HH点mm分");
                        return ReturnFailUtils.getRepInfo("账号已锁定,请于" + sdf.format(ctAccount.getLimitTime()) + "后重试,或者请联系管理员解锁!");
                    }
                }
                // 判断密码是否正确
                if (checkCredential(ctAccountLoginDTO.getCipher(), ctAccount)) {
                    //返回结果
                    UserLoginDTO userLoginDTO = new UserLoginDTO();
                    CtUser ctUser = ctUserService.getByIdFromRedis(ctAccount.getUserId());
                    if (ObjectUtils.isEmpty(ctUser)){
                        return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");
                    }
                    if (UserBaseConsts.USER_STATUS_2.equals(ctUser.getStatus()) || UserBaseConsts.USER_STATUS_3.equals(ctUser.getStatus())){
                        return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");
                    }
                    // 封装用户信息
                    userLoginDTO.setToken(JwtUtils.buildJWT(ctUser.getId()));
                    userLoginDTO.setUserId(ctUser.getId());
                    userLoginDTO.setTrueName(ctUser.getTrueName());
                    userLoginDTO.setUnit(ctUser.getUnitName());
                    userLoginDTO.setDept(ctUser.getDeptName());
                    userLoginDTO.setCustId(ctUser.getCustId());
                    // 登录用户角色
                    List<CtUserole> ctUseroleList = ctUseroleService.listByUserId(ctUser.getId());
                    userLoginDTO.setCtUseroleList(ctUseroleList);
                    // 登录用户地址信息
                    CtUserAddrDTO ctUserAddrDTO = new CtUserAddrDTO();
                    ctUserAddrDTO.setProv(ctUser.getProv());
                    ctUserAddrDTO.setProvName(ctUser.getProvName());
                    ctUserAddrDTO.setCity(ctUser.getCity());
                    ctUserAddrDTO.setCityName(ctUser.getCityName());
                    ctUserAddrDTO.setArea(ctUser.getArea());
                    ctUserAddrDTO.setAreaName(ctUser.getAreaName());
                    ctUserAddrDTO.setRoad(ctUser.getRoad());
                    ctUserAddrDTO.setRoadName(ctUser.getRoadName());
                    ctUserAddrDTO.setVillage(ctUser.getVillage());
                    ctUserAddrDTO.setVillageName(ctUser.getVillageName());
                    userLoginDTO.setCtUserAddrDTO(ctUserAddrDTO);
                    // 最后登录时间
                    Date loginTime = ctAccount.getLoginTime();
                    if (ObjectUtils.isEmpty(ctAccount.getLoginTime())){
                        loginTime = DateUtils.getNowDate();
                    }
                    userLoginDTO.setLastLoginTime(loginTime);
                    // 更新最新登录时间
                    UpdateWrapper<CtAccount> accountUpdateWrapper = new UpdateWrapper<>();
                    accountUpdateWrapper.set("login_time", loginTime).eq("id", ctAccount.getId());
                    this.update(accountUpdateWrapper);
                    return ReturnSucUtils.getRepInfo(userLoginDTO);
                } else {
                    return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");
                }
            }else{
                return ReturnFailUtils.getRepInfo("账号或密码错误,请确认后重试!");
            }
        }catch (Exception e){
            log.error("service方法[AccountService.webLogin]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtAccountService.webLogin", e);
        }
    }
    /**
     * web端-工作人员-修改密码
     * @param userId 用户编号
     * @param ctCipherDTO 修改密码信息
     * @return
     */
    public ReturnBO webChangeCipher(String userId, CtCipherDTO ctCipherDTO){
        try{
            CtAccount ctAccount = this.getByAccAndType(userId, UserBaseConsts.ACC_TYPE_1);
            if (ObjectUtils.isEmpty(ctAccount)){
                return ReturnFailUtils.getRepInfo("输入账号或密码不正确,请确认后重试!");
            }
            String oldCredentialMd5 = DigestUtils.md5DigestAsHex(ctCipherDTO.getOldCipher().getBytes());
            if (!StringUtils.equals(oldCredentialMd5, ctAccount.getCipher())) {
                return ReturnFailUtils.getRepInfo("输入账号或密码不正确,请确认后重试!");
            }
            String newCredentialMd5 = DigestUtils.md5DigestAsHex(ctCipherDTO.getNewCipher().getBytes());
            if (StringUtils.equals(newCredentialMd5, ctAccount.getCipher())) {
                return ReturnFailUtils.getRepInfo("新密码不能和旧密码相同!");
            }
            UpdateWrapper<CtAccount> accountUpdateWrapper = new UpdateWrapper<>();
            accountUpdateWrapper.set("cipher", newCredentialMd5).set("cipher_open", ctCipherDTO.getNewCipher())
                    .set("update_time", DateUtils.getNowDate()).eq("id", ctAccount.getId());
            this.update(accountUpdateWrapper);
            return ReturnSucUtils.getRepInfo();
        }catch (Exception e){
            log.error("service方法[AccountService.webChangeCipher]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtAccountService.webChangeCipher", e);
        }
    }
}