# -*- coding: utf-8 -*-
import logging

from odoo import fields, models, api
from odoo.exceptions import UserError
# from odoo.addons import sf_base, sf_common
from datetime import datetime


# 功能刀具物料
class CuttingToolMaterial(models.Model):
    _name = 'sf.cutting.tool.material'
    _description = '刀具物料'

    code = fields.Char('编码')
    name = fields.Char('名称')
    remark = fields.Char('备注')


# 测试模型
class aa_test(models.Model):
    _name = 'sf.test'
    _description = '测试'

    model_number = fields.Char('型号', readonly=True)
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 关联整体式刀具型号
    integral_model_number = fields.Many2one('sf.integral.cutting.tool.model', '整体式刀具型号')
    # 关联刀片型号
    blade_model_number = fields.Many2one('sf.blade', '刀片型号')
    # 关联刀杆型号
    cutterbar_model_number = fields.Many2one('sf.cutter.bar', '刀杆型号')
    # 关联刀盘型号
    cutterpad_model_number = fields.Many2one('sf.cutter.pad', '刀盘型号')
    # 关联刀柄型号
    handle_model_number = fields.Many2one('sf.handle', '刀柄型号')
    # 关联夹头型号
    chuck_model_number = fields.Many2one('sf.chuck', '夹头型号')

    hide_integral_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏整体式刀具型号')
    hide_blade_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏刀片型号')
    hide_cutterbar_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏刀杆型号')
    hide_cutterpad_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏刀盘型号')
    hide_handle_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏刀柄型号')
    hide_chuck_model_number = fields.Boolean(compute='_compute_hide_model_number', default=False, string='隐藏夹头型号')
    hide_model_number = fields.Boolean(compute='_compute_hide_model_number', default=True, string='隐藏型号')

    @api.depends('cutting_tool_material')
    def _compute_hide_model_number(self):
        for record in self:
            record.hide_integral_model_number = False
            record.hide_blade_model_number = False
            record.hide_cutterbar_model_number = False
            record.hide_cutterpad_model_number = False
            record.hide_handle_model_number = False
            record.hide_chuck_model_number = False
            record.hide_model_number = True

            if record.cutting_tool_material and record.cutting_tool_material.name == '整体式刀具':
                record.hide_model_number = False
                record.hide_integral_model_number = True
            elif record.cutting_tool_material and record.cutting_tool_material.name == '刀片':
                record.hide_model_number = False
                record.hide_blade_model_number = True
            elif record.cutting_tool_material and record.cutting_tool_material.name == '刀杆':
                record.hide_model_number = False
                record.hide_cutterbar_model_number = True
            elif record.cutting_tool_material and record.cutting_tool_material.name == '刀盘':
                record.hide_model_number = False
                record.hide_cutterpad_model_number = True
            elif record.cutting_tool_material and record.cutting_tool_material.name == '刀柄':
                record.hide_model_number = False
                record.hide_handle_model_number = True
            elif record.cutting_tool_material and record.cutting_tool_material.name == '夹头':
                record.hide_model_number = False
                record.hide_chuck_model_number = True
            else:
                record.hide_model_number = True


# 功能刀具
class FunctionalCuttingTool(models.Model):
    _name = 'sf.functional.cutting.tool'
    _description = '功能刀具'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 增加功能刀具类型、整体式刀具型号、刀片型号、刀杆型号、刀盘型号、刀柄型号、夹头型号、直径(mm)、刀具等级、加工精度(mm)、装刀长、刃数、整体刃长(mm)、有效刃长(mm)、最大寿命值、是否标准刀、适用范围
    functional_model_number = fields.Many2one('sf.functional.cutting.tool.model', string='功能刀具类型')
    integral_model_number = fields.Many2one('sf.integral.cutting.tool', string='整体式刀具型号')
    blade_model_number = fields.Many2one('sf.blade', string='刀片型号')
    cutterbar_model_number = fields.Many2one('sf.cutter.bar', string='刀杆型号')
    cutterpad_model_number = fields.Many2one('sf.cutter.pad', string='刀盘型号')
    handle_model_number = fields.Many2one('sf.handle', string='刀柄型号')
    chuck_model_number = fields.Many2one('sf.chuck', string='夹头型号')
    diameter = fields.Float('直径(mm)')
    tool_grade = fields.Selection([('1', 'P1'), ('2', 'P2'), ('3', 'P3'), ('4', 'P4'), ('5', 'P5'), ('6', 'P6')],
                                  string='刀具等级')
    machining_accuracy = fields.Float('加工精度(mm)')
    tool_length = fields.Float('装刀长')
    blade_number = fields.Integer('刃数')
    integral_blade_length = fields.Float('整体刃长(mm)')
    effective_blade_length = fields.Float('有效刃长(mm)')
    max_life = fields.Float('最大寿命值')
    is_standard = fields.Boolean('是否标准刀')
    applicable_range = fields.Char('适用范围')
    image = fields.Binary('图片')


