lijilei 3 anos atrás
pai
commit
2d5de304fa

+ 1 - 1
book-dao/src/main/java/com/book/dao/cps/mapper/ManageCoverMapper.java

@@ -18,5 +18,5 @@ public interface ManageCoverMapper {
 
     int updateByPrimaryKey(ManageCover record);
 
-    ManageCover selectRandom(@Param("type") String type, @Param("status") String status);
+    ManageCover selectRandom(@Param("type") String type, @Param("sex") String sex);
 }

+ 18 - 1
book-dao/src/main/java/com/book/dao/utils/DateUtils.java

@@ -5,6 +5,8 @@ import org.springframework.util.Assert;
 import java.time.*;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.TemporalAdjusters;
+import java.util.Calendar;
+import java.util.Date;
 
 /**
  * 日期工具类
@@ -55,6 +57,20 @@ public class DateUtils {
         return (int) LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
     }
 
+    /**
+     * 获取当天的秒
+     * @return
+     */
+    public static long getDateNow() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(new Date());
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        Date zero = calendar.getTime();
+        return  zero.getTime()/1000;
+    }
+
     /**
      * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
      */
@@ -114,6 +130,7 @@ public class DateUtils {
     }
 
     public static void main(String[] args) {
-        System.out.println(DateUtils.getNowSecond());
+        System.out.println(getDateNow());
     }
+
 }

+ 333 - 0
book-dao/src/main/java/com/book/dao/utils/TimeUtil.java

