lin
2024-04-19 73ceaaa73dd6b73808aac425c70e0d9184f9f415
src/main/java/org/springblade/modules/task/service/impl/TaskServiceImpl.java
@@ -23,8 +23,10 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.SysCache;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.constant.DictConstant;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.SpringUtils;
@@ -42,17 +44,22 @@
import org.springblade.modules.place.service.IPlaceExtService;
import org.springblade.modules.place.service.IPlaceService;
import org.springblade.modules.place.vo.PlaceVO;
import org.springblade.modules.sms.service.ISmsSendService;
import org.springblade.modules.task.entity.*;
import org.springblade.modules.task.mapper.TaskMapper;
import org.springblade.modules.task.service.*;
import org.springblade.modules.task.vo.TaskVO;
import org.springblade.modules.taskPlaceRectification.service.ITaskPlaceRectificationService;
import org.springblade.modules.taskPlaceSelfCheck.entity.TaskPlaceSelfCheckEntity;
import org.springblade.modules.taskPlaceSelfCheck.service.ITaskPlaceSelfCheckService;
import org.springblade.modules.task.service.ITaskPlaceRectificationService;
import org.springblade.modules.task.entity.TaskPlaceSelfCheckEntity;
import org.springblade.modules.task.service.ITaskPlaceSelfCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
@@ -64,6 +71,8 @@
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements ITaskService {
   private Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
   @Autowired
   private IPlaceService placeService;
@@ -88,6 +97,15 @@
   @Autowired
   private IGridWorkLogService gridWorkLogService;
   @Autowired
   private ITaskResidencePermitApplyService residencePermitApplyService;
   @Autowired
   private ISmsSendService smsSendService;
   // 确保该常量值正确地从配置文件中读取或以更合适的方式定义
   private static final long SMS_TEMPLATE_ID = 1775429789463142401L;
   @Override
   public IPage<TaskVO> selectTaskPage(IPage<TaskVO> page, TaskVO task) {
@@ -116,7 +134,7 @@
//      if (AuthUtil.getUserAccount().equals("18879306957")) {
//         task.setCommunityCode("361102003027");
//      }
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class,task);
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class, task);
      if (null != task.getReportType() && task.getReportType() == 1) {
         // 查询取保候审任务列表(人房相关)
         return page.setRecords(baseMapper.selectTaskPageByPerson(page, task, commonParamSet.getRegionChildCodesList(),
@@ -129,11 +147,13 @@
   @Override
   public IPage<TaskVO> getBailReportingPage(IPage<TaskVO> page, TaskVO task) {
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class, task);
      // 民警角色
      if (AuthUtil.getUserRole().equals("mj")) {
         CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class, task);
         task.setUserId(AuthUtil.getUserId());
         return page.setRecords(baseMapper.selectTaskPageBy(page, task, commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator()));
         return page.setRecords(baseMapper.selectTaskPageBy(page, task,
            commonParamSet.getRegionChildCodesList(),
            commonParamSet.getIsAdministrator()));
      } else {
         if (AuthUtil.getUserAccount().equals("18879306957")) {
            task.setCommunityCode("361102003027");
@@ -143,7 +163,12 @@
            task.setUserId(AuthUtil.getUserId());
         }
         // 非民警角色
         List<TaskVO> taskVOS = baseMapper.getBailReportingPage(page, task);
         List<TaskVO> taskVOS = baseMapper.getBailReportingPage(page,
            task,
            commonParamSet.getRegionChildCodesList(),
            commonParamSet.getGridCodeList(),
            commonParamSet.getIsAdministrator());
         // 返回
         return page.setRecords(taskVOS);
      }
   }
@@ -214,38 +239,48 @@
   public Object countTypeNumber(Integer roleType, String neiCode) {
      TaskVO taskVO = new TaskVO();
      Map<String, Object> objectObjectHashMap = new HashMap<>();
      taskVO.setStatus(1);
      taskVO.setUserId(roleType > 0 ? AuthUtil.getUserId() : null);
      taskVO.setStatus(CommonConstant.NUMBER_ONE);
      taskVO.setUserId(roleType > CommonConstant.NUMBER_ZERO ? AuthUtil.getUserId() : null);
      taskVO.setNeiCode(neiCode);
      taskVO.setReportType(5);
      taskVO.setIsDeleted(0);
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class,taskVO);
      taskVO.setReportType(CommonConstant.NUMBER_FIVE);
      taskVO.setIsDeleted(CommonConstant.NUMBER_ZERO);
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class, taskVO);
      // 标签报事-二手交易
      Integer bqsj = baseMapper.selectTaskCount(taskVO,commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(),commonParamSet.getGridCodeList());
      taskVO.setReportType(1);
      Integer bqsj = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      taskVO.setReportType(CommonConstant.NUMBER_ONE);
      // 取保候审
      Integer qbhs = baseMapper.selectTaskCount(taskVO,commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(),commonParamSet.getGridCodeList());
      Integer qbhs = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      // 报事报修
      Integer bsbx = SpringUtils.getBean(ITaskReportForRepairsService.class).getStatistics(roleType > 0 ? AuthUtil.getUserId() : null, neiCode);
      Integer bsbx = SpringUtils.getBean(ITaskReportForRepairsService.class).getStatistics(roleType > 0 ? AuthUtil.getUserId() : null, neiCode, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      // 住户审核
      Integer zhsh = iHouseholdService.statistics(roleType > 0 ? AuthUtil.getUserId() : null, neiCode,0);
      Integer zhsh = iHouseholdService.statistics(roleType > 0 ? AuthUtil.getUserId() : null, neiCode, 0);
      // 出租审核
      HouseRentalTenantVO houseRentalTenantVO = new HouseRentalTenantVO();
      houseRentalTenantVO.setNeiCode(neiCode);
      houseRentalTenantVO.setCommunityCode(neiCode);
      houseRentalTenantVO.setAuditStatus(2);
      Integer czsh = iHouseRentalService.getStatisticsCount(houseRentalTenantVO);
      // 场所审核
      Integer cssh = SpringUtils.getBean(IPlaceExtService.class).selectCount(roleType > 0 ? AuthUtil.getUserId() : null, neiCode, 1);
      Integer cssh = SpringUtils.getBean(IPlaceExtService.class).selectCount(roleType > 0 ? AuthUtil.getUserId() : null, neiCode, CommonConstant.NUMBER_ONE);
      // 走访日志
      // 查询网格员对应的网格id
      String gridCode = getGridCode();
      Integer zfrw = SpringUtils.getBean(IGridWorkLogService.class).getGridWorkCountHandleCount(gridCode, 1);
      Integer zfrw = SpringUtils.getBean(IGridWorkLogService.class).getGridWorkCountHandleCount(gridCode, CommonConstant.NUMBER_ONE);
      // 消防自查
      Integer xfzc = SpringUtils.getBean(ITaskPlaceSelfCheckService.class).getCount(neiCode, 1);
      Integer xfzc = SpringUtils.getBean(ITaskPlaceSelfCheckService.class).getCount(neiCode, CommonConstant.NUMBER_ONE, CommonConstant.NUMBER_ONE);
      // 校园检查
      Integer xyjc = SpringUtils.getBean(ITaskPlaceSelfCheckService.class).getCount(neiCode, CommonConstant.NUMBER_ONE, CommonConstant.NUMBER_TWO);
      // 消防隐患整改
      Integer xfyhzg = SpringUtils.getBean(ITaskPlaceRectificationService.class).getCount(neiCode, 1);
      Integer xfyhzg = SpringUtils.getBean(ITaskPlaceRectificationService.class).getCount(neiCode, CommonConstant.NUMBER_ONE);
      // 居住证申请
      Integer jzzsq = residencePermitApplyService.getCount(neiCode, CommonConstant.NUMBER_ONE);
      // 无诈统计
      taskVO.setReportType(6);
      Integer wztj = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      // 设置
      objectObjectHashMap.put("qbhs", qbhs);// 取保候审
      objectObjectHashMap.put("bqsj", bqsj);// 标签报事-二手交易
@@ -256,6 +291,9 @@
      objectObjectHashMap.put("zfrw", zfrw);// 走访日志
      objectObjectHashMap.put("xfzc", xfzc);// 消防自查
      objectObjectHashMap.put("xfyhzg", xfyhzg);// 消防隐患整改
      objectObjectHashMap.put("jzzsq", jzzsq);// 居住证申请
      objectObjectHashMap.put("wztj", wztj);// 无诈
      objectObjectHashMap.put("xyjc", xyjc);// 校园检查
      return objectObjectHashMap;
   }
@@ -279,9 +317,9 @@
      taskVO.setUserId(AuthUtil.getUserId());
      taskVO.setFrequency(1);
      taskVO.setStatus(1);
      Integer disposable = baseMapper.selectTaskCount(taskVO,null,null,null);
      Integer disposable = baseMapper.selectTaskCount(taskVO, null, null, null);
      taskVO.setFrequency(2);
      Integer periodicity = baseMapper.selectTaskCount(taskVO,null,null,null);
      Integer periodicity = baseMapper.selectTaskCount(taskVO, null, null, null);
      objectObjectHashMap.put("disposable", disposable);
      objectObjectHashMap.put("periodicity", periodicity);
      return objectObjectHashMap;
@@ -594,33 +632,160 @@
   }
   /**
    * 任务审核
    * @param task
    * @return
    * 审核任务。
    * 根据任务的报告类型,更新相应的任务状态,并触发相应的事件更新。
    *
    * @param task 任务实体,包含任务信息和报告类型。
    * @return 返回审核结果,成功为true,失败为false。
    */
   @Override
   public Boolean examine(TaskEntity task) {
      // 二手交易
      if (task.getReportType().equals(5)) {
         boolean b = updateById(task);
         if (b) {
            ITaskLabelReportingEventService bean = SpringUtils.getBean(ITaskLabelReportingEventService.class);
            return bean.update(Wrappers.<TaskLabelReportingEventEntity>lambdaUpdate()
               .set(TaskLabelReportingEventEntity::getConfirmFlag, task.getStatus())
               .eq(TaskLabelReportingEventEntity::getTaskId, task.getId()));
         }
      if (task == null || task.getReportType() == null) {
         // 检查任务和报告类型是否为空,若为空则直接返回false
         return false;
      }
      // 根据任务报告类型,更新任务状态并触发相应的事件更新
      boolean result = updateById(task);
      if (!result) {
         return false;
      }
      if (task.getReportType().equals(CommonConstant.REPORT_TYPE_SECONDHAND_TRADE)) {
         // 如果报告类型匹配,尝试更新任务标签报告事件状态
         ITaskLabelReportingEventService bean = SpringUtils.getBean(ITaskLabelReportingEventService.class);
         return bean.update(Wrappers.<TaskLabelReportingEventEntity>lambdaUpdate()
            .set(TaskLabelReportingEventEntity::getConfirmFlag, task.getStatus())
            .eq(TaskLabelReportingEventEntity::getTaskId, task.getId()));
      } else if (task.getReportType().
         equals(CommonConstant.REPORT_TYPE_FIRE_INSPECTION)) {
         // 如果报告类型为消防自查,尝试更新消防自查状态
         ITaskPlaceSelfCheckService bean = SpringUtils.getBean(ITaskPlaceSelfCheckService.class);
         return bean.update(Wrappers.<TaskPlaceSelfCheckEntity>lambdaUpdate()
            .set(TaskPlaceSelfCheckEntity::getStatus, task.getStatus())
            .eq(TaskPlaceSelfCheckEntity::getTaskId, task.getId()));
      } else if (task.getReportType().equals(CommonConstant.REPORT_TYPE_NO_FRAUD)) {
         // 如果报告类型为无诈,尝试更新无诈报告状态
         ITaskNoFraudReportingService bean = SpringUtils.getBean(ITaskNoFraudReportingService.class);
         return bean.update(Wrappers.<TaskNoFraudReportingEntity>lambdaUpdate()
            .set(TaskNoFraudReportingEntity::getStatus, task.getStatus())
            .eq(TaskNoFraudReportingEntity::getTaskId, task.getId()));
      }
      // 消防只查
      if (task.getReportType().equals(2)) {
         boolean b = updateById(task);
         if (b) {
            ITaskPlaceSelfCheckService bean = SpringUtils.getBean(ITaskPlaceSelfCheckService.class);
            return bean.update(Wrappers.<TaskPlaceSelfCheckEntity>lambdaUpdate()
               .set(TaskPlaceSelfCheckEntity::getStatus, task.getStatus())
               .eq(TaskPlaceSelfCheckEntity::getTaskId, task.getId()));
      // 如果没有匹配的报告类型,返回false
      return false;
   }
   /**
    * 创建居住证发送短信任务。
    * 该方法首先计算六个月前的日期,然后查询在该日期之后申请的居住证任务实体列表,
    * 并对这些实体发送短信通知。
    *
    * @param param 传递给发送短信任务的参数,具体用途根据实现而定。
    * @return 返回一个布尔值,通常用于指示任务是否创建成功。
    * 根据实际逻辑需要调整返回值,当前示例中返回值始终为false。
    */
   @Override
   public boolean createResidenceSendSms(String param) {
      try {
         // 计算六个月前的日期
         LocalDate sixMonthsAgo = getSixMonthsAgo();
         // 查询需要发送短信的居住证申请实体列表
         List<TaskResidencePermitApplyEntity> entitiesToSms = queryEntitiesToSms(sixMonthsAgo);
         // 发送短信并更新相关实体状态
         sendSmsAndUpdateEntities(entitiesToSms);
      } catch (Exception e) {
         // 处理发送短信过程中可能出现的异常,如记录日志
         logger.error("Error occurred while sending SMS: " + e.getMessage(), e);
         return false; // 根据异常处理逻辑决定是否返回false或其他值
      }
      return false; // 根据实际逻辑调整返回值
   }
   @Override
   public boolean createSchoolSendSms(String param) {
      try {
         // 计算当前时间加一天
         LocalDate endDate = LocalDate.now().plusDays(1);
         // 创建表示一天结束时刻的 LocalTime 对象
         LocalTime endOfDayTime = LocalTime.of(23, 59, 59, 999_000_000); // 纳秒精度为999毫微秒,接近最大值
         // 合并得到加一天后的最后时刻的 LocalDateTime
         LocalDateTime endTime = LocalDateTime.of(endDate, endOfDayTime);
         ITaskPlaceSelfCheckService taskPlaceSelfCheckService = SpringUtils.getBean(ITaskPlaceSelfCheckService.class);
         // 查询整改完毕且未发送短信的记录
         List<TaskPlaceSelfCheckEntity> list = taskPlaceSelfCheckService.list(new LambdaQueryWrapper<TaskPlaceSelfCheckEntity>()
            .eq(TaskPlaceSelfCheckEntity::getRectificationFlag, CommonConstant.NUMBER_TWO)
            .eq(TaskPlaceSelfCheckEntity::getSendSmsFlag, CommonConstant.NUMBER_ONE)
            .le(TaskPlaceSelfCheckEntity::getRectificationEndTime, endTime));
         if (list == null || list.isEmpty()) {
            return false;
         }
         // 用于收集成功发送短信并需更新状态的实体列表
         List<TaskPlaceSelfCheckEntity> updatedEntities = new ArrayList<>();
         for (TaskPlaceSelfCheckEntity updatedEntity : list) {
            // 尝试发送短信,发送成功则更新实体状态
            Boolean smsSent = smsSendService.sendNotice(SMS_TEMPLATE_ID, updatedEntity.getId());
            if (smsSent) {
               updatedEntity.setSendSmsFlag("2"); // 标记为已发送短信
               updatedEntities.add(updatedEntity); // 加入到需更新的实体列表中
            }
         }
         // 若存在需更新的实体,则批量更新这些实体的状态
         if (!updatedEntities.isEmpty()) {
            taskPlaceSelfCheckService.updateBatchById(updatedEntities);
         }
      } catch (Exception e) {
         // 处理发送短信过程中可能出现的异常,如记录日志
         logger.error("Error occurred while sending SMS: " + e.getMessage(), e);
         return false; // 根据异常处理逻辑决定是否返回false或其他值
      }
      return false; // 根据实际逻辑调整返回值
   }
   /**
    * 获取六个月前的日期。
    * 该方法不接受任何参数,仅返回当前日期减去六个月的结果。
    *
    * @return 返回一个LocalDate对象,表示六个月前的日期。
    */
   private LocalDate getSixMonthsAgo() {
      // 获取当前日期并减去6个月
      return LocalDate.now().minus(6, ChronoUnit.MONTHS);
   }
   /**
    * 查询需要发送短信的居住证申请实体列表。
    *
    * @param sixMonthsAgo 六个月前的日期,用于筛选确认时间在六个月之前的申请记录。
    * @return 返回一个包含所有符合条件的居住证申请实体的列表。
    */
   private List<TaskResidencePermitApplyEntity> queryEntitiesToSms(LocalDate sixMonthsAgo) {
      // 使用LambdaQueryWrapper构建查询条件,筛选出确认状态为2(即确认),且未发送过短信(发送短信标志为1),确认时间在指定日期(sixMonthsAgo)之前的所有申请记录。
      return residencePermitApplyService.list(new LambdaQueryWrapper<TaskResidencePermitApplyEntity>()
         .eq(TaskResidencePermitApplyEntity::getConfirmFlag, 2)
         .eq(TaskResidencePermitApplyEntity::getSendSmsFlag, 1)
         .le(TaskResidencePermitApplyEntity::getConfirmTime, sixMonthsAgo));
   }
   /**
    * 发送短信并更新实体状态
    *
    * @param entitiesToSms 需要发送短信并更新状态的实体列表,每个实体代表一个居住证申请任务。
    *                      实体列表中每个元素应包含至少一个ID属性,用于标识申请任务。
    */
   private void sendSmsAndUpdateEntities(List<TaskResidencePermitApplyEntity> entitiesToSms) {
      List<TaskResidencePermitApplyEntity> updatedEntities = new ArrayList<>(); // 用于收集成功发送短信并需更新状态的实体列表
      for (TaskResidencePermitApplyEntity entity : entitiesToSms) {
         // 尝试发送短信,发送成功则更新实体状态
         Boolean smsSent = smsSendService.sendNotice(SMS_TEMPLATE_ID, entity.getId());
         if (smsSent) {
            entity.setSendSmsFlag(2); // 标记为已发送短信
            updatedEntities.add(entity); // 加入到需更新的实体列表中
         }
      }
      return false;
      // 若存在需更新的实体,则批量更新这些实体的状态
      if (!updatedEntities.isEmpty()) {
         residencePermitApplyService.updateBatchById(updatedEntities);
      }
   }
}