# 功能刀具类型
class FunctionalCuttingToolModel(models.Model):
    _name = 'sf.functional.cutting.tool.model'
    _description = '功能刀具类型'

    name = fields.Char('名称')
    code = fields.Char('编码')
    remark = fields.Text('备注')


# 整体式刀具
class IntegralCuttingTool(models.Model):
    _name = 'sf.integral.cutting.tool'
    _description = '整体式刀具'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 整体式刀具类型字段，关联整体式刀具类型对象
    integral_model_number = fields.Many2one('sf.integral.cutting.tool.model', '整体式刀具类型')
    # 增加品牌、总长度(mm)、柄部长度(mm)、刃部长度(mm)、直径(mm)、刃数、前角(°)、后角(°)、主偏角(°)、材料型号、配对螺母(mm)、适用夹头型号、适用范围、图片、创建人、创建时间等字段
    brand = fields.Many2one('sf.machine.brand', '品牌')
    total_length = fields.Float('总长度(mm)')
    shank_length = fields.Float('柄部长度(mm)')
    blade_length = fields.Float('刃部长度(mm)')
    diameter = fields.Float('直径(mm)')
    blade_number = fields.Integer('刃数')
    front_angle = fields.Float('前角(°)')
    rear_angle = fields.Float('后角(°)')
    main_included_angle = fields.Float('主偏角(°)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    nut = fields.Float('配对螺母(mm)')
    # 适用夹头型号可以多选
    chuck_model = fields.Many2many('sf.chuck', string='适用夹头型号')
    scope = fields.Char('适用范围')
    image = fields.Binary('图片')


# 整体式刀具类型
class IntegralCuttingToolModel(models.Model):
    _name = 'sf.integral.cutting.tool.model'
    _description = '整体式刀具类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    remark = fields.Text('备注')


# 刀片
class Blade(models.Model):
    _name = 'sf.blade'
    _description = '刀片'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 刀片类型字段，关联刀片类型对象
    blade_model_number = fields.Many2one('sf.blade.model', '刀片类型')
    # 编码、型号名称、刀片类型、品牌、长度L(mm)、宽度D(mm)、高度T(mm)、顶角(°)、前角(°)、后角(°)、主偏角(°)、R角(°)、材料型号、加工硬度、配对螺母(mm)、适用刀杆/刀盘型号、刀尖半径(mm)、图片、创建人、创建时间字段
    brand = fields.Many2one('sf.machine.brand', '品牌', )
    length = fields.Float('长度L(mm)')
    width = fields.Float('宽度D(mm)')
    height = fields.Float('高度T(mm)')
    top_angle = fields.Float('顶角(°)')
    front_angle = fields.Float('前角(°)')
    rear_angle = fields.Float('后角(°)')
    main_included_angle = fields.Float('主偏角(°)')
    r_angle = fields.Float('R角(°)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    hardness = fields.Char('加工硬度')
    nut = fields.Float('配对螺母(mm)')
    # 适用刀杆型号可以多选
    cutter_bar = fields.Many2many('sf.cutter.bar', string='适用刀杆型号')
    # 适用刀盘型号可以多选
    cutter_pad = fields.Many2many('sf.cutter.pad', string='适用刀盘型号')
    radius = fields.Float('刀尖半径(mm)')
    image = fields.Binary('图片')


# 刀片类型
class BladeModel(models.Model):
    _name = 'sf.blade.model'
    _description = '刀片类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 刀片类型字段，关联刀片对象
    blade_ids = fields.One2many('sf.blade', 'blade_model_number', '刀片类型')
    remark = fields.Text('备注')


# 刀杆
class CutterBar(models.Model):
    _name = 'sf.cutter.bar'
    _description = '刀杆'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 刀杆类型字段，关联刀杆类型对象
    cutter_bar_model_number = fields.Many2one('sf.cutter.bar.model', '刀杆类型')
    # 品牌、C柄径(mm)、L总长(mm)、材料型号、刃数、D刃径(mm)、适用刀片型号、配对扳手(mm)、配备螺丝(mm)、刀尖圆角半径、精度等级、硬度(°)、适用范围、图片、创建人、创建时间
    brand = fields.Many2one('sf.machine.brand', '品牌')
    c_diameter = fields.Float('C柄径(mm)')
    total_length = fields.Float('L总长(mm)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    blade_number = fields.Integer('刃数')
    d_diameter = fields.Float('D刃径(mm)')
    blade = fields.Many2many('sf.blade', string='适用刀片型号')
    wrench = fields.Float('配对扳手(mm)')
    screw = fields.Float('配备螺丝(mm)')
    radius = fields.Float('刀尖圆角半径')
    accuracy = fields.Char('精度等级')
    hardness = fields.Char('硬度(°)')
    scope = fields.Char('适用范围')
    image = fields.Binary('图片')


# 刀杆类型
class CutterBarModel(models.Model):
    _name = 'sf.cutter.bar.model'
    _description = '刀杆类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    remark = fields.Text('备注')


# 刀盘
class CutterPad(models.Model):
    _name = 'sf.cutter.pad'
    _description = '刀盘'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 刀盘类型字段，关联刀盘类型对象
    cutter_pad_model_number = fields.Many2one('sf.cutter.pad.model', '刀盘类型')
    # 增加品牌、C柄径(mm)、L总长(mm)、材料型号、刃数、D刃径(mm)、适用刀片型号、配对扳手(mm)、配备螺丝(mm)、刀尖圆角半径、精度等级、硬度(°)、适用范围、图片、创建人、创建时间
    brand = fields.Many2one('sf.machine.brand', '品牌')
    c_diameter = fields.Float('C柄径(mm)')
    total_length = fields.Float('L总长(mm)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    blade_number = fields.Integer('刃数')
    d_diameter = fields.Float('D刃径(mm)')
    blade = fields.Many2many('sf.blade', string='适用刀片型号')
    wrench = fields.Float('配对扳手(mm)')
    screw = fields.Float('配备螺丝(mm)')
    radius = fields.Float('刀尖圆角半径')
    accuracy = fields.Char('精度等级')
    hardness = fields.Char('硬度(°)')
    scope = fields.Char('适用范围')
    image = fields.Binary('图片')


# 刀盘类型
class CutterPadModel(models.Model):
    _name = 'sf.cutter.pad.model'
    _description = '刀盘类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    remark = fields.Text('备注')


# 刀柄
class Handle(models.Model):
    _name = 'sf.handle'
    _description = '刀柄'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 刀柄类型字段，关联刀柄类型对象
    handle_model_number = fields.Many2one('sf.handle.model', '刀柄类型')
    # 增加、刀柄类型、品牌、L(mm)、L1(mm)、D1(mm)、重量(kg)、材料型号、本体精度(mm)、配对螺母(mm)、适用夹头型号、夹持范围(mm)、检测精度、检测硬度、标准转速、图片、创建人、创建时间
    brand = fields.Many2one('sf.machine.brand', '品牌')
    length = fields.Float('L(mm)')
    length1 = fields.Float('L1(mm)')
    diameter1 = fields.Float('D1(mm)')
    weight = fields.Float('重量(kg)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    body_accuracy = fields.Float('本体精度(mm)')
    nut = fields.Float('配对螺母(mm)')
    chuck_model = fields.Many2many('sf.chuck.model', string='适用夹头型号')
    clamping_range = fields.Float('夹持范围(mm)')
    detection_accuracy = fields.Float('检测精度')
    detection_hardness = fields.Char('检测硬度')
    standard_speed = fields.Float('标准转速')
    image = fields.Binary('图片')


# 刀柄类型
class HandleModel(models.Model):
    _name = 'sf.handle.model'
    _description = '刀柄类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    remark = fields.Text('备注')


# 夹头对象（夹头型号对象）
class Chuck(models.Model):
    _name = 'sf.chuck'
    _description = '夹头'
    _rec_name = 'code'

    code = fields.Char('编码')
    name = fields.Char('型号名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    # 夹头类型字段，关联夹头类型对象
    chuck_model_number = fields.Many2one('sf.chuck.model', '夹头类型')
    # 增加品牌、精度(mm)、外径(mm)、内径(mm)、高度(mm)、材料型号、配对螺母(mm)、适用刀柄型号、夹持范围(mm)、特性、图片、创建人、创建时间
    brand = fields.Many2one('sf.machine.brand', '品牌')
    accuracy = fields.Float('精度(mm)')
    diameter = fields.Float('外径(mm)')
    inner_diameter = fields.Float('内径(mm)')
    height = fields.Float('高度(mm)')
    material_model = fields.Many2one('sf.materials.model', '材料型号')
    nut = fields.Float('配对螺母(mm)')
    handle_model = fields.Many2many('sf.handle.model', string='适用刀柄型号')
    clamping_range = fields.Float('夹持范围(mm)')
    feature = fields.Char('特性')
    image = fields.Binary('图片')


# 夹头类型
class ChuckModel(models.Model):
    _name = 'sf.chuck.model'
    _description = '夹头类型'

    code = fields.Char('编码')
    name = fields.Char('名称')
    # 关联刀具物料
    cutting_tool_material = fields.Many2one('sf.cutting.tool.material', '刀具物料')
    remark = fields.Text('备注')
