package com.dy.pipIrrBase.user; 
 | 
  
 | 
  
 | 
import com.dy.common.webUtil.QueryResultVo; 
 | 
import com.dy.pipIrrGlobal.daoBa.BaUserMapper; 
 | 
import com.dy.pipIrrGlobal.daoBa.BaUserRoleMapper; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaUser; 
 | 
import com.dy.pipIrrGlobal.pojoBa.BaUserRole; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.apache.dubbo.common.utils.PojoUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
  
 | 
import java.util.List; 
 | 
import java.util.Map; 
 | 
import java.util.Random; 
 | 
  
 | 
@Slf4j 
 | 
@Service 
 | 
public class UserSv { 
 | 
  
 | 
    private BaUserMapper dao; 
 | 
    private BaUserRoleMapper urDao; 
 | 
  
 | 
    @Autowired 
 | 
    private void setDao(BaUserMapper dao) { 
 | 
        this.dao = dao; 
 | 
    } 
 | 
  
 | 
    @Autowired 
 | 
    private void setDao(BaUserRoleMapper dao) { 
 | 
        this.urDao = dao; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 得到一个用户 
 | 
     * 
 | 
     * @param id 用户ID 
 | 
     * @return 用户实体 
 | 
     */ 
 | 
    public BaUser selectById(Long id) { 
 | 
        return this.dao.selectById(id); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 得到一个用户 
 | 
     * 
 | 
     * @param vo 查询条件值对象 
 | 
     * @return 用户实体 
 | 
     */ 
 | 
    @SuppressWarnings("unchecked") 
 | 
    public QueryResultVo<List<BaUser>> selectSome(QueryVo vo) { 
 | 
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo); 
 | 
        Long itemTotal = this.dao.selectTotal(params); 
 | 
  
 | 
        QueryResultVo<List<BaUser>> rsVo = new QueryResultVo<>(); 
 | 
        rsVo.pageSize = vo.pageSize; 
 | 
        rsVo.pageCurr = vo.pageCurr; 
 | 
        rsVo.calculateAndSet(itemTotal, params); 
 | 
        rsVo.obj = this.dao.selectSome(params); 
 | 
  
 | 
        return rsVo; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 保存实体 
 | 
     * @param po 实体 
 | 
     * @return 影响记录数量 
 | 
     */ 
 | 
    @Transactional 
 | 
    public int save(BaUser po){ 
 | 
        return this.dao.putin(po) ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 保存修改实体 
 | 
     * @param po 实体 
 | 
     * @return 影响记录数量 
 | 
     */ 
 | 
    @Transactional 
 | 
    public int update(BaUser po) { 
 | 
        return this.dao.updateByPrimaryKeySelective(po); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 修改密码 
 | 
     * 
 | 
     * @param id       用户ID 
 | 
     * @param password 新密码 
 | 
     * @return 影响记录数量 
 | 
     */ 
 | 
    public int changePassword(Long id, String password) { 
 | 
        return this.dao.changePassword(id, password); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置用户角色 
 | 
     * 
 | 
     * @param userId  用户id 
 | 
     * @param roleIds 选择的角色id集合 
 | 
     * @return 插入用户与角色关联记录数量 
 | 
     */ 
 | 
    public int setRoles(Long userId, Long[] roleIds) { 
 | 
        this.urDao.deleteByUserId(userId); 
 | 
        int count = 0; 
 | 
        if (roleIds != null && roleIds.length > 0) { 
 | 
            for (Long roleId : roleIds) { 
 | 
                count += this.urDao.insertSelective(new BaUserRole(userId, roleId)); 
 | 
            } 
 | 
        } 
 | 
        return count ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置用户角色 
 | 
     * 
 | 
     * @param userId  用户id 
 | 
     * @param roleIds 选择的角色id集合 
 | 
     * @return 插入用户与角色关联记录数量 
 | 
     */ 
 | 
    public int setRoles(Long userId, String[] roleIds) { 
 | 
        this.urDao.deleteByUserId(userId); 
 | 
        int count = 0; 
 | 
        if (roleIds != null && roleIds.length > 0) { 
 | 
            for (String roleId : roleIds) { 
 | 
                count += this.urDao.insertSelective(new BaUserRole(userId, Long.parseLong(roleId))); 
 | 
            } 
 | 
        } 
 | 
        return count; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 保存修改实体 
 | 
     * @param id 实体ID 
 | 
     * @return 影响记录数量 
 | 
     */ 
 | 
    @Transactional 
 | 
    public int delete(Long id) { 
 | 
        return this.dao.deleteLogicById(id); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 生成指定位数随机数字密码 
 | 
     * 
 | 
     * @param length 多长随机数 
 | 
     * @return 随机数 
 | 
     */ 
 | 
    public String getStringRandom(int length) { 
 | 
        String val = ""; 
 | 
        Random random = new Random(); 
 | 
        for (int i = 0; i < length; i++) { 
 | 
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; 
 | 
            if ("char".equalsIgnoreCase(charOrNum)) { 
 | 
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; 
 | 
                val += (char) (random.nextInt(26) + temp); 
 | 
            } else if ("num".equalsIgnoreCase(charOrNum)) { 
 | 
                val += String.valueOf(random.nextInt(10)); 
 | 
            } 
 | 
        } 
 | 
        return val; 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |