package com.newfiber.api.pc.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.newfiber.api.config.UrlConfig; import com.newfiber.api.core.bean.RespBodyObj; import com.newfiber.api.pc.dao.RiverManage.PatrolManageUserRelationMapper; import com.newfiber.api.pc.dao.RiverManage.PatrolManageUserTypeMapper; import com.newfiber.api.pc.dao.RiverManage.PatrolUserInfoMapper; import com.newfiber.api.pc.model.River.PatrolManageUserRelation; import com.newfiber.api.pc.model.River.PatrolManageUserType; import com.newfiber.api.pc.model.River.PatrolUserInfo; import com.newfiber.api.pc.model.River.UserPatrolInfoDto; import com.newfiber.api.pc.service.PatrolUserInfoService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; @Slf4j @Service("patrolUserInfoService") public class PatrolUserInfoServiceImpl extends ServiceImpl<PatrolUserInfoMapper, PatrolUserInfo> implements PatrolUserInfoService { @Autowired private PatrolUserInfoMapper patrolUserInfoMapper; @Autowired private RestTemplate restTemplate; @Resource private UrlConfig urlConfig; @Autowired private PatrolManageUserRelationMapper patrolManageUserRelationMapper; @Autowired private PatrolManageUserTypeMapper patrolManageUserTypeMapper; @Override public PageInfo<PatrolUserInfo> listForPage(String patrolType, String bindNo, Integer pageNo, Integer pageSize) { PageHelper.startPage(pageNo, pageSize); EntityWrapper<PatrolUserInfo> wapper = new EntityWrapper<>(); //name 模糊匹配的字段 wapper.like("patrol_type", patrolType); wapper.eq("bind_no", bindNo); List<PatrolUserInfo> list = patrolUserInfoMapper.selectList(wapper); PageInfo<PatrolUserInfo> result = new PageInfo<>(); if (!list.isEmpty()) { result = new PageInfo<PatrolUserInfo>(list); } return result; } @Override public RespBodyObj saveOrUpdate(PatrolUserInfo data) { RespBodyObj respBodyObj = new RespBodyObj(); if (StringUtils.isEmpty(data.getId())) { //1.需要新创建用户 UserPatrolInfoDto user = new UserPatrolInfoDto(); user.setUserNo(data.getUserNo()); user.setPhoneNum(data.getPhone()); user.setUserName(data.getName()); user.setIsUsed(data.getIsUsed()); user.setPatrolType(data.getPatrolType().longValue()); respBodyObj = addUserInfo(user); } else { //2.不需要新创建用户 UserPatrolInfoDto user = new UserPatrolInfoDto(); user.setUserNo(data.getUserNo()); user.setPhoneNum(data.getPhone()); user.setUserName(data.getName()); user.setIsUsed(data.getIsUsed()); user.setPatrolType(data.getPatrolType().longValue()); respBodyObj = updateUserInfo(user); } if(200!=respBodyObj.getCode() && !"该用户名被使用".equals(respBodyObj.getMessage())){ return respBodyObj; } PatrolUserInfo info = new PatrolUserInfo(); BeanUtils.copyProperties(data, info); //处理绑定关系 changeUser(data); boolean b = this.insertOrUpdate(info); //修改这个人所有相关的账号名称 if("该用户名被使用".equals(respBodyObj.getMessage()) || "修改用户成功".equals(respBodyObj.getMessage())){ patrolUserInfoMapper.updateByUserNo(info.getUserNo(),info.getName(),info.getPhone()); } return RespBodyObj.ok(b); } /** * 设置用户权限 * @param data */ public void changeUser(PatrolUserInfo data) { //2.添加新的用户信息 //1.删除以前的用户信息 patrolManageUserRelationMapper.deleteByData(data.getPatrolType(),data.getUserNo(),data.getId()); patrolManageUserTypeMapper.deleteByData(data.getPatrolType(),data.getUserNo()); if(data.getIsUsed()!=null && data.getIsUsed()==1){ //2.添加现在的信息 //关联项目信息 PatrolManageUserRelation relation = new PatrolManageUserRelation(); relation.setPatrolType(data.getPatrolType()); relation.setBindNo(Long.valueOf(data.getBindNo())); //relation.setProjectName(data.getProjectName()); relation.setHandlerNo(data.getUserNo()); relation.setHandlerName(data.getName()); patrolManageUserRelationMapper.insert(relation); patrolManageUserRelationMapper.updatebatchName(data.getUserNo(),data.getName()); //用户类型信息 PatrolManageUserType type = new PatrolManageUserType(); type.setPatrolType(data.getPatrolType()); type.setUserNo(data.getUserNo()); type.setUserName(data.getName()); type.setUserType(5); type.setParentName(data.getParentUserNo()); type.setPhone(data.getPhone()); patrolManageUserTypeMapper.insert(type); patrolManageUserTypeMapper.updatebatchName(data.getUserNo(),data.getName(),data.getPhone()); } } /** * 添加用户信息 */ private RespBodyObj addUserInfo(UserPatrolInfoDto user) { JSONObject data = new JSONObject(); data.put("data",user); String url = urlConfig.getHuludaoMainUrl() + "/users/addPatrolUser"; log.info("保存系统用户,保存历史数据:{}",url); ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, data, String.class); if(postForEntity.getStatusCode().is2xxSuccessful()){ JSONObject jsonObject = JSONObject.parseObject(postForEntity.getBody(), JSONObject.class); if(jsonObject.get("code").toString().equals("500")){ log.info("创建用户失败"+JSON.toJSONString(null)); return RespBodyObj.error(jsonObject.get("message").toString()); }else{ log.info("创建用户成功"+ JSON.toJSONString(user)); return RespBodyObj.ok("创建成功"); } } return RespBodyObj.error("添加用户失败"); } /** * 添加用户信息 */ private RespBodyObj updateUserInfo(UserPatrolInfoDto user) { JSONObject data = new JSONObject(); data.put("data",user); String url = urlConfig.getHuludaoMainUrl() + "/users/updatePatrolUser"; log.info("修改用户,保存历史数据:{}",url); ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, data, String.class); if(postForEntity.getStatusCode().is2xxSuccessful()){ JSONObject jsonObject = JSONObject.parseObject(postForEntity.getBody(), JSONObject.class); if(jsonObject.get("code").equals("500")){ log.info("修改用户失败"+ JSON.toJSONString(user)); return RespBodyObj.error(jsonObject.get("message").toString()); }else{ log.info("修改用户成功"+ JSON.toJSONString(user)); return RespBodyObj.ok("修改用户成功"); } } return RespBodyObj.error("修改用户失败"); } }