ssm框架下实现手机营业厅连签送流量

功能概述

模拟常见手机营业厅APP登录签到领取流量功能,利用SSM框架完成登录签到领取流量,具体功能如下:

用户登录之后方可签到,点击签到,领取10积分,每天最多签到一次

一个月内只要连续登录签到7天,除每日10积分之外,额外奖励流量30M

如果中间遗漏签到,则之前累积天数清零,重新计算,但签到积分保留

数据库设计

ssm框架下实现手机营业厅连签送流量_第1张图片

项目结构

ssm框架下实现手机营业厅连签送流量_第2张图片

实体类

package com.xszx.beans;

import java.sql.Date;


public class Signin {
    private Integer id;
    private User user;
    private Date beginTime;

    public Signin() {
    }

    public Signin(Integer id, User user, Date beginTime) {
        this.id = id;
        this.user = user;
        this.beginTime = beginTime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    @Override
    public String toString() {
        return "Signin{" +
                "id=" + id +
                ", user=" + user +
                ", beginTime=" + beginTime +
                '}';
    }
}




package com.xszx.beans;

public class User {
    private Integer id;
    private String name;
    private String phone;
    private int integral;
    private int flow;
    private Integer state;

    public User() {
    }

    public User(Integer id, String name, String phone, int integral, int flow, Integer state) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.integral = integral;
        this.flow = flow;
        this.state = state;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getIntegral() {
        return integral;
    }

    public void setIntegral(int integral) {
        this.integral = integral;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", integral=" + integral +
                ", flow=" + flow +
                ", state=" + state +
                '}';
    }
}




控制层

package com.xszx.controller;

import com.xszx.beans.User;
import com.xszx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
public class UserController {
    private UserService userService;
@Resource
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/login")
    public String login(String phone,Model model,HttpSession session) {
        User user1 = userService.getUserByPhone(phone);
        if (user1 != null) {
            model.addAttribute("msg", "登陆成功");
            session.setAttribute("user", user1);
            return "index";
        } else {
            model.addAttribute("msg", "登陆失败");
            return "login";
        }
    }
    @RequestMapping("/signin")
    public String signin(int uid,Model model) {
        boolean b = userService.signToday(uid);
        if (b){
            model.addAttribute("msg", "今天已签到");
            return "index";
        }else{
            //判断今天是否和之前是一个月,若不是则重置连签次数和状态
            boolean a = userService.getLastSignin(uid);
            if(!a){
                //重置连签次数和状态
                userService.setStateAndFlag(uid);
            }
            //签到
            userService.signin(uid);

            //查看连签次数
            int state = userService.selectState(uid);
            int flag = userService.selectFlage(uid);
            if(state==7&&flag==0){
                userService.addFlow(uid);
                model.addAttribute("msg", "签到成功,连签七天恭喜您获得30M流量");
            }else{
                model.addAttribute("msg", "签到成功");
            }
            return "index";
        }

    }

}

数据层

package com.xszx.dao;

import com.xszx.beans.Signin;
import com.xszx.beans.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;
@Repository
public interface UserDao {

    User getUserByPhone(@Param("phone") String phone);

    List signToday(@Param("uid") int uid);

    void addIntegral(@Param("uid") int uid);

    void addState(@Param("uid") int uid);

    void signin(@Param("uid") int uid);

    Signin signinYesterday(@Param("uid") int uid);

    void setState(@Param("uid") int uid);

    int selectState(@Param("uid") int uid);

    void addFlow(@Param("uid") int uid);

    void updateFlag(@Param("uid")int uid);

    int selectFlage(@Param("uid") int uid);

    List getLastSignin(@Param("uid")int uid);

    void setStateAndFlag(@Param("uid")int uid);
}

业务层

package com.xszx.service;

import com.xszx.beans.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

public interface UserService {

    User getUserByPhone(String phone);

    boolean signToday(int uid);

    void signin(int uid);

    int selectState(int uid);

    void addFlow(int uid);

    int selectFlage(int uid);


    boolean getLastSignin(int uid);

    void setStateAndFlag(int uid);
}

业务实现层

package com.xszx.service.impl;

import com.xszx.beans.Signin;
import com.xszx.beans.User;
import com.xszx.dao.UserDao;
import com.xszx.service.UserService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private SqlSessionTemplate sqlSessionTemplate;
@Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    @Override
    public User getUserByPhone(String phone) {
    UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
    return userDao.getUserByPhone(phone);
    }

    @Override
    public boolean signToday(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        List list = userDao.signToday(uid);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void signin(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        //加积分
        userDao.addIntegral(uid);
        //查看前一天是否签到,若没签到断签
        Signin Signin  =  userDao.signinYesterday(uid);
        System.out.println(Signin);
        if(Signin==null){
            //断签,重置天数
            userDao.setState(uid);
        }else{
            //连签,增加天数
            userDao.addState(uid);
        }
        //签到
        userDao.signin(uid);
    }

    @Override
    public int selectState(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        return userDao.selectState(uid);
    }

    @Override
    public void addFlow(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        userDao.addFlow(uid);
        userDao.updateFlag(uid);
    }

    @Override
    public int selectFlage(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        return userDao.selectFlage(uid);
    }

    @Override
    public boolean getLastSignin(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        List list = userDao.getLastSignin(uid);
        if (list.size()>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public void setStateAndFlag(int uid) {
        UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
        userDao.setStateAndFlag(uid);
    }


}

前端代码

前端只实现功能,无任何美化

<%--
  Created by IntelliJ IDEA.
  User: 51475
  Date: 2024/9/10
  Time: 19:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    签到
    





你可能感兴趣的:(JavaWeb,java,数据库)