diff --git a/sf_machine_connect/controllers/controllers.py b/sf_machine_connect/controllers/controllers.py index 96708dd8..35f4a257 100644 --- a/sf_machine_connect/controllers/controllers.py +++ b/sf_machine_connect/controllers/controllers.py @@ -201,7 +201,7 @@ class Sf_Dashboard_Connect(http.Controller): SELECT time, device_state, program_name FROM device_data WHERE device_name = %s AND time >= %s AND time <= %s - ORDER BY time ASC; + ORDER BY time DESC; ''' # 执行SQL命令,使用参数绑定 cur.execute(sql, (item, begin_time, end_time)) @@ -308,7 +308,7 @@ class Sf_Dashboard_Connect(http.Controller): plan_data_total_counts = plan_obj.search_count([('production_line_id.name', '=', line)]) # 工单完成量 plan_data_finish_counts = plan_obj.search_count( - [('production_line_id.name', '=', line), ('state', 'not in', ['draft'])]) + [('production_line_id.name', '=', line), ('state', 'in', ['finished'])]) # 工单计划量 plan_data_plan_counts = plan_obj.search_count( [('production_line_id.name', '=', line), ('state', 'not in', ['finished'])]) @@ -349,67 +349,66 @@ class Sf_Dashboard_Connect(http.Controller): return json.dumps(res) # 日完成量统计 - class DailyFinishCount(http.Controller): - @http.route('/api/DailyFinishCount', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*") - def DailyFinishCount(self, **kw): - """ - 获取日完成量统计 - :param kw: - :return: - """ - res = {'status': 1, 'message': '成功', 'data': {}} - plan_obj = request.env['sf.production.plan'].sudo() - line_list = ast.literal_eval(kw['line_list']) - begin_time_str = kw['begin_time'].strip('"') - end_time_str = kw['end_time'].strip('"') - begin_time = datetime.strptime(begin_time_str, '%Y-%m-%d %H:%M:%S') - end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S') - print('line_list: %s' % line_list) + @http.route('/api/DailyFinishCount', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*") + def DailyFinishCount(self, **kw): + """ + 获取日完成量统计 + :param kw: + :return: + """ + res = {'status': 1, 'message': '成功', 'data': {}} + plan_obj = request.env['sf.production.plan'].sudo() + line_list = ast.literal_eval(kw['line_list']) + begin_time_str = kw['begin_time'].strip('"') + end_time_str = kw['end_time'].strip('"') + begin_time = datetime.strptime(begin_time_str, '%Y-%m-%d %H:%M:%S') + end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S') + print('line_list: %s' % line_list) - def get_date_list(start_date, end_date): - date_list = [] - current_date = start_date - while current_date <= end_date: - date_list.append(current_date) - current_date += timedelta(days=1) - return date_list + def get_date_list(start_date, end_date): + date_list = [] + current_date = start_date + while current_date <= end_date: + date_list.append(current_date) + current_date += timedelta(days=1) + return date_list - for line in line_list: - date_list = get_date_list(begin_time, end_time) - order_counts = [] + for line in line_list: + date_list = get_date_list(begin_time, end_time) + order_counts = [] - date_field_name = 'actual_end_time' # 替换为你模型中的实际字段名 + date_field_name = 'actual_end_time' # 替换为你模型中的实际字段名 - for date in date_list: - next_day = date + timedelta(days=1) - orders = plan_obj.search([('production_line_id.name', '=', line), ('state', 'not in', ['draft']), - (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), - (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) - ]) + for date in date_list: + next_day = date + timedelta(days=1) + orders = plan_obj.search([('production_line_id.name', '=', line), ('state', 'in', ['finished']), + (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), + (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) + ]) - rework_orders = plan_obj.search( - [('production_line_id.name', '=', line), ('state', 'in', ['rework']), - (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), - (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) - ]) - not_passed_orders = plan_obj.search( - [('production_line_id.name', '=', line), ('state', 'in', ['scrap', 'cancel']), - (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), - (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) - ]) - order_counts.append({ - 'date': date.strftime('%Y-%m-%d'), - 'order_count': len(orders), - 'rework_orders': len(rework_orders), - 'not_passed_orders': len(not_passed_orders) - }) - # 外面包一层,没什么是包一层不能解决的,包一层就能区分了,类似于包一层div - # 外面包一层的好处是,可以把多个数据结构打包在一起,方便前端处理 + rework_orders = plan_obj.search( + [('production_line_id.name', '=', line), ('production_id.state', 'in', ['rework']), + (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), + (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) + ]) + not_passed_orders = plan_obj.search( + [('production_line_id.name', '=', line), ('production_id.state', 'in', ['scrap', 'cancel']), + (date_field_name, '>=', date.strftime('%Y-%m-%d 00:00:00')), + (date_field_name, '<', next_day.strftime('%Y-%m-%d 00:00:00')) + ]) + order_counts.append({ + 'date': date.strftime('%Y-%m-%d'), + 'order_count': len(orders), + 'rework_orders': len(rework_orders), + 'not_passed_orders': len(not_passed_orders) + }) + # 外面包一层,没什么是包一层不能解决的,包一层就能区分了,类似于包一层div + # 外面包一层的好处是,可以把多个数据结构打包在一起,方便前端处理 - # date_list_dict = {line: order_counts} + # date_list_dict = {line: order_counts} - res['data'][line] = order_counts - return json.dumps(res) + res['data'][line] = order_counts + return json.dumps(res) # 实时产量 @http.route('/api/RealTimeProduct', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*") @@ -770,10 +769,11 @@ class Sf_Dashboard_Connect(http.Controller): @http.route('/api/OEEByTime', type='http', auth='public', methods=['GET', 'POST'], csrf=False, cors="*") def OEEByTime(self, **kw): """ - 获取某段时间的oee,根据用户指定的时间单位(day或hour)返回对应的平均值 + 获取某段时间的OEE,根据用户指定的时间单位(day或hour)返回对应的平均值。 + 如果不传time_unit,则默认按天返回,并补全没有数据的时间段,填充0值。 """ res = {'status': 1, 'message': '成功', 'data': {}} - logging.info('前端请求获取某段时间的oee的参数为:%s' % kw) + logging.info('前端请求获取某段时间的OEE的参数为:%s' % kw) # 获取并解析参数 workcenter_list = ast.literal_eval(kw['workcenter_list']) @@ -790,8 +790,10 @@ class Sf_Dashboard_Connect(http.Controller): # 根据时间单位选择不同的时间格式 if time_unit == 'hour': time_format = 'YYYY-MM-DD HH24:00:00' + time_delta = timedelta(hours=1) else: # 默认为'day' time_format = 'YYYY-MM-DD' + time_delta = timedelta(days=1) # 查询并计算OEE平均值 oee_data = {} @@ -806,7 +808,20 @@ class Sf_Dashboard_Connect(http.Controller): """, (workcenter, begin_time, end_time)) results = cur.fetchall() - oee_data[workcenter] = {row[0]: row[1] for row in results} + # 初始化当前产线的OEE数据字典 + workcenter_oee = {row[0]: row[1] for row in results} + + # 补全缺失的时间段 + current_time = begin_time + if time_unit != 'hour': + while current_time <= end_time: + time_key = current_time.strftime('%Y-%m-%d') + if time_key not in workcenter_oee: + workcenter_oee[time_key] = 0 + current_time += time_delta + + # 按时间排序 + oee_data[workcenter] = dict(sorted(workcenter_oee.items())) # 关闭数据库连接 cur.close()