3.7
fxl
2023-03-07 52cffc4ab8e9787a6f233295502c7c9788dddae1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.hxzkoa.services;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.hxzkoa.json.User;
import com.hxzkoa.json.tb_achor;
import com.hxzkoa.json.tb_login;
import com.hxzkoa.json.tb_user;
import com.hxzkoa.json.tb_xunjianbaobiao;
import com.hxzkoa.util.Config;
import com.hxzkoa.util.ModifyConfig;
 
@Service
public class LoginService {
    @PersistenceContext
    private EntityManager em;
 
    public tb_user login(String username) {
        String sql = null;
        Query query = null;
        sql = "SELECT username,password,superuser,deleteq,alertq,deleteall,justlook,fenceq FROM tb_user WHERE username=:username";
        query = this.em.createNativeQuery(sql);
        query.setParameter("username", username);
        List resultList = query.getResultList();
        tb_user user = new tb_user();
        if (resultList.size() > 0) {
            Object[] obj = (Object[]) resultList.get(0);
            user.setUsername(obj[0] == null ? "" : obj[0].toString());
            user.setPassword(obj[1] == null ? "" : obj[1].toString());
            user.setSuperuser(obj[2] == null ? "" : obj[2].toString());
            user.setDeleteq(obj[3] == null ? "" : obj[3].toString());
            user.setAlertq(obj[4] == null ? "" : obj[4].toString());
            user.setDeleteall(obj[5] == null ? "" : obj[5].toString());
            user.setJustlook(obj[6] == null ? "" : obj[6].toString());
            user.setFenceq(obj[7] == null ? "" : obj[7].toString());
        }
        return user;
    }
    
    @Transactional
    public void login_add(tb_login login) {
        String sql = null;
        Query query = null;
        sql = "INSERT tb_login (ip,username,time) VALUES (:ip,:username,now())";
        query = this.em.createNativeQuery(sql);
        query.setParameter("ip", login.getIp());
        query.setParameter("username", login.getUsername());
        query.executeUpdate();
    }
    
    public List<tb_login> getloginManagement(int page) {
        String sql = null;
        Query query = null;
        sql = "SELECT id,ip,username,time FROM (SELECT id,ip,username,time FROM tb_login ORDER BY id DESC) s LIMIT :start,:end";
        query = this.em.createNativeQuery(sql);
        query.setParameter("start", (page - 1) * Integer.parseInt(ModifyConfig.readData(Config.getPageConfig(), "perPage")));
        query.setParameter("end", Integer.parseInt(ModifyConfig.readData(Config.getPageConfig(), "perPage")));
        List resultList = query.getResultList();
        List<tb_login> tb_loginlist = new ArrayList<tb_login>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_login login = new tb_login();
                Object[] obj = (Object[]) resultList.get(i);
                login.setId((int) obj[0]);
                login.setIp(obj[1] == null ? "" : obj[1].toString());
                login.setUsername(obj[2] == null ? "" : obj[2].toString());
                login.setTime(obj[3] == null ? "" : obj[3].toString());
                tb_loginlist.add(login);
            }
        }
        return tb_loginlist;
    }
    
    public List<tb_login> getloginid(String username) {
        String sql = null;
        Query query = null;
        sql = "SELECT id,ip,username,time FROM tb_login WHERE username = :username";
        query = this.em.createNativeQuery(sql);
        query.setParameter("username", username);
        List resultList = query.getResultList();
        List<tb_login> tb_loginlist = new ArrayList<tb_login>();
        if (resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                tb_login login = new tb_login();
                Object[] obj = (Object[]) resultList.get(i);
                login.setId((int) obj[0]);
                login.setIp(obj[1] == null ? "" : obj[1].toString());
                login.setUsername(obj[2] == null ? "" : obj[2].toString());
                login.setTime(obj[3] == null ? "" : obj[3].toString());
                tb_loginlist.add(login);
            }
        }
        return tb_loginlist;
    }
    
    public int getloginManagementCount() {
        String sql = null;
        Query query = null;
        sql = "SELECT count(1) FROM tb_login";
        query = this.em.createNativeQuery(sql);
        return Integer.parseInt(query.getSingleResult().toString());
    }
}