# -*- encoding: utf-8 -*-
"""
@File    : EquipmentInfoService.py
@Time    : 2022/4/14 17:28
@Author  : stephen
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""
import logging

from django.db import transaction

from Model.models import EquipmentInfoMonday, EquipmentInfoTuesday, EquipmentInfoWednesday, EquipmentInfoThursday, \
    EquipmentInfoFriday, EquipmentInfoSaturday, EquipmentInfoSunday
from Object.utils import LocalDateTimeUtil


class EquipmentInfoService:

    @staticmethod
    def get_equipment_info_obj(dt, **kwargs):
        """
        根据日期判断是星期几，返回相应的对象实例
        @param dt: 日期 例：2022-03-03
        @param kwargs: 设备信息属性值
        @return: 星期一至星期天equipment_info对象实例
        """
        logger = logging.getLogger('info')
        week = LocalDateTimeUtil.date_to_week(dt)
        logger.info('本周{}'.format(str(week)))
        equipment_info = None
        if week == 1:
            equipment_info = EquipmentInfoMonday(**kwargs)
        elif week == 2:
            equipment_info = EquipmentInfoTuesday(**kwargs)
        elif week == 3:
            equipment_info = EquipmentInfoWednesday(**kwargs)
        elif week == 4:
            equipment_info = EquipmentInfoThursday(**kwargs)
        elif week == 5:
            equipment_info = EquipmentInfoFriday(**kwargs)
        elif week == 6:
            equipment_info = EquipmentInfoSaturday(**kwargs)
        elif week == 7:
            equipment_info = EquipmentInfoSunday(**kwargs)
        logger.info(type(equipment_info))
        logger.info(equipment_info)
        return equipment_info

    @staticmethod
    def equipment_info_bulk_create(week_val, equipment_info_list):
        """
        根据week_val判断，进行表对象选择存储
        @param week_val: 星期一至星期天，int类型
        @param equipment_info_list: 设备信息实例列表
        @return: True or False
        """
        try:
            with transaction.atomic():
                if equipment_info_list and len(equipment_info_list) > 0:
                    if week_val == 0:
                        return False
                    if week_val == 1:
                        EquipmentInfoMonday.objects.bulk_create(equipment_info_list)
                    elif week_val == 2:
                        EquipmentInfoTuesday.objects.bulk_create(equipment_info_list)
                    elif week_val == 3:
                        EquipmentInfoWednesday.objects.bulk_create(equipment_info_list)
                    elif week_val == 4:
                        EquipmentInfoThursday.objects.bulk_create(equipment_info_list)
                    elif week_val == 5:
                        EquipmentInfoFriday.objects.bulk_create(equipment_info_list)
                    elif week_val == 6:
                        EquipmentInfoSaturday.objects.bulk_create(equipment_info_list)
                    elif week_val == 7:
                        EquipmentInfoSunday.objects.bulk_create(equipment_info_list)
                    return True
                return False
        except Exception as e:
            er_log = logging.getLogger('django')
            er_log.exception(repr(e))
            er_log.error(repr(e))
            return False
