154 lines
6.9 KiB
Python
154 lines
6.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
import logging
|
||
import json
|
||
import base64
|
||
import requests
|
||
from odoo import models
|
||
from odoo.exceptions import ValidationError
|
||
|
||
_logger = logging.getLogger(__name__)
|
||
|
||
|
||
class MrsShelfLocationDataSync(models.Model):
|
||
_name = 'sf.shelf.location.datasync'
|
||
_description = '同步库存信息'
|
||
|
||
def get_total_data(self):
|
||
|
||
# 建立对应关系的函数
|
||
def align_data(my_data, their_data):
|
||
paired_data = list(zip(my_data, their_data))
|
||
return paired_data
|
||
|
||
logging.info('============================get_total_data()======================')
|
||
shelf_1_obj = self.env['sf.shelf'].search([('name', '=', '一号产线-一号线边刀架')], limit=1)
|
||
tool_location_objs_1 = self.env['sf.shelf.location'].search([('shelf_id', '=', shelf_1_obj.id)], order='id')
|
||
|
||
location_codes_1 = [location.barcode for location in tool_location_objs_1]
|
||
print(location_codes_1)
|
||
# 对方的数据列表
|
||
their_data_1 = [f"ToolCab1-{i:02}" for i in range(1, 73)]
|
||
|
||
# 执行对齐
|
||
aligned_data_1 = align_data(location_codes_1, their_data_1)
|
||
|
||
# 2
|
||
shelf_2_obj = self.env['sf.shelf'].search([('name', '=', '一号产线-二号线边刀架')], limit=1)
|
||
tool_location_objs_2 = self.env['sf.shelf.location'].search([('shelf_id', '=', shelf_2_obj.id)], order='id')
|
||
|
||
location_codes_2 = [location.barcode for location in tool_location_objs_2]
|
||
print(location_codes_2)
|
||
# 对方的数据列表
|
||
their_data_2 = [f"ToolCab2-{i:02}" for i in range(1, 73)]
|
||
|
||
# 执行对齐
|
||
aligned_data_2 = align_data(location_codes_2, their_data_2)
|
||
|
||
# 4
|
||
shelf_4_obj = self.env['sf.shelf'].search([('name', '=', '一号产线-一号线边料架')], limit=1)
|
||
tool_location_objs_4 = self.env['sf.shelf.location'].search([('shelf_id', '=', shelf_4_obj.id)], order='id')
|
||
|
||
location_codes_4 = [location.barcode for location in tool_location_objs_4]
|
||
print(location_codes_4)
|
||
# 对方的数据列表
|
||
their_data_4 = [f"PartCab4-{i:02}" for i in range(1, 17)]
|
||
|
||
# 执行对齐
|
||
aligned_data_4 = align_data(location_codes_4, their_data_4)
|
||
|
||
# 3
|
||
shelf_3_obj = self.env['sf.shelf'].search([('name', '=', '一号产线-二号线边料架')], limit=1)
|
||
tool_location_objs_3 = self.env['sf.shelf.location'].search([('shelf_id', '=', shelf_3_obj.id)], order='id')
|
||
|
||
location_codes_3 = [location.barcode for location in tool_location_objs_3]
|
||
print(location_codes_3)
|
||
# 对方的数据列表
|
||
their_data_3 = [f"PartCab3-{i:02}" for i in range(1, 13)]
|
||
|
||
# 执行对齐
|
||
aligned_data_3 = align_data(location_codes_3, their_data_3)
|
||
|
||
# 5
|
||
shelf_5_obj = self.env['sf.shelf'].search([('name', '=', '一号产线-三号线边料架')], limit=1)
|
||
tool_location_objs_5 = self.env['sf.shelf.location'].search([('shelf_id', '=', shelf_5_obj.id)], order='id')
|
||
|
||
location_codes_5 = [location.barcode for location in tool_location_objs_5]
|
||
print(location_codes_5)
|
||
# 对方的数据列表
|
||
their_data_5 = [f"PartCab5-{i:02}" for i in range(1, 13)]
|
||
|
||
# 执行对齐
|
||
aligned_data_5 = align_data(location_codes_5, their_data_5)
|
||
|
||
total_data = aligned_data_1 + aligned_data_2 + aligned_data_3 + aligned_data_4 + aligned_data_5
|
||
print(total_data)
|
||
logging.info(f"total_data: {total_data}")
|
||
return total_data
|
||
|
||
def find_our_code(self, total_data, their_code):
|
||
for code_pair in total_data:
|
||
if code_pair[1] == their_code:
|
||
return code_pair[0]
|
||
return None # 如果没有找到对应的值,返回None或适当的默认值
|
||
|
||
def _cron_shelf_location_datasync(self):
|
||
try:
|
||
def find_their_code(my_code, aligned_data):
|
||
for code_pair in aligned_data:
|
||
if code_pair[0] == my_code:
|
||
return code_pair[1]
|
||
return None # 如果没有找到对应的值,返回None或适当的默认值
|
||
|
||
# 定时更新所有设备机床刀库信息
|
||
equipment_ids = self.env['maintenance.equipment'].search(
|
||
[('equipment_type', '=', '机床'), ('function_type', '!=', False)])
|
||
for equipment_id in equipment_ids:
|
||
if equipment_id:
|
||
equipment_id.register_equipment_tool()
|
||
|
||
shelfinfo = self.env['sf.shelf.location'].get_sf_shelf_location_info()
|
||
self.set_shelf_location(shelfinfo)
|
||
|
||
except Exception as e:
|
||
logging.info("库区信息同步失败:%s" % e)
|
||
raise ValidationError("数据错误导致同步失败,请联系管理员")
|
||
|
||
def set_shelf_location(self, shelfinfo):
|
||
print('shelfinfo:', shelfinfo)
|
||
total_data = self.get_total_data()
|
||
for item in shelfinfo:
|
||
logging.info('货架已获取信息:%s' % item)
|
||
shelf_barcode = self.env['sf.shelf.location.datasync'].sudo().find_our_code(
|
||
total_data, item['Postion'])
|
||
location_id = self.env['sf.shelf.location'].sudo().search(
|
||
[('barcode', '=', shelf_barcode)],
|
||
limit=1)
|
||
if location_id:
|
||
# 如果是线边刀库信息,则对功能刀具移动生成记录
|
||
if 'Tool' in item['Postion']:
|
||
tool = self.env['sf.functional.cutting.tool.entity'].sudo().search(
|
||
[('rfid', '=', item['RfidCode']), ('functional_tool_status', '!=', '已拆除')])
|
||
tool.sudo().tool_in_out_stock_location(location_id)
|
||
if tool:
|
||
location_id.product_sn_id = tool.barcode_id.id
|
||
if item.get('State') == '报警' and tool.functional_tool_status != '报警':
|
||
# 创建报警刀具拆解单和刀具报警记录
|
||
tool.create_tool_dismantle()
|
||
# 修改功能刀具标准状态值和已使用寿命值、功能刀具状态
|
||
if 'LifeStd' in item and 'LifeUse' in item:
|
||
tool.sudo().write({
|
||
'max_lifetime_value': item['LifeStd'],
|
||
'used_value': item['LifeUse'],
|
||
'functional_tool_status': item['State'],
|
||
})
|
||
else:
|
||
location_id.product_sn_id = False
|
||
if item['RfidCode']:
|
||
logging.info('Rfid为【%s】的功能刀具在系统中不存在!' % item['RfidCode'])
|
||
else:
|
||
stock_lot_obj = self.env['stock.lot'].search([('rfid', '=', item['RfidCode'])], limit=1)
|
||
if stock_lot_obj:
|
||
location_id.product_sn_id = stock_lot_obj.id
|
||
else:
|
||
location_id.product_sn_id = False
|