@@ -0,0 +1,333 @@
+package com.book.dao.utils;
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoField;
+import java.time.temporal.ChronoUnit;
+import java.time.temporal.TemporalAdjuster;
+import java.time.temporal.TemporalAdjusters;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+/**
+ * 时间工具类
+ * @author caosongsong
+ *
+ */
+public class TimeUtil {
+	
+	public static final String YYYYMMDD1 = "yyyyMMdd";
+	
+	public static final String YYYY_MM = "yyyy-MM";
+	public static final String _YYYY_MM = "yyyy_MM";
+	public static final String YYYY = "yyyy";
+
+	public static final String YYYY_MM_DD = "yyyy-MM-dd";
+	
+	public static final String YYYYMMDD = "yyyy.mm.dd";
+	public static final String YYYYMMDD_N = "yyyyMMdd";
+
+	public static final String HH_MM_SS = "HH:mm:ss";
+	public static final String HHMMSS = "HHmmss";
+
+	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
+	
+	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
+
+    public static final String YYYYMMDDHHMMSS="yyyyMMddHHmmss";
+
+    public static final String YYYYMMDDHHMM="yyyyMMddHHmm";
+
+    public static final String YYYY_MM_DD_HH_MM_SS_Z = "yyyy-MM-dd HH:mm:ss z";
+    public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
+
+    public static final String MMDDHHMMSS = "MMddHHmmss";
+    
+    public static final String YYYY_M_DD_HH_MM_SS = "yyyy-M-dd HH:mm:ss";
+    
+    
+	/**
+	 * @todo 获取当前时间的字符串格式
+	 * @param format
+	 *            String 格式化样式"yyyy-MM-dd HH:mm:ss"
+	 * @return String 时间字符串
+	 */
+	public static String getCurrentTimestamp(String format) {
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return sdf.format(new Date());
+	}
+	
+	/**
+	 * 获取昨天的日期或者固定前几日的或者未来几日的date
+	 * @param dateNo    数字 ,-1表示昨天;0今天;1明天;2后天
+	 * @return
+	 */
+	public static Date getYesterday(int dateNo){
+		Calendar   cal   =   Calendar.getInstance();
+		cal.add(Calendar.DATE, dateNo);
+		return cal.getTime();
+	}
+
+	/**
+	 * @todo 获取当前时间戳
+	 * @return Timestamp 当前时间
+	 */
+	public static Timestamp getCurrentTimestamp() {
+		return new Timestamp(new Date().getTime());
+	}
+
+	/**
+	 * @todo 获取当前日期字符串
+	 * @param format
+	 *            String yyyy-MM-dd
+	 * @return String
+	 */
+	public static String getCurrentDate(String format) {
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return sdf.format(new Date());
+	}
+	public static Integer getCurrentIntDate(String format) {
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return Integer.valueOf(sdf.format(new Date()));
+	}
+
+	/**
+	 * @todo 格式化时间日期
+	 * @param date
+	 * @param format
+	 * @return
+	 */
+	public static String dateToStr(Date date, String format) {
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return sdf.format(date);
+	}
+
+	/**
+	 * @todo 字符串转日期
+	 * @param date
+	 * @param format
+	 * @return
+	 * @throws Exception
+	 */
+	public static Date dateFromStr(String date, String format) throws ParseException{
+		SimpleDateFormat sdf = new SimpleDateFormat(format);
+		return sdf.parse(date);
+	}
+
+
+	/**
+	 * @todo 日期比较
+	 * @param date1
+	 * @param date2
+	 * @return {[ 0 - d1等于d2 ]/[ 1 - d1 大于 d2 ]/[ -1 - d1 小于 d2 ]}
+	 */
+	public static int compareDate(Date date1, Date date2) {
+		// 参数检查
+		if (date1 == null || date2 == null) {
+			throw new IllegalArgumentException("非法参数异常,参数不能为空!");
+		}
+
+		return date1.compareTo(date2);
+	}
+
+	/**
+	 * @todo 只比较时分秒
+	 * @param date1
+	 * @param date2
+	 * @return {[ 0 - d1等于d2 ]/[ 1 - d1 大于 d2 ]/[ -1 - d1 小于 d2 ]}
+	 */
+	public static int compareTime(Date date1, Date date2) {
+		// 参数检查
+		if (date1 == null || date2 == null) {
+			throw new IllegalArgumentException("非法参数异常,参数不能为空!");
+		}
+		Calendar cal1 = Calendar.getInstance();
+		cal1.setTime(date1);
+		int time1 = cal1.get(Calendar.HOUR_OF_DAY) * 60 * 60
+				+ cal1.get(Calendar.MINUTE) * 60 + cal1.get(Calendar.SECOND);
+
+		Calendar cal2 = Calendar.getInstance();
+		cal2.setTime(date2);
+		int time2 = cal2.get(Calendar.HOUR_OF_DAY) * 60 * 60
+				+ cal2.get(Calendar.MINUTE) * 60 + cal2.get(Calendar.SECOND);
+
+		int tmp = time1 - time2;
+		if (tmp > 0) {
+			return 1;
+		} else if (tmp == 0) {
+			return 0;
+		} else {
+			return -1;
+		}
+	}
+
+	/**
+	 * @todo 获取日期的星期几
+	 * @param date
+	 * @return int {1- 星期日 / 2 -星期一 / 7-星期六}
+	 */
+	public static int getDateOfWeek(Date date) {
+		// 参数检查
+		if (date == null) {
+			throw new IllegalArgumentException("非法参数异常,参数不能为空!");
+		}
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(date);
+		return calendar.get(Calendar.DAY_OF_WEEK);
+	}
+
+	/**
+	 * 当前日期的星期
+	 * 
+	 * @return
+	 */
+	public static int currentDayInWeek() {
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(new Date());
+		return calendar.get(Calendar.DAY_OF_WEEK);
+	}
+
+	public static Date firstTime(Date curDate) {
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(curDate);
+		calendar.set(Calendar.HOUR_OF_DAY, 0);
+		calendar.set(Calendar.MINUTE, 0);
+		calendar.set(Calendar.SECOND, 0);
+		return calendar.getTime();
+	}
+
+	public static Date lastTime(Date curDate) {
+		Calendar calendar = Calendar.getInstance();
+		calendar.setTime(curDate);
+		calendar.set(Calendar.HOUR_OF_DAY, 23);
+		calendar.set(Calendar.MINUTE, 59);
+		calendar.set(Calendar.SECOND, 59);
+		return calendar.getTime();
+	}
+
+	/**
+	 * 日、时、分、秒的间距
+	 * @param valueDate
+	 * @param begin
+	 * @param end
+	 * @return
+	 */
+	public static int interval(Date valueDate,Date begin,Date end){
+		if(begin.getTime() > end.getTime()){
+			throw new IllegalArgumentException("begin or end is invalid");
+		}
+		if(valueDate.getTime() < begin.getTime()){
+			return -1;
+		}else if(valueDate.getTime()>= begin.getTime() && valueDate.getTime() <= end.getTime()){
+			return 0;
+		}else{
+			return 1;
+		}
+	}
+	
+	/**
+	 * 时、分、秒的间距
+	 * @param valueDate
+	 * @param begin
+	 * @param end
+	 * @return
+	 */
+	public static int intervalItime(Date valueDate,Date begin,Date end){
+		if(compareTime(end, begin) == -1){
+			throw new IllegalArgumentException("begin or end is invalid");
+		}
+		if(compareTime(valueDate, begin) == -1){
+			return -1;
+		}else if(compareTime(valueDate, begin) == 1 && compareTime(end, valueDate) == 1){
+			return 0;
+		}else{
+			return 1;
+		}
+	}
+
+	/**
+	 * 获取前一天的工作日
+	 * @param format 日期格式
+	 * @return
+	 */
+	public static String getBeforeWorkDay(String format) {
+		TemporalAdjuster temporalAdjuster = nextWorkingDay();
+		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
+		return  LocalDate.now().with(temporalAdjuster).format(formatter);
+	}
+
+	/**
+	 * 获取指定日期前一个工作日
+	 * @param year 年
+	 * @param month 月
+	 * @param dayOfMonth  日
+	 * @param format 日期格式
+	 * @return
+	 */
+	public static String getBeforeWorkDay(int year, int month, int dayOfMonth, String format) {
+		TemporalAdjuster temporalAdjuster = nextWorkingDay();
+		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
+		return  LocalDate.of(year,month,dayOfMonth).with(temporalAdjuster).format(formatter);
+	}
+
+	private static TemporalAdjuster nextWorkingDay() {
+		return TemporalAdjusters.ofDateAdjuster(
+
+				temporal -> {
+
+					DayOfWeek dow =
+
+							DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
+
+					int dayToAdd = -1;
+
+					if (dow ==DayOfWeek.MONDAY){
+
+						dayToAdd = -3;
+
+					}
+
+					if (dow ==DayOfWeek.SATURDAY){
+
+						dayToAdd = -1;
+
+					}
+					if (dow ==DayOfWeek.SUNDAY){
+
+						dayToAdd = -2;
+
+					}
+
+					return temporal.plus(dayToAdd, ChronoUnit.DAYS);
+
+				});
+	}
+	public static String getDateString(long time){
+        Date date = new Date(time);
+        return dateToStr(date,YYYY_MM_DD);
+    }
+    
+    public static String getPointDateString(long time){
+        Date date = new Date(time);
+        return dateToStr(date,YYYYMMDD);
+    }
+    
+    public static String getTimeString(long time){
+    	Date date = new Date(time);
+    	return dateToStr(date,HH_MM_SS);
+    }
+
+    public static Date dateFromStringWithTimezoneEtc_GMT(String datestr){
+        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
+        try {
+            sdf.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
+            return sdf.parse(datestr);
+        } catch (ParseException e) {
+            return null;
+        }
+    }
+}

