lin
2024-04-15 5b33d886cb6da7a8b0f66f0c0bb0a3d0b2b19d6a
src/main/java/org/springblade/modules/task/service/impl/TaskServiceImpl.java
@@ -23,7 +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.constant.CommonConstant;
import org.springblade.common.constant.DictConstant;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.SpringUtils;
@@ -41,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.*;
@@ -52,6 +56,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
@@ -63,6 +69,8 @@
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements ITaskService {
   private Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
   @Autowired
   private IPlaceService placeService;
@@ -87,6 +95,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) {
@@ -115,7 +132,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(),
@@ -225,22 +242,24 @@
      taskVO.setNeiCode(neiCode);
      taskVO.setReportType(5);
      taskVO.setIsDeleted(0);
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class,taskVO);
      CommonParamSet commonParamSet = new CommonParamSet().invoke(TaskVO.class, taskVO);
      // 标签报事-二手交易
      Integer bqsj = baseMapper.selectTaskCount(taskVO,commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(),commonParamSet.getGridCodeList());
      Integer bqsj = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      taskVO.setReportType(1);
      // 取保候审
      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);
@@ -253,7 +272,11 @@
      // 消防隐患整改
      Integer xfyhzg = SpringUtils.getBean(ITaskPlaceRectificationService.class).getCount(neiCode, 1);
      // 居住证申请
      Integer jzzsq = SpringUtils.getBean(ITaskResidencePermitApplyService.class).getCount(neiCode, 1);
      Integer jzzsq = residencePermitApplyService.getCount(neiCode, 1);
      // 无诈统计
      taskVO.setReportType(6);
      Integer wztj = baseMapper.selectTaskCount(taskVO, commonParamSet.getRegionChildCodesList(),
         commonParamSet.getIsAdministrator(), commonParamSet.getGridCodeList());
      // 设置
      objectObjectHashMap.put("qbhs", qbhs);// 取保候审
      objectObjectHashMap.put("bqsj", bqsj);// 标签报事-二手交易
@@ -265,6 +288,7 @@
      objectObjectHashMap.put("xfzc", xfzc);// 消防自查
      objectObjectHashMap.put("xfyhzg", xfyhzg);// 消防隐患整改
      objectObjectHashMap.put("jzzsq", jzzsq);// 居住证申请
      objectObjectHashMap.put("wztj", wztj);// 无诈
      return objectObjectHashMap;
   }
@@ -288,9 +312,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;
@@ -603,33 +627,121 @@
   }
   /**
    * 任务审核
    * @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; // 根据实际逻辑调整返回值
   }
   /**
    * 获取六个月前的日期。
    * 该方法不接受任何参数,仅返回当前日期减去六个月的结果。
    *
    * @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);
         }
      }
}