lin
2024-04-19 73ceaaa73dd6b73808aac425c70e0d9184f9f415
src/main/java/org/springblade/modules/task/service/impl/TaskServiceImpl.java
@@ -23,6 +23,8 @@
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.constant.CommonConstant;
import org.springblade.common.constant.DictConstant;
@@ -42,6 +44,7 @@
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.*;
@@ -53,6 +56,10 @@
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) {
@@ -221,22 +239,23 @@
   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);
      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);
      taskVO.setReportType(CommonConstant.NUMBER_ONE);
      // 取保候审
      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);
      // 出租审核
@@ -245,17 +264,19 @@
      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 = SpringUtils.getBean(ITaskResidencePermitApplyService.class).getCount(neiCode, 1);
      Integer jzzsq = residencePermitApplyService.getCount(neiCode, CommonConstant.NUMBER_ONE);
      // 无诈统计
      taskVO.setReportType(6);
      Integer wztj = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
@@ -272,6 +293,7 @@
      objectObjectHashMap.put("xfyhzg", xfyhzg);// 消防隐患整改
      objectObjectHashMap.put("jzzsq", jzzsq);// 居住证申请
      objectObjectHashMap.put("wztj", wztj);// 无诈
      objectObjectHashMap.put("xyjc", xyjc);// 校园检查
      return objectObjectHashMap;
   }
@@ -652,4 +674,118 @@
      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); // 加入到需更新的实体列表中
         }
      }
      // 若存在需更新的实体,则批量更新这些实体的状态
      if (!updatedEntities.isEmpty()) {
         residencePermitApplyService.updateBatchById(updatedEntities);
      }
   }
}