+ 1 - 1
book-dao/src/main/resources/mapper/ManageCoverMapper.xml

@@ -23,7 +23,7 @@
         select *
         from manage_cover
         where `type` = #{type}
-          and `status` = {status}
+          and `status` = 'normal'
             and sex = #{sex}
         order by rand()
         limit 1

+ 1 - 0
book-push/src/main/java/com/book/push/cons/SiteCons.java

@@ -12,4 +12,5 @@ public class SiteCons {
      * 静默授权的渠道
      */
     public static final String SILENT_DEFAULT_CHANNEL = "silent_default_channel";
+    public static final String SIGN_KANDIAN = "kandian_sign";
 }

+ 97 - 18
book-push/src/main/java/com/book/push/handler/MenuHandler.java

@@ -1,14 +1,16 @@
 package com.book.push.handler;
 
-import com.book.dao.cps.pojo.AdminConfig;
-import com.book.dao.cps.pojo.SignRecommand;
-import com.book.dao.cps.pojo.User;
-import com.book.push.service.dao.AdminConfigService;
-import com.book.push.service.dao.PlatformService;
-import com.book.push.service.dao.SignRecommendService;
-import com.book.push.service.dao.UserService;
+import com.book.dao.cps.pojo.*;
+import com.book.dao.cpsshard.entity.Sign;
+import com.book.dao.cpsshard.mapper.SignMapper;
+import com.book.dao.cpsshard.pojo.example.SignExample;
+import com.book.dao.utils.DateUtils;
+import com.book.dao.utils.TimeUtil;
+import com.book.push.cons.SiteCons;
+import com.book.push.service.dao.*;
 import com.book.push.service.push.PushService;
 import com.book.push.service.wx.WxThirdPartService;
