package com.ld.igds.timer.service;
|
|
import java.util.*;
|
|
import com.ld.igds.models.Device;
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang.StringUtils;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Repository;
|
import org.springframework.util.Assert;
|
|
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
|
import com.bstek.bdf2.job.model.JobCalendarRelation;
|
import com.bstek.bdf2.job.model.JobDefinition;
|
import com.bstek.bdf2.job.model.JobHistory;
|
import com.bstek.bdf2.job.model.JobParameter;
|
import com.bstek.bdf2.job.model.JobState;
|
import com.ld.igds.constant.BizType;
|
import com.ld.igds.constant.Constant;
|
import com.ld.igds.models.Timing;
|
import com.ld.igds.models.TimingDepot;
|
import com.ld.igds.models.TimingDevice;
|
import com.ld.igds.timer.BdfJobService;
|
import com.ld.igds.timer.service.ITimerService;
|
import com.ld.igds.util.ContextUtil;
|
|
@Slf4j
|
@Repository(ITimerService.BEAN_ID)
|
public class TimperServiceImpl extends HibernateDao implements ITimerService {
|
|
@Autowired
|
private BdfJobService bdfJobService;
|
|
@Override
|
public List<Timing> getTiming(Map<String, Object> param) throws Exception {
|
StringBuilder hql = new StringBuilder();
|
hql.append(" from " + Timing.class.getName()
|
+ " where companyId = :companyId ");
|
Map<String, Object> arg = new HashMap<String, Object>();
|
if (null != param) {
|
bulidHql(hql, param, arg);
|
}
|
arg.put("companyId", ContextUtil.getCompanyId());
|
|
hql.append(" order by updateTime ");
|
|
List<Timing> list = this.query(hql.toString(), arg);
|
|
if (null == list || list.isEmpty())
|
return list;
|
|
// 添加job信息
|
for (Timing tim : list) {
|
tim.setJob(bdfJobService.getJobById(tim.getId()));
|
}
|
|
return list;
|
}
|
|
@Override
|
public List<TimingDevice> getDevieByTimerId(String timId) {
|
if (StringUtils.isEmpty(timId))
|
return null;
|
|
Map<String, Object> p = new HashMap<String, Object>();
|
String hql = " from "
|
+ TimingDevice.class.getName()
|
+ " where companyId = :companyId and timId =:timId order by depotId";
|
p.put("companyId", ContextUtil.getCompanyId());
|
p.put("timId", timId);
|
|
return this.query(hql, p);
|
}
|
|
private void bulidHql(StringBuilder hql, Map<String, Object> param,
|
Map<String, Object> arg) {
|
String depotId = (String) param.get("depotId");
|
if (StringUtils.isNotBlank(depotId)) {
|
hql.append(" and depotId = :depotId ");
|
arg.put("depotId", depotId);
|
}
|
|
String type = (String) param.get("type");
|
if (StringUtils.isNotBlank(type)) {
|
hql.append(" and type = :type ");
|
arg.put("type", type);
|
}
|
|
}
|
|
@Override
|
public void addTimer(Timing tim) throws Exception {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
//更新定时规则
|
String cron = Constant.CRON_DAY;
|
if (tim.getTimType().equals(Constant.TIM_TYPE_DAY)) {
|
cron = cron.replace("H", tim.getHour() + "");
|
cron = cron.replace("M", tim.getMinute() + "");
|
}
|
|
if (tim.getTimType().equals(Constant.TIM_TYPE_WEEK)) {
|
cron = Constant.CRON_WEEK;
|
cron = cron.replace("H", tim.getHour() + "");
|
cron = cron.replace("M", tim.getMinute() + "");
|
cron = cron.replace("W", tim.getWeek());
|
}
|
|
if (tim.getTimType().equals(Constant.TIM_TYPE_HOUR)) {
|
cron = Constant.CRON_HOUR;
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(tim.getAppointTime());
|
cron = cron.replace("S", cal.get(Calendar.SECOND) + "");
|
cron = cron.replace("M", cal.get(Calendar.MINUTE) + "");
|
cron = cron.replace("H", cal.get(Calendar.HOUR) + "");
|
cron = cron.replace("D", cal.get(Calendar.DATE) + "");
|
cron = cron.replace("&", cal.get(Calendar.MONTH) + 1 + "");
|
}
|
tim.setCron(cron);
|
|
tim.setUpdateTime(new Date());
|
tim.setUpdateUser(ContextUtil.getLoginUserCName());
|
|
// 新增数据
|
if (null == tim.getId()) {
|
tim.setId(ContextUtil.getCurTimeMillis());
|
tim.setCompanyId(ContextUtil.getCompanyId());
|
//新增job
|
tim.setJob(this.saveJob(tim, true));
|
session.save(tim);
|
log.info("==========" + tim.getUpdateUser() + "添加仓库定时配置……" + "==========");
|
} else { //更新数据
|
//更新job
|
tim.setJob(this.saveJob(tim, false));
|
session.update(tim);
|
log.info("==========" + tim.getUpdateUser() + "更新仓库定时配置……" + "==========");
|
}
|
|
this.updateTimDevice(tim, session);
|
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
// 处理定时任务中的设备信息
|
private void updateTimDevice(Timing tim, Session session) {
|
if (null == tim.getDevices())
|
return;
|
// 删除现有的设备信息
|
String hql = " delete " + TimingDevice.class.getName()
|
+ " where timId=:timId";
|
session.createQuery(hql).setString("timId", tim.getId())
|
.executeUpdate();
|
|
for (TimingDevice device : tim.getDevices()) {
|
device.setId(ContextUtil.getUUID());
|
device.setTimId(tim.getId());
|
device.setCompanyId(tim.getCompanyId());
|
device.setDepotId(tim.getDepotId());
|
session.save(device);
|
}
|
}
|
|
private JobDefinition saveJob(Timing tim, boolean flag) throws Exception {
|
String type = tim.getType();
|
if (StringUtils.isEmpty(type)) {
|
throw new Exception("没有获获取到检测类型,请重新刷新页面添加……");
|
}
|
JobDefinition job = new JobDefinition();
|
job.setId(tim.getId());
|
job.setCompanyId(tim.getCompanyId());
|
job.setCronExpression(tim.getCron());
|
job.setState(JobState.ready);
|
job.setDesc("业务人员配置,请勿操作!");
|
|
if (type.equals(BizType.GRAIN.getCode())) {
|
job.setBeanId(Constant.JOB_BEAN_GRAIN);
|
job.setName(tim.getName() + "-粮情定时检测");
|
}
|
|
if (type.equals(BizType.GAS.getCode())) {
|
job.setBeanId(Constant.JOB_BEAN_GAS);
|
job.setName(tim.getName() + "-气体定时检测");
|
}
|
|
if (type.equals(BizType.PEST.getCode())) {
|
job.setBeanId(Constant.JOB_BEAN_PEST);
|
job.setName(tim.getName() + "-虫害定时检测");
|
}
|
|
if ("lamp".equals(type)) {
|
job.setBeanId(Constant.JOB_BEAN_LAMP);
|
job.setName(tim.getName() + "-照明定时控制");
|
}
|
|
if(flag){ //新增
|
bdfJobService.saveJob(job);
|
}else { //更新
|
bdfJobService.updateJob(job);
|
}
|
return job;
|
}
|
|
@Override
|
public void delTimer(String id) {
|
Assert.notNull(id, "定时配置ID为空");
|
Session session = this.getSessionFactory().openSession();
|
try {
|
String hql = " delete " + Timing.class.getName() + " where id=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = "delete " + JobDefinition.class.getName() + " where id=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = "delete " + JobHistory.class.getName() + " where jobId=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = "delete " + JobCalendarRelation.class.getName()
|
+ " where jobId=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = "delete " + JobParameter.class.getName() + " where jobId=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = " delete " + TimingDevice.class.getName()
|
+ " where timId=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
hql = " delete " + TimingDepot.class.getName() + " where timId=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
@Override
|
public List<TimingDepot> getDepotByTimerId(String timId) {
|
if (null == timId)
|
return null;
|
String hql = " from " + TimingDepot.class.getName()
|
+ " where timId = :timId";
|
Map<String, Object> args = new HashMap<String, Object>();
|
args.put("timId", timId);
|
return this.query(hql, args);
|
}
|
|
@Override
|
public void saveDepot(List<TimingDepot> items) {
|
if (null == items)
|
return;
|
Session session = this.getSessionFactory().openSession();
|
try {
|
for (TimingDepot item : items) {
|
if (null == item.getId()) {
|
item.setId(ContextUtil.getUUID());
|
session.save(item);
|
} else {
|
session.update(item);
|
}
|
}
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
@Override
|
public String delDepotById(String id) {
|
if (null == id)
|
return "没有获取到ID无法删除";
|
Session session = this.getSessionFactory().openSession();
|
try {
|
String hql = " delete " + TimingDepot.class.getName()
|
+ " where id=:id";
|
session.createQuery(hql).setString("id", id).executeUpdate();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
@Override
|
public void delDeviceByTimerId(String timId, String depotId) {
|
Session session = null;
|
try {
|
session = this.getSessionFactory().openSession();
|
String hql = " delete from " + TimingDevice.class.getName()
|
+ " where timId=:timId and depotId=:depotId ";
|
session.createQuery(hql).setString("timId", timId)
|
.setString("depotId", depotId).executeUpdate();
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
|
}
|
|
@Override
|
public void addTimingDevice(TimingDevice timingDevice) {
|
Session session = null;
|
try {
|
session = this.getSessionFactory().openSession();
|
session.save(timingDevice);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
|
}
|
@Override
|
public String delSettings(List<TimingDevice> list) {
|
for (TimingDevice device : list) {
|
Session session = null;
|
try {
|
session = this.getSessionFactory().openSession();
|
session.delete(device);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
return null;
|
}
|
@Override
|
public String addSettings(List<Device> list, String timId) {
|
List<TimingDevice> timingDevices = new ArrayList<>();
|
String depotId = list.get(0).getDepotId();
|
// 首先过滤掉未操作数据
|
/*
|
* for (Device device : list) {
|
* if(device.getTargetStatus().equals("ZERO")){ list.remove(device); } }
|
*/
|
for (int i = 0; i < list.size(); i++) {
|
if (list.get(i).getTargetStatus() == null || list.get(i).getTargetStatus().equals("ZERO") || list.get(i).getTargetStatus().equals("")) {
|
//list.remove(list.get(i));
|
} else {
|
TimingDevice timingDevice = new TimingDevice();
|
timingDevice.setId(UUID.randomUUID().toString());
|
timingDevice.setDepotId(list.get(i).getDepotId());
|
timingDevice.setDeviceId(list.get(i).getId());
|
timingDevice.setTimId(timId);
|
timingDevice.setCompanyId(ContextUtil.getCompanyId());
|
timingDevice.setTargetStatus(list.get(i).getTargetStatus());
|
timingDevices.add(timingDevice);
|
}
|
}
|
//System.out.println(timingDevices.toString());
|
|
// 其次新增配置
|
for (TimingDevice timingDevice : timingDevices) {
|
//先删除已经有的,避免重复
|
delDeviceByTimerId(timId,timingDevice.getDeviceId());
|
addTimingDevice(timingDevice);
|
}
|
return null;
|
}
|
}
|