/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.springblade.modules.place.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.logging.log4j.util.Strings; import org.springblade.common.node.TreeStringNode; import org.springblade.core.mp.support.Condition; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.modules.doorplateAddress.entity.DoorplateAddressEntity; import org.springblade.modules.doorplateAddress.service.IDoorplateAddressService; import org.springblade.modules.grid.service.IGridService; import org.springblade.modules.place.entity.PlaceEntity; import org.springblade.modules.place.entity.PlaceExtEntity; import org.springblade.modules.place.entity.PlacePoiLabel; import org.springblade.modules.place.entity.PlaceRelEntity; import org.springblade.modules.place.excel.PlaceAndRelExcel; import org.springblade.modules.place.excel.PlaceExcel; import org.springblade.modules.place.service.IPlaceExtService; import org.springblade.modules.place.service.IPlacePoiLabelService; import org.springblade.modules.place.service.IPlaceRelService; import org.springblade.modules.place.vo.PlaceVO; import org.springblade.modules.place.mapper.PlaceMapper; import org.springblade.modules.place.service.IPlaceService; import org.springblade.modules.system.entity.User; import org.springblade.modules.system.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.metadata.IPage; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; /** * 场所表 服务实现类 * * @author BladeX * @since 2023-10-28 */ @Service public class PlaceServiceImpl extends ServiceImpl implements IPlaceService { @Autowired private IUserService userService; @Autowired private IPlacePoiLabelService placePoiLabelService; @Autowired private IPlaceExtService placeExtService; @Autowired private IPlaceRelService placeRelService; @Autowired private IDoorplateAddressService doorplateAddressService; @Autowired private IGridService gridService; /** * 自定义列表查询 * @param page * @param place * @return */ @Override public IPage selectPlacePage(IPage page, PlaceVO place) { List list = new ArrayList<>(); if (null!=place.getRoleName() && !place.getRoleName().equals("")){ if (place.getRoleName().equals("网格员")){ // 查询对应的房屋地址code list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId()); } } place.setCreateUser(AuthUtil.getUserId()); return page.setRecords(baseMapper.selectPlacePage(page, place,list)); } /** * 查询场所集合信息 * @param userId * @return */ @Override public List selectPlaceNodeList(Long userId) { return baseMapper.selectPlaceNodeList(userId.toString()); } /** * 场所信息自定义新增 * @param placeVO * @return */ @Override @Transactional(rollbackFor = Exception.class) public Boolean addVO(PlaceVO placeVO) { // 设置基础数据 placeVO.setCreateUser(AuthUtil.getUserId()); placeVO.setCreateTime(new Date()); placeVO.setUpdateUser(AuthUtil.getUserId()); placeVO.setUpdateTime(new Date()); // 绑定用户信息 bindUserHandle(placeVO); // 新增场所信息 boolean save = save(placeVO); // 保存场所详情及任务信息 savePlaceExtAndTaskInfo(placeVO); // 场所标签信息绑定 placeLabelBind(placeVO); // 返回结果 return save; } /** * 场所标签信息绑定入库 * @param placeVO */ @Transactional(rollbackFor = Exception.class) public void placeLabelBind(PlaceVO placeVO) { List labelList = Arrays.asList(placeVO.getLabel().split(",")); // 遍历 labelList.forEach(labelId->{ PlacePoiLabel placePoiLabel = new PlacePoiLabel(); placePoiLabel.setPlaceId(placeVO.getId()); placePoiLabel.setPoiCode(Integer.parseInt(labelId)); placePoiLabelService.save(placePoiLabel); }); } /** * 场所负责人和用户绑定 * @param placeVO */ @Transactional(rollbackFor = Exception.class) public User bindUserHandle(PlaceVO placeVO) { User newUser = new User(); if (null!=placeVO.getPhone() && !placeVO.getPhone().equals("")) { //根据手机号查询库里的数据 User userParams = new User(); userParams.setPhone(placeVO.getPhone()); User user = userService.getOne(Condition.getQueryWrapper(userParams)); if (null!=user) { //如果用户存在,则该用户id绑定场所 placeVO.setPrincipalUserId(user.getId()); newUser = user; // 判断用户是否包含了居民角色,不包含则需更新 if (!user.getRoleId().contains("1717429059648606209")){ user.setRoleId(user.getRoleId() + ",1717429059648606209"); //更新 userService.updateById(user); } } else { //如果用户不存在,则新增一个用户 newUser.setAccount(placeVO.getPhone()); newUser.setPhone(placeVO.getPhone()); newUser.setName(placeVO.getUsername()); newUser.setRealName(placeVO.getUsername()); // 社区群众部门 newUser.setDeptId("1727979636479037441"); // 目前暂定居民角色, newUser.setRoleId("1717429059648606209"); //默认密码为 123456 newUser.setPassword("123456"); // 设置机构 // 用户新增 boolean submit = userService.submit(newUser); //绑定id placeVO.setPrincipalUserId(newUser.getId()); //给人员打上场所负责人的标签 baseMapper.saveUserLabel(newUser.getId(),1002); } } return newUser; } /** * 保存场所详情及任务信息 * @param placeVO */ @Transactional(rollbackFor = Exception.class) public void savePlaceExtAndTaskInfo(PlaceVO placeVO) { PlaceExtEntity placeExtEntity = new PlaceExtEntity(); placeExtEntity.setPlaceId(placeVO.getId()); // 判断是否已存在,已存在则不新增 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("is_deleted",0) .eq("place_id",placeVO.getId()); PlaceExtEntity one = placeExtService.getOne(wrapper); if (null == one) { placeExtEntity.setPlaceId(placeVO.getId()); //新增 placeExtService.savePlaceExt(placeExtEntity); } } /** * 历史场所挂接处理-临时 * @param place * @return */ @Override public Object historyPlaceHandle(PlaceVO place) { // 查询所有的场所(手机号不为空) List list = baseMapper.getPlaceNotNullPhone(); // 遍历 for (PlaceVO placeVO : list) { User user = bindUserHandle(placeVO); if (null!=user){ placeVO.setPrincipalUserId(user.getId()); //更新场所用户id绑定 baseMapper.updatePlaceEntity(placeVO); } } return null; } /** * 历史场所标签挂接处理-临时 * @param place * @return */ @Override @Transactional public Object historyPlaceLabelHandle(PlaceVO place) { // 查询所有的场所 List list = baseMapper.getAllHistoryPlace(); // 遍历 for (PlaceVO placeVO : list) { if (null!=placeVO.getLabel()){ String[] split = placeVO.getLabel().split(","); for (String s : split) { PlacePoiLabel placePoiLabel = new PlacePoiLabel(); placePoiLabel.setPlaceId(placeVO.getId()); placePoiLabel.setPoiCode(Integer.parseInt(s)); placePoiLabelService.save(placePoiLabel); } } } return null; } /** * 场所表 自定义详情查询 * @param place * @return */ @Override public PlaceVO getDetail(PlaceVO place) { // 查询场所信息 PlaceVO placeVO = baseMapper.getDetail(place); if (null!= place.getAddressType() && place.getAddressType()==4){ if (null != placeVO) { // 查询地址编码信息(社区派出所相关信息) DoorplateAddressEntity addressEntity = placeRelService.getDoorplateAddressEntity(placeVO); placeVO.setDoorplateAddressEntity(addressEntity); // 查询网格信息 placeVO.setGrid(gridService.getGridDetailByParam(placeVO)); } }else { if (null != placeVO) { if (null != placeVO.getHouseCode() && !placeVO.getHouseCode().equals("")) { place.setHouseCode(placeVO.getHouseCode()); } // 查询门牌地址信息 if (null != place.getHouseCode() && !place.getHouseCode().equals("")) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("address_code", place.getHouseCode()); List list = doorplateAddressService.list(wrapper); if (list.size() > 0) { placeVO.setDoorplateAddressEntity(list.get(0)); } // 查询网格数据 placeVO.setGrid(gridService.getGridDetailByHouseCode(place.getHouseCode())); } else { // 通过定位点落面分析网格位置,反向推出社区派出所相关数据 } } } // 返回 return placeVO; } /** * 场所数据到导入 * @param data * @param isCovered */ @Override public void importPlace(List data, Boolean isCovered) { for (PlaceExcel placeExcel : data) { // 判断是否存在,不存在则插入,否则不操作 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("is_deleted",0) .eq("house_code",placeExcel.getHouseCode()); PlaceEntity one = getOne(wrapper); if (null == one){ Long userId = updateUser(placeExcel); // 插入场所 PlaceEntity placeEntity = new PlaceEntity(); placeEntity.setHouseCode(placeExcel.getHouseCode()); placeEntity.setPrincipalUserId(userId); placeEntity.setCreateTime(new Date()); placeEntity.setCreateUser(AuthUtil.getUserId()); placeEntity.setUpdateTime(new Date()); placeEntity.setUpdateUser(AuthUtil.getUserId()); //一个一个插入,防止同一个表中有相同的数据 save(placeEntity); } } } /** * 更新用户信息 * @param placeExcel * @return */ public Long updateUser(PlaceExcel placeExcel) { if (!Strings.isBlank(placeExcel.getPhoneNumber()) && !Strings.isBlank(placeExcel.getName())) { PlaceVO placeVO = new PlaceVO(); placeVO.setPhone(placeExcel.getPhoneNumber()); placeVO.setUsername(placeExcel.getName()); // 更新场所负责人 User user = bindUserHandle(placeVO); // 返回 return user.getId(); } return null; } /** * 更新用户信息 * @param placeExcel * @return */ public Long updateUser(PlaceAndRelExcel placeExcel) { if (!Strings.isBlank(placeExcel.getPhoneNumber()) && !Strings.isBlank(placeExcel.getName())) { PlaceVO placeVO = new PlaceVO(); placeVO.setPhone(placeExcel.getPhoneNumber()); placeVO.setUsername(placeExcel.getName()); // 更新场所负责人 User user = bindUserHandle(placeVO); // 返回 return user.getId(); } return null; } /** * 场所(商超)导入 * @param data * @param isCovered */ @Override public void importAndRelPlace(List data, Boolean isCovered) { for (PlaceAndRelExcel placeExcel : data) { // 判断是否存在,不存在则插入,否则不操作 PlaceEntity one = baseMapper.getPlaceAndRelInfo(placeExcel); if (null == one){ Long userId = updateUser(placeExcel); // 插入场所 PlaceEntity placeEntity = new PlaceEntity(); placeEntity.setPlaceName(placeExcel.getPlaceName()); placeEntity.setLocaltion(placeExcel.getAddress()); placeEntity.setPrincipalUserId(userId); placeEntity.setCreateTime(new Date()); placeEntity.setCreateUser(AuthUtil.getUserId()); placeEntity.setUpdateTime(new Date()); placeEntity.setUpdateUser(AuthUtil.getUserId()); //一个一个插入,防止同一个表中有相同的数据 save(placeEntity); // 插入标签关系表 savPlaceLabelBind(placeExcel,placeEntity); // 插入关联数据表 PlaceRelEntity placeRelEntity = new PlaceRelEntity(); placeRelEntity.setPlaceId(placeEntity.getId()); placeRelEntity.setStreetName(placeExcel.getStreetName()); placeRelEntity.setCommunityName(placeExcel.getCommunityName()); placeRelEntity.setGridName(placeExcel.getGridName()); placeRelEntity.setBuildingName(placeExcel.getBuildingName()); placeRelEntity.setDoorplateNum(placeExcel.getDoorplateNum()); placeRelEntity.setFloor(placeExcel.getFloor()); placeRelEntity.setCreateTime(new Date()); placeRelEntity.setCreateUser(AuthUtil.getUserId()); placeRelEntity.setUpdateTime(new Date()); placeRelEntity.setUpdateUser(AuthUtil.getUserId()); // 新增 placeRelService.save(placeRelEntity); }else { // 只更新商铺信息 Long userId = updateUser(placeExcel); // 插入场所 PlaceEntity placeEntity = new PlaceEntity(); placeEntity.setId(one.getId()); placeEntity.setPlaceName(placeExcel.getPlaceName()); placeEntity.setLocaltion(placeExcel.getAddress()); placeEntity.setPrincipalUserId(userId); placeEntity.setCreateTime(new Date()); placeEntity.setCreateUser(AuthUtil.getUserId()); placeEntity.setUpdateTime(new Date()); placeEntity.setUpdateUser(AuthUtil.getUserId()); //一个一个插入,防止同一个表中有相同的数据 updateById(placeEntity); } } } /** * 插入标签关系表 * @param placeExcel */ public void savPlaceLabelBind(PlaceAndRelExcel placeExcel,PlaceEntity placeEntity) { if (!Strings.isBlank(placeExcel.getLabelCode())){ PlaceVO placeVO = new PlaceVO(); placeVO.setId(placeEntity.getId()); placeVO.setLabel(placeExcel.getLabelCode()); // 插入标签 placeLabelBind(placeVO); } } }