# -*- 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:
                    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,

                    })
        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,

                    })
        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:
            for item in result['mrs_production_process_parameter_all_list']:
                if 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("认证未通过")
