# -*- coding: utf-8 -*-
import requests
import json
import base64
from odoo import models
from odoo.exceptions import ValidationError
import logging
from odoo.addons.sf_base.commons.common import Common

_logger = logging.getLogger(__name__)


class sfProductionMaterials(models.Model):
    _inherit = "sf.production.materials"
    _description = "材料"
    url = '/api/production_materials/list'

    # 定时同步每日材料
    def sync_production_materials(self):

        # 配置中获取token
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url

        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_materials_yesterday_list']:
                if item:
                    brand = self.env['sf.production.materials'].search(
                        [("materials_no", '=', item['materials_no'])])
                    if brand:
                        brand.id = item['id'],
                        brand.name = item['name'],
                        brand.remark = item['remark'],
                        brand.active = item['active']
                    else:
                        self.env['sf.production.materials'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "materials_no": item['materials_no'],
                            "remark": item['remark'],
                            "active": item['active']
                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有材料
    def sync_all_production_materials(self):
        _logger = '正在同步所有材料'
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_materials_all_list']:
                if item:

                    brand = self.env['sf.production.materials'].search(
                        [("materials_no", '=', item['materials_no'])])
                    if not brand:
                        self.env['sf.production.materials'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "materials_no": item['materials_no'],
                            "remark": item['remark'],
                            "active": item['active']
                        })
        else:
            raise ValidationError("认证未通过")


