# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/3/14
# 描述:
import logging

import allure

from unit.public.DataDic import DataDic

log = logging.getLogger(__name__)
dd = DataDic()


def get_results(index, col):
    def query(func):
        def wrapper(self, *args, **kwargs):
            db = dd.get_value('db')
            sql = func(self, *args, **kwargs)
            log.info(sql)
            result = db.exec(sql)
            try:
                return result if index is None else result[index] if col is None else result[index][col]
            except IndexError:
                return "IndexError"

        return wrapper

    return query


class DBQuery(object):
    @get_results(0, 0)
    def get_stock_cargo(self, warehouse):
        """仓库是否启用货位管理"""
        with allure.step('DB:查询仓库是否启用货位管理'):
            return """select 
                    SPACE_MANAGEMENT 
                from 
                    BO_EU_STOCK_CARGO_USING 
                where 
                    BUSINESS_UNITS='{}';""".format(warehouse)

    @get_results(0, 0)
    def get_pro_num(self, warehouse, good):
        """获取生产批号"""
        with allure.step('DB:获取生产批号'):
            return """select
                    F_PRO_NUM
                from
                    BO_EU_STOCK_CURRENT
                where
                    F_ENTREPOT_CODE = '{warehouse}'
                    and F_COMMODITY_CODE = '{good}'
                    and F_CARGO_CODE in (
                    select
                        F_CARGO_CODE
                    from
                        BO_EU_STOCK_MGMT_CARGO_SPACE
                    where
                        F_ENTREPOT_CODE = '{warehouse}'
                        and F_COMMODITY_CODE = '{good}');""".format(**locals())

    @get_results(0, 0)
    def get_serial_flag(self, warehouse, good):
        """商品序列号标识"""
        with allure.step('DB:查询商品序列号标识'):
            return """select 
                    F_IS_SERIAL_NO 
                from 
                    BO_EU_BSI_GOODS_MCU 
                where 
                    BUSINESS_UNITS='{warehouse}' 
                    and COMMODITY_CODE='{good}';""".format(**locals())

    @get_results(0, None)
    def get_storage_sterilization_flag(self, company, good):
        """获取商品存储条件、灭菌管理标识"""
        with allure.step('DB:获取商品存储条件、灭菌管理标识'):
            return """select 
                    STORAGECON, STERILIZATION_FLAG 
                from 
                    bo_eu_bsi_commodity 
                where 
                    COMPANY='{company}' 
                    and COMMODITY_CODE='{good}';""".format(**locals())

    @get_results(None, None)
    def get_enable_serial(self, warehouse, good, batch, num):
        """获取可用序列号"""
        with allure.step('DB:获取可用序列号'):
            return """select
                    F_SERIAL
                from
                    bo_eu_stock_serial
                where
                    F_ENTREPOT_CODE = '{warehouse}'
                    and F_COMMODITY_CODE = '{good}'
                    and F_BATCH_SERIA = '{batch}'
                    and F_STATUS = '1'
                    and F_SERIAL not in (
                    select
                        F_SERIAL
                    from
                        bo_eu_stock_serial_occupy
                    where
                        F_ENTREPOT_CODE = '{warehouse}' and F_COMMODITY_CODE = '{good}'
                        and F_BATCH_SERIA = '{batch}')
                limit 0,
                {num};""".format(**locals())

    @get_results(0, 0)
    def get_follow_ql_code(self, hh_code):
        """获取连台请领单号"""
        with allure.step('DB:获取连台请领单号'):
            return """select
                F_CST_ENQUIRY_NO
            from
                bo_eu_surgery_ior_header
            where
                BINDID in(
                select
                    id
                from
                    wfc_process
                where
                    CREATEUSERDEPTID in(
                    select
                        TARGETDEPTID
                    from
                        wfc_task
                    where
                        TASKTITLE like '%{}%'))
            order by
                CREATEDATE desc
            limit 0,1;""".format(hh_code)

    @get_results(None, None)
    def get_back_able_serial(self, order_code):
        """获取还回可用序列号"""
        with allure.step('DB:获取还回可用序列号'):
            return """select
            F_SERIAL
        from
            aws.bo_eu_surgery_serial_detail
        where
            F_ORDER_NO = '{}' 
            and F_SERIAL_STATUS = 0;""".format(order_code)

    @get_results(None, None)
    def get_jr_info(self, order_code):
        """获取借入单信息"""
        with allure.step('DB:获取借入单信息'):
            return """select
                F_COMMODITY_CODE,
                F_SERIAL_MANGE_FLAG,
                F_BATCH_NO,
                F_RECEIVE_NUM
            from
                bo_eu_surgery_borrow_detail
            where
                F_REL_SALE_BILL_NO = '{}';""".format(order_code)

    @get_results(None, None)
    def get_surgery_serial(self, order_code, good, batch):
        """获取手术跟台序列号"""
        with allure.step('DB:获取借出单商品序列号'):
            return """select F_SERIAL from
                bo_eu_surgery_serial_detail
            where
                F_ORDER_NO = '{order_code}' 
                and F_COMMODITY_CODE = '{good}'
                and F_BATCH_NO = '{batch}';""".format(**locals())
