#
luxiaotao1123
2022-02-26 b164776bd12f2598b3d07662f7af9a82db29e6a1
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
package com.zy.sc.manager.controller;
 
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.sc.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
/**
 * Created by vincent on 2021/3/5
 */
@RestController
@RequestMapping("console")
public class ConsoleController extends BaseController {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    /**
     * 当天数据:select * from tableName where datediff(day, 字段名,getdate())=0
     * 本周数据:select * from tableName where datediff(week, 字段名,getdate())=0
     * 本月:select * from tableName where datediff(month, 字段名,getdate())=0
     * 本季度:内select * from tableName where datediff(quarter, 字段名,getdate())=0
     * 本年:select * from tableName where datediff(year, 字段名,getdate())=0
     */
    @RequestMapping(value = "/header/auth")
    @ManagerAuth
    public R header() {
        Long hostId = getHostId();
        Integer logQtyDay = jdbcTemplate.queryForObject("select IFNULL(count(1), 0) from man_sensor_log where date(create_time) = curdate()".concat(hostId==null?"":" and host_id="+hostId), Integer.class);
        Integer logQty = jdbcTemplate.queryForObject("select IFNULL(count(1), 0) from man_sensor_log".concat(hostId==null?"":" where host_id="+hostId), Integer.class);
 
        Integer sensorQty = jdbcTemplate.queryForObject("SELECT COUNT(1) FROM man_sensor".concat(hostId==null?"":" where host_id="+hostId), Integer.class);
        Integer sensorOnlineQty0 = jdbcTemplate.queryForObject("SELECT IFNULL(count(1), 0) FROM man_sensor where SUBDATE(now(),interval 3 minute) < io_time AND sensor_type = 1".concat(hostId==null?"":" and host_id="+hostId), Integer.class);
        Integer sensorOnlineQty1 = jdbcTemplate.queryForObject("SELECT IFNULL(COUNT(1), 0) FROM (SELECT DISTINCT sensor_id FROM man_sensor_log WHERE 1=1 AND sensor_type > 1 AND create_time >=  NOW() - INTERVAL 24 HOUR".concat(hostId==null?"":" and host_id="+hostId) + ") a", Integer.class);
        int sensorOnlineQty = Optional.ofNullable(sensorOnlineQty0).orElse(0) + Optional.ofNullable(sensorOnlineQty1).orElse(0);
 
        Integer commandQty = jdbcTemplate.queryForObject("select COUNT(1) from man_command_log".concat(hostId==null?"":" where host_id="+hostId), Integer.class);
        Integer commandQtyMonth = jdbcTemplate.queryForObject("select COUNT(1) from man_command_log where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(send_time)".concat(hostId==null?"":" and host_id="+hostId), Integer.class);
 
        Integer usersQty = jdbcTemplate.queryForObject("select count(*) from sys_user".concat(hostId==null?"":" where host_id="+hostId), Integer.class);
        Integer deptQty = jdbcTemplate.queryForObject("select count(*) from sys_dept", Integer.class);
        Integer optQty = jdbcTemplate.queryForObject("select count(*) from sys_operate_log", Integer.class);
 
        return R.ok().add(Cools
                .add("logQtyDay", Optional.ofNullable(logQtyDay).orElse(0)) // 组托数量(当天)
                .add("logQty", Optional.ofNullable(logQty).orElse(0)) // 组托数量
 
                .add("sensorQty", Optional.ofNullable(sensorQty).orElse(0)) // 物料数量
                .add("sensorOnlineQty", sensorOnlineQty) // 货位数量
 
                .add("commandQty", Optional.ofNullable(commandQty).orElse(0)) // 总出库单数
                .add("commandQtyMonth", Optional.ofNullable(commandQtyMonth).orElse(0)) // 出库单数(当月)
 
                .add("usersQty", Optional.ofNullable(usersQty).orElse(0)) // 用户数量
                .add("deptQty", Optional.ofNullable(deptQty).orElse(0)) // 部门数量
                .add("optQty", Optional.ofNullable(optQty).orElse(0)) // 操作次数
        );
    }
 
    @RequestMapping(value = "/body/auth")
    @ManagerAuth
    public R body() {
        Long hostId = getHostId();
        // 报警记录
        List<Map<String, Object>> warnList = jdbcTemplate.queryForList("" +
                "SELECT \n" +
                "mst.name AS sensorType,\n" +
                "ma.sensor_uuid AS UUID,\n" +
                "ma.create_time AS createTime,\n" +
                "ma.desc AS msg\n" +
                "FROM man_alarm ma\n" +
                "LEFT JOIN man_sensor_type mst ON ma.sensor_type = mst.id\n" +
                "WHERE 1=1".concat(hostId==null?"":" and ma.host_id="+hostId) + " \n" +
                "ORDER BY ma.create_time DESC \n" +
                "LIMIT 0, 4");
        for (int i=0;i<warnList.size();i++) {
            warnList.get(i).put("no", i+1);
        }
        return R.ok().add(Cools
                .add("warnList", warnList) // 安全库存警告
        );
    }
 
}