From 1e729f1465e4bbbb012509e4f85570070de7addb Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Wed, 21 May 2025 09:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/bweb/config/AuthConfig.java | 1 + .../com/bweb/controller/CmsController.java | 53 ++++---- .../com/hfkj/consumer/CmsOfflineConsumer.java | 39 ++++++ .../com/hfkj/consumer/CmsOnlineConsumer.java | 38 ++++++ .../java/com/hfkj/dao/CmsContentMapper.java | 12 +- .../com/hfkj/dao/CmsContentSqlProvider.java | 28 ++++ .../main/java/com/hfkj/entity/CmsContent.java | 32 +++++ .../com/hfkj/entity/CmsContentExample.java | 120 ++++++++++++++++++ .../main/java/com/hfkj/mqtopic/TaskTopic.java | 4 + .../hfkj/service/cms/CmsContentService.java | 23 +++- .../cms/impl/CmsContentServiceImpl.java | 93 +++++++++++++- 11 files changed, 412 insertions(+), 31 deletions(-) create mode 100644 schedule/src/main/java/com/hfkj/consumer/CmsOfflineConsumer.java create mode 100644 schedule/src/main/java/com/hfkj/consumer/CmsOnlineConsumer.java diff --git a/bweb/src/main/java/com/bweb/config/AuthConfig.java b/bweb/src/main/java/com/bweb/config/AuthConfig.java index 26c77f0..3f477e9 100644 --- a/bweb/src/main/java/com/bweb/config/AuthConfig.java +++ b/bweb/src/main/java/com/bweb/config/AuthConfig.java @@ -100,6 +100,7 @@ public class AuthConfig implements WebMvcConfigurer { .excludePathPatterns("/tuanYou/*") .excludePathPatterns("/merchantChainBrandAccount") .excludePathPatterns("/userSpread/*") + .excludePathPatterns("/cms/*") ; } diff --git a/bweb/src/main/java/com/bweb/controller/CmsController.java b/bweb/src/main/java/com/bweb/controller/CmsController.java index cef6aae..75f719f 100644 --- a/bweb/src/main/java/com/bweb/controller/CmsController.java +++ b/bweb/src/main/java/com/bweb/controller/CmsController.java @@ -5,22 +5,27 @@ import com.github.pagehelper.PageInfo; import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; +import com.hfkj.common.utils.DateUtil; import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.entity.CmsContent; import com.hfkj.model.ResponseData; +import com.hfkj.mqtopic.TaskTopic; import com.hfkj.service.cms.CmsContentService; import com.hfkj.service.cms.impl.CmsContentServiceImpl; import com.hfkj.sysenum.cms.CmsStatusEnum; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -36,8 +41,10 @@ public class CmsController { private static Logger log = LoggerFactory.getLogger(CmsController.class); @Resource private CmsContentService cmsContentService; - @Resource - private RedisUtil redisUtil; + + public static void main(String[] args) { + System.out.println(new Date().compareTo(new Date())); + } @RequestMapping(value="/edit",method = RequestMethod.POST) @ResponseBody @@ -47,9 +54,18 @@ public class CmsController { if (body == null || body.getType() == null || StringUtils.isBlank(body.getImgUrl()) + || body.getStartTime() == null + || body.getEndTime() == null ) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR); } + if (body.getStartTime().compareTo(new Date()) < 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "开始时间,必须大于当前时间"); + } + if (body.getEndTime().compareTo(body.getStartTime()) < 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "结束时间,必须大于开始时间"); + } + CmsContent cmsContent = null; if (body.getId() != null) { // 查询内容 @@ -68,7 +84,10 @@ public class CmsController { cmsContent.setImgUrl(body.getImgUrl()); cmsContent.setJumpType(body.getJumpType()); cmsContent.setJumpUrl(body.getJumpUrl()); + cmsContent.setStartTime(body.getStartTime()); + cmsContent.setEndTime(body.getEndTime()); cmsContentService.editData(cmsContent); + return ResponseMsgUtil.success("操作成功"); } catch (Exception e) { @@ -84,19 +103,9 @@ public class CmsController { if (body == null || body.getId() == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR); } - // 查询内容 - CmsContent cmsContent = cmsContentService.getDetail(body.getId()); - if (cmsContent == null) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); - } - if (!CmsStatusEnum.status1.getStatus().equals(cmsContent.getStatus())) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法上线,状态错误"); - } - cmsContent.setStatus(CmsStatusEnum.status2.getStatus()); - cmsContentService.editData(cmsContent); - // 删除缓存 - redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType()); + cmsContentService.online(body.getId()); + return ResponseMsgUtil.success("操作成功"); } catch (Exception e) { @@ -112,19 +121,9 @@ public class CmsController { if (body == null || body.getId() == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR); } - // 查询内容 - CmsContent cmsContent = cmsContentService.getDetail(body.getId()); - if (cmsContent == null) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); - } - if (!CmsStatusEnum.status2.getStatus().equals(cmsContent.getStatus())) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法下线,状态错误"); - } - cmsContent.setStatus(CmsStatusEnum.status3.getStatus()); - cmsContentService.editData(cmsContent); - // 删除缓存 - redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType()); + cmsContentService.offline(body.getId()); + return ResponseMsgUtil.success("操作成功"); } catch (Exception e) { @@ -149,7 +148,7 @@ public class CmsController { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法删除,状态错误"); } cmsContent.setStatus(CmsStatusEnum.status0.getStatus()); - cmsContentService.editData(cmsContent); + cmsContentService.update(cmsContent); return ResponseMsgUtil.success("操作成功"); diff --git a/schedule/src/main/java/com/hfkj/consumer/CmsOfflineConsumer.java b/schedule/src/main/java/com/hfkj/consumer/CmsOfflineConsumer.java new file mode 100644 index 0000000..981de18 --- /dev/null +++ b/schedule/src/main/java/com/hfkj/consumer/CmsOfflineConsumer.java @@ -0,0 +1,39 @@ +package com.hfkj.consumer; + +import com.hfkj.entity.BsGasOilPriceTask; +import com.hfkj.entity.CmsContent; +import com.hfkj.service.cms.CmsContentService; +import com.hfkj.service.gas.BsGasOilPriceTaskService; +import com.hfkj.sysenum.cms.CmsStatusEnum; +import com.hfkj.sysenum.gas.GasTaskStatusEnum; +import lombok.extern.slf4j.Slf4j; +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; + +@Component +@Slf4j +@RocketMQMessageListener(consumerGroup = "cms-offline", topic = "task-topic",selectorExpression = "cms-offline") +public class CmsOfflineConsumer implements RocketMQListener { + @Resource + private CmsContentService cmsContentService; + + @Override + public void onMessage(String id) { + System.out.println("CMS内容下线id:" + id); + try { + CmsContent content = cmsContentService.getDetail(Long.parseLong(id)); + if (content != null && content.getEndTime() != null && content.getStatus().equals(CmsStatusEnum.status2.getStatus())) { + if (content.getEndTime().compareTo(new Date()) <= 0) { + cmsContentService.offline(Long.parseLong(id)); + } + } + + } catch (Exception e) { + + } + } +} diff --git a/schedule/src/main/java/com/hfkj/consumer/CmsOnlineConsumer.java b/schedule/src/main/java/com/hfkj/consumer/CmsOnlineConsumer.java new file mode 100644 index 0000000..7aea7b9 --- /dev/null +++ b/schedule/src/main/java/com/hfkj/consumer/CmsOnlineConsumer.java @@ -0,0 +1,38 @@ +package com.hfkj.consumer; + +import com.hfkj.entity.BsGasOilPriceTask; +import com.hfkj.entity.CmsContent; +import com.hfkj.service.cms.CmsContentService; +import com.hfkj.service.gas.BsGasOilPriceTaskService; +import com.hfkj.sysenum.cms.CmsStatusEnum; +import com.hfkj.sysenum.gas.GasTaskStatusEnum; +import lombok.extern.slf4j.Slf4j; +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; + +@Component +@Slf4j +@RocketMQMessageListener(consumerGroup = "cms-online", topic = "task-topic",selectorExpression = "cms-online") +public class CmsOnlineConsumer implements RocketMQListener { + @Resource + private CmsContentService cmsContentService; + + @Override + public void onMessage(String id) { + System.out.println("CMS内容上线id:" + id); + try { + CmsContent content = cmsContentService.getDetail(Long.parseLong(id)); + if (content != null && content.getStartTime() != null && content.getStatus().equals(CmsStatusEnum.status1.getStatus())) { + if (content.getStartTime().compareTo(new Date()) <= 0) { + cmsContentService.online(Long.parseLong(id)); + } + } + } catch (Exception e) { + + } + } +} diff --git a/service/src/main/java/com/hfkj/dao/CmsContentMapper.java b/service/src/main/java/com/hfkj/dao/CmsContentMapper.java index 734f063..1e3c18b 100644 --- a/service/src/main/java/com/hfkj/dao/CmsContentMapper.java +++ b/service/src/main/java/com/hfkj/dao/CmsContentMapper.java @@ -42,11 +42,13 @@ public interface CmsContentMapper extends CmsContentMapperExt { "insert into cms_content (`type`, `name`, ", "img_url, jump_type, ", "jump_url, `status`, ", + "start_time, end_time, ", "create_time, update_time, ", "ext_1, ext_2, ext_3)", "values (#{type,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ", "#{imgUrl,jdbcType=VARCHAR}, #{jumpType,jdbcType=INTEGER}, ", "#{jumpUrl,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ", + "#{startTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, ", "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ", "#{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR}, #{ext3,jdbcType=VARCHAR})" }) @@ -66,6 +68,8 @@ public interface CmsContentMapper extends CmsContentMapperExt { @Result(column="jump_type", property="jumpType", jdbcType=JdbcType.INTEGER), @Result(column="jump_url", property="jumpUrl", jdbcType=JdbcType.VARCHAR), @Result(column="status", property="status", jdbcType=JdbcType.INTEGER), + @Result(column="start_time", property="startTime", jdbcType=JdbcType.TIMESTAMP), + @Result(column="end_time", property="endTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="ext_1", property="ext1", jdbcType=JdbcType.VARCHAR), @@ -76,8 +80,8 @@ public interface CmsContentMapper extends CmsContentMapperExt { @Select({ "select", - "id, `type`, `name`, img_url, jump_type, jump_url, `status`, create_time, update_time, ", - "ext_1, ext_2, ext_3", + "id, `type`, `name`, img_url, jump_type, jump_url, `status`, start_time, end_time, ", + "create_time, update_time, ext_1, ext_2, ext_3", "from cms_content", "where id = #{id,jdbcType=BIGINT}" }) @@ -89,6 +93,8 @@ public interface CmsContentMapper extends CmsContentMapperExt { @Result(column="jump_type", property="jumpType", jdbcType=JdbcType.INTEGER), @Result(column="jump_url", property="jumpUrl", jdbcType=JdbcType.VARCHAR), @Result(column="status", property="status", jdbcType=JdbcType.INTEGER), + @Result(column="start_time", property="startTime", jdbcType=JdbcType.TIMESTAMP), + @Result(column="end_time", property="endTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="ext_1", property="ext1", jdbcType=JdbcType.VARCHAR), @@ -114,6 +120,8 @@ public interface CmsContentMapper extends CmsContentMapperExt { "jump_type = #{jumpType,jdbcType=INTEGER},", "jump_url = #{jumpUrl,jdbcType=VARCHAR},", "`status` = #{status,jdbcType=INTEGER},", + "start_time = #{startTime,jdbcType=TIMESTAMP},", + "end_time = #{endTime,jdbcType=TIMESTAMP},", "create_time = #{createTime,jdbcType=TIMESTAMP},", "update_time = #{updateTime,jdbcType=TIMESTAMP},", "ext_1 = #{ext1,jdbcType=VARCHAR},", diff --git a/service/src/main/java/com/hfkj/dao/CmsContentSqlProvider.java b/service/src/main/java/com/hfkj/dao/CmsContentSqlProvider.java index fdc0d5b..c76f97a 100644 --- a/service/src/main/java/com/hfkj/dao/CmsContentSqlProvider.java +++ b/service/src/main/java/com/hfkj/dao/CmsContentSqlProvider.java @@ -52,6 +52,14 @@ public class CmsContentSqlProvider { sql.VALUES("`status`", "#{status,jdbcType=INTEGER}"); } + if (record.getStartTime() != null) { + sql.VALUES("start_time", "#{startTime,jdbcType=TIMESTAMP}"); + } + + if (record.getEndTime() != null) { + sql.VALUES("end_time", "#{endTime,jdbcType=TIMESTAMP}"); + } + if (record.getCreateTime() != null) { sql.VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}"); } @@ -88,6 +96,8 @@ public class CmsContentSqlProvider { sql.SELECT("jump_type"); sql.SELECT("jump_url"); sql.SELECT("`status`"); + sql.SELECT("start_time"); + sql.SELECT("end_time"); sql.SELECT("create_time"); sql.SELECT("update_time"); sql.SELECT("ext_1"); @@ -138,6 +148,14 @@ public class CmsContentSqlProvider { sql.SET("`status` = #{record.status,jdbcType=INTEGER}"); } + if (record.getStartTime() != null) { + sql.SET("start_time = #{record.startTime,jdbcType=TIMESTAMP}"); + } + + if (record.getEndTime() != null) { + sql.SET("end_time = #{record.endTime,jdbcType=TIMESTAMP}"); + } + if (record.getCreateTime() != null) { sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}"); } @@ -173,6 +191,8 @@ public class CmsContentSqlProvider { sql.SET("jump_type = #{record.jumpType,jdbcType=INTEGER}"); sql.SET("jump_url = #{record.jumpUrl,jdbcType=VARCHAR}"); sql.SET("`status` = #{record.status,jdbcType=INTEGER}"); + sql.SET("start_time = #{record.startTime,jdbcType=TIMESTAMP}"); + sql.SET("end_time = #{record.endTime,jdbcType=TIMESTAMP}"); sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}"); sql.SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}"); sql.SET("ext_1 = #{record.ext1,jdbcType=VARCHAR}"); @@ -212,6 +232,14 @@ public class CmsContentSqlProvider { sql.SET("`status` = #{status,jdbcType=INTEGER}"); } + if (record.getStartTime() != null) { + sql.SET("start_time = #{startTime,jdbcType=TIMESTAMP}"); + } + + if (record.getEndTime() != null) { + sql.SET("end_time = #{endTime,jdbcType=TIMESTAMP}"); + } + if (record.getCreateTime() != null) { sql.SET("create_time = #{createTime,jdbcType=TIMESTAMP}"); } diff --git a/service/src/main/java/com/hfkj/entity/CmsContent.java b/service/src/main/java/com/hfkj/entity/CmsContent.java index a16e000..c52f6c3 100644 --- a/service/src/main/java/com/hfkj/entity/CmsContent.java +++ b/service/src/main/java/com/hfkj/entity/CmsContent.java @@ -48,6 +48,16 @@ public class CmsContent implements Serializable { */ private Integer status; + /** + * 上线时间 + */ + private Date startTime; + + /** + * 下线时间 + */ + private Date endTime; + /** * 创建时间 */ @@ -122,6 +132,22 @@ public class CmsContent implements Serializable { this.status = status; } + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + public Date getCreateTime() { return createTime; } @@ -181,6 +207,8 @@ public class CmsContent implements Serializable { && (this.getJumpType() == null ? other.getJumpType() == null : this.getJumpType().equals(other.getJumpType())) && (this.getJumpUrl() == null ? other.getJumpUrl() == null : this.getJumpUrl().equals(other.getJumpUrl())) && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) + && (this.getStartTime() == null ? other.getStartTime() == null : this.getStartTime().equals(other.getStartTime())) + && (this.getEndTime() == null ? other.getEndTime() == null : this.getEndTime().equals(other.getEndTime())) && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())) && (this.getExt1() == null ? other.getExt1() == null : this.getExt1().equals(other.getExt1())) @@ -199,6 +227,8 @@ public class CmsContent implements Serializable { result = prime * result + ((getJumpType() == null) ? 0 : getJumpType().hashCode()); result = prime * result + ((getJumpUrl() == null) ? 0 : getJumpUrl().hashCode()); result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); + result = prime * result + ((getStartTime() == null) ? 0 : getStartTime().hashCode()); + result = prime * result + ((getEndTime() == null) ? 0 : getEndTime().hashCode()); result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); result = prime * result + ((getExt1() == null) ? 0 : getExt1().hashCode()); @@ -220,6 +250,8 @@ public class CmsContent implements Serializable { sb.append(", jumpType=").append(jumpType); sb.append(", jumpUrl=").append(jumpUrl); sb.append(", status=").append(status); + sb.append(", startTime=").append(startTime); + sb.append(", endTime=").append(endTime); sb.append(", createTime=").append(createTime); sb.append(", updateTime=").append(updateTime); sb.append(", ext1=").append(ext1); diff --git a/service/src/main/java/com/hfkj/entity/CmsContentExample.java b/service/src/main/java/com/hfkj/entity/CmsContentExample.java index 49055a8..42755a0 100644 --- a/service/src/main/java/com/hfkj/entity/CmsContentExample.java +++ b/service/src/main/java/com/hfkj/entity/CmsContentExample.java @@ -575,6 +575,126 @@ public class CmsContentExample { return (Criteria) this; } + public Criteria andStartTimeIsNull() { + addCriterion("start_time is null"); + return (Criteria) this; + } + + public Criteria andStartTimeIsNotNull() { + addCriterion("start_time is not null"); + return (Criteria) this; + } + + public Criteria andStartTimeEqualTo(Date value) { + addCriterion("start_time =", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeNotEqualTo(Date value) { + addCriterion("start_time <>", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeGreaterThan(Date value) { + addCriterion("start_time >", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeGreaterThanOrEqualTo(Date value) { + addCriterion("start_time >=", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeLessThan(Date value) { + addCriterion("start_time <", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeLessThanOrEqualTo(Date value) { + addCriterion("start_time <=", value, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeIn(List values) { + addCriterion("start_time in", values, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeNotIn(List values) { + addCriterion("start_time not in", values, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeBetween(Date value1, Date value2) { + addCriterion("start_time between", value1, value2, "startTime"); + return (Criteria) this; + } + + public Criteria andStartTimeNotBetween(Date value1, Date value2) { + addCriterion("start_time not between", value1, value2, "startTime"); + return (Criteria) this; + } + + public Criteria andEndTimeIsNull() { + addCriterion("end_time is null"); + return (Criteria) this; + } + + public Criteria andEndTimeIsNotNull() { + addCriterion("end_time is not null"); + return (Criteria) this; + } + + public Criteria andEndTimeEqualTo(Date value) { + addCriterion("end_time =", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeNotEqualTo(Date value) { + addCriterion("end_time <>", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeGreaterThan(Date value) { + addCriterion("end_time >", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeGreaterThanOrEqualTo(Date value) { + addCriterion("end_time >=", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeLessThan(Date value) { + addCriterion("end_time <", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeLessThanOrEqualTo(Date value) { + addCriterion("end_time <=", value, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeIn(List values) { + addCriterion("end_time in", values, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeNotIn(List values) { + addCriterion("end_time not in", values, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeBetween(Date value1, Date value2) { + addCriterion("end_time between", value1, value2, "endTime"); + return (Criteria) this; + } + + public Criteria andEndTimeNotBetween(Date value1, Date value2) { + addCriterion("end_time not between", value1, value2, "endTime"); + return (Criteria) this; + } + public Criteria andCreateTimeIsNull() { addCriterion("create_time is null"); return (Criteria) this; diff --git a/service/src/main/java/com/hfkj/mqtopic/TaskTopic.java b/service/src/main/java/com/hfkj/mqtopic/TaskTopic.java index 2f376c1..12d469b 100644 --- a/service/src/main/java/com/hfkj/mqtopic/TaskTopic.java +++ b/service/src/main/java/com/hfkj/mqtopic/TaskTopic.java @@ -12,6 +12,10 @@ public enum TaskTopic { TASK_TOPIC("task-topic", "","任务主题"), // 油价任务 OIL_PRICE(TASK_TOPIC.getTopic(),"oil-price", "油价任务"), + // CMS定时上线 + CMS_ONLINE(TASK_TOPIC.getTopic(),"cms-online", "CMS内容定时上线"), + // CMS定时下线 + CMS_OFFLINE(TASK_TOPIC.getTopic(),"cms-offline", "CMS内容定时下线"), ; private String topic; diff --git a/service/src/main/java/com/hfkj/service/cms/CmsContentService.java b/service/src/main/java/com/hfkj/service/cms/CmsContentService.java index 6f330f4..126bbf3 100644 --- a/service/src/main/java/com/hfkj/service/cms/CmsContentService.java +++ b/service/src/main/java/com/hfkj/service/cms/CmsContentService.java @@ -1,6 +1,9 @@ package com.hfkj.service.cms; import com.hfkj.entity.CmsContent; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.remoting.exception.RemotingException; import java.util.List; import java.util.Map; @@ -16,7 +19,25 @@ public interface CmsContentService { * 编辑数据 * @param data */ - void editData(CmsContent data); + void editData(CmsContent data) ; + + /** + * 修改 + * @param data + */ + void update(CmsContent data); + + /** + * 上线 + * @param id + */ + void online(Long id); + + /** + * 下线 + * @param id + */ + void offline(Long id); /** * 详情 diff --git a/service/src/main/java/com/hfkj/service/cms/impl/CmsContentServiceImpl.java b/service/src/main/java/com/hfkj/service/cms/impl/CmsContentServiceImpl.java index 8cd7399..43d156e 100644 --- a/service/src/main/java/com/hfkj/service/cms/impl/CmsContentServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/cms/impl/CmsContentServiceImpl.java @@ -1,12 +1,26 @@ package com.hfkj.service.cms.impl; +import com.alibaba.fastjson.JSONObject; +import com.hfkj.common.exception.BaseException; +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; +import com.hfkj.common.utils.DateUtil; +import com.hfkj.common.utils.RedisUtil; import com.hfkj.dao.CmsContentMapper; import com.hfkj.entity.CmsContent; import com.hfkj.entity.CmsContentExample; +import com.hfkj.mqtopic.TaskTopic; import com.hfkj.service.cms.CmsContentService; import com.hfkj.sysenum.cms.CmsStatusEnum; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -23,16 +37,93 @@ import java.util.Map; public class CmsContentServiceImpl implements CmsContentService { @Resource private CmsContentMapper cmsContentMapper; + @Resource + private RocketMQTemplate rocketMQTemplate; + @Resource + private RedisUtil redisUtil; public final static String CACHE_CMS_ONLINE = "CACHE_CMS_ONLINE"; @Override - public void editData(CmsContent data) { + public void editData(CmsContent data){ data.setUpdateTime(new Date()); if (data.getId() == null) { data.setCreateTime(new Date()); cmsContentMapper.insert(data); + } else { cmsContentMapper.updateByPrimaryKey(data); } + + try { + if (data.getStartTime() != null) { + if (DateUtil.getSecondDiff(new Date(), data.getStartTime()) >= 1) { + Message message = new Message(TaskTopic.TASK_TOPIC.getTopic(), TaskTopic.CMS_ONLINE.getTag(), data.getId().toString().getBytes()); + // 定时投递,执行时间 + message.setDeliverTimeMs(data.getStartTime().getTime()); + SendResult sendResult = rocketMQTemplate.getProducer().send(message); + System.out.println("开始时间:"+ JSONObject.toJSONString(sendResult)); + } else { + // 开始时间小于当前时间 + // 上线 + online(data.getId()); + } + } + + if (data.getEndTime() != null) { + if (DateUtil.getSecondDiff(new Date(), data.getEndTime()) >= 1) { + Message message = new Message(TaskTopic.TASK_TOPIC.getTopic(), TaskTopic.CMS_OFFLINE.getTag(), data.getId().toString().getBytes()); + // 定时投递,执行时间 + message.setDeliverTimeMs(data.getEndTime().getTime()); + SendResult sendResult = rocketMQTemplate.getProducer().send(message); + System.out.println("结束时间:"+ JSONObject.toJSONString(sendResult)); + } else { + // 下线 + offline(data.getId()); + } + } + } catch (BaseException e) { + throw ErrorHelp.genException(SysCode.System,ErrorCode.COMMON_ERROR, e.getErrorMsg()); + } catch (Exception e) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.UNKNOW_ERROR, e.getMessage() ); + } + } + + @Override + public void update(CmsContent data) { + data.setUpdateTime(new Date()); + cmsContentMapper.updateByPrimaryKey(data); + } + + @Override + public void online(Long id) { + // 查询内容 + CmsContent cmsContent = getDetail(id); + if (cmsContent == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); + } + if (!CmsStatusEnum.status1.getStatus().equals(cmsContent.getStatus())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法上线,状态错误"); + } + cmsContent.setStatus(CmsStatusEnum.status2.getStatus()); + update(cmsContent); + // 删除缓存 + redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType()); + } + + @Override + public void offline(Long id) { + // 查询内容 + CmsContent cmsContent = getDetail(id); + if (cmsContent == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据"); + } + if (!CmsStatusEnum.status2.getStatus().equals(cmsContent.getStatus())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法下线,状态错误"); + } + cmsContent.setStatus(CmsStatusEnum.status3.getStatus()); + update(cmsContent); + + // 删除缓存 + redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType()); } @Override