diff --git a/Common/Utils.py b/Common/Utils.py index f632fb6..3c60824 100644 --- a/Common/Utils.py +++ b/Common/Utils.py @@ -12,6 +12,7 @@ import hashlib import uuid import psutil import math +from decimal import Decimal from config.SystemConfig import SystemConfig # 判断当前系统是linux还是windows @@ -33,6 +34,15 @@ class Utils(object): def get_str_datetime(): return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + def data_float_type_convent(data_list): + new_list = [] + for i in data_list: + new_dic = {} + for k,v in i.items(): + new_dic[k]= v if not isinstance(v, Decimal) else float(v) + new_list.append(new_dic) + return new_list def sql_paging_assemble(sql_all, page_param): sql_all += ' limit ' + str((page_param.curPage - 1) * page_param.pageRows) + ',' + str(page_param.pageRows) diff --git a/apps/monitor/views.py b/apps/monitor/views.py new file mode 100644 index 0000000..4949c93 --- /dev/null +++ b/apps/monitor/views.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +''' +@Date:2022/08/10 16:53:41 +''' +from dataclasses import dataclass +import sys + +from db_logic.humiture_record import BllHumitureRecord +sys.path.append('.') + +from sqlalchemy import and_, or_ + +from Common.auth import token_auth +from flask import Blueprint, jsonify, request, g +from Common.Utils import PageParam, Utils + + +monitor_router = Blueprint('monitor', __name__) + + + +# 每日温度记录 +@monitor_router.route("/day_monitor", methods=["POST"]) +@token_auth.login_required +def get_day_monitor(): + client_id = request.values.get("client_id") + start_time = request.values.get("start_time") + end_time = request.values.get("end_time") + page = request.values.get("page", 1) + page_size = request.values.get("page_size", 10) + page_param = PageParam(int(page), int(page_size)) + data_list = BllHumitureRecord().get_client_day_log( + client_id=client_id, start_time=start_time, + end_time=end_time, page_param=page_param + ) + data_list = Utils.msyql_table_model(data_list) + return jsonify(Utils.true_return(data={"data_list": Utils.data_float_type_convent(data_list), "total_count": page_param.totalRecords})) + + +# 获取每日记录详情 +@monitor_router.route("/day_monitor_info", methods=["POST"]) +@token_auth.login_required +def get_day_monitor_info(): + client_id = request.values.get("client_id") + start_time = request.values.get("start_time") + end_time = request.values.get("end_time") + page = request.values.get("page", 1) + page_size = request.values.get("page_size", 10) + page_param = PageParam(int(page), int(page_size)) + data_list = BllHumitureRecord().get_client_day_log_info( + client_id=client_id, + start_time=start_time, + end_time=end_time, + page_param=page_param + ) + data_list = Utils.msyql_table_model(data_list) + return jsonify(Utils.true_return(data={"data_list": Utils.data_float_type_convent(data_list), "total_count": page_param.totalRecords})) diff --git a/db_logic/humiture_record.py b/db_logic/humiture_record.py index 76c4ce9..478f46b 100644 --- a/db_logic/humiture_record.py +++ b/db_logic/humiture_record.py @@ -4,6 +4,7 @@ @Date:2022/07/18 16:51:28 ''' import sys +from Common.Utils import Utils sys.path.append(".") from db_logic.db_base import Repository @@ -50,6 +51,61 @@ class BllHumitureRecord(Repository): return self.execute(sql_all).fetchall() + # 获取每日记录,按时间,柜体分组展示 + def get_client_day_log(self, client_id, start_time, end_time, page_param): + filter_base = "" + if client_id: + filter_base += f" clint_id='{client_id}'" + if start_time and end_time: + if filter_base: + filter_base += " and " + filter_base += f" record_date>= '{start_time}' and record_date<= '{end_time}' " + if filter_base: + filter_base = f" where {filter_base} " + + sql_all = f""" + select a.client_id, a.client_name, a.day_date, a.avg_wd, a.avg_sd, a.avg_voc, b.place + from ( + select client_id, client_name, + DATE_FORMAT(record_date,'%Y-%m-%d') day_date, + avg(temperature) avg_wd, avg(humidity) avg_sd, + avg(voc_content) avg_voc + from rms_humiture_record {filter_base} GROUP BY client_id, DATE_FORMAT(record_date,'%Y-%m-%d') + ) a LEFT JOIN rms_client as b on a.client_id=b.client_id ORDER BY day_date desc + """ + try: + count_number = len(self.execute(sql_all).fetchall()) + except Exception: + count_number = 0 + page_param.totalRecords = count_number + page_sql = Utils.sql_paging_assemble(sql_all, page_param) + return self.execute(page_sql).fetchall() + + # 获取每日记录详情 + def get_client_day_log_info(self, client_id, start_time, end_time, page_param): + filter_base = "" + if client_id: + filter_base += f" client_id='{client_id}'" + if start_time and end_time: + if filter_base: + filter_base += " and " + filter_base += f" record_date>= '{start_time}' and record_date<= '{end_time}' " + + sql_all = f""" + select + DATE_FORMAT(record_date,'%H:%i:%s') day_date, + FORMAT(temperature,2) as wd, + FORMAT(humidity,2) as sd, + FORMAT(voc_content,2) as voc + from rms_humiture_record {filter_base} ORDER BY record_date + """ + try: + count_number = len(self.execute(sql_all).fetchall()) + except Exception: + count_number = 0 + page_param.totalRecords=count_number + page_sql = Utils.sql_paging_assemble(sql_all, page_param) + return self.execute(page_sql).fetchall() # 获取温湿度列表 # def getHumitureList(self, customerId, pageParam): diff --git a/db_logic/medicament.py b/db_logic/medicament.py index a02e2b7..f65bcfa 100644 --- a/db_logic/medicament.py +++ b/db_logic/medicament.py @@ -4,10 +4,7 @@ @Date:2022/07/18 16:54:45 ''' import sys - -import sqlalchemy sys.path.append('.') -import datetime from sqlalchemy import distinct, or_, and_, desc, asc from sqlalchemy.sql import func from db_logic.db_base import Repository diff --git a/db_logic/medicament_record.py b/db_logic/medicament_record.py index 5aba181..55292d5 100644 --- a/db_logic/medicament_record.py +++ b/db_logic/medicament_record.py @@ -3,13 +3,10 @@ ''' @Date:2022/07/19 10:49:15 ''' -from decimal import Decimal + import sys sys.path.append('.') -import json -import datetime -from sqlalchemy import or_, and_, asc, desc -from sqlalchemy.sql import func +from decimal import Decimal from db_logic.db_base import Repository from models.medicament_models import EntityMedicamentRecord diff --git a/db_logic/role.py b/db_logic/role.py index 335a95d..242b8da 100644 --- a/db_logic/role.py +++ b/db_logic/role.py @@ -3,7 +3,7 @@ ''' @Date:2022/08/01 17:14:39 ''' -from gc import isenabled + import sys sys.path.append('.') diff --git a/db_logic/user_medicament.py b/db_logic/user_medicament.py index d6bc1df..2e0e348 100644 --- a/db_logic/user_medicament.py +++ b/db_logic/user_medicament.py @@ -6,11 +6,11 @@ import sys -from Common.Utils import Utils + sys.path.append('.') from sqlalchemy import and_ - +from Common.Utils import Utils from db_logic.db_base import Repository from models.user_models import EntityUserMedicament