+import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.session.WxSessionManager;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
@@ -42,7 +44,17 @@ public class MenuHandler extends AbstractHandler {
     private WxThirdPartService wxThirdPartService;
     @Autowired
     private SignRecommendService signRecommendService;
+    @Autowired
+    private SignedRecommendService signedRecommendService;
+    @Autowired
+    private ManageTitleService manageTitleService;
+    @Autowired
+    private ManageCoverService manageCoverService;
+    @Autowired
+    private ConfigService configService;
 
+    @Autowired
+    private SignMapper signMapper;
     @Override
     public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                     Map<String, Object> context, WxMpService weixinService,
@@ -53,21 +65,33 @@ public class MenuHandler extends AbstractHandler {
         if (EventType.VIEW.equals(wxMessage.getEvent())) {
             return null;
         }
+        String appid = wxMessage.getToUser();
+        User user = userService.selectByOpenId(wxMessage.getFromUser());
+        Config config = configService.selectByName(SiteCons.SIGN_KANDIAN);
+        int kandian = Integer.valueOf(config.getValue());
+
+
+        Long id = user.getId();
+        SignExample example = SignExample.newAndCreateCriteria().andUidEqualTo(id.intValue()).andCreatedateEqualTo(TimeUtil.getCurrentIntDate(TimeUtil.YYYYMMDD)).example();
+        Sign sign = signMapper.selectOneByExample(example);
+        pushMessage(user,appid,sign!=null);
+        if (sign==null){
+            sign = new Sign();
+            sign.setUid(id.intValue());
+            sign.setCreatedate(TimeUtil.getCurrentIntDate(TimeUtil.YYYYMMDD));
+            sign.setCreatetime(DateUtils.getNow());
+            sign.setKandian(kandian);
+            signMapper.insert(sign);
+
+
+            addKandian(user,kandian);
+        }
+
+
 
-        String openid = wxMessage.getFromUser();
-        AdminConfig adminConfig = userService.selectAdminConfigByOpenid(openid);
-        Integer platformId = adminConfig.getPlatformId();
-        WxOpenService wxOpenService = wxThirdPartService.getWxOpenServiceByPlatFormId(platformId);
-        WxOpenMpService wxMpService = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(wxMessage.getToUser());
 
 
 
-        SignRecommand signRecommand = signRecommendService.selectRandom();
-        WxMpKefuMessage.WxArticle  wxArticle = new  WxMpKefuMessage.WxArticle();
-        wxArticle.setTitle();
-        new NewsBuilder().toUser(openid).addArticle()
-        wxMpService.getKefuService().sendKefuMessage()
-        pushService.addBookPushTask();
 
 
         return WxMpXmlOutMessage.TEXT().content(msg)
@@ -75,4 +99,59 @@ public class MenuHandler extends AbstractHandler {
                 .build();
     }
 
+
+    /**
+     * 添加看点
+     * @param user
+     * @param kandian
+     */
+    private void addKandian(User user, int kandian) {
+
+    }
+
+
+    /**
+     * 签到推送
+     * @param user
+     * @param b 是否已经签到
+     */
+    private void pushMessage(User user,String appid, boolean b) {
+        Long bookId;
+        if (b){
+            SignRecommand signRecommand = signedRecommendService.selectRandom(user.getSex().toString());
+             bookId = signRecommand.getBookId();
+        }else {
+            SignRecommand signRecommand = signRecommendService.selectRandom(user.getSex().toString());
+             bookId = signRecommand.getBookId();
+
+        }
+        ManageTitle manageTitle = manageTitleService.selectRandom(user.getSex().toString());
+        ManageCover manageCover = manageCoverService.selectRandom("1", user.getSex().toString());
+        WxMpKefuMessage.WxArticle  wxArticle = new  WxMpKefuMessage.WxArticle();
+        wxArticle.setTitle(manageTitle.getTitle());
+        wxArticle.setPicUrl(manageCover.getImage());
+        wxArticle.setUrl(createUrl(bookId));
+        WxMpKefuMessage build = new NewsBuilder().toUser(user.getOpenid()).addArticle(wxArticle).build();
+
+
+        Integer channelId = user.getChannelId();
+        AdminConfig adminConfig = adminConfigService.selectByAdminId(channelId);
+        Integer platformId = adminConfig.getPlatformId();
+        WxOpenService wxOpenService = wxThirdPartService.getWxOpenServiceByPlatFormId(platformId);
+        WxOpenMpService wxMpService = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appid);
+        try {
+            wxMpService.getKefuService().sendKefuMessage(build);
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+
+            System.out.println("发送失败");
+        }
+
+    }
+
+    private String createUrl(Long bookId) {
+        return "";
+
+    }
+
 }

