from odoo import fields, models


class ModelType(models.Model):
    _name = 'sf.model.type'
    _description = '模型类型'

    name = fields.Char('名称')
    embryo_tolerance = fields.Integer('胚料容余')
    product_routing_tmpl_ids = fields.One2many('sf.product.model.type.routing.sort', 'product_model_type_id',
                                               '成品工序模板')
    embryo_routing_tmpl_ids = fields.One2many('sf.embryo.model.type.routing.sort', 'embryo_model_type_id',
                                              '胚料工序模板')


class ProductModelTypeRoutingSort(models.Model):
    _name = 'sf.product.model.type.routing.sort'
    _description = '成品工序排序'

    sequence = fields.Integer('Sequence')
    route_workcenter_id = fields.Many2one('mrp.routing.workcenter')
    is_repeat = fields.Boolean('重复', related='route_workcenter_id.is_repeat')
    routing_type = fields.Selection([
        ('获取CNC加工程序', '获取CNC加工程序'),
        ('装夹', '装夹'),
        ('前置三元定位检测', '前置三元定位检测'),
        ('CNC加工', 'CNC加工'),
        ('后置三元质量检测', '后置三元质量检测'),
        ('解除装夹', '解除装夹'), ('切割', '切割')
    ], string="工序类型", related='route_workcenter_id.routing_type')
    workcenter_ids = fields.Many2many('mrp.workcenter', required=False, related='route_workcenter_id.workcenter_ids')
    product_model_type_id = fields.Many2one('sf.model.type')

    _sql_constraints = [
        ('route_model_type_uniq', 'unique (route_workcenter_id,product_model_type_id)', '成品工序不能重复!')
    ]


class EmbryoModelTypeRoutingSort(models.Model):
    _name = 'sf.embryo.model.type.routing.sort'
    _description = '胚料工序排序'

    sequence = fields.Integer('Sequence')
    route_workcenter_id = fields.Many2one('mrp.routing.workcenter')
    is_repeat = fields.Boolean('重复', related='route_workcenter_id.is_repeat')
    routing_type = fields.Selection([
        ('获取CNC加工程序', '获取CNC加工程序'),
        ('装夹', '装夹'),
        ('前置三元定位检测', '前置三元定位检测'),
        ('CNC加工', 'CNC加工'),
        ('后置三元质量检测', '后置三元质量检测'),
        ('解除装夹', '解除装夹'), ('切割', '切割')
    ], string="工序类型", related='route_workcenter_id.routing_type')
    workcenter_ids = fields.Many2many('mrp.workcenter', required=False, related='route_workcenter_id.workcenter_ids')
    embryo_model_type_id = fields.Many2one('sf.model.type')

    _sql_constraints = [
        ('route_model_type_uniq', 'unique (route_workcenter_id,embryo_model_type_id)', '胚料工序不能重复!')
    ]
