linwe
2024-09-03 764d883b5ea3bdc06abbec548b6df0511e567978
src/main/java/org/springblade/modules/backblast/service/impl/BackblastPubRecordServiceImpl.java
New file
@@ -0,0 +1,239 @@
package org.springblade.modules.backblast.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.backblast.entity.BackblastPubPersonEntity;
import org.springblade.modules.backblast.entity.BackblastPubRecordEntity;
import org.springblade.modules.backblast.excel.BackblastPubRecordExcel;
import org.springblade.modules.backblast.mapper.BackblastPubRecordMapper;
import org.springblade.modules.backblast.service.IBackblastPubPersonService;
import org.springblade.modules.backblast.service.IBackblastPubRecordService;
import org.springblade.modules.backblast.vo.BackblastPubRecordVO;
import org.springblade.modules.grid.entity.GridEntity;
import org.springblade.modules.grid.service.IGridService;
import org.springblade.modules.police.entity.PoliceAffairsGridEntity;
import org.springblade.modules.police.service.IPoliceAffairsGridService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 反炸宣传记录表 服务实现类
 *
 * @author BladeX
 * @since 2024-03-15
 */
@Service
public class BackblastPubRecordServiceImpl extends ServiceImpl<BackblastPubRecordMapper, BackblastPubRecordEntity> implements IBackblastPubRecordService {
   /**
    * 自定义分页列表查询
    * @param page
    * @param backblastPubRecord
    * @return
    */
   @Override
   public IPage<BackblastPubRecordVO> selectBackblastPubRecordPage(IPage<BackblastPubRecordVO> page, BackblastPubRecordVO backblastPubRecord) {
      CommonParamSet commonParamSet = new CommonParamSet<>().invoke(BackblastPubRecordVO.class, backblastPubRecord);
      if (!Strings.isBlank(backblastPubRecord.getRoleName())){
         backblastPubRecord.setCreateUser(AuthUtil.getUserId());
      }
      return page.setRecords(baseMapper.selectBackblastPubRecordPage(page,
         backblastPubRecord,
         commonParamSet.getIsAdministrator(),
         commonParamSet.getRegionChildCodesList(),
         commonParamSet.getGridCodeList()));
   }
   /**
    * 反炸宣传记录表 自定义新增/修改
    * @param backblastPubRecord
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean addOrUpdateBackblastPubRecordEntity(BackblastPubRecordVO backblastPubRecord) {
      backblastPubRecord.setCreateUser(AuthUtil.getUserId());
      boolean flag = false;
      // 点落面计算警格,网格,警格
      setGridInfo(backblastPubRecord);
      // "otherId":"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"[1]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""
      backblastPubRecord.setOtherId(cleanEscapes(backblastPubRecord.getOtherId()));
      // 设置民警姓名电话(非民警暂时也记录)
      User user = SpringUtils.getBean(IUserService.class).getById(AuthUtil.getUserId());
      if (null!=user){
         backblastPubRecord.setPoliceman(user.getRealName());
         backblastPubRecord.setPolicemanPhone(user.getPhone());
      }
      if (null!=backblastPubRecord.getId()){
         // 更新
         flag = updateById(backblastPubRecord);
      }else {
         // 新增
         flag = save(backblastPubRecord);
      }
      // 宣防对象操作
      updateBackblastPubPerson(backblastPubRecord);
      // 返回
      return flag;
   }
   /**
    * 清理字符串中的多余转义字符
    * @param originalStr 原始字符串
    * @return 清理后的字符串
    */
   private static String cleanEscapes(String originalStr) {
      if (StringUtils.isBlank(originalStr)) {
         return "";
      }
      // 替换多个反斜杠为单个反斜杠
      String replacedBackslashes = originalStr.replaceAll("\\\\+", "\\\\");
      // 替换转义的双引号为空
      String cleanedStr = replacedBackslashes.replaceAll("\\\\\"", "").replaceAll("\"", "");
      return cleanedStr;
   }
   /**
    * 宣防对象操作
    * @param backblastPubRecord
    * @return
    */
   public void updateBackblastPubPerson(BackblastPubRecordVO backblastPubRecord) {
      IBackblastPubPersonService backblastPubPersonService = SpringUtil.getBean(IBackblastPubPersonService.class);
      if (backblastPubRecord.getBackblastPubPersonEntityList().size()>0){
         // 查询对应已存在的反诈宣传对象
         QueryWrapper<BackblastPubPersonEntity> wrapper = new QueryWrapper<>();
         wrapper.eq("backblast_pub_record_id", backblastPubRecord.getId()).eq("is_deleted",0);
         List<BackblastPubPersonEntity> oldList = backblastPubPersonService.list(wrapper);
         // 取出反诈宣传对象信息
         List<BackblastPubPersonEntity> backblastPubPersonEntityList = backblastPubRecord.getBackblastPubPersonEntityList();
         for (BackblastPubPersonEntity pubPersonEntity : backblastPubPersonEntityList) {
            pubPersonEntity.setBackblastPubRecordId(backblastPubRecord.getId());
         }
         // 申明新增,修改,删除集合
         List<BackblastPubPersonEntity> newList = new ArrayList<>();
         List<BackblastPubPersonEntity> addList = new ArrayList<>();
         List<BackblastPubPersonEntity> updateList = new ArrayList<>();
         List<BackblastPubPersonEntity> removeList = new ArrayList<>();
         // 遍历设置数据
         for (BackblastPubPersonEntity pubPersonEntity : backblastPubPersonEntityList) {
            if (null == pubPersonEntity.getId()) {
               // 新增
               addList.add(pubPersonEntity);
            } else {
               newList.add(pubPersonEntity);
            }
         }
         // 遍历去差集,判断是新增还是删除还是更新
         // 取旧数据和新提交数据差集--删除
         removeList = oldList.stream().filter(vo -> !newList.stream().map(e ->
            e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
         // 取旧数据和新提交数据交集--更新
         updateList = newList.stream().filter(vo -> oldList.stream().map(e ->
            e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
         // 批量新增
         if (addList.size() > 0) {
            backblastPubPersonService.saveBatch(addList);
         }
         // 批量修改
         if (updateList.size() > 0) {
            backblastPubPersonService.updateBatchById(updateList);
         }
         // 批量删除
         if (removeList.size() > 0) {
            backblastPubPersonService.removeBatchByIds(removeList);
         }
      }else {
         // 删除所有
         QueryWrapper<BackblastPubPersonEntity> wrapper = new QueryWrapper<>();
         wrapper.eq("backblast_pub_record_id", backblastPubRecord.getId());
         boolean remove = backblastPubPersonService.remove(wrapper);
      }
   }
   /**
    * 设置警格网格信息
    * @param backblastPubRecord
    */
   public void setGridInfo(BackblastPubRecordVO backblastPubRecord) {
      // 根据位置设置网格,警格编号
      IGridService gridService = SpringUtils.getBean(IGridService.class);
      IPoliceAffairsGridService policeAffairsGridService = SpringUtils.getBean(IPoliceAffairsGridService.class);
      String point = "'POINT(" + backblastPubRecord.getLng() + " " + backblastPubRecord.getLat() + ")'";
      if(StringUtils.isBlank(backblastPubRecord.getGridCode())){
         //点坐标解析网格
         List<GridEntity> gridEntityList = gridService.spatialAnalysis(point);
         if (gridEntityList.size()>0){
            GridEntity gridEntity = gridEntityList.get(0);
            backblastPubRecord.setGridCode(gridEntity.getGridCode());
         }
      }
      //点坐标解析警格
      List<PoliceAffairsGridEntity> policeAffairsGridEntityList = policeAffairsGridService.spatialAnalysis(point);
      if (policeAffairsGridEntityList.size()>0){
         PoliceAffairsGridEntity policeAffairsGridEntity = policeAffairsGridEntityList.get(0);
         backblastPubRecord.setJwGridCode(policeAffairsGridEntity.getJwGridCode());
      }
   }
   /**
    * 反炸宣传记录表 自定义详情
    * @param backblastPubRecord
    * @return
    */
   @Override
   public BackblastPubRecordVO getDetail(BackblastPubRecordVO backblastPubRecord) {
      return baseMapper.getDetail(backblastPubRecord);
   }
   /**
    * 反炸宣传记录表 删除
    */
   @Override
   public boolean removeBackblastPubByIds(List<Long> toLongList) {
      // 先删除对应的宣传记录
      boolean flag = removeByIds(toLongList);
      // 再删除对应的宣传对象信息
      if (flag){
         IBackblastPubPersonService backblastPubPersonService = SpringUtil.getBean(IBackblastPubPersonService.class);
            // 删除所有
            QueryWrapper<BackblastPubPersonEntity> wrapper = new QueryWrapper<>();
            wrapper.in("backblast_pub_record_id", toLongList);
            backblastPubPersonService.remove(wrapper);
         // for (Long id : toLongList) {
         //    // 删除所有
         //    QueryWrapper<BackblastPubPersonEntity> wrapper = new QueryWrapper<>();
         //    wrapper.eq("backblast_pub_record_id", id);
         //    backblastPubPersonService.remove(wrapper);
         // }
      }
      return flag;
   }
   @Override
   public List<BackblastPubRecordExcel> export(BackblastPubRecordVO backblastPubRecord) {
      CommonParamSet commonParamSet = new CommonParamSet<>().invoke(BackblastPubRecordVO.class, backblastPubRecord);
      if (!Strings.isBlank(backblastPubRecord.getRoleName())){
         backblastPubRecord.setCreateUser(AuthUtil.getUserId());
      }
      return baseMapper.selectBackblastPubRecordExport(backblastPubRecord,
         commonParamSet.getIsAdministrator(),
         commonParamSet.getRegionChildCodesList(),
         commonParamSet.getGridCodeList());
   }
}