|
@@ -0,0 +1,242 @@
|
|
|
+package com.management.platform.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.management.platform.entity.SysDict;
|
|
|
+import com.management.platform.entity.User;
|
|
|
+import com.management.platform.entity.VisitPlan;
|
|
|
+import com.management.platform.mapper.SysDictMapper;
|
|
|
+import com.management.platform.mapper.UserMapper;
|
|
|
+import com.management.platform.mapper.VisitPlanMapper;
|
|
|
+import com.management.platform.service.VisitPlanService;
|
|
|
+import com.management.platform.time.VisitPlanDelayHandler;
|
|
|
+import com.management.platform.time.VisitPlanDelayItem;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class VisitPlanServiceImpl extends ServiceImpl<VisitPlanMapper, VisitPlan> implements VisitPlanService {
|
|
|
+
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
+
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private VisitPlanMapper visitPlanMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SysDictMapper sysDictMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg addVisitPlan(VisitPlan visitPlan, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ Date now = new Date();
|
|
|
+ User user= userMapper.selectById(request.getHeader("token"));
|
|
|
+ if(null == user){
|
|
|
+ httpRespMsg.setError("用户不存在");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(visitPlan.getVisitTime().before(now)){
|
|
|
+ httpRespMsg.setError("拜访时间不能在当前时段之前");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SysDict> remindTypeList = sysDictMapper.selectList(new LambdaQueryWrapper<SysDict>()
|
|
|
+ .eq(SysDict::getCode, "RemindType")
|
|
|
+ .eq(SysDict::getCompanyId, user.getCompanyId())
|
|
|
+ );
|
|
|
+ if(CollectionUtils.isEmpty(remindTypeList)){
|
|
|
+ httpRespMsg.setError("公司未设置提醒类型字典项,请联系管理员");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ Map<Integer, SysDict> timeTypeMap = remindTypeList.stream().collect(Collectors.toMap(SysDict::getId, t->t));
|
|
|
+ SysDict timeTypeDict = timeTypeMap.get(visitPlan.getRemindType());
|
|
|
+ if(null == timeTypeDict){
|
|
|
+ httpRespMsg.setError("提醒类型不存在,请重新填写");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long delayTime = 0L;
|
|
|
+ Long startTimeMilliSec = 0L;
|
|
|
+ Long endTimeMilliSec = 0L;
|
|
|
+ if(timeTypeDict.getName().equals("不提醒")){
|
|
|
+ //不加入队列,不需要提醒
|
|
|
+ visitPlan.setRemindState(2);
|
|
|
+ } else if (timeTypeDict.getName().equals("自定义时间")) {
|
|
|
+ //重新计算delayTime 自定义时间-当前时间
|
|
|
+ if(visitPlan.getRemindTime().before(now)
|
|
|
+ || visitPlan.getRemindTime().after(visitPlan.getVisitTime())){
|
|
|
+ httpRespMsg.setError("提醒时间不能在当前时段之前或拜访时间之后");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ //毫秒
|
|
|
+ startTimeMilliSec = now.getTime();
|
|
|
+ endTimeMilliSec = visitPlan.getRemindTime().getTime();
|
|
|
+ delayTime = endTimeMilliSec - startTimeMilliSec;
|
|
|
+ }else{
|
|
|
+ startTimeMilliSec = now.getTime();
|
|
|
+ endTimeMilliSec = visitPlan.getVisitTime().getTime()-Integer.parseInt(timeTypeDict.getExt1()) * 1000L;
|
|
|
+ if(endTimeMilliSec < startTimeMilliSec){
|
|
|
+ httpRespMsg.setError("提醒时间不能在当前时段之前或拜访时间之后");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ delayTime = endTimeMilliSec-startTimeMilliSec;
|
|
|
+ }
|
|
|
+
|
|
|
+ visitPlan.setCreateBy(user.getId());
|
|
|
+ visitPlanMapper.insert(visitPlan);
|
|
|
+
|
|
|
+ //加入执行队列
|
|
|
+ if(!timeTypeDict.getName().equals("不提醒")){
|
|
|
+ VisitPlanDelayHandler tool = new VisitPlanDelayHandler();
|
|
|
+ VisitPlan taskPlan = visitPlanMapper.selectById(visitPlan.getId());
|
|
|
+ VisitPlanDelayItem item = new VisitPlanDelayItem(delayTime
|
|
|
+ ,startTimeMilliSec
|
|
|
+ ,endTimeMilliSec
|
|
|
+ ,taskPlan.getId(),taskPlan);
|
|
|
+ tool.addItem(item);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ System.out.println("当前时间: "+format.format(new Date()));
|
|
|
+ threadPoolTaskExecutor.execute(tool);
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg finishVisitPlan(Long planId, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+
|
|
|
+ VisitPlanDelayHandler tool = new VisitPlanDelayHandler();
|
|
|
+ tool.removeIfExist(planId);
|
|
|
+
|
|
|
+ //完成计划则修改:1、是否到达提醒时间为不需要提醒 2、完成状态为 已完成
|
|
|
+ visitPlanMapper.update(null,new LambdaUpdateWrapper<VisitPlan>()
|
|
|
+ .set(VisitPlan::getRemindState, 2)
|
|
|
+ .set(VisitPlan::getFinishState,1)
|
|
|
+ .eq(VisitPlan::getId,planId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg delayVisitPlan(Long planId, Date newVisitTime, HttpServletRequest request) {
|
|
|
+ //删除原队列,计算时间后重新纳入队列
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ Date now = new Date();
|
|
|
+ User user= userMapper.selectById(request.getHeader("token"));
|
|
|
+ if(null == user){
|
|
|
+ httpRespMsg.setError("用户不存在");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ if(newVisitTime.before(now)){
|
|
|
+ httpRespMsg.setError("拜访时间不能在当前时段之前");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ VisitPlan taskPlan = visitPlanMapper.selectById(planId);
|
|
|
+ SysDict timeTypeDict = sysDictMapper.selectOne(new LambdaQueryWrapper<SysDict>()
|
|
|
+ .eq(SysDict::getCode, "RemindType")
|
|
|
+ .eq(SysDict::getId, taskPlan.getRemindType())
|
|
|
+ .eq(SysDict::getCompanyId, user.getCompanyId())
|
|
|
+ );
|
|
|
+
|
|
|
+ Long delayTime = 0L;
|
|
|
+ Long startTimeMilliSec = 0L;
|
|
|
+ Long endTimeMilliSec = 0L;
|
|
|
+ taskPlan.setVisitTime(newVisitTime);
|
|
|
+ if(timeTypeDict.getName().equals("不提醒")){
|
|
|
+ //不加入队列,不修改状态,只修改拜访时间
|
|
|
+ } else if (timeTypeDict.getName().equals("自定义时间")) {
|
|
|
+ //重新计算delayTime 自定义时间-当前时间
|
|
|
+ if(taskPlan.getRemindTime().before(now)
|
|
|
+ || taskPlan.getRemindTime().after(newVisitTime)){
|
|
|
+ httpRespMsg.setError("提醒时间不能在当前时段之前或拜访时间之后");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ //毫秒
|
|
|
+ startTimeMilliSec = now.getTime();
|
|
|
+ endTimeMilliSec = taskPlan.getRemindTime().getTime();
|
|
|
+ delayTime = endTimeMilliSec - startTimeMilliSec;
|
|
|
+ }else{
|
|
|
+ startTimeMilliSec = now.getTime();
|
|
|
+ endTimeMilliSec = newVisitTime.getTime()-Integer.parseInt(timeTypeDict.getExt1()) * 1000L;
|
|
|
+ if(endTimeMilliSec < startTimeMilliSec){
|
|
|
+ httpRespMsg.setError("提醒时间不能在当前时段之前或拜访时间之后");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ delayTime = endTimeMilliSec-startTimeMilliSec;
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除队列
|
|
|
+ VisitPlanDelayHandler tool = new VisitPlanDelayHandler();
|
|
|
+ tool.removeIfExist(planId);
|
|
|
+ //重新添加
|
|
|
+ VisitPlanDelayItem item = new VisitPlanDelayItem(delayTime
|
|
|
+ ,startTimeMilliSec
|
|
|
+ ,endTimeMilliSec
|
|
|
+ ,taskPlan.getId(),taskPlan);
|
|
|
+ tool.addItem(item);
|
|
|
+
|
|
|
+ visitPlanMapper.update(null,new LambdaUpdateWrapper<VisitPlan>()
|
|
|
+ .set(VisitPlan::getVisitTime,newVisitTime)
|
|
|
+ .eq(VisitPlan::getId,planId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg delVisitPlan(Long planId, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ User user= userMapper.selectById(request.getHeader("token"));
|
|
|
+ if(null == user){
|
|
|
+ httpRespMsg.setError("用户不存在");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ //删除队列
|
|
|
+ VisitPlanDelayHandler tool = new VisitPlanDelayHandler();
|
|
|
+ tool.removeIfExist(planId);
|
|
|
+
|
|
|
+ //删除数据
|
|
|
+ visitPlanMapper.delete(new LambdaQueryWrapper<VisitPlan>()
|
|
|
+ .eq(VisitPlan::getId,planId)
|
|
|
+ );
|
|
|
+
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg getPageVisitPlan(Integer pageIndex, Integer pageSize, Date calenderTime, HttpServletRequest request) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ User user= userMapper.selectById(request.getHeader("token"));
|
|
|
+ if(null == user){
|
|
|
+ httpRespMsg.setError("用户不存在");
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+ IPage<VisitPlan> page = new Page<>(pageIndex,pageSize);
|
|
|
+ page = visitPlanMapper.selectPage(page, new LambdaQueryWrapper<VisitPlan>()
|
|
|
+ .eq(VisitPlan::getVisitTime, calenderTime)
|
|
|
+ .eq(VisitPlan::getCreateBy, user.getId())
|
|
|
+ );
|
|
|
+ httpRespMsg.setData(page);
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+}
|