package com.huajun.resources.service.impl; import java.io.Serializable; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; import com.huajun.common.core.domain.R; import com.huajun.common.core.utils.StringUtils; import com.huajun.common.core.web.domain.AjaxResult; import com.huajun.system.api.RemoteConfigService; import com.huajun.system.api.RemoteUserService; import com.huajun.system.api.domain.SysUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.huajun.resources.mapper.ResourceExpertMapper; import com.huajun.resources.domain.ResourceExpert; import com.huajun.resources.service.IResourceExpertService; import org.springframework.transaction.annotation.Transactional; /** * 应急专家信息Service业务层处理 * * @author huajun * @date 2021-03-31 */ @Service public class ResourceExpertServiceImpl extends ServiceImpl<ResourceExpertMapper,ResourceExpert> implements IResourceExpertService { @Autowired private ResourceExpertMapper resourceExpertMapper; @Autowired private RemoteUserService remoteUserService; @Autowired private RemoteConfigService remoteConfigService; /** * 查询应急专家信息列表 * * @param resourceExpert 应急专家信息 * @return 应急专家信息 */ @Override public List<ResourceExpert> selectResourceExpertList(ResourceExpert resourceExpert) { return resourceExpertMapper.selectResourceExpertList(resourceExpert); } /** * 同步应急专家账号 * 2021-06-04 */ @Override public AjaxResult SyncExpert(String id) { AjaxResult result = null; if (StringUtils.isEmpty(id)) { return AjaxResult.error("获取应急专家参数错误!"); } R<AjaxResult> ajaxResultR=remoteConfigService.getConfigKey("emergencyexpertdept"); ResourceExpert resourceExpert = resourceExpertMapper.selectById(id); if (StringUtils.isEmpty(resourceExpert.getUserId()))//同步操作 { String expertdeptid = remoteConfigService.getConfigInfo("emergencyexpertdept");//得到专家部门 if (StringUtils.isEmpty(expertdeptid)) { return AjaxResult.error("同步专家用户'" + resourceExpert.getName() + "'失败,未设置专家部门"); } String expertroleid = remoteConfigService.getConfigInfo("emergencyexpertrole");//得到专家角色 if (StringUtils.isEmpty(expertroleid)) { return AjaxResult.error("同步专家用户'" + resourceExpert.getName() + "'失败,未设置专家角色"); } SysUser sysuser = new SysUser(); sysuser.setDeptId(Long.valueOf(expertdeptid));//默认部门 sysuser.setUserName(resourceExpert.getMobileTel().trim());//用户账号,使用专家手机号 sysuser.setNickName(resourceExpert.getName());//使用专家姓名 sysuser.setEmail(resourceExpert.getEmail());//专家邮箱 sysuser.setPhonenumber(resourceExpert.getMobileTel());//专家手机号 sysuser.setSex("2");//专家性别0=男,1=女,2=未知 sysuser.setPassword(resourceExpert.getMobileTel().trim());//默认使用手机号 sysuser.setStatus("0");//帐号状态0=正常,1=停用 sysuser.setDelFlag("0");//删除标志(0代表存在 2代表删除) sysuser.setRemark("");//专家其他信息可以设置进此字段 Long[] roleIds = {Long.valueOf(expertroleid)}; sysuser.setRoleIds(roleIds); sysuser.setExpertId(id); result = remoteUserService.addUser(sysuser); if (result.get("msg").equals("操作成功") && result.get("code").equals(200)) { LinkedHashMap<String, Object> data = (LinkedHashMap) result.get("data"); for (Map.Entry<String, Object> entry : data.entrySet()) { if (entry.getKey().equals("userId")) { resourceExpert.setUserId(entry.getValue().toString()); break; } } resourceExpertMapper.updateById(resourceExpert); } } else//解除绑定,需要设置专家表外键为null,删除用户表,用户角色表中信息 { Long[] userIds = {Long.valueOf(resourceExpert.getUserId())}; result = remoteUserService.removeUser(userIds); if (result.get("msg").equals("操作成功") && result.get("code").equals(200)) { int UpdateCount = resourceExpertMapper.update( null, Wrappers.<ResourceExpert>lambdaUpdate() .set(ResourceExpert::getUserId, null) //把用户主键设置为null .eq(ResourceExpert::getId, id) ); } } return result; } /** * 重写Mybatis-Plus的批量删除方法removeByIds,实现自定义逻辑 *2021-06-04 * @param idList * @return */ @Override public boolean removeByIds(Collection<? extends Serializable> idList) { if (CollectionUtils.isEmpty(idList)) { return false; } QueryWrapper<ResourceExpert> wrapper = new QueryWrapper<>(); wrapper.in("id", idList).isNotNull("user_id"); List<ResourceExpert> resourceexperts = resourceExpertMapper.selectList(wrapper); // users.forEach(System.out::println); boolean isok = SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(idList)); if (isok) { Long[] userIds = new Long[resourceexperts.size()]; for (int j = 0; j < resourceexperts.size(); j++) { userIds[j] = Long.valueOf(resourceexperts.get(j).getUserId()); } remoteUserService.removeUser(userIds); } return isok; //return SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(idList)); //return CollectionUtils.isEmpty(idList) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(idList)); } /** * 批量更新专家信息,把专家外键置为null * 2021-06-04 * @param Ids 需要更新的专家ID * @return */ @Override @Transactional public int updateExpertByIds(Long[] Ids) { return resourceExpertMapper.updateExpertByIds(Ids); } }