欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从两个表中按照条件筛选数据,然后执行定时任务插入第三张表中,无则插入,有则不插入

程序员文章站 2024-03-21 11:22:46
...

实现类

@Resource
    private SubscribeMapper subscribeMapper;

    /**
     * @param
     * @return
     * @description 循环遍历两个表的数据
     * @author 宫崎不骏
     * @date 2020/4/27 10:07
     */
    @Override
    @Scheduled(cron = "0 */1 * * * ?")
    public OutputObject getAllSubscribe() {
        List<OneVo> lista = subscribeMapper.getAllSubscribe();
        ThreeDto threeDto = new ThreeDto();
        for (OneVo oneVo : lista) {
            List<TwoVo> listb = subscribeMapper.getAllNoticeInfoNb(oneVo.getProvinceName(), oneVo.getCityName(), oneVo.getBeginTimel(), oneVo.getEndTime());
            for (TwoVo twoVo : listb) {
                threeDto.setNoticeCreateTime(twoVo.getNoticeCreateTime());
                threeDto.setNoticeName(twoVo.getNoticeName());
                threeDto.setNoticeType(twoVo.getNoticeType());
                threeDto.setNoticeUrl(twoVo.getNoticeUrl());
                threeDto.setQx(twoVo.getQx());
                threeDto.setSiteName(twoVo.getSiteName());
                threeDto.setSiteSheng(twoVo.getSiteSheng());
                threeDto.setSiteShi(twoVo.getSiteShi());
                threeDto.setUserId(oneVo.getUserId());
                threeDto.setProvinceName(oneVo.getProvinceName());
                threeDto.setCityName(oneVo.getCityName());
                threeDto.setBeginTimel(oneVo.getBeginTimel());
                threeDto.setEndTime(oneVo.getEndTime());
                int b = subscribeMapper.selectCount(twoVo.getNoticeUrl(),oneVo.getUserId());
                if (b>0){
                    continue;
                }
                int a = subscribeMapper.insertInto(threeDto);

            }

        }
        return new OutputObject(ReturnCode.SUCCESS, "插入成功", "插入成功");
    }

Service接口

/**
     * @description 循环遍历两个表的数据
     * @author 宫崎不骏
     * @date 2020/4/27 10:07
     * @param
     * @return
     */
    OutputObject getAllSubscribe();

Mapper

/**
     * @description 查询出订阅表信息
     * @author 宫崎不骏
     * @date 2020/4/27 9:55
     * @param
     * @return
     */
    List<OneVo> getAllSubscribe();
    List<TwoVo> getAllNoticeInfoNb(String provinceName, String cityName, Date beginTimel,Date endTime);
    /**
     * @description 插入第三张表
     * @author 宫崎不骏
     * @date 2020/4/27 11:09
     * @param
     * @return
     */
    int insertInto(ThreeDto threeDto);
    /**
     * @description 判断数据库中是否有此数据
     * @author 宫崎不骏
     * @date 2020/4/27 17:31
     * @param
     * @return
     */
    int selectCount(String noticeUrl,String userId);

Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.subscribe.mapper.SubscribeMapper">


    <select id="getAllSubscribe" resultType="org.jeecg.modules.subscribe.entity.vo.OneVo">
        SELECT
        	user_id as userId,
        	province_name as provinceName,
        	city_name as cityName,
			begin_time as beginTimel,
			end_time as endTime
        FROM
        	gk_subscribe
        WHERE
        	subscribe_state = 1
    </select>
	<insert id="insertInto">
		INSERT INTO gk_subscribe_notice (
			notice_create_time,
			notice_name,
			site_sheng,
			site_shi,
			site_qx,
			site_name,
			notice_url,
			notice_type,
			user_id
		)
		VALUES
			(#{noticeCreateTime},
			#{noticeName},
			#{siteSheng},
			#{siteShi},
			#{qx},
			#{siteName},
			#{noticeUrl},
			#{noticeType},
			#{userId}
			)
	</insert>

	<select id="getAllNoticeInfoNb" resultType="org.jeecg.modules.subscribe.entity.vo.TwoVo">
		SELECT
        	notice_url AS noticeUrl,
        	site_sheng AS siteSheng,
        	site_shi AS siteShi,
        	site_qx AS qx,
        	site_name AS siteName,
        	notice_name AS noticeName,
        	notice_create_time AS noticeCreateTime,
        	notice_type as noticeType
        FROM
        	cj_notice_info
        	<where>
				<if test="provinceName !=null and provinceName !=''">
					AND site_sheng=#{provinceName}
				</if>
				<if test="cityName !=null and cityName !=''">
					AND site_shi=#{cityName}
				</if>
				<if test = "beginTimel != null and endTime != null">
					and notice_create_time between #{beginTimel} and #{endTime}
				</if>
			</where>

	</select>
	<select id="selectCount" resultType="int">
		SELECT
			COUNT(1)
		FROM
			gk_subscribe_notice
		WHERE
			notice_url = #{noticeUrl}
		AND user_id = #{userId}
	</select>
</mapper>

Vo

@Data
public class OneVo {
    private String userId;
    private String provinceName;
    private String cityName;
    /** 开始时间*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date beginTimel;
    /** 结束时间*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date endTime;
    private List<TwoVo> twoVo;
@Data
public class TwoVo {
    private String noticeCreateTime;
    private String noticeName;
    private String siteSheng;
    private String siteShi;
    private String qx;
    private String noticeUrl;
    private String siteName;
    private Integer noticeType;

}

控制台输出

从两个表中按照条件筛选数据,然后执行定时任务插入第三张表中,无则插入,有则不插入