+ 1 - 1
book-push/src/main/java/com/book/push/service/dao/ManageCoverService.java

@@ -12,5 +12,5 @@ import org.springframework.beans.factory.annotation.Autowired;
 
 public interface ManageCoverService {
 
-  ManageCover selectRandom(String type,String status);
+  ManageCover selectRandom(String type,String sex);
 }

+ 4 - 3
book-push/src/main/java/com/book/push/service/dao/impl/ManageCoverServiceImpl.java

@@ -4,6 +4,7 @@ import com.book.dao.cps.mapper.ManageCoverMapper;
 import com.book.dao.cps.pojo.ManageCover;
 import com.book.push.service.dao.ManageCoverService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 /**
  * created in 2021/8/28
@@ -11,12 +12,12 @@ import org.springframework.beans.factory.annotation.Autowired;
  *
  * @author win7
  */
-
+@Service
 public class ManageCoverServiceImpl implements ManageCoverService {
     @Autowired
     private ManageCoverMapper manageCoverMapper;
     @Override
-    public ManageCover selectRandom(String type,String status) {
-        return manageCoverMapper.selectRandom(type,status);
+    public ManageCover selectRandom(String type,String sex) {
+        return manageCoverMapper.selectRandom(type,sex);
     }
 }

+ 2 - 1
book-push/src/main/java/com/book/push/service/dao/impl/ManageTitleServiceImpl.java

@@ -4,6 +4,7 @@ import com.book.dao.cps.mapper.ManageTitleMapper;
 import com.book.dao.cps.pojo.ManageTitle;
 import com.book.push.service.dao.ManageTitleService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 /**
  * created in 2021/8/28
@@ -11,7 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
  *
  * @author win7
  */
-
+@Service
 public class ManageTitleServiceImpl implements ManageTitleService {
     @Autowired
     private ManageTitleMapper manageTitleMapper;

+ 2 - 1
book-push/src/main/java/com/book/push/service/dao/impl/SignRecommendServiceImpl.java

@@ -4,6 +4,7 @@ import com.book.dao.cps.mapper.SignRecommandMapper;
 import com.book.dao.cps.pojo.SignRecommand;
 import com.book.push.service.dao.SignRecommendService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 /**
  * created in 2021/8/28
@@ -11,7 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
  *
  * @author win7
  */
-
+@Service
 public class SignRecommendServiceImpl implements SignRecommendService {
     @Autowired
     private SignRecommandMapper signRecommandMapper;

+ 2 - 1
book-push/src/main/java/com/book/push/service/dao/impl/SignedRecommendServiceImpl.java

@@ -4,6 +4,7 @@ import com.book.dao.cps.mapper.SignedRecommandMapper;
 import com.book.dao.cps.pojo.SignRecommand;
 import com.book.push.service.dao.SignedRecommendService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
 /**
  * created in 2021/8/28
@@ -11,7 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
  *
  * @author win7
  */
-
+@Service
 public class SignedRecommendServiceImpl implements SignedRecommendService {
     @Autowired
     private SignedRecommandMapper signedRecommandMapper;