class sfMaterialModel(models.Model):
    _inherit = 'sf.materials.model'
    _description = '材料型号'
    url = '/api/materials_model/list'

    # 定时同步每日材料型号
    def sync_materials_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['materials_model_yesterday_list']:
                if item:
                    brand = self.env['sf.materials.model'].search(
                        [("materials_no", '=', item['materials_no'])])
                    if brand:
                        brand.id = item['id'],
                        brand.name = item['name'],
                        brand.materials_no = item['materials_no'],
                        brand.remark = item['remark'],
                        brand.active = item['active'],
                        brand.mf_materia_post = item['mf_materia_post'],
                        brand.materials_id = self.env['sf.production.materials'].search(
                            [("materials_no", '=', item['materials_id.materials_no'])]).id,
                        brand.need_h = item['need_h'],
                        brand.density = item['density']
                    else:
                        self.env['sf.materials.model'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "materials_no": item['materials_no'],
                            "remark": item['remark'],
                            "active": item['active'],
                            "materials_id": self.env['sf.production.materials'].search(
                                [("materials_no", '=', item['materials_id.materials_no'])]).id,
                            "need_h": item['need_h'],
                            "mf_materia_post": item['mf_materia_post'],
                            "density": item['density'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有材料型号
    def sync_all_materials_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)
        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['materials_model_all_list']:
                if item:
                    brand = self.env['sf.materials.model'].search(
                        [("materials_no", '=', item['materials_no'])])
                    if not brand:
                        self.env['sf.materials.model'].create({
                            "name": item['name'],
                            "materials_no": item['materials_no'],
                            "remark": item['remark'],
                            "active": item['active'],
                            "materials_id": self.env['sf.production.materials'].search(
                                [("materials_no", '=', item['materials_id.materials_no'])]).id,
                            "need_h": item['need_h'],
                            "mf_materia_post": item['mf_materia_post'],
                            "density": item['density'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")


class sfProductionProcessCategory(models.Model):
    _inherit = 'sf.production.process.category'
    _description = '表面工艺类别'
    url = '/api/production_process_category/list'

    # 定时同步每日表面工艺类别
    def sync_production_process_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_process_category_yesterday_list']:
                if item:
                    production_process_category = self.env['sf.production.process.category'].search(
                        [("code", '=', item['code'])])
                    if production_process_category:
                        production_process_category.name = item['name'],
                        production_process_category.code = item['code'],
                        production_process_category.active = item['active']
                    else:
                        self.env['sf.production.process.category'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "active": item['active'],
                        })
        else:
            raise ValidationError("认证未通过")  # 定时同步表面工艺

    # 同步所有表面工艺类别
    def sync_all_production_process_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_process_category_all_list']:
                if item:
                    category = self.env['sf.production.process.category'].search(
                        [("code", '=', item['code'])])
                    if not category:
                        self.env['sf.production.process.category'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "active": item['active'],
                        })
        else:
            raise ValidationError("认证未通过")


class sfProductionProcess(models.Model):
    _inherit = 'sf.production.process'
    _description = '表面工艺'
    url = '/api/production_process/list'

    # 定时同步每日表面工艺
    def sync_production_process(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_process_yesterday_list']:
                if item:
                    brand = self.env['sf.production.process'].search(
                        [("process_encode", '=', item['process_encode'])])
                    if brand:
                        brand.name = item['name'],
                        brand.category_id = self.env['sf.production.process.category'].search(
                            [("code", '=', item['category_code'])]).id,
                        brand.process_encode = item['process_encode'],
                        brand.remark = item['remark'],
                        brand.active = item['active'],
                        brand.remark = item['remark']
                    else:
                        self.env['sf.production.process'].create({
                            "name": item['name'],
                            "category_id": self.env['sf.production.process.category'].search(
                                [("code", '=', item['category_code'])]).id,
                            "process_encode": item['process_encode'],
                            "remark": item['remark'],
                            "active": item['active'],
                        })
        else:
            raise ValidationError("认证未通过")  # 定时同步表面工艺

    # 同步所有表面工艺
    def sync_all_production_process(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['production_process_all_list']:
                if item:
                    brand = self.env['sf.production.process'].search(
                        [("process_encode", '=', item['process_encode'])])
                    if not brand:
                        self.env['sf.production.process'].create({
                            "name": item['name'],
                            "category_id": self.env['sf.production.process.category'].search(
                                [("code", '=', item['category_code'])]).id,
                            "process_encode": item['process_encode'],
                            "remark": item['remark'],
                            "active": item['active'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")


class sfProcessingTechnology(models.Model):
    _inherit = 'sf.processing.technology'
    _description = '加工工艺'
    url = '/api/processing_technology/list'

    # 定时同步加工工艺
    def sync_processing_technology(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['processing_technology_yesterday_list']:
                if item:
                    brand = self.env['sf.processing.technology'].search(
                        [("process_encode", '=', item['process_encode'])])
                    if brand:
                        brand.id = item['id'],
                        brand.name = item['name'],
                        brand.process_encode = item['process_encode'],
                        brand.remark = item['remark'],
                        brand.active = item['active']
                    else:
                        self.env['sf.processing.technology'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "process_encode": item['process_encode'],
                            "remark": item['remark'],
                            "active": item['active'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有加工工艺
    def sync_all_processing_technology(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['processing_technology_all_list']:
                if item:
                    brand = self.env['sf.processing.technology'].search(
                        [("process_encode", '=', item['process_encode'])])
                    if not brand:
                        self.env['sf.processing.technology'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "process_encode": item['process_encode'],
                            "remark": item['remark'],
                            "active": item['active'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")


class MachineBrandTags(models.Model):
    _inherit = 'sf.machine.brand.tags'
    _description = '品牌类别'
    url = '/api/machine_brand_tags/list'

    # 定时同步品牌类别
    def sync_machine_brand_tags(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_brand_tags_yesterday_list']:
                brand = self.env['sf.machine.brand.tags'].search(
                    [("id", '=', item['id'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.color = item['color']
                else:
                    self.env['sf.machine.brand.tags'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "color": item['color'],

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有品牌类别
    def sync_all_machine_brand_tags(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_brand_tags_all_list']:
                brand = self.env['sf.machine.brand.tags'].search(
                    [("name", '=', item['name'])])
                if not brand:
                    self.env['sf.machine.brand.tags'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "color": item['color'],

                    })

        else:
            raise ValidationError("认证未通过")


class MachineControlSystem(models.Model):
    _inherit = 'sf.machine.control_system'
    _description = '控制系统'
    url = '/api/machine_control_system/list'

    # 定时同步控制系统
    def sync_machine_tool_type_control_system(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_control_system_yesterday_list']:
                if item:
                    brand = self.env['sf.machine.control_system'].search(
                        [("code", '=', item['code'])])
                    if brand:
                        brand.id = item['id'],
                        brand.name = item['name'],
                        brand.code = item['code'],
                        brand.brand_id = self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        brand.active = item['active']
                    else:
                        self.env['sf.machine.control_system'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "code": item['code'],

                            'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                            "active": item['active'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有控制系统
    def sync_all_machine_tool_type_control_system(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url

        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_control_system_all_list']:
                if item:
                    brand = self.env['sf.machine.control_system'].search(
                        [("code", '=', item['code'])])
                    if not brand:
                        self.env['sf.machine.control_system'].create({
                            "id": item['id'],
                            "name": item['name'],
                            "code": item['code'],
                            'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,

                            "active": item['active'],
                            # "tag_ids": item['tag_ids']

                        })
        else:
            raise ValidationError("认证未通过")


class MachineBrand(models.Model):
    _inherit = 'sf.machine.brand'
    _description = '品牌'
    url = '/api/machine_brand/list'

    # 定时同步品牌
    def sync_machine_brand(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url

        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_brand_yesterday_list']:
                brand = self.env['sf.machine.brand'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.active = item['active']
                else:
                    self.env['sf.machine.brand'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],
                        # "image_brand": item['image_brand'],
                        "active": item['active'],
                        # "tag_ids": item['tag_ids']

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有品牌
    def sync_all_machine_brand(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url

        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_brand_all_list']:
                brand = self.env['sf.machine.brand'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.machine.brand'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],
                        "image_brand": '' if not item['image_brand'] else base64.b64encode(item.image_brand),
                        "tag_ids": self.env['sf.machine.brand.tags'].search(
                            [("name", 'in', item['tag_ids'])]).ids
                    })
        else:
            raise ValidationError("认证未通过")


class MachineTool(models.Model):
    _inherit = 'sf.machine_tool'
    _description = '机床'
    url = '/api/machine_tool/list'
    crea_url = '/api/machine_tool/create'

    # 定时同步机床
    def sync_machine_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_all_yesterday_list']:
                brand = self.env['sf.machine_tool'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.precision = item['precision'],

                    brand.knife_type = item['knife_type'],
                    brand.registration_date = item['registration_date'],
                    brand.number_of_knife_library = item['number_of_knife_library'],
                    brand.rotate_speed = item['rotate_speed'],
                    brand.number_of_axles = item['number_of_axles'],
                    # brand.control_system_id = item['control_system_id'],
                    brand.type_id = item['type_id'],
                    brand.brand_id = item['brand_id'],
                    brand.x_axis = item['x_axis'],
                    brand.y_axis = item['y_axis'],
                    brand.z_axis = item['z_axis'],
                    brand.b_axis = item['b_axis'],
                    brand.c_axis = item['c_axis'],
                    brand.state = item['state'],
                    brand.active = item['active']

                else:
                    self.env['sf.machine_tool'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "precision": item['precision'],
                        "code": item['code'],
                        "status": item['status'],
                        "knife_type": item['knife_type'],
                        "registration_date": item['registration_date'],
                        "number_of_knife_library": item['number_of_knife_library'],
                        "rotate_speed": item['rotate_speed'],
                        "number_of_axles": item['number_of_axles'],
                        # "control_system_id": item['control_system_id'],
                        "type_id": item['type_id'],
                        "brand_id": item['brand_id'],
                        "x_axis": item['x_axis'],
                        "y_axis": item['y_axis'],
                        "z_axis": item['z_axis'],
                        "b_axis": item['b_axis'],
                        "c_axis": item['c_axis'],
                        "state": item['state'],
                        "active": item['active'],
                        # "tag_ids": item['tag_ids']

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有机床
    def sync_all_machine_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_all_list']:
                brand = self.env['sf.machine_tool'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.machine_tool'].create({

                        "id": item['id'],
                        "name": item['name'],
                        "precision": item['precision'],
                        "code": item['code'],
                        "status": item['status'],
                        "knife_type": item['knife_type'],
                        "registration_date": item['registration_date'],
                        "number_of_knife_library": item['number_of_knife_library'],
                        "rotate_speed": item['rotate_speed'],
                        "number_of_axles": item['number_of_axles'],
                        # "control_system_id": item['control_system_id'],
                        "type_id": item['type_id'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        "x_axis": item['x_axis'],
                        "y_axis": item['y_axis'],
                        "z_axis": item['z_axis'],
                        "b_axis": item['b_axis'],
                        "c_axis": item['c_axis'],
                        "state": item['state'],
                        "active": item['active'],
                        # "tag_ids": item['tag_ids']

                    })
        else:
            raise ValidationError("认证未通过")


class MachineToolType(models.Model):
    _inherit = 'sf.machine_tool.type'
    _description = '机床型号'
    url = '/api/machine_tool_type/list'

    # 定时同步机床型号
    def sync_machine_tool_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_type_yesterday_list']:
                brand = self.env['sf.machine_tool.type'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.precision = item['precision'],
                    brand.number_of_knife_library = item['number_of_knife_library'],
                    brand.rotate_speed = item['rotate_speed'],
                    brand.number_of_axles = item['number_of_axles'],
                    brand.control_system_id = self.env['sf.machine.control_system'].search(
                        [('code', '=', item['control_system_id'])]).id,

                    brand.x_axis = item['x_axis'],
                    brand.y_axis = item['y_axis'],
                    brand.z_axis = item['z_axis'],
                    brand.b_axis = item['b_axis'],
                    brand.c_axis = item['c_axis'],
                    brand.active = item['active'],
                    brand.remark = item['remark'],
                    brand.brand_id = self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                    brand.machine_tool_id = self.env['sf.machine_tool'].search(
                        [('code', '=', item['machine_tool_id'])]).id

                else:
                    # print(item['machine_tool_picture'].encode('utf-8'))
                    self.env['sf.machine_tool.type'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],

                        "number_of_knife_library": item['number_of_knife_library'],
                        "rotate_speed": item['rotate_speed'],
                        'machine_tool_id': self.env['sf.machine_tool'].search(
                            [('code', '=', item['machine_tool_id'])]).id,
                        "number_of_axles": item['number_of_axles'],
                        "x_axis": item['x_axis'],
                        "y_axis": item['y_axis'],
                        "z_axis": item['z_axis'],
                        "b_axis": item['b_axis'],
                        "c_axis": item['c_axis'],
                        "remark": item['remark'],
                        "precision": item['precision'],
                        'control_system_id': self.env['sf.machine.control_system'].search(
                            [('code', '=', item['control_system_id'])]).id,
                        "active": item['active'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        'machine_tool_picture': item['machine_tool_picture'].encode('utf-8'),
                        "heightened_way": item['heightened_way'],
                        "workpiece_load": item['workpiece_load'],
                        "lead_screw": item['lead_screw'],
                        "workbench_L": item['workbench_L'],
                        "workbench_W": item['workbench_W'],
                        "guide_rail": item['guide_rail'],
                        "machine_tool_L": item['machine_tool_L'],
                        "machine_tool_W": item['machine_tool_W'],
                        "machine_tool_H": item['machine_tool_H'],
                        "feed_speed": item['feed_speed'],
                        "tool_speed": item['tool_speed'],
                        "distance": item['distance'],
                        "taper": item['taper'],
                        "torque": item['torque'],
                        "motor_power": item['motor_power'],
                        "tool_quality_max": item['tool_quality_max'],
                        "tool_long_max": item['tool_long_max'],
                        "tool_diameter_max": item['tool_diameter_max'],
                        "machine_tool_category": self.env['sf.machine_tool.category'].search(
                            [('code', '=', item['machine_tool_category'])]).id,

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有机床型号
    def sync_all_machine_tool_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_type_all_list']:
                brand = self.env['sf.machine_tool.type'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.machine_tool.type'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],

                        "number_of_knife_library": item['number_of_knife_library'],
                        "rotate_speed": item['rotate_speed'],
                        'machine_tool_id': self.env['sf.machine_tool'].search(
                            [('code', '=', item['machine_tool_id'])]).id,
                        "number_of_axles": item['number_of_axles'],
                        "x_axis": item['x_axis'],
                        "y_axis": item['y_axis'],
                        "z_axis": item['z_axis'],
                        "b_axis": item['b_axis'],
                        "c_axis": item['c_axis'],
                        "remark": item['remark'],
                        "precision": item['precision'],
                        'control_system_id': self.env['sf.machine.control_system'].search(
                            [('code', '=', item['control_system_id'])]).id,
                        "active": item['active'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        'machine_tool_picture': base64.b64decode(item['machine_tool_picture']),
                        "heightened_way": item['heightened_way'],
                        "workpiece_load": item['workpiece_load'],
                        "lead_screw": item['lead_screw'],
                        "workbench_L": item['workbench_L'],
                        "workbench_W": item['workbench_W'],
                        "guide_rail": item['guide_rail'],
                        "machine_tool_L": item['machine_tool_L'],
                        "machine_tool_W": item['machine_tool_W'],
                        "machine_tool_H": item['machine_tool_H'],
                        "feed_speed": item['feed_speed'],
                        "tool_speed": item['tool_speed'],
                        "distance": item['distance'],
                        "taper": item['taper'],
                        "torque": item['torque'],
                        "motor_power": item['motor_power'],
                        "tool_quality_max": item['tool_quality_max'],
                        "tool_long_max": item['tool_long_max'],
                        "tool_diameter_max": item['tool_diameter_max'],
                        "machine_tool_category": self.env['sf.machine_tool.category'].search(
                            [('code', '=', item['machine_tool_category'])]).id,

                    })
                else:
                    brand.write({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],

                        "number_of_knife_library": item['number_of_knife_library'],
                        "rotate_speed": item['rotate_speed'],
                        'machine_tool_id': self.env['sf.machine_tool'].search(
                            [('code', '=', item['machine_tool_id'])]).id,
                        "number_of_axles": item['number_of_axles'],
                        "x_axis": item['x_axis'],
                        "y_axis": item['y_axis'],
                        "z_axis": item['z_axis'],
                        "b_axis": item['b_axis'],
                        "c_axis": item['c_axis'],
                        "remark": item['remark'],
                        "precision": item['precision'],
                        'control_system_id': self.env['sf.machine.control_system'].search(
                            [('code', '=', item['control_system_id'])]).id,
                        "active": item['active'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        'machine_tool_picture': base64.b64decode(item['machine_tool_picture']),
                        "heightened_way": item['heightened_way'],
                        "workpiece_load": item['workpiece_load'],
                        "lead_screw": item['lead_screw'],
                        "workbench_L": item['workbench_L'],
                        "workbench_W": item['workbench_W'],
                        "guide_rail": item['guide_rail'],
                        "machine_tool_L": item['machine_tool_L'],
                        "machine_tool_W": item['machine_tool_W'],
                        "machine_tool_H": item['machine_tool_H'],
                        "feed_speed": item['feed_speed'],
                        "tool_speed": item['tool_speed'],
                        "distance": item['distance'],
                        "taper": item['taper'],
                        "torque": item['torque'],
                        "motor_power": item['motor_power'],
                        "tool_quality_max": item['tool_quality_max'],
                        "tool_long_max": item['tool_long_max'],
                        "tool_diameter_max": item['tool_diameter_max'],
                        "machine_tool_category": self.env['sf.machine_tool.category'].search(
                            [('code', '=', item['machine_tool_category'])]).id,

                    })
        else:
            raise ValidationError("认证未通过")


class CuttingTool(models.Model):
    _inherit = 'sf.cutting_tool.category'
    _description = '刀具类别'
    url = '/api/cutting_tool_category/list'

    # 定时同步刀具类别
    def sync_cutting_tool_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['cutting_tool_category_yesterday_list']:
                brand = self.env['sf.cutting_tool.category'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.active = item['active'],
                    brand.remark = item['remark']
                else:
                    self.env['sf.cutting_tool.category'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],
                        "remark": item['remark'],
                        "active": item['active'],
                        # "tag_ids": item['tag_ids']

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀具类别
    def sync_all_cutting_tool_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['cutting_tool_category_all_list']:
                brand = self.env['sf.cutting_tool.category'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.cutting_tool.category'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "code": item['code'],
                        "remark": item['remark'],
                        "active": item['active'],
                        # "tag_ids": item['tag_ids']

                    })
        else:
            raise ValidationError("认证未通过")


class CuttingToolType(models.Model):
    _inherit = 'sf.cutting_tool.type'
    _description = '刀具型号'
    url = '/api/cutting_tool_type/list'

    # 定时同步刀具型号
    def sync_cutting_tool_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['cutting_tool_type_yesterday_list']:
                brand = self.env['sf.cutting_tool.type'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.id = item['id'],
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.active = item['active'],
                    brand.remark = item['remark'],
                    brand.diameter = item['diameter'],
                    brand.cone_angle_pitch = item['cone_angle_pitch'],
                    brand.shank_diameter = item['shank_diameter'],
                    brand.long_blade = item['long_blade'],
                    brand.taper_shank_length = item['taper_shank_length'],
                    brand.tool_length = item['tool_length'],
                    brand.blade_number = item['blade_number']
                    brand.brand_id = self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                    brand.category_id = self.env['sf.cutting_tool.category'].search(
                        [('code', '=', item['category_id'])]).id
                else:
                    self.env['sf.cutting_tool.type'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "remark": item['remark'],
                        "code": item['code'],
                        "active": item['active'],
                        "diameter": item['diameter'],
                        "cone_angle_pitch": item['cone_angle_pitch'],
                        "shank_diameter": item['shank_diameter'],
                        "long_blade": item['long_blade'],
                        "taper_shank_length": item['taper_shank_length'],
                        "tool_length": item['tool_length'],
                        "blade_number": item['blade_number'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        "category_id": self.env['sf.cutting_tool.category'].search(
                            [('code', '=', item['category_id'])]).id,
                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀具型号
    def sync_all_cutting_tool_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['cutting_tool_type_all_list']:
                brand = self.env['sf.cutting_tool.type'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.cutting_tool.type'].create({
                        "id": item['id'],
                        "name": item['name'],
                        "remark": item['remark'],
                        "code": item['code'],
                        "active": item['active'],
                        "diameter": item['diameter'],
                        "cone_angle_pitch": item['cone_angle_pitch'],
                        "shank_diameter": item['shank_diameter'],
                        "long_blade": item['long_blade'],
                        "taper_shank_length": item['taper_shank_length'],
                        "tool_length": item['tool_length'],
                        "blade_number": item['blade_number'],
                        'brand_id': self.env['sf.machine.brand'].search([('code', '=', item['brand_id'])]).id,
                        "category_id": self.env['sf.cutting_tool.category'].search(
                            [('code', '=', item['category_id'])]).id,
                    })
        else:
            raise ValidationError("认证未通过")


class sfProcessingOrder(models.Model):
    _inherit = 'sf.processing.order'
    _description = '工序'
    url = '/api/processing_order/list'

    # 定时同步工序
    def sync_processing_order(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['processing_order_yesterday_list']:
                brand = self.env['sf.processing.order'].search(
                    [("id", '=', item['id'])])
                if brand:
                    brand.id = item['id']
                    brand.sequence = item['sequence']
                else:
                    self.env['sf.processing.order'].create({
                        "id": item['id'],
                        "sequence": item['sequence'],

                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有工序
    def sync_all_processing_order(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['processing_order_all_list']:
                brand = self.env['sf.processing.order'].search(
                    [("id", '=', item['id'])])
                if not brand:
                    self.env['sf.processing.order'].create({
                        "id": item['id'],
                        "sequence": item['sequence'],
                    })
        else:
            raise ValidationError("认证未通过")


class sfProductionProcessParameter(models.Model):
    _inherit = 'sf.production.process.parameter'
    _description = '表面工艺可选参数'
    url = '/api/production_process_parameter/list'

    # 定时同步每日表面工艺
    def sync_production_process_parameter(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['mrs_production_process_parameter_yesterday_list']:
                if item:
                    brand = self.env['sf.production.process.parameter'].search(
                        [("code", '=', item['code'])])
                    if brand:
                        brand.name = item['name'],
                        brand.code = item['code'],
                        brand.active = item['active'],
                        # brand.price = item['price'],
                    else:
                        self.env['sf.production.process.parameter'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "active": item['active'],
                            # "price": item['price'],
                            "process_id": self.env['sf.production.process'].search(
                                [('process_encode', '=', item['process_id_code'])]).id,
                            'materials_model_ids': self.env['sf.materials.model'].search(
                                [('materials_no', 'in', item['materials_model_ids_codes'])])

                        })
        else:
            raise ValidationError("认证未通过")  # 定时同步表面工艺

    # 同步所有表面工艺
    def sync_all_production_process_parameter(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_production_process_parameter_all_list']:
                if item:
                    _logger.info('create可选参数:%s' % item)
                    brand = self.env['sf.production.process.parameter'].search(
                        [("code", '=', item['code'])])
                    if not brand:
                        _logger.info('create可选参数:%s' % item)
                        self.env['sf.production.process.parameter'].create({
                            "name": item['name'],
                            "code": item['code'],
                            # "price": item['price'],
                            "active": item['active'],
                            "process_id": self.env['sf.production.process'].search(
                                [('process_encode', '=', item['process_id_code'])]).id,
                            'materials_model_ids': self.env['sf.materials.model'].search(
                                [('materials_no', 'in', item['materials_model_ids_codes'])])

                        })
        else:
            raise ValidationError("认证未通过")


class MachineToolCategory(models.Model):
    _inherit = 'sf.machine_tool.category'
    _description = '机床类型'
    url = '/api/machine_tool_category/list'

    # 定时同步机床类型
    def sync_machine_tool_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)
        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_category_yesterday_list']:
                brand = self.env['sf.machine_tool.category'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.category = item['category'],
                    brand.remark = item['remark']

                else:
                    brand.name = item['name'],
                    brand.code = item['code'],
                    brand.category = item['category'],
                    brand.remark = item['remark']
        else:
            raise ValidationError("认证未通过")

    # 同步所有机床类型
    def sync_all_machine_tool_category(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['machine_tool_category_all_list']:
                brand = self.env['sf.machine_tool.category'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.machine_tool.category'].create({
                        "name": item['name'],
                        "code": item['code'],
                        "category": item['category'],
                        "remark": item['remark'],

                    })
        else:
            raise ValidationError("认证未通过")


# 同步刀具物料
class sfSyncCutting_tool_Material(models.Model):
    _inherit = 'sf.cutting.tool.material'
    _description = '刀具物料同步'

    url = '/api/mrs_cutting_tool_material/list'

    # 定时同步每日刀具物料
    def sync_cutting_tool_material(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutting_tool_material_yesterday_list'):
                for item in result['mrs_cutting_tool_material_yesterday_list']:
                    if item:
                        brand = self.env['sf.cutting.tool.material'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutting.tool.material'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀具物料
    def sync_all_cutting_tool_material(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutting_tool_material_all_list'):
                for item in result['mrs_cutting_tool_material_all_list']:
                    if item:
                        brand = self.env['sf.cutting.tool.material'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutting.tool.material'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })


# 同步功能刀具列表
class sfSyncFunctional_cutting_tool(models.Model):
    _inherit = 'sf.functional.cutting.tool'
    _description = '功能刀具同步'
    url = '/api/functional_cutting_tool/list'

    # 定时同步每日功能刀具
    def sync_functional_cutting_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_functional_cutting_tool_yesterday_list'):
                for item in result['mrs_functional_cutting_tool_yesterday_list']:
                    if item:
                        brand = self.env['sf.functional.cutting.tool'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.functional.cutting.tool'].create({
                                "name": item['name'],
                                "code": item['code'],
                                # 功能刀具类型
                                "functional_model_number": self.env['sf.functional.cutting.tool.model'].search(
                                    [('code', '=', item['functional_model_number'])]).id,
                                # 整体刀具型号
                                "integral_model_number": self.env['sf.integral.cutting.tool'].search(
                                    [('code', '=', item['integral_model_number'])]).id,
                                # 刀片型号
                                "blade_model_number": self.env['sf.blade'].search(
                                    [('code', '=', item['blade_model_number'])]).id,
                                # 刀杆型号
                                "cutterbar_model_number": self.env['sf.cutter.bar'].search(
                                    [('code', '=', item['cutterbar_model_number'])]).id,
                                # 刀盘型号
                                "cutterpad_model_number": self.env['sf.cutter.pad'].search(
                                    [('code', '=', item['cutterpad_model_number'])]).id,
                                # 刀柄型号
                                "handle_model_number": self.env['sf.handle'].search(
                                    [('code', '=', item['handle_model_number'])]).id,
                                # 夹头型号
                                "chuck_model_number": self.env['sf.chuck'].search(
                                    [('code', '=', item['chuck_model_number'])]).id,
                                "diameter": item['diameter'],
                                "tool_grade": item['tool_grade'],
                                "machining_accuracy": item['machining_accuracy'],
                                "tool_length": item['tool_length'],
                                "blade_number": item['blade_number'],
                                "integral_blade_length": item['integral_blade_length'],
                                "effective_blade_length": item['effective_blade_length'],
                                "max_life": item['max_life'],
                                "is_standard": item['is_standard'],
                                "applicable_range": item['applicable_range'],
                                "image": image,

                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                # 功能刀具类型
                                "functional_model_number": self.env['sf.functional.cutting.tool.model'].search(
                                    [('code', '=', item['functional_model_number'])]).id,
                                # 整体刀具型号
                                "integral_model_number": self.env['sf.integral.cutting.tool'].search(
                                    [('code', '=', item['integral_model_number'])]).id,
                                # 刀片型号
                                "blade_model_number": self.env['sf.blade'].search(
                                    [('code', '=', item['blade_model_number'])]).id,
                                # 刀杆型号
                                "cutterbar_model_number": self.env['sf.cutter.bar'].search(
                                    [('code', '=', item['cutterbar_model_number'])]).id,
                                # 刀盘型号
                                "cutterpad_model_number": self.env['sf.cutter.pad'].search(
                                    [('code', '=', item['cutterpad_model_number'])]).id,
                                # 刀柄型号
                                "handle_model_number": self.env['sf.handle'].search(
                                    [('code', '=', item['handle_model_number'])]).id,
                                # 夹头型号
                                "chuck_model_number": self.env['sf.chuck'].search(
                                    [('code', '=', item['chuck_model_number'])]).id,
                                "diameter": item['diameter'],
                                "tool_grade": item['tool_grade'],
                                "machining_accuracy": item['machining_accuracy'],
                                "tool_length": item['tool_length'],
                                "blade_number": item['blade_number'],
                                "integral_blade_length": item['integral_blade_length'],
                                "effective_blade_length": item['effective_blade_length'],
                                "max_life": item['max_life'],
                                "is_standard": item['is_standard'],
                                "applicable_range": item['applicable_range'],
                                "image": image,

                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有功能刀具
    def sync_all_functional_cutting_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_functional_cutting_tool_all_list'):
                for item in result['mrs_functional_cutting_tool_all_list']:
                    if item:
                        brand = self.env['sf.functional.cutting.tool'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.functional.cutting.tool'].create({
                                "name": item['name'],
                                "code": item['code'],
                                # 功能刀具类型
                                "functional_model_number": self.env['sf.functional.cutting.tool.model'].search(
                                    [('code', '=', item['functional_model_number'])]).id,
                                # 整体刀具型号
                                "integral_model_number": self.env['sf.integral.cutting.tool'].search(
                                    [('code', '=', item['integral_model_number'])]).id,
                                # 刀片型号
                                "blade_model_number": self.env['sf.blade'].search(
                                    [('code', '=', item['blade_model_number'])]).id,
                                # 刀杆型号
                                "cutterbar_model_number": self.env['sf.cutter.bar'].search(
                                    [('code', '=', item['cutterbar_model_number'])]).id,
                                # 刀盘型号
                                "cutterpad_model_number": self.env['sf.cutter.pad'].search(
                                    [('code', '=', item['cutterpad_model_number'])]).id,
                                # 刀柄型号
                                "handle_model_number": self.env['sf.handle'].search(
                                    [('code', '=', item['handle_model_number'])]).id,
                                # 夹头型号
                                "chuck_model_number": self.env['sf.chuck'].search(
                                    [('code', '=', item['chuck_model_number'])]).id,
                                "diameter": item['diameter'],
                                "tool_grade": item['tool_grade'],
                                "machining_accuracy": item['machining_accuracy'],
                                "tool_length": item['tool_length'],
                                "blade_number": item['blade_number'],
                                "integral_blade_length": item['integral_blade_length'],
                                "effective_blade_length": item['effective_blade_length'],
                                "max_life": item['max_life'],
                                "is_standard": item['is_standard'],
                                "applicable_range": item['applicable_range'],
                                "image": image,

                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                # 功能刀具类型
                                "functional_model_number": self.env['sf.functional.cutting.tool.model'].search(
                                    [('code', '=', item['functional_model_number'])]).id,
                                # 整体刀具型号
                                "integral_model_number": self.env['sf.integral.cutting.tool'].search(
                                    [('code', '=', item['integral_model_number'])]).id,
                                # 刀片型号
                                "blade_model_number": self.env['sf.blade'].search(
                                    [('code', '=', item['blade_model_number'])]).id,
                                # 刀杆型号
                                "cutterbar_model_number": self.env['sf.cutter.bar'].search(
                                    [('code', '=', item['cutterbar_model_number'])]).id,
                                # 刀盘型号
                                "cutterpad_model_number": self.env['sf.cutter.pad'].search(
                                    [('code', '=', item['cutterpad_model_number'])]).id,
                                # 刀柄型号
                                "handle_model_number": self.env['sf.handle'].search(
                                    [('code', '=', item['handle_model_number'])]).id,
                                # 夹头型号
                                "chuck_model_number": self.env['sf.chuck'].search(
                                    [('code', '=', item['chuck_model_number'])]).id,
                                "diameter": item['diameter'],
                                "tool_grade": item['tool_grade'],
                                "machining_accuracy": item['machining_accuracy'],
                                "tool_length": item['tool_length'],
                                "blade_number": item['blade_number'],
                                "integral_blade_length": item['integral_blade_length'],
                                "effective_blade_length": item['effective_blade_length'],
                                "max_life": item['max_life'],
                                "is_standard": item['is_standard'],
                                "applicable_range": item['applicable_range'],
                                "image": image,
                            })
        else:
            raise ValidationError("认证未通过")


# 同步功能刀具类型列表
class SyncFunctionalCuttingToolModel(models.Model):
    _inherit = 'sf.functional.cutting.tool.model'
    _description = '同步功能刀具类型列表'
    url = '/api/mrs_functional_cutting_tool_model/list'

    # 定时同步每日功能刀具类型列表
    def sync_functional_cutting_tool_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_functional_cutting_tool_model_yesterday_list'):
                for item in result['mrs_functional_cutting_tool_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.functional.cutting.tool.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.functional.cutting.tool.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有功能刀具类型列表
    def sync_all_functional_cutting_tool_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_functional_cutting_tool_model_all_list'):
                for item in result['mrs_functional_cutting_tool_model_all_list']:
                    if item:
                        brand = self.env['sf.functional.cutting.tool.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.functional.cutting.tool.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                            })
                print('同步功能刀具类型列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步整体式刀具列表
class SyncIntegralCuttingTool(models.Model):
    _inherit = 'sf.integral.cutting.tool'
    _description = '同步整体式刀具列表'
    url = '/api/mrs_integral_cutting_tool/list'

    # 定时同步每日整体式刀具列表
    def sync_integral_cutting_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_integral_cutting_tool_yesterday_list'):
                for item in result['mrs_integral_cutting_tool_yesterday_list']:
                    chuck_list = []
                    if item.get('chuck_model'):
                        for chuck in item.get('chuck_model'):
                            chuck_list.append(self.env['sf.chuck'].search(
                                [("code", '=', chuck)]).id)
                    if item:
                        brand = self.env['sf.integral.cutting.tool'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.integral.cutting.tool'].create({
                                # code, name, integral_model_number, brand, total_length, shank_length, blade_length, diameter, blade_number, front_angle, rear_angle, main_included_angle, material_model, nut, chuck_model, scope, image, create_uid, create_date
                                "name": item['name'],
                                "code": item['code'],
                                "integral_model_number": self.env['sf.integral.cutting.tool.model'].search(
                                    [("code", '=', item['integral_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "total_length": item['total_length'],
                                "shank_length": item['shank_length'],
                                "blade_length": item['blade_length'],
                                "diameter": item['diameter'],
                                "blade_number": item['blade_number'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "chuck_model": chuck_list,
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "integral_model_number": self.env['sf.integral.cutting.tool.model'].search(
                                    [("code", '=', item['integral_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "total_length": item['total_length'],
                                "shank_length": item['shank_length'],
                                "blade_length": item['blade_length'],
                                "diameter": item['diameter'],
                                "blade_number": item['blade_number'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "chuck_model": chuck_list,
                                "scope": item['scope'],
                                "image": image,
                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有整体式刀具列表
    def sync_all_integral_cutting_tool(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_integral_cutting_tool_all_list'):
                for item in result['mrs_integral_cutting_tool_all_list']:
                    chuck_list = []
                    if item.get('chuck_model'):
                        for chuck in item.get('chuck_model'):
                            chuck_list.append(self.env['sf.chuck'].search(
                                [("code", '=', chuck)]).id)
                    if item:
                        brand = self.env['sf.integral.cutting.tool'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.integral.cutting.tool'].create({
                                # code, name, integral_model_number, brand, total_length, shank_length, blade_length, diameter, blade_number, front_angle, rear_angle, main_included_angle, material_model, nut, chuck_model, scope, image, create_uid, create_date
                                "name": item['name'],
                                "code": item['code'],
                                "integral_model_number": self.env['sf.integral.cutting.tool.model'].search(
                                    [("code", '=', item['integral_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "total_length": item['total_length'],
                                "shank_length": item['shank_length'],
                                "blade_length": item['blade_length'],
                                "diameter": item['diameter'],
                                "blade_number": item['blade_number'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "chuck_model": chuck_list,
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "integral_model_number": self.env['sf.integral.cutting.tool.model'].search(
                                    [("code", '=', item['integral_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "total_length": item['total_length'],
                                "shank_length": item['shank_length'],
                                "blade_length": item['blade_length'],
                                "diameter": item['diameter'],
                                "blade_number": item['blade_number'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "chuck_model": chuck_list,
                                "scope": item['scope'],
                                "image": image,
                            })
        else:
            raise ValidationError("认证未通过")


# 同步整体式刀具类型列表
class SyncIntegralCuttingToolModel(models.Model):
    _inherit = 'sf.integral.cutting.tool.model'
    _description = '同步整体式刀具类型列表'

    url = '/api/mrs_integral_cutting_tool_model/list'

    # 定时同步整体式刀具类型列表
    def sync_integral_cutting_tool_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_integral_cutting_tool_model_list'):
                for item in result['mrs_integral_cutting_tool_model_list']:
                    if item:
                        brand = self.env['sf.integral.cutting.tool.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.integral.cutting.tool.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有整体式刀具类型列表
    def sync_all_integral_cutting_tool_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_integral_cutting_tool_model_all_list'):
                for item in result['mrs_integral_cutting_tool_model_all_list']:
                    if item:
                        brand = self.env['sf.integral.cutting.tool.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.integral.cutting.tool.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
        else:
            raise ValidationError("认证未通过")


# 同步刀片列表
class SyncBlade(models.Model):
    _inherit = 'sf.blade'
    _description = '同步刀片列表'
    url = '/api/mrs_blade/list'

    # 定时同步每日刀片列表
    def sync_blade(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_blade_yesterday_list'):
                for item in result['mrs_blade_yesterday_list']:
                    bar_list = []
                    pad_list = []
                    if item.get('cutter_bar'):
                        for i in item.get('cutter_bar'):
                            bar_list.append(self.env['sf.cutter.bar'].search(
                                [("code", '=', i)]).id)
                    if item.get('cutter_pad'):
                        for j in item.get('cutter_pad'):
                            pad_list.append(self.env['sf.cutter.pad'].search(
                                [("code", '=', j)]).id)
                    if item:
                        # code, name, blade_model_number, brand, length, width, height, top_angle, front_angle, rear_angle, main_included_angle, r_angle, material_model, hardness, nut, cutter_bar, cutter_pad, radius, image, create_uid, create_date
                        brand = self.env['sf.blade'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.blade'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "blade_model_number": self.env['sf.blade.model'].search(
                                    [("code", '=', item['blade_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "width": item['width'],
                                "height": item['height'],
                                "top_angle": item['top_angle'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "r_angle": item['r_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "hardness": item['hardness'],
                                "nut": item['nut'],
                                "cutter_bar": bar_list,
                                "cutter_pad": pad_list,
                                "radius": item['radius'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "blade_model_number": self.env['sf.blade.model'].search(
                                    [("code", '=', item['blade_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "width": item['width'],
                                "height": item['height'],
                                "top_angle": item['top_angle'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "r_angle": item['r_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "hardness": item['hardness'],
                                "nut": item['nut'],
                                "cutter_bar": bar_list,
                                "cutter_pad": pad_list,
                                "radius": item['radius'],
                                "image": image,
                            })
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀片列表
    def sync_all_blade(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_blade_all_list'):
                for item in result['mrs_blade_all_list']:
                    bar_list = []
                    pad_list = []
                    if item.get('cutter_bar'):
                        for i in item.get('cutter_bar'):
                            bar_list.append(self.env['sf.cutter.bar'].search(
                                [("code", '=', i)]).id)
                    if item.get('cutter_pad'):
                        for j in item.get('cutter_pad'):
                            pad_list.append(self.env['sf.cutter.pad'].search(
                                [("code", '=', j)]).id)

                    if item:
                        brand = self.env['sf.blade'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            self.env['sf.blade'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "blade_model_number": self.env['sf.blade.model'].search(
                                    [("code", '=', item['blade_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "width": item['width'],
                                "height": item['height'],
                                "top_angle": item['top_angle'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "r_angle": item['r_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "hardness": item['hardness'],
                                "nut": item['nut'],
                                "cutter_bar": bar_list,
                                "cutter_pad": pad_list,
                                "radius": item['radius'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "blade_model_number": self.env['sf.blade.model'].search(
                                    [("code", '=', item['blade_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "width": item['width'],
                                "height": item['height'],
                                "top_angle": item['top_angle'],
                                "front_angle": item['front_angle'],
                                "rear_angle": item['rear_angle'],
                                "main_included_angle": item['main_included_angle'],
                                "r_angle": item['r_angle'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "hardness": item['hardness'],
                                "nut": item['nut'],
                                "cutter_bar": bar_list,
                                "cutter_pad": pad_list,
                                "radius": item['radius'],
                                "image": image,
                            })
        else:
            raise ValidationError("认证未通过")


# 同步刀片类型列表
class SyncBladeModel(models.Model):
    _inherit = 'sf.blade.model'
    _description = '同步刀片类型列表'

    url = '/api/mrs_blade_model/list'

    # 定时同步每日刀片列表
    def sync_blade_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_blade_model_yesterday_list'):
                for item in result['mrs_blade_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.blade.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.blade.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步每日刀片类型列表成功')
        else:
            raise ValidationError("认证未通过")

    # 定时同步所有刀片列表
    def sync_all_blade_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_blade_model_all_list'):
                for item in result['mrs_blade_model_all_list']:
                    if item:
                        brand = self.env['sf.blade.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.blade.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步刀片类型列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀杆列表
class SyncCutterBar(models.Model):
    _inherit = 'sf.cutter.bar'
    _description = '同步刀杆列表'

    url = '/api/mrs_cutter_bar/list'

    # 定时同步每日刀杆列表
    def sync_cutter_bar(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_bar_yesterday_list'):
                for item in result['mrs_cutter_bar_yesterday_list']:
                    blade_list = []
                    if item.get('blade'):
                        for i in item.get('blade'):
                            blade_list.append(self.env['sf.blade'].search(
                                [("code", '=', i)]).id)
                    if item:
                        brand = self.env['sf.cutter.bar'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, cutter_bar_model_number, brand, c_diameter, total_length, material_model, blade_number, d_diameter, blade, wrench, screw, radius, accuracy, hardness, scope, image
                            self.env['sf.cutter.bar'].create({
                                "code": item['code'],
                                "name": item['name'],
                                "cutter_bar_model_number": self.env['sf.cutter.bar'].search(
                                    [("code", '=', item['cutter_bar_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "code": item['code'],
                                "name": item['name'],
                                "cutter_bar_model_number": self.env['sf.cutter.bar'].search(
                                    [("code", '=', item['cutter_bar_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                print('同步昨日刀杆列表成功')
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀杆列表
    def sync_all_cutter_bar(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_bar_all_list'):
                for item in result['mrs_cutter_bar_all_list']:
                    blade_list = []
                    if item.get('blade'):
                        for i in item.get('blade'):
                            blade_list.append(self.env['sf.blade'].search(
                                [("code", '=', i)]).id)
                    if item:
                        brand = self.env['sf.cutter.bar'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, cutter_bar_model_number, brand, c_diameter, total_length, material_model, blade_number, d_diameter, blade, wrench, screw, radius, accuracy, hardness, scope, image
                            self.env['sf.cutter.bar'].create({
                                "code": item['code'],
                                "name": item['name'],
                                "cutter_bar_model_number": self.env['sf.cutter.bar'].search(
                                    [("code", '=', item['cutter_bar_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "code": item['code'],
                                "name": item['name'],
                                "cutter_bar_model_number": self.env['sf.cutter.bar'].search(
                                    [("code", '=', item['cutter_bar_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                print('同步所有刀杆列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀杆类型列表
class SyncCutterBarModel(models.Model):
    _inherit = 'sf.cutter.bar.model'
    _description = '同步刀杆类型列表'

    url = '/api/mrs_cutter_bar_model/list'

    # 定时同步每日刀杆类型列表
    def sync_cutter_bar_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_bar_model_yesterday_list'):
                for item in result['mrs_cutter_bar_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.cutter.bar.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutter.bar.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步每日刀片类型列表成功')
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀杆类型列表
    def sync_all_cutter_bar_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_bar_model_all_list'):
                for item in result['mrs_cutter_bar_model_all_list']:
                    if item:
                        brand = self.env['sf.cutter.bar.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutter.bar.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步刀杆类型列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀盘列表
class SyncCutterPad(models.Model):
    _inherit = 'sf.cutter.pad'
    _description = '同步刀盘列表'

    url = '/api/mrs_cutter_pad/list'

    # 定时同步每日刀盘列表
    def sync_cutter_pad(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_pad_yesterday_list'):
                for item in result['mrs_cutter_pad_yesterday_list']:
                    blade_list = []
                    if item.get('blade'):
                        for i in item.get('blade'):
                            blade_list.append(self.env['sf.blade'].search(
                                [("code", '=', i)]).id)
                    if item:
                        brand = self.env['sf.cutter.pad'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # 'code', 'name', 'cutter_pad_model_number', 'brand', 'c_diameter', 'total_length', 'material_model', 'blade_number', 'd_diameter', 'blade', 'wrench', 'screw', 'radius', 'accuracy', 'hardness', 'scope', 'image'
                            self.env['sf.cutter.pad'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "cutter_pad_model_number": self.env['sf.cutter.pad.model'].search(
                                    [("code", '=', item['cutter_pad_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                # "blade": [(6, 0, blade_list)],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "cutter_pad_model_number": self.env['sf.cutter.pad.model'].search(
                                    [("code", '=', item['cutter_pad_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                # "blade": [(6, 0, blade_list)],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                print('同步每日刀盘列表成功')
        else:
            raise ValidationError("认证未通过")

    # 定时同步所有刀盘列表
    def sync_all_cutter_pad(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_pad_all_list'):
                for item in result['mrs_cutter_pad_all_list']:
                    blade_list = []
                    if item.get('blade'):
                        for i in item.get('blade'):
                            blade_list.append(self.env['sf.blade'].search(
                                [("code", '=', i)]).id)
                    if item:
                        brand = self.env['sf.cutter.pad'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # 'code', 'name', 'cutter_pad_model_number', 'brand', 'c_diameter', 'total_length', 'material_model', 'blade_number', 'd_diameter', 'blade', 'wrench', 'screw', 'radius', 'accuracy', 'hardness', 'scope', 'image'
                            self.env['sf.cutter.pad'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "cutter_pad_model_number": self.env['sf.cutter.pad.model'].search(
                                    [("code", '=', item['cutter_pad_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                # "blade": [(6, 0, blade_list)],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "cutter_pad_model_number": self.env['sf.cutter.pad.model'].search(
                                    [("code", '=', item['cutter_pad_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "c_diameter": item['c_diameter'],
                                "total_length": item['total_length'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "blade_number": item['blade_number'],
                                "d_diameter": item['d_diameter'],
                                # "blade": [(6, 0, blade_list)],
                                "blade": blade_list,
                                "wrench": item['wrench'],
                                "screw": item['screw'],
                                "radius": item['radius'],
                                "accuracy": item['accuracy'],
                                "hardness": item['hardness'],
                                "scope": item['scope'],
                                "image": image,
                            })
                print('同步所有刀盘列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀盘类型列表
class SyncCutterPadModel(models.Model):
    _inherit = 'sf.cutter.pad.model'
    _description = '同步刀盘类型列表'

    url = '/api/mrs_cutter_pad_model/list'

    # 定时同步每日刀盘类型列表
    def sync_cutter_pad_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_pad_model_yesterday_list'):
                for item in result['mrs_cutter_pad_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.cutter.pad.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutter.pad.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步每日刀盘类型列表成功')
        else:
            raise ValidationError("认证未通过")

    # 定时同步所有刀盘类型列表
    def sync_all_cutter_pad_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_cutter_pad_model_all_list'):
                for item in result['mrs_cutter_pad_model_all_list']:
                    if item:
                        brand = self.env['sf.cutter.pad.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.cutter.pad.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步刀盘类型列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀柄列表
class SyncHandle(models.Model):
    _inherit = 'sf.handle'
    _description = '同步刀柄列表'

    url = '/api/mrs_handle/list'

    # 定时同步每日刀柄列表
    def sync_handle(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_handle_yesterday_list'):
                for item in result['mrs_handle_yesterday_list']:
                    chuck_list = []
                    if item.get('chuck_model'):
                        for chuck in item.get('chuck_model'):
                            chuck_list.append(self.env['sf.chuck'].search(
                                [("code", '=', chuck)]).id)
                    if item:
                        brand = self.env['sf.handle'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, handle_model_number, brand, length, length1, diameter1, weight, material_model, body_accuracy, nut, chuck_model, clamping_range, detection_accuracy, detection_hardness, standard_speed, image
                            self.env['sf.handle'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "handle_model_number": self.env['sf.handle.model'].search(
                                    [("code", '=', item['handle_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "length1": item['length1'],
                                "diameter1": item['diameter1'],
                                "weight": item['weight'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "body_accuracy": item['body_accuracy'],
                                "nut": item['nut'],
                                # "chuck_model": [(6, 0, chuck_list)],
                                "chuck_model": chuck_list,
                                "clamping_range": item['clamping_range'],
                                "detection_accuracy": item['detection_accuracy'],
                                "detection_hardness": item['detection_hardness'],
                                "standard_speed": item['standard_speed'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "handle_model_number": self.env['sf.handle.model'].search(
                                    [("code", '=', item['handle_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "length1": item['length1'],
                                "diameter1": item['diameter1'],
                                "weight": item['weight'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "body_accuracy": item['body_accuracy'],
                                "nut": item['nut'],
                                # "chuck_model": [(6, 0, chuck_list)],
                                "chuck_model": chuck_list,
                                "clamping_range": item['clamping_range'],
                                "detection_accuracy": item['detection_accuracy'],
                                "detection_hardness": item['detection_hardness'],
                                "standard_speed": item['standard_speed'],
                                "image": image,
                            })
                print('同步昨日刀柄列表成功')
        else:
            raise ValidationError("认证未通过")

    # 同步所有刀柄列表
    def sync_all_handle(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_handle_all_list'):
                for item in result['mrs_handle_all_list']:
                    chuck_list = []
                    if item.get('chuck_model'):
                        for chuck in item.get('chuck_model'):
                            chuck_list.append(self.env['sf.chuck'].search(
                                [("code", '=', chuck)]).id)
                    if item:
                        brand = self.env['sf.handle'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, handle_model_number, brand, length, length1, diameter1, weight, material_model, body_accuracy, nut, chuck_model, clamping_range, detection_accuracy, detection_hardness, standard_speed, image
                            self.env['sf.handle'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "handle_model_number": self.env['sf.handle.model'].search(
                                    [("code", '=', item['handle_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "length1": item['length1'],
                                "diameter1": item['diameter1'],
                                "weight": item['weight'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "body_accuracy": item['body_accuracy'],
                                "nut": item['nut'],
                                # "chuck_model": [(6, 0, chuck_list)],
                                "chuck_model": chuck_list,
                                "clamping_range": item['clamping_range'],
                                "detection_accuracy": item['detection_accuracy'],
                                "detection_hardness": item['detection_hardness'],
                                "standard_speed": item['standard_speed'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "handle_model_number": self.env['sf.handle.model'].search(
                                    [("code", '=', item['handle_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "length": item['length'],
                                "length1": item['length1'],
                                "diameter1": item['diameter1'],
                                "weight": item['weight'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "body_accuracy": item['body_accuracy'],
                                "nut": item['nut'],
                                # "chuck_model": [(6, 0, chuck_list)],
                                "chuck_model": chuck_list,
                                "clamping_range": item['clamping_range'],
                                "detection_accuracy": item['detection_accuracy'],
                                "detection_hardness": item['detection_hardness'],
                                "standard_speed": item['standard_speed'],
                                "image": image,
                            })
                print('同步所有刀柄列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步刀柄类型列表
class SyncHandleModel(models.Model):
    _inherit = 'sf.handle.model'
    _description = '同步刀柄类型列表'

    url = '/api/mrs_handle_model/list'

    # 定时同步每日刀柄类型列表
    def sync_handle_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_handle_model_yesterday_list'):
                for item in result['mrs_handle_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.handle.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.handle.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步每日刀柄类型列表成功')
        else:
            raise ValidationError("认证未通过")

    # 定时同步所有刀柄类型列表
    def sync_all_handle_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_handle_model_all_list'):
                for item in result['mrs_handle_model_all_list']:
                    if item:
                        brand = self.env['sf.handle.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.handle.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步刀柄类型列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步夹头型号列表
class SyncChuck(models.Model):
    _inherit = 'sf.chuck'
    _description = '同步夹头型号列表'

    url = '/api/mrs_chuck/list'

    # 定时同步每日夹头型号列表
    def sync_chuck(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_chuck_yesterday_list'):
                for item in result['mrs_chuck_yesterday_list']:
                    handle_model_list = []
                    if item.get('handle_model'):
                        for handle_model in item['handle_model']:
                            handle_model_list.append(self.env['sf.handle.model'].search(
                                [("code", '=', handle_model)]).id)
                    if item:
                        brand = self.env['sf.chuck'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, chuck_model_number, brand, accuracy, diameter, inner_diameter, height, material_model, nut, handle_model, clamping_range, feature, image
                            self.env['sf.chuck'].create({
                                "code": item['code'],
                                "name": item['name'],
                                "chuck_model_number": self.env['sf.chuck.model'].search(
                                    [("code", '=', item['chuck_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "accuracy": item['accuracy'],
                                "diameter": item['diameter'],
                                "inner_diameter": item['inner_diameter'],
                                "height": item['height'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "handle_model": handle_model_list,
                                "clamping_range": item['clamping_range'],
                                "feature": item['feature'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "code": item['code'],
                                "name": item['name'],
                                "chuck_model_number": self.env['sf.chuck.model'].search(
                                    [("code", '=', item['chuck_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "accuracy": item['accuracy'],
                                "diameter": item['diameter'],
                                "inner_diameter": item['inner_diameter'],
                                "height": item['height'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "handle_model": handle_model_list,
                                "clamping_range": item['clamping_range'],
                                "feature": item['feature'],
                                "image": image,
                            })
                print('同步每日夹头型号列表成功')
        else:
            raise ValidationError("认证未通过")

    # 同步所有夹头型号列表
    def sync_all_chuck(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_chuck_all_list'):
                for item in result['mrs_chuck_all_list']:
                    handle_model_list = []
                    if item.get('handle_model'):
                        for handle_model in item['handle_model']:
                            handle_model_list.append(self.env['sf.handle.model'].search(
                                [("code", '=', handle_model)]).id)
                    if item:
                        brand = self.env['sf.chuck'].search(
                            [("code", '=', item['code'])])
                        if item.get('image'):
                            image = base64.b64decode(item['image'])
                        else:
                            image = ''
                        if not brand:
                            # code, name, chuck_model_number, brand, accuracy, diameter, inner_diameter, height, material_model, nut, handle_model, clamping_range, feature, image
                            self.env['sf.chuck'].create({
                                "code": item['code'],
                                "name": item['name'],
                                "chuck_model_number": self.env['sf.chuck.model'].search(
                                    [("code", '=', item['chuck_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "accuracy": item['accuracy'],
                                "diameter": item['diameter'],
                                "inner_diameter": item['inner_diameter'],
                                "height": item['height'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "handle_model": handle_model_list,
                                "clamping_range": item['clamping_range'],
                                "feature": item['feature'],
                                "image": image,
                            })
                        else:
                            brand.write({
                                "code": item['code'],
                                "name": item['name'],
                                "chuck_model_number": self.env['sf.chuck.model'].search(
                                    [("code", '=', item['chuck_model_number'])]).id,
                                "brand": self.env['sf.machine.brand'].search(
                                    [("code", '=', item['brand'])]).id,
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                                "accuracy": item['accuracy'],
                                "diameter": item['diameter'],
                                "inner_diameter": item['inner_diameter'],
                                "height": item['height'],
                                "material_model": self.env['sf.materials.model'].search(
                                    [("materials_no", '=', item['material_model'])]).id,
                                "nut": item['nut'],
                                "handle_model": handle_model_list,
                                "clamping_range": item['clamping_range'],
                                "feature": item['feature'],
                                "image": image,
                            })
                print('同步所有夹头型号列表成功')
        else:
            raise ValidationError("认证未通过")


# 同步夹头类型列表
class SyncChuckModel(models.Model):
    _inherit = 'sf.chuck.model'
    _description = '同步夹头类型列表'

    url = '/api/mrs_chuck_model/list'

    # 定时同步每日夹头型号列表
    def sync_chuck_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_chuck_model_yesterday_list'):
                for item in result['mrs_chuck_model_yesterday_list']:
                    if item:
                        brand = self.env['sf.chuck.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.chuck.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步每日夹头类型列表成功')
        else:
            raise ValidationError("认证未通过")

    # 定时同步所有夹头型号列表
    def sync_all_chuck_model(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        # print('result:%s' % result)
        if result['status'] == 1:
            if result.get('mrs_chuck_model_all_list'):
                for item in result['mrs_chuck_model_all_list']:
                    if item:
                        brand = self.env['sf.chuck.model'].search(
                            [("code", '=', item['code'])])
                        if not brand:
                            self.env['sf.chuck.model'].create({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                        else:
                            brand.write({
                                "name": item['name'],
                                "code": item['code'],
                                "remark": item['remark'],
                                "cutting_tool_material": self.env['sf.cutting.tool.material'].search(
                                    [("code", '=', item['cutting_tool_material'])]).id,
                            })
                print('同步夹头类型列表成功')
        else:
            raise ValidationError("认证未通过")


'''=========================================夹紧元件类型==============================================='''


class sfClampingElementType(models.Model):
    _inherit = 'sf.clamping.element.type'
    _description = '夹紧元件类型'
    url = '/api/mrs_clamping_element_type/list'

    # 定时同步每日夹紧元件类型
    def sync_clamping_element_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['mrs_clamping_element_type_yesterday_list']:
                if item:
                    brand = self.env['sf.clamping.element.type'].search(
                        [("code", '=', item['code'])])
                    if brand:
                        brand.name = item['name'],
                        brand.code = item['code'],
                        brand.comment = item['comment']
                    else:
                        self.env['sf.clamping.element.type'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment']
                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有夹紧元件类型
    def sync_all_clamping_element_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_clamping_element_type_all_list']:
                if item:
                    brand = self.env['sf.clamping.element.type'].search(
                        [("code", '=', item['code'])])
                    if not brand:
                        self.env['sf.clamping.element.type'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment']
                        })
        else:
            raise ValidationError("认证未通过")


'''=================================夹紧元件型号============================================='''


class sfFixtureClampingElement(models.Model):
    _inherit = 'sf.fixture.clamping.element'
    _description = '夹紧元件型号'
    url = '/api/mrs_fixture_clamping_element/list'

    # 定时同步夹紧元件型号
    def sync_fixture_clamping_element(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            for item in result['mrs_fixture_clamping_element_yesterday_list']:
                brand = self.env['sf.fixture.clamping.element'].search(
                    [("code", '=', item['code'])])
                if brand:
                    brand.code = item['code'],
                    brand.name = item['name'],

                    brand.length = item['length'],
                    brand.width = item['width'],
                    brand.height = item['height'],
                    brand.diameter = item['diameter'],
                    brand.hardness = item['hardness'],
                    brand.chucking_power = item['chucking_power'],
                    brand.surface_treatment = item['surface_treatment'],
                    brand.accuracy_grade = item['accuracy_grade'],
                    # brand.picture = item['picture'].encode('utf-8'),
                    brand.picture = base64.b64decode(item['picture']),
                    brand.comment = item['comment'],

                    brand.brand_id = self.env['sf.machine.brand'].search(
                        [('code', '=', item['brand_id'])]).id,
                    brand.material_type_id = self.env['sf.materials.model'].search(
                        [('materials_no', '=', item['material_type_id'])]).id,
                    brand.clamping_ele_type_id = self.env['sf.clamping.element.type'].search(
                        [('code', '=', item['clamping_ele_type_id'])]).id

                else:
                    self.env['sf.fixture.clamping.element'].create({
                        "code": item['code'],
                        "name": item['name'],

                        "length": item['length'],
                        "width": item['width'],
                        "height": item['height'],
                        "diameter": item['diameter'],
                        "hardness": item['hardness'],
                        "chucking_power": item['chucking_power'],
                        "surface_treatment": item['surface_treatment'],
                        "accuracy_grade": item['accuracy_grade'],

                        'picture': base64.b64decode(item['picture']),
                        # "picture": item['picture'].encode('utf-8'),
                        "comment": item['comment'],

                        'brand_id': self.env['sf.machine.brand'].search(
                            [('code', '=', item['brand_id'])]).id,
                        "material_type_id": self.env['sf.materials.model'].search(
                            [('materials_no', '=', item['material_type_id'])]).id,
                        "clamping_ele_type_id": self.env['sf.clamping.element.type'].search(
                            [('code', '=', item['clamping_ele_type_id'])]).id,
                    })
        else:
            raise ValidationError("认证未通过")

    # 同步所有夹紧元件型号
    def sync_all_fixture_clamping_element(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        print(result)
        if result['status'] == 1:
            for item in result['mrs_fixture_clamping_element_all_list']:
                brand = self.env['sf.fixture.clamping.element'].search(
                    [("code", '=', item['code'])])
                if not brand:
                    self.env['sf.fixture.clamping.element'].create({
                        "code": item['code'],
                        "name": item['name'],

                        "length": item['length'],
                        "width": item['width'],
                        "height": item['height'],
                        "diameter": item['diameter'],
                        "hardness": item['hardness'],
                        "chucking_power": item['chucking_power'],
                        "surface_treatment": item['surface_treatment'],
                        "accuracy_grade": item['accuracy_grade'],

                        # "picture": item['picture'].encode('utf-8'),
                        'picture': base64.b64decode(item['picture']),
                        "comment": item['comment'],

                        'brand_id': self.env['sf.machine.brand'].search(
                            [('code', '=', item['brand_id'])]).id,
                        "material_type_id": self.env['sf.materials.model'].search(
                            [('materials_no', '=', item['material_type_id'])]).id,
                        "clamping_ele_type_id": self.env['sf.clamping.element.type'].search(
                            [('code', '=', item['clamping_ele_type_id'])]).id,
                    })


        else:
            raise ValidationError("认证未通过")


'''===================================导向元件类型=========================================='''


class sfGuideElementType(models.Model):
    _inherit = 'sf.guide.element.type'
    _description = '导向元件类型'
    url = '/api/mrs_guide_element_type/list'

    # 定时同步每日导向元件类型
    def sync_guide_element_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_guide_element_type_yesterday_list']:
                if item:
                    brand = self.env['sf.guide.element.type'].search(
                        [("code", '=', item['code'])])
                    if brand:
                        brand.name = item['name'],
                        brand.code = item['code'],
                        brand.comment = item['comment']
                    else:
                        self.env['sf.guide.element.type'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment']
                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有导向元件类型
    def sync_all_guide_element_type(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_guide_element_type_all_list']:
                if item:
                    brand = self.env['sf.guide.element.type'].search(
                        [("code", '=', item['code'])])
                    if not brand:
                        self.env['sf.guide.element.type'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment']
                        })
                        # guide_element_type = self.env['sf.guide.element.type'].search([('code', '=', code)])
                        # tags = self.env['sf.tag'].search([('name', '=', 'new_tag_name')])
                        # guide_element_type.write({'tag_ids': [(6, 0, tags.ids)]})
        else:
            raise ValidationError("认证未通过")


'''===================================导向元件导向方式=========================================='''


class sfGuideElementGuideMode(models.Model):
    _inherit = 'sf.guide.element.guide.mode'
    _description = '导向元件导向方式'
    url = '/api/mrs_guide_element_guide_mode/list'

    # 定时同步每日导向元件导向方式
    def sync_guide_element_guide_mode(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_guide_element_guide_mode_yesterday_list']:
                name_id_list = []
                if item.get('guide_ele_guide_mode_id'):
                    for i in item.get('guide_ele_guide_mode_id'):
                        name_id_list.append(self.env['sf.fixture.guide.element'].search(
                            [("code", '=', i)]).id)
                if item:
                    brand = self.env['sf.guide.element.guide.mode'].search(
                        [("code", '=', item['code'])])
                    if brand:
                        brand.name = item['name'],
                        brand.code = item['code'],
                        brand.comment = item['comment']
                        brand.name_id = name_id_list

                    else:
                        self.env['sf.guide.element.guide.mode'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment'],
                            "name_id": name_id_list
                        })
        else:
            raise ValidationError("认证未通过")

    # 同步所有导向元件导向方式
    def sync_all_guide_element_guide_mode(self):
        sf_sync_config = self.env['res.config.settings'].get_values()
        token = sf_sync_config['token']
        sf_secret_key = sf_sync_config['sf_secret_key']
        headers = Common.get_headers(self, token, sf_secret_key)

        strUrl = sf_sync_config['sf_url'] + self.url
        r = requests.post(strUrl, json={}, data=None, headers=headers)
        r = r.json()
        result = json.loads(r['result'])
        if result['status'] == 1:
            _logger.info('result:%s' % result)
            for item in result['mrs_guide_element_guide_mode_all_list']:
                name_id_list = []
                if item.get('guide_ele_guide_mode_id'):
                    for i in item.get('guide_ele_guide_mode_id'):
                        name_id_list.append(self.env['sf.fixture.guide.element'].search(
                            [("code", '=', i)]).id)
                if item:
                    brand = self.env['sf.guide.element.guide.mode'].search(
                        [("code", '=', item['code'])])
                    if not brand:
                        self.env['sf.guide.element.guide.mode'].create({
                            "name": item['name'],
                            "code": item['code'],
                            "comment": item['comment'],
                            "name_id": name_id_list
                        })
        else:
            raise ValidationError("认证未通过")
