package com.dy.pmsBase.user; import cn.hutool.core.codec.Base64; import com.dy.common.webUtil.QueryResultVo; import com.dy.pmsGlobal.daoBa.BaUserMapper; import com.dy.pmsGlobal.daoBa.BaUserRoleMapper; import com.dy.pmsGlobal.daoSta.StaAssemblyWorkLastMapper; import com.dy.pmsGlobal.pojoBa.BaUser; import com.dy.pmsGlobal.pojoBa.BaUserRole; import com.dy.pmsGlobal.pojoSta.StaAssemblyWorkLast; import com.dy.pmsGlobal.util.QrCodeConstant; import com.dy.pmsGlobal.util.QrCodeUtil; import com.google.zxing.WriterException; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.common.utils.PojoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; @Slf4j @Service public class UserSv { @Autowired private BaUserMapper dao; @Autowired private BaUserRoleMapper urDao; @Autowired private StaAssemblyWorkLastMapper assemblyWorkLastDao; /** * 生成指定位数随机数字密码 * * @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; } /** * 得到一个用户 * * @param id 用户ID * @return 用户实体 */ public BaUser selectById(Long id) { return this.dao.selectByPrimaryKey(id); } /** * 获取用户列表 */ public QueryResultVo> selectSome(QueryVo queryVo) { Map params = (Map) PojoUtils.generalize(queryVo); //查询符合条件的记录总数 Long itemTotal = this.dao.selectSomeCount(params); QueryResultVo> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ; //计算分页等信息 rsVo.calculateAndSet(itemTotal, params); //查询符合条件的记录 rsVo.obj = this.dao.selectSome(params) ; rsVo.obj.parallelStream().forEach(item->{ try { byte[] codes = QrCodeUtil.genQrCode(QrCodeConstant.TypeWorker+item.id); item.qrCode = "data:image/jpeg;base64," + Base64.encode(codes); } catch (IOException e) { e.printStackTrace(); } catch (WriterException e) { e.printStackTrace(); } }); return rsVo ; } /** * 保存实体 * @param po 实体 * @return 影响记录数量 */ @Transactional public Long save(BaUser po){ try{ dao.insertSelective(po) ; }catch(DuplicateKeyException e){ log.error(e.getMessage()); throw new RuntimeException("手机号码重复"); } Long id = po.getId(); this.saveUserRoles(id, po.roleIds) ; return id ; } /** * 保存修改实体 * @param po 实体 * @return 影响记录数量 */ @Transactional public int update(BaUser po) { int count=0; try{ count = this.dao.updateByPrimaryKeySelective(po); }catch(DuplicateKeyException e){ log.error(e.getMessage()); throw new RuntimeException("手机号码重复"); } this.saveUserRoles(po.id, po.roleIds) ; return count ; } /** * 禁用或启用用户信息 * @param id * @param disabled * @return */ @Transactional public int disabled(Long id,Boolean disabled) { checkStationLogin(id); BaUser user=new BaUser(); user.id=id; user.disabled=disabled; return dao.updateByPrimaryKeySelective(user); } /** * 修改密码 * * @param id 用户ID * @param password 新密码 * @return 影响记录数量 */ @Transactional public int changePassword(Long id, String password) { return this.dao.changePassword(id, password); } /** * 保存用户与角色关系 * @param userId 用户id * @param roleIds 选择的角色id集合 * @return 插入用户与角色关联记录数量 */ @Transactional public int saveUserRoles(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) { checkStationLogin(id); return this.dao.deleteLogicById(id); } public void checkStationLogin(Long id){ //如果有工站登录信息,则提示先到触屏端登录页面logout Map params = new HashMap<>(); params.put("userId", id); List workLasts = assemblyWorkLastDao.selectSome(params); if (workLasts.size() > 0) { String stationStr = "("; for (StaAssemblyWorkLast workLast:workLasts) { stationStr += workLast.getStationName() +","; } throw new RuntimeException("该用户在"+ stationStr +")工站存在登录信息,请先到触屏端登录页面logout,或联系管理员删除"); } } }