# -*- coding: utf-8 -*-
# @Time    : 2018/8/15 下午2:13
# @Author  : 范佳楠

import ssl
import tempfile
import time
from datetime import datetime

import boto3
import pandas as pd
import pytz

ssl._create_default_https_context = ssl._create_unverified_context

_TZINFO = pytz.timezone("Asia/Shanghai")

S3_BUCKET = "ext-etl-data"
S3 = boto3.resource("s3")
CLEANED_PATH = "clean_data/source_id={source_id}/clean_date={date}/target_table={target_table}/dump={timestamp}&rowcount={rowcount}.csv.gz"

category_dict = {
    '69YYYYYYYYYYYYY': (1, 2, 3),
    '72YYYYYYYYYYYYY': (2, 4, 6),
    '54YYYYYYYYYYYYY': (2, 4, 6),
    '56YYYYYYYYYYYYY': (2, 4, 6),
    '57YYYYYYYYYYYYY': (2, 4, 6),
    '74YYYYYYYYYYYYY': (2, 4, 6),
    '83YYYYYYYYYYYYY': (2, 4, 6)
}

branch_dict = {
    '69YYYYYYYYYYYYY': 2,
    '72YYYYYYYYYYYYY': 2,
    '54YYYYYYYYYYYYY': 2,
    '56YYYYYYYYYYYYY': 3,
    '57YYYYYYYYYYYYY': 3,
    '74YYYYYYYYYYYYY': 3,
    '83YYYYYYYYYYYYY': 2,
}

def clean_sixun(source_id, date, target_table, data_frames):
    if target_table == "goodsflow":
        return clean_goodsflow(source_id, date, target_table, data_frames)
    elif target_table == "cost":
        return clean_cost(source_id, date, target_table, data_frames)
    elif target_table == "store":
        return clean_store(source_id, date, target_table, data_frames)
    elif target_table == "goods":
        return clean_goods(source_id, date, target_table, data_frames)
    elif target_table == "category":
        return clean_category(source_id, date, target_table, data_frames)
    elif target_table == 'sales_target':
        return sales_target(source_id, date, target_table, data_frames)
    elif target_table == 'goods_loss':
        return clean_goods_loss(source_id, date, target_table, data_frames)
    elif target_table == 'requireorder':
        return clean_requireorder(source_id, date, target_table, data_frames)

# 门店表
'''
    'origin_table_columns': {
            "t_bd_branch_info": ['branch_no',
                                 'branch_name',
                                 'address',
                                 'dj_yw',
                                 'init_date',
                                 'branch_no',
                                 'branch_tel',
                                 'branch_fax',
                                 'other1',
                                 'trade_type',
                                 'property',
                                 ]
        },

        'converts': {"t_bd_branch_info": {'branch_no': 'str', 'property': 'int', 'trade_type': 'int', 'dj_yw': 'int'}}
'''

def clean_store(source_id, date, target_table, data_frames):
    cmid = source_id.split("Y")[0]
    branch_info_frame = data_frames['t_bd_branch_info']
    branch_info_frame['dj_yw'] = branch_info_frame['dj_yw'].map(lambda value: '已冻结' if value == '1' else '未冻结')
    branch_info_frame = (branch_info_frame[(branch_info_frame['property'] == 0)])

    def trade_type_convert(value):
        if value == 2:
            return '加盟店'
        elif value == 0:
            return '总部托管'
        else:
            return '独立管理'

    branch_info_frame['property'] = branch_info_frame['trade_type'].map(trade_type_convert)
    branch_info_frame['cmid'] = cmid
    branch_info_frame['source_id'] = source_id
    branch_info_frame['address_code'] = ''
    branch_info_frame['device_id'] = ''
    branch_info_frame['lat'] = None
    branch_info_frame['lng'] = None
    branch_info_frame['area_code'] = None
    branch_info_frame['area_name'] = None

    if source_id == '54YYYYYYYYYYYYY':
        branch_info_frame['business_area'] = None
        branch_info_frame['contacts'] = branch_info_frame['branch_man']
    else:
        branch_info_frame['business_area'] = branch_info_frame['other1']
        branch_info_frame['contacts'] = branch_info_frame['branch_fax']

    branch_info_frame = branch_info_frame.rename(columns={'branch_no': 'foreign_store_id',
                                                          'branch_name': 'store_name',
                                                          'address': 'store_address',
                                                          'dj_yw': 'store_status',
                                                          'init_date': 'create_date',
                                                          'branch_tel': 'phone_number',
                                                          'trade_type': 'property_id'
                                                          })

    branch_info_frame['show_code'] = branch_info_frame['foreign_store_id']
    branch_info_frame['last_updated'] = datetime.now(_TZINFO)

    branch_info_frame = branch_info_frame[[
        'cmid',
        'foreign_store_id',
        'store_name',
        'store_address',
        'address_code',
        'device_id',
        'store_status',
        'create_date',
        'lat',
        'lng',
        'show_code',
        'phone_number',
        'contacts',
        'area_code',
        'area_name',
        'business_area',
        'property_id',
        'property',
        'source_id',
        'last_updated'
    ]]

    branch_info_frame['foreign_store_id'] = branch_info_frame['foreign_store_id'].str.strip()
    branch_info_frame['show_code'] = branch_info_frame['show_code'].str.strip()

    return upload_to_s3(branch_info_frame, source_id, date, target_table)

# 商品分类

'''
    'origin_table_columns': {
            "t_bd_item_cls": ['item_clsno',
                              'item_clsname',
                              'cls_parent',
                              ]
        },

    'converts': {"t_bd_item_cls": {'item_clsno': 'str', 'item_clsname': 'str', 'cls_parent': 'str'}}
'''

def clean_category(source_id, date, target_table, data_frames):
    cmid = source_id.split("Y")[0]
    lv1_len, lv2_len, lv3_len = category_dict[source_id]
    item_cls_frame_1 = data_frames['t_bd_item_cls']
    # item_cls_frame_1 = pd.read_csv(filepath_or_buffer=item_cls_path)
    item_cls_frame_2 = item_cls_frame_1.copy(deep=True)
    item_cls_frame_3 = item_cls_frame_1.copy(deep=True)
    # 处理item_cls_frame1
    item_cls_frame_1 = item_cls_frame_1[item_cls_frame_1['item_clsno'].str.len() == lv1_len]
    item_cls_frame_1 = item_cls_frame_1[['item_clsno', 'item_clsname']]
    item_cls_frame_1 = item_cls_frame_1.rename(columns={'item_clsno': 'foreign_category_lv1',
                                                        'item_clsname': 'foreign_category_lv1_name'})
    item_cls_frame_1['cmid'] = cmid
    item_cls_frame_1['level'] = 1
    item_cls_frame_1['last_updated'] = datetime.now(_TZINFO)
    item_cls_frame_1['foreign_category_lv2'] = ''
    item_cls_frame_1['foreign_category_lv2_name'] = None
    item_cls_frame_1['foreign_category_lv3'] = ''
    item_cls_frame_1['foreign_category_lv3_name'] = None
    item_cls_frame_1['foreign_category_lv4'] = ''
    item_cls_frame_1['foreign_category_lv4_name'] = None
    item_cls_frame_1['foreign_category_lv5'] = ''
    item_cls_frame_1['foreign_category_lv5_name'] = None
    item_cls_frame_1['last_updated'] = datetime.now(_TZINFO)

    # 处理item_cls_frame2
    item_cls_frame_2 = pd.merge(item_cls_frame_2, item_cls_frame_2, how='left', left_on='item_clsno',
                                right_on='cls_parent',
                                suffixes=('_lv1', '_lv2'))
    item_cls_frame_2 = item_cls_frame_2[item_cls_frame_2['item_clsno_lv2'].str.len() == lv2_len]

    item_cls_frame_2 = item_cls_frame_2[['item_clsno_lv1', 'item_clsname_lv1', 'item_clsno_lv2', 'item_clsname_lv2']]
    item_cls_frame_2 = item_cls_frame_2.rename(columns={'item_clsno_lv1': 'foreign_category_lv1',
                                                        'item_clsname_lv1': 'foreign_category_lv1_name',
                                                        'item_clsno_lv2': 'foreign_category_lv2',
                                                        'item_clsname_lv2': 'foreign_category_lv2_name'})
    item_cls_frame_2['level'] = 2
    item_cls_frame_2['foreign_category_lv3'] = ''
    item_cls_frame_2['foreign_category_lv3_name'] = None
    item_cls_frame_2['foreign_category_lv4'] = ''
    item_cls_frame_2['foreign_category_lv4_name'] = None
    item_cls_frame_2['foreign_category_lv5'] = ''
    item_cls_frame_2['foreign_category_lv5_name'] = None
    item_cls_frame_2['cmid'] = cmid
    item_cls_frame_2['last_updated'] = datetime.now(_TZINFO)

    # 处理item_cls_frame3

    item_cls_frame_3 = pd.merge(item_cls_frame_3, item_cls_frame_3,
                                left_on='item_clsno',
                                right_on='cls_parent',
                                suffixes=('_lv1', '_lv2'),
                                how='left').merge(item_cls_frame_3, how='left', left_on='item_clsno_lv2',
                                                  right_on='cls_parent')

    item_cls_frame_3 = item_cls_frame_3[item_cls_frame_3['item_clsno'].str.len() == lv3_len]
    item_cls_frame_3 = item_cls_frame_3[
        ['item_clsno_lv1', 'item_clsname_lv1', 'item_clsno_lv2', 'item_clsname_lv2', 'item_clsno', 'item_clsname']]
    item_cls_frame_3 = item_cls_frame_3.rename(columns={'item_clsno_lv1': 'foreign_category_lv1',
                                                        'item_clsname_lv1': 'foreign_category_lv1_name',
                                                        'item_clsno_lv2': 'foreign_category_lv2',
                                                        'item_clsname_lv2': 'foreign_category_lv2_name',
                                                        'item_clsno': 'foreign_category_lv3',
                                                        'item_clsname': 'foreign_category_lv3_name'})
    item_cls_frame_3['foreign_category_lv4'] = ''
    item_cls_frame_3['foreign_category_lv4_name'] = None
    item_cls_frame_3['foreign_category_lv5'] = ''
    item_cls_frame_3['foreign_category_lv5_name'] = None
    item_cls_frame_3['level'] = 3
    item_cls_frame_3['cmid'] = cmid
    item_cls_frame_3['last_updated'] = datetime.now(_TZINFO)

    category_frame = pd.concat([item_cls_frame_1, item_cls_frame_2, item_cls_frame_3])
    category_frame = category_frame[[
        "cmid",
        "level",
        "foreign_category_lv1",
        "foreign_category_lv1_name",
        "foreign_category_lv2",
        "foreign_category_lv2_name",
        "foreign_category_lv3",
        "foreign_category_lv3_name",
        "last_updated",
        "foreign_category_lv4",
        "foreign_category_lv4_name",
        "foreign_category_lv5",
        "foreign_category_lv5_name"
    ]]
    return upload_to_s3(category_frame, source_id, date, target_table)

# 商品

'''
     'origin_table_columns': {
            't_bd_item_info': ['item_clsno',
                               'main_supcust',
                               'status',
                               'num2',
                               'item_no',
                               'item_name',
                               'price',
                               'sale_price',
                               'unit_no',
                               'item_subno',
                               'item_brandname',
                               'build_date'
                               ],
            't_bd_supcust_info': ['supcust_no', 'sup_name', 'supcust_flag'],
            "t_bd_item_cls": ['item_clsno']

        },
        'converts': {
            "t_bd_item_cls": {'item_clsno': 'str'},
            't_bd_item_info': {'item_clsno': 'str', 'num2': 'str',
                               'main_supcust': 'str',
                               'status': 'int',
                               'item_no': 'str',

                               },
            't_bd_supcust_info': {'supcust_no': 'str'}

'''

def clean_goods(source_id, date, target_table, data_frames):
    cmid = source_id.split("Y")[0]
    lv1_len, lv2_len, lv3_len = category_dict[source_id]
    goods_frame = data_frames['t_bd_item_info']
    item_cls_frame = data_frames['t_bd_item_cls']
    supcust_info_frame = data_frames['t_bd_supcust_info']

    def num2_convert(row):
        try:
            return float(row['num2'])
        except Exception:
            return 0

    goods_frame['num2'] = goods_frame.apply(num2_convert, axis=1)

    goods_frame_1 = goods_frame.copy(deep=True)
    goods_frame_1['item_clsno_1'] = goods_frame['item_clsno'].str.slice(0, lv1_len)
    goods_frame_1['item_clsno_2'] = goods_frame['item_clsno'].str.slice(0, lv2_len)
    goods_frame_1['item_clsno_3'] = goods_frame['item_clsno'].str.slice(0, lv3_len)

    goods_frame_1 = pd.merge(goods_frame_1, item_cls_frame, left_on='item_clsno_1', right_on='item_clsno', how='left',
                             suffixes=('_goods', '_lv1'))

    lv2 = item_cls_frame.copy(deep=True)
    lv2 = lv2[lv2['item_clsno'].str.len() == lv2_len]

    goods_frame_1 = pd.merge(goods_frame_1, lv2, left_on='item_clsno_2', right_on='item_clsno', how='left',
                             suffixes=('_2', '_lv2'))

    lv3 = item_cls_frame.copy(deep=True)
    lv3 = lv3[lv3['item_clsno'].str.len() == lv3_len]

    goods_frame_1 = pd.merge(goods_frame_1, lv3, how='left', left_on='item_clsno_3', right_on='item_clsno',
                             suffixes=('_end', '_lv3'))

    goods_frame_1 = pd.merge(goods_frame_1, supcust_info_frame, left_on='main_supcust', right_on='supcust_no')
    goods_frame_1 = goods_frame_1[goods_frame_1['supcust_flag'] == 'S']

    # 映射字段
    goods_frame_1 = goods_frame_1.rename(columns={
        'item_no': 'barcode',
        'item_name': 'item_name',
        'price': 'lastin_price',
        'sale_price': 'sale_price',
        'unit_no': 'item_unit',
        'status': 'item_status',
        'item_clsno_lv1': 'foreign_category_lv1',
        'item_clsno_end': 'foreign_category_lv2',
        'item_clsno_lv3': 'foreign_category_lv3',
        'build_date': 'storage_time',
        'num2': 'warranty',
        'item_subno': 'show_code',
        'sup_name': 'supplier_name',
        'supcust_no': 'supplier_code',
        'item_brandname': 'brand_name'
    })

    def status_convert(status):
        if status == 0:
            return '建档'
        elif status == 1:
            return '新品'
        elif status == 2:
            return '正常'
        elif status == 3 or status == 5:
            return '停购'
        elif status == 4:
            return '停售'
        else:
            return '其他'

    goods_frame_1['item_status'] = goods_frame_1['item_status'].map(status_convert)
    goods_frame_1['cmid'] = cmid
    goods_frame_1['last_updated'] = datetime.now(_TZINFO)
    goods_frame_1['isvalid'] = '1'
    goods_frame_1['foreign_category_lv4'] = ''
    goods_frame_1['foreign_category_lv5'] = ''
    goods_frame_1['allot_method'] = ''
    goods_frame_1['foreign_item_id'] = goods_frame_1['barcode']

    goods_frame_1['warranty'] = goods_frame_1.apply(lambda row: int(row['warranty']), axis=1)

    goods_frame_1 = goods_frame_1[[
        "cmid",
        "barcode",
        "foreign_item_id",
        "item_name",
        "lastin_price",
        "sale_price",
        "item_unit",
        "item_status",
        "foreign_category_lv1",
        "foreign_category_lv2",
        "foreign_category_lv3",
        "foreign_category_lv4",
        "storage_time",
        "last_updated",
        "isvalid",
        "warranty",
        "show_code",
        "foreign_category_lv5",
        "allot_method",
        "supplier_name",
        "supplier_code",
        "brand_name",
    ]]

    goods_frame_1['barcode'] = goods_frame_1['barcode'].str.strip()
    goods_frame_1['foreign_item_id'] = goods_frame_1['foreign_item_id'].str.strip()
    goods_frame_1['brand_name'] = goods_frame_1['brand_name'].str.strip()

    return upload_to_s3(goods_frame_1, source_id, date, target_table)

# 商品销售表
'''
     'origin_table_columns': {
            't_rm_saleflow': ['branch_no',
                              'item_no',
                              'sale_price',
                              'sale_qnty',
                              'sell_way',
                              'sale_money',
                              'flow_no',
                              'oper_date'],
            't_bd_branch_info': ['branch_no', 'branch_name'],
            't_bd_item_info': ['item_no', 'item_clsno', 'item_name', 'unit_no'],
            't_bd_item_cls': ['item_clsno', 'item_clsname'],
        },

        'converts': {
            't_rm_saleflow': {'branch_no': 'str',
                              'item_no': 'str',
                              'sale_price': 'float',
                              'sale_qnty': 'float',
                              'sell_way': 'str',
                              'sale_money': 'float',
                              'flow_no': 'str'
                              },

            't_bd_branch_info': {'branch_no': 'str'},
            't_bd_item_info': {'item_no': 'str', 'item_clsno': 'str'， 'unit_no':'str'},
            't_bd_item_cls': {'item_clsno': 'str'},
        }

'''

def clean_goodsflow(source_id, date, target_table, data_frames):
    lv1_len, lv2_len, lv3_len = category_dict[source_id]
    cmid = source_id.split("Y")[0]
    branch_no_len = branch_dict[source_id]
    sale_flow_frame = data_frames['t_rm_saleflow']
    branch_info_frame = data_frames['t_bd_branch_info']
    goods_frame = data_frames['t_bd_item_info']
    item_cls_frame = data_frames['t_bd_item_cls']

    columns = [
        'source_id',
        'cmid',
        'foreign_store_id',
        'store_name',
        'receipt_id',
        'consumer_id',
        'saletime',
        'last_updated',
        'foreign_item_id',
        'barcode',
        'item_name',
        'item_unit',
        'saleprice',
        'quantity',
        'subtotal',
        'foreign_category_lv1',
        'foreign_category_lv1_name',
        'foreign_category_lv2',
        'foreign_category_lv2_name',
        'foreign_category_lv3',
        'foreign_category_lv3_name',
        'foreign_category_lv4',
        'foreign_category_lv4_name',
        'foreign_category_lv5',
        'foreign_category_lv5_name',
        'pos_id'
    ]

    if not len(sale_flow_frame):
        return upload_to_s3(pd.DataFrame(columns=columns), source_id, date, target_table)

    # 分类

    # 商品
    goods_frame['item_clsno_1'] = goods_frame['item_clsno'].str.slice(0, lv1_len)
    goods_frame['item_clsno_2'] = goods_frame['item_clsno'].str.slice(0, lv2_len)
    goods_frame['item_clsno_3'] = goods_frame['item_clsno'].str.slice(0, lv3_len)

    # 销售
    sale_flow_frame['branch_no_1'] = sale_flow_frame['branch_no'].str.slice(0, branch_no_len)

    # 门店
    result_frame = pd.merge(sale_flow_frame, branch_info_frame, left_on='branch_no_1', right_on='branch_no', how='left',
                            suffixes=('_s1', '_store'))

    # item_clsno
    result_frame = pd.merge(result_frame, goods_frame, left_on='item_no', right_on='item_no', how='left',
                            suffixes=('_s2', '_goods'))
    '''
        lv1: item_cls_lv1 item_clsname
    '''

    result_frame = pd.merge(result_frame, item_cls_frame, left_on='item_clsno_1', right_on='item_clsno', how='left',
                            suffixes=('_s3', '_lv1'))

    lv2 = item_cls_frame[item_cls_frame['item_clsno'].str.len() == lv2_len]

    '''
        lv1 item_cls_lv1 item_clsname_s4
        lv2 item_clsno item_clsname_lv2
    '''
    result_frame = pd.merge(result_frame, lv2, left_on='item_clsno_2', right_on='item_clsno', how='left',
                            suffixes=('_s4', '_lv2'))

    lv3 = item_cls_frame[item_cls_frame['item_clsno'].str.len() == lv3_len]

    '''
        lv2 item_clsno_s5  item_clsname_lv2
        lv3 item_clsno_lv3 item_clsname
    '''
    result_frame = pd.merge(result_frame, lv3, left_on='item_clsno_3', right_on='item_clsno', how='left',
                            suffixes=('_s5', '_lv3'))

    result_frame = result_frame[(result_frame['item_no'].notna()) & (result_frame['branch_no_store'].notna())]

    def saleprice_convert(row):
        print(row['sell_way'])
        if row['sell_way'] == 'C':
            return 0
        else:
            return row['sale_price']

    def quantity_convert(row):
        if row['sell_way'] == 'A' or row['sell_way'] == 'C':
            return row['sale_qnty']
        else:
            return -1 * row['sale_qnty']

    def subtotal(row):
        if row['sell_way'] == 'A':
            return row['sale_money']
        elif row['sell_way'] == 'B':
            return -1 * row['sale_money']
        elif row['sell_way'] == 'C':
            return 0
        else:
            pass

    result_frame['saleprice'] = result_frame.apply(saleprice_convert, axis=1)
    result_frame['quantity'] = result_frame.apply(quantity_convert, axis=1)
    result_frame['subtotal'] = result_frame.apply(subtotal, axis=1)
    result_frame['item_clsno_s5'] = result_frame['item_clsno_s5'].map(lambda x: x if x else '')
    result_frame['item_clsno_lv3'] = result_frame['item_clsno_lv3'].map(lambda x: x if x else '')

    if source_id == '83YYYYYYYYYYYYY':
        result_frame['foreign_category_lv1'] = result_frame['item_clsno_s3']
    else:
        result_frame['foreign_category_lv1'] = result_frame['item_clsno_lv1']

    result_frame = result_frame.rename(columns={
        'branch_no_store': 'foreign_store_id',
        'branch_name': 'store_name',
        'flow_no': 'receipt_id',
        'oper_date': 'saletime',
        'item_no': 'foreign_item_id',
        'item_name': 'item_name',
        'unit_no': 'item_unit',
        'item_clsname_s4': 'foreign_category_lv1_name',
        'item_clsno_s5': 'foreign_category_lv2',
        'item_clsname_lv2': 'foreign_category_lv2_name',
        'item_clsno_lv3': 'foreign_category_lv3',
        'item_clsname': 'foreign_category_lv3_name',
    })

    result_frame['source_id'] = source_id
    result_frame['cmid'] = cmid
    result_frame['consumer_id'] = None
    result_frame['last_updated'] = datetime.now(_TZINFO)
    result_frame['barcode'] = result_frame['foreign_item_id']
    result_frame['foreign_category_lv4'] = ''
    result_frame['foreign_category_lv4_name'] = None
    result_frame['foreign_category_lv5'] = ''
    result_frame['foreign_category_lv5_name'] = None
    result_frame['pos_id'] = ''

    result_frame = result_frame[[
        'source_id',
        'cmid',
        'foreign_store_id',
        'store_name',
        'receipt_id',
        'consumer_id',
        'saletime',
        'last_updated',
        'foreign_item_id',
        'barcode',
        'item_name',
        'item_unit',
        'saleprice',
        'quantity',
        'subtotal',
        'foreign_category_lv1',
        'foreign_category_lv1_name',
        'foreign_category_lv2',
        'foreign_category_lv2_name',
        'foreign_category_lv3',
        'foreign_category_lv3_name',
        'foreign_category_lv4',
        'foreign_category_lv4_name',
        'foreign_category_lv5',
        'foreign_category_lv5_name',
        'pos_id'
    ]]
    return upload_to_s3(result_frame, source_id, date, target_table)

# 成本表
'''
    'origin_table_columns': {
            "t_bd_item_cls": ['item_clsno', ],
            't_da_jxc_daysum': [
                'branch_no',
                'oper_date',
                'so_qty',
                'pos_qty',
                'so_amt',
                'pos_amt',
                'fifo_cost_amt',
                'item_no'
            ],
            't_bd_item_info': ['item_no', 'item_clsno']
        },

        'converts': {
            "t_bd_item_cls": {'item_clsno': 'str'},
            't_bd_item_info': {'item_no': 'str', 'item_clsno': 'str'},
            't_da_jxc_daysum': {
                'branch_no': 'str',
                'so_qty': 'float',
                'pos_qty': 'float',
                'so_amt': 'float',
                'pos_amt': 'float',
                'fifo_cost_amt': 'float',
                'item_no': 'str'
            }
        }
'''

def clean_cost(source_id, date, target_table, data_frames):
    columns = [
        'source_id',
        'foreign_store_id',
        'foreign_item_id',
        'date',
        'cost_type',
        'total_quantity',
        'total_sale',
        'total_cost',
        'foreign_category_lv1',
        'foreign_category_lv2',
        'foreign_category_lv3',
        'foreign_category_lv4',
        'foreign_category_lv5',
        'cmid'
    ]

    cmid = source_id.split("Y")[0]
    cost_frame = data_frames['t_da_jxc_daysum']

    if not len(cost_frame):
        return upload_to_s3(pd.DataFrame(columns=columns), source_id, date, target_table)

    goods_frame = data_frames['t_bd_item_info']
    item_cls_frame = data_frames['t_bd_item_cls']

    lv1_len, lv2_len, lv3_len = category_dict[source_id]
    branch_no_len = branch_dict[source_id]
    goods_frame['item_clsno_1'] = goods_frame['item_clsno'].str.slice(0, lv1_len)
    goods_frame['item_clsno_2'] = goods_frame['item_clsno'].str.slice(0, lv2_len)
    goods_frame['item_clsno_3'] = goods_frame['item_clsno'].str.slice(0, lv3_len)

    result_frame = pd.merge(cost_frame, goods_frame, left_on='item_no', right_on='item_no', how='left',
                            suffixes=('_cost', '_goods'))
    # s3 -> c1
    '''
        lv1 item_clsno_lv1 item_clsname
    '''

    result_frame = pd.merge(result_frame, item_cls_frame, left_on='item_clsno_1', right_on='item_clsno', how='left',
                            suffixes=('_c1', '_lv1'))

    lv2 = item_cls_frame[item_cls_frame['item_clsno'].str.len() == lv2_len]

    '''
        lv1 item_clsno_lv1 item_clsname_c2
        lv2 item_clsno item_clsname_lv2
    '''
    # s4 - c2
    result_frame = pd.merge(result_frame, lv2, left_on='item_clsno_2', right_on='item_clsno', how='left',
                            suffixes=('_c2', '_lv2'))

    lv3 = item_cls_frame[item_cls_frame['item_clsno'].str.len() == lv3_len]

    '''
        lv1 item_clsno_lv1 item_clsname_c2
        lv2 item_clsno_c3 item_clsname_lv2
        lv3 item_clsno_lv3 item_clsname
    '''
    # s5 - c3
    result_frame = pd.merge(result_frame, lv3, left_on='item_clsno_3', right_on='item_clsno', how='left',
                            suffixes=('_c3', '_lv3'))

    result_frame = result_frame[(result_frame['so_qty'] != 0)
                                | (result_frame['pos_qty'] != 0)
                                | (result_frame['so_amt'] != 0)
                                | (result_frame['pos_amt'] != 0)
                                ]
    result_frame['branch_no'] = result_frame['branch_no'].str.slice(0, branch_no_len)

    branch_no_zero = '0' * branch_no_len
    result_frame = result_frame[(result_frame['branch_no'] != branch_no_zero) & (result_frame['item_no'].notna())]

    result_frame['item_clsno_c3'] = result_frame['item_clsno_c3'].map(lambda x: x if x else '')
    result_frame['item_clsno_lv3'] = result_frame['item_clsno_lv3'].map(lambda x: x if x else '')

    if source_id == '54YYYYYYYYYYYYY':
        result_frame['total_cost'] = result_frame.apply(lambda row: row['so_cost'] + row['pos_cost'], axis=1)
    else:
        result_frame['total_cost'] = result_frame['fifo_cost_amt']

    if source_id == '83YYYYYYYYYYYYY':
        result_frame['foreign_category_lv1'] = result_frame['item_clsno_c1']
    else:
        result_frame['foreign_category_lv1'] = result_frame['item_clsno_lv1']

    result_frame = result_frame.rename(columns={
        'branch_no': 'foreign_store_id',
        'item_no': 'foreign_item_id',
        'item_clsno_c3': 'foreign_category_lv2',
        'item_clsno_lv3': 'foreign_category_lv3',
    })

    result_frame['date'] = pd.to_datetime(result_frame['oper_date'], format='%Y-%m-%d')
    result_frame['cost_type'] = ''

    def total_qualtity_convert(row):
        return row['so_qty'] + row['pos_qty']

    def total_sale_convert(row):
        return row['so_amt'] + row['pos_amt']

    result_frame['total_quantity'] = result_frame.apply(total_qualtity_convert, axis=1)
    result_frame['total_sale'] = result_frame.apply(total_sale_convert, axis=1)

    result_frame['foreign_category_lv4'] = ''
    result_frame['foreign_category_lv5'] = ''
    result_frame['source_id'] = source_id
    result_frame['cmid'] = cmid

    result_frame = result_frame[columns]
    return upload_to_s3(result_frame, source_id, date, target_table)

def upload_to_s3(frame, source_id, date, target_table):
    filename = tempfile.NamedTemporaryFile(mode="w", encoding="utf-8")
    count = len(frame)
    frame.to_csv(filename.name, index=False, compression="gzip", float_format='%.4f')
    filename.seek(0)
    key = CLEANED_PATH.format(
        source_id=source_id,
        target_table=target_table,
        date=date,
        timestamp=now_timestamp(),
        rowcount=count,
    )
    S3.Bucket(S3_BUCKET).upload_file(filename.name, key)

    return key

"""
    'origin_table_columns': {
            "t_bd_item_cls": ['item_clsno', ],
            't_im_check_master': [
                check_no,branch_no,sheet_no,oper_date,approve_flag
            ],
            't_im_check_sum': ['item_no', 'sheet_no', 'branch_no', 'balance_qty', 'sale_price'],
            't_bd_branch_info':['branch_no','branch_name'],
            't_bd_item_info':['item_no', 'item_subno', 'item_name', 'unit_no', 'item_clsno'],

        },

        'converts': {
            "t_bd_item_cls": {'item_clsno':'str'}
            't_im_check_master': {
                    "check_no":"str",
                    'branch_no':"str",
                    'sheet_no':'str',
                    'oper_date':'str',
                    'approve_flag':'str'
            },
            't_im_check_sum': {
                'item_no':'str',
                'sheet_no':'str',
                'branch_no':'str',
                'balance_qty':'str',
                'sale_price':'float'
            },
            't_bd_branch_info':{
                'branch_no':'str'
            },
            't_bd_item_info':{
                'item_no':'str',
                'item_subno':'str',
                'item_name':'str',
                'unit_no':'str',
                'item_clsno':'str'
            },
        }
"""

def clean_goods_loss(source_id, date, target_table, data_frames):
    columns = [
        "cmid",
        "source_id",
        "lossnum",
        "lossdate",
        "foreign_store_id",
        "store_show_code",
        "store_name",
        "foreign_item_id",
        "item_showcode",
        "barcode",
        "item_name",
        "item_unit",
        "quantity",
        "subtotal",
        "foreign_category_lv1",
        "foreign_category_lv2",
        "foreign_category_lv3",
        "foreign_category_lv4",
        "foreign_category_lv5",
    ]
    cmid = source_id.split("Y")[0]
    header = data_frames['t_im_check_master']

    if not len(header):
        return upload_to_s3(pd.DataFrame(columns=columns), source_id, date, target_table)

    detail = data_frames['t_im_check_sum']

    def qty_convert(row):
        # print(row['balance_qty'])
        try:
            value = float(row['balance_qty'])
            # print(value)
            return value
        except Exception:
            return 0

    detail['balance_qty'] = detail.apply(qty_convert, axis=1)

    store = data_frames['t_bd_branch_info']
    goods = data_frames['t_bd_item_info']
    item_cls = data_frames['t_bd_item_cls']

    len_1, len_2, len_3 = category_dict[source_id]

    goods['item_clsno_1'] = goods.apply(lambda row: row['item_clsno'][:len_1], axis=1)
    goods['item_clsno_2'] = goods.apply(lambda row: row['item_clsno'][:len_2], axis=1)

    header['branch_no_1'] = header.apply(lambda row: row['branch_no'][:branch_dict[source_id]], axis=1)

    result = header.merge(
        detail,
        how='left',
        left_on=['check_no', 'branch_no'],
        right_on=['sheet_no', 'branch_no'],
        suffixes=('.header', '.detail')
    ).merge(
        store,
        how='left',
        left_on='branch_no_1',
        right_on='branch_no',
        suffixes=('', '.store'),
    ).merge(
        goods,
        how='left',
        on='item_no',
    ).merge(
        item_cls,
        how='left',
        left_on='item_clsno_1',
        right_on='item_clsno',
        suffixes=('', '_lv1'),
    ).merge(
        item_cls,
        how='left',
        left_on='item_clsno_2',
        right_on='item_clsno',
        suffixes=('', '_lv2'),
    ).merge(
        item_cls,
        how='left',
        left_on='item_clsno',
        right_on='item_clsno',
        suffixes=('', '_lv3'),
    )

    result = result[result['balance_qty'] < 0]
    result = result[result['approve_flag'] == '1']
    result = result[result['branch_no_1'] != '00']

    if not len(result):
        return upload_to_s3(pd.DataFrame(columns=columns), source_id, date, target_table)

    result['cmid'] = cmid
    result['source_id'] = source_id
    result['item_showcode'] = result['branch_no.store']

    result['subtotal'] = result.apply(lambda row: row['balance_qty'] * row['sale_price'], axis=1)
    result['foreign_category_lv4'] = ''
    result['foreign_category_lv5'] = ''
    result['barcode'] = result['item_no']
    result = result.rename(columns={
        'sheet_no.header': 'lossnum',
        'oper_date': 'lossdate',
        'branch_no.store': 'foreign_store_id',
        'branch_name': 'store_name',
        'item_no': 'foreign_item_id',
        'item_subno': 'store_show_code',
        'item_name': 'item_name',
        'unit_no': 'item_unit',
        'balance_qty': 'quantity',
        'item_clsno_lv1': 'foreign_category_lv1',
        'item_clsno_lv2': 'foreign_category_lv2',
        'item_clsno': 'foreign_category_lv3',
    })

    result['lossdate'] = result.apply(lambda row: row['lossdate'].split()[0], axis=1)

    result = result[columns]

    return upload_to_s3(result, source_id, date, target_table)

"""
'origin_table_columns': {
            "t_da_sell_aim": ['branch_no', 'current_date', 'month_aim'],
            't_bd_branch_info': [
                'branch_no',
                'branch_name',

            ],

        },

        'converts': {
            "t_da_sell_aim": {
                'branch_no':'str',
                'current_date':'str'
            }

            't_bd_branch_info':{
                'branch_no':'str'
            },
        }
"""

def sales_target(source_id, date, target_table, data_frames):
    cmid = source_id.split("Y")[0]
    target = data_frames['t_da_sell_aim']
    store = data_frames['t_bd_branch_info']

    result = target.merge(
        store,
        how='left',
        on='branch_no',
    )

    result['source_id'] = source_id
    result['cmid'] = cmid
    result['target_date'] = datetime.now(_TZINFO).strftime('%Y-%m-01')
    result['foreign_store_id'] = result['branch_no']
    result['store_show_code'] = result['branch_no']
    result['store_name'] = result['branch_name']
    result['target_sales'] = result['month_aim']

    result['target_gross_profit'] = 0
    result['category_level'] = 1

    result['last_updated'] = datetime.now(_TZINFO)

    result['foreign_category_lv1'] = ''
    result['foreign_category_lv2'] = ''
    result['foreign_category_lv3'] = ''
    result['foreign_category_lv4'] = ''
    result['foreign_category_lv5'] = ''

    result = result[[
        "source_id",
        "cmid",
        "target_date",
        "foreign_store_id",
        "store_show_code",
        "store_name",
        "target_sales",
        "target_gross_profit",
        "category_level",
        "foreign_category_lv1",
        "foreign_category_lv2",
        "foreign_category_lv3",
        "foreign_category_lv4",
        "foreign_category_lv5",
        "last_updated",
    ]]

    return upload_to_s3(result, source_id, date, target_table)

def clean_requireorder(source_id, date, target_table, data_frames):
    columns = [
        "source_id",
        "cmid",
        "order_num",
        "order_date",
        "order_type",
        "foreign_store_id",
        "store_show_code",
        "store_name",
        "foreign_item_id",
        "item_show_code",
        "barcode",
        "item_name",
        "item_unit",
        "order_qty",
        "order_price",
        "order_total",
        "vendor_id",
        "vendor_show_code",
        "vendor_name",
        "foreign_category_lv1",
        "foreign_category_lv2",
        "foreign_category_lv3",
        "foreign_category_lv4",
        "foreign_category_lv5",
        "purchaser",
    ]
    cmid = source_id.split("Y")[0]
    lv1_len, lv2_len, lv3_len = category_dict[source_id]
    master = data_frames['t_pm_sheet_master']
    detail = data_frames['t_pm_sheet_detail']
    store = data_frames['t_bd_branch_info']
    goods = data_frames['t_bd_item_info']
    sup = data_frames['t_bd_supcust_info']
    master = master[master['trans_no'] == 'YH']
    result = master.merge(
        detail, how='left', on='sheet_no').merge(
        store, how='left', on='branch_no').merge(
        goods, how='left', on='item_no').merge(
        sup, how='left', on='supcust_no')
    result['item_clsno'] = result['item_clsno'].fillna('')
    result['foreign_category_lv1'] = result['item_clsno'].apply(lambda x: '' if len(x) < lv1_len else x[:lv1_len])
    result['foreign_category_lv2'] = result['item_clsno'].apply(lambda x: '' if len(x) < lv2_len else x[:lv2_len])
    result['foreign_category_lv3'] = result['item_clsno'].apply(lambda x: '' if len(x) < lv3_len else x[:lv3_len])
    result['cmid'] = cmid
    result['source_id'] = source_id
    result['store_show_code'] = result['branch_no']
    result['item_show_code'] = result['item_no']
    result['barcode'] = result['item_no']
    result['vendor_show_code'] = result['supcust_no']
    result['foreign_category_lv4'] = ''
    result['foreign_category_lv5'] = ''
    result['purchaser'] = ''
    result['order_type'] = '门店要货'
    result = result.rename(columns={
        'sheet_no': 'order_num',
        'oper_date': 'order_date',
        'branch_no': 'foreign_store_id',
        'branch_name': 'store_name',
        'item_no': 'foreign_item_id',
        'unit_no': 'item_unit',
        'real_qty': 'order_qty',
        'valid_price': 'order_price',
        'sub_amt': 'order_total',
        'supcust_no': 'vendor_id',
        'sup_name': 'vendor_name'
    })
    result = result[columns]

    return upload_to_s3(result, source_id, date, target_table)

def now_timestamp():
    _timestamp = datetime.fromtimestamp(time.time(), tz=_TZINFO)
    return _timestamp


import numpy as np
import matplotlib.pyplot as plt

def contrast(matrix):
    height, width = matrix.shape
    sum_contrast = 0

    for i in range(height):
        for j in range(width):
            sum_contrast += ((abs(i - j))**2) * matrix[i, j]
    return sum_contrast

def energy(matrix):
    height, width = matrix.shape
    sum_energy = 0

    for i in range(height):
        for j in range(width):
            sum_energy += (matrix[i, j])**2
    return sum_energy

def homogeneity(matrix):
    height, width = matrix.shape
    sum_homogeneity = 0

    for i in range(height):
        for j in range(width):
            sum_homogeneity += matrix[i, j]/(1 + (abs(i - j)))
    return sum_homogeneity

def correlation(matrix):
    height, width = matrix.shape
    list_sum_i = []
    list_sum_j = []

    for i, j in zip(range(height), range(width)):
        sum_i = np.sum(matrix[i, :])
        sum_j = np.sum(matrix[:, j])
        list_sum_i.append(sum_i)
        list_sum_j.append(sum_j)
        sum_i = 0
        sum_j = 0

    average_sum_i = 0
    average_sum_j = 0

    for i in range(1, height+1):
        average_sum_i += i * (list_sum_i[i-1])
    for j in range(1, width+1):
        average_sum_j += j * (list_sum_j[j-1])

    sum_aux = 0
    for i in range(1, height+1):
        sum_aux += ((i - average_sum_i)**2) * list_sum_i[i - 1]
    std_i = sum_aux**(1/2)
    sum_aux = 0
    for j in range(1, width+1):
        sum_aux += ((j - average_sum_j)**2) * list_sum_j[j - 1]
    std_j = sum_aux**(1/2)

    sum_corr = 0
    for i in range(1, height+1):
        for j in range(1, width+1):
            sum_corr += ((i - average_sum_i) * (j - average_sum_j) * matrix[i-1, j-1])/(std_i * std_j)
    return sum_corr

def all_features(matrix, exclude = 0):
    height, width = matrix.shape
    sum_contrast = 0
    sum_corr = 0
    sum_energy = 0
    sum_homogeneity = 0
    list_sum_i = []
    list_sum_j = []

    for i, j in zip(range(height), range(width)):
        sum_i = np.sum(matrix[i, :])
        sum_j = np.sum(matrix[:, j])
        list_sum_i.append(sum_i)
        list_sum_j.append(sum_j)

    average_sum_i = 0
    average_sum_j = 0
    if exclude != 4:
        for i in range(1, height + 1):
            average_sum_i += i * (list_sum_i[i - 1])
        for j in range(1, width + 1):
            average_sum_j += j * (list_sum_j[j - 1])

        sum_aux = 0
        for i in range(1, height + 1):
            sum_aux += ((i - average_sum_i) ** 2) * list_sum_i[i - 1]
        std_i = sum_aux ** (1 / 2)
        sum_aux = 0
        for j in range(1, width + 1):
            sum_aux += ((j - average_sum_j) ** 2) * list_sum_j[j - 1]
        std_j = sum_aux ** (1 / 2)

    for i in range(1, height + 1):
        for j in range(1, width + 1):
            if exclude != 1:
                sum_contrast += ((abs(i - j))**2) * matrix[i - 1, j - 1]
            if exclude != 2:
                sum_energy += (matrix[i - 1, j - 1])**2
            if exclude != 3:
                sum_homogeneity += matrix[i - 1, j - 1]/(1 + (abs(i - j)))
            if exclude != 4:
                sum_corr += ((i - average_sum_i) * (j - average_sum_j) *
                                 matrix[i - 1, j - 1]) / (std_i * std_j)

    if(exclude == 3):
        return [sum_contrast, sum_corr, sum_energy]
    return [sum_contrast, sum_corr, sum_energy, sum_homogeneity]

# Faço a relação de todas as features entre os três tipos de terreno #
# 1 = Asfalto, primeiras 25 imagens #
# 2 = Perigo, próximas 25 imagens #
# 3 = Grama, próximas 25 imagens #
# Com esses valores, seleciono o maior valor de cada terreno e identifico a característica em comum nas 3 relações, então a elimino #

def feature_relation(feat_array):
    for type in range(3):
        if type == 1:
            relation_con_cor = np.corrcoef(feat_array[:25, 0], feat_array[:25, 1])
            relation_con_ene = np.corrcoef(feat_array[:25, 0], feat_array[:25, 2])
            relation_con_hom = np.corrcoef(feat_array[:25, 0], feat_array[:25, 3])
            relation_cor_ene = np.corrcoef(feat_array[:25, 1], feat_array[:25, 2])
            relation_cor_hom = np.corrcoef(feat_array[:25, 1], feat_array[:25, 3])
            relation_ene_hom = np.corrcoef(feat_array[:25, 2], feat_array[:25, 3])
            relation_list_1 = [(abs(relation_con_cor[0, 1]), 'Contrast&Correlation'), (abs(relation_con_ene[0, 1]), 'Contrast&Energy'), (abs(relation_con_hom[0, 1]), 'Contrast&Homogeneity'),
                             (abs(relation_cor_ene[0, 1]), 'Correlation&Energy'), (abs(relation_cor_hom[0, 1]), 'Correlation&Homogeneity'), (abs(relation_ene_hom[0, 1]), 'Energy&Homogeneity')]
            relation_list_1.sort()
        elif type == 2:
            relation_con_cor = np.corrcoef(feat_array[25:50, 0], feat_array[25:50, 1])
            relation_con_ene = np.corrcoef(feat_array[25:50, 0], feat_array[25:50, 2])
            relation_con_hom = np.corrcoef(feat_array[25:50, 0], feat_array[25:50, 3])
            relation_cor_ene = np.corrcoef(feat_array[25:50, 1], feat_array[25:50, 2])
            relation_cor_hom = np.corrcoef(feat_array[25:50, 1], feat_array[25:50, 3])
            relation_ene_hom = np.corrcoef(feat_array[25:50, 2], feat_array[25:50, 3])
            relation_list_2 = [(abs(relation_con_cor[0, 1]), 'Contrast&Correlation'), (abs(relation_con_ene[0, 1]), 'Contrast&Energy'), (abs(relation_con_hom[0, 1]), 'Contrast&Homogeneity'),
                             (abs(relation_cor_ene[0, 1]), 'Correlation&Energy'), (abs(relation_cor_hom[0, 1]), 'Correlation&Homogeneity'), (abs(relation_ene_hom[0, 1]), 'Energy&Homogeneity')]
            relation_list_2.sort()
        else:
            relation_con_cor = np.corrcoef(feat_array[50:, 0], feat_array[50:, 1])
            relation_con_ene = np.corrcoef(feat_array[50:, 0], feat_array[50:, 2])
            relation_con_hom = np.corrcoef(feat_array[50:, 0], feat_array[50:, 3])
            relation_cor_ene = np.corrcoef(feat_array[50:, 1], feat_array[50:, 2])
            relation_cor_hom = np.corrcoef(feat_array[50:, 1], feat_array[50:, 3])
            relation_ene_hom = np.corrcoef(feat_array[50:, 2], feat_array[50:, 3])
            relation_list_3 = [(abs(relation_con_cor[0, 1]), 'Contrast&Correlation'), (abs(relation_con_ene[0, 1]), 'Contrast&Energy'), (abs(relation_con_hom[0, 1]), 'Contrast&Homogeneity'),
                             (abs(relation_cor_ene[0, 1]), 'Correlation&Energy'), (abs(relation_cor_hom[0, 1]), 'Correlation&Homogeneity'), (abs(relation_ene_hom[0, 1]), 'Energy&Homogeneity')]
            relation_list_3.sort()

    greatest_relation_1 = relation_list_1[5]
    greatest_relation_2 = relation_list_2[5]
    greatest_relation_3 = relation_list_3[5]

    print("- Greatest Relations -")
    print("Asphalt: \n - Value: [", greatest_relation_1[0], "]  Relation: [", greatest_relation_1[1], "]")
    print("Danger: \n - Value: [", greatest_relation_2[0], "]  Relation: [", greatest_relation_2[1], "]")
    print("Grass: \n - Value: [", greatest_relation_3[0], "]  Relation: [", greatest_relation_3[1], "]")
    print()

    # Contraste e Correlação #
    plt.scatter(feat_array[:26, 0], feat_array[:26, 1], color='blue')
    plt.scatter(feat_array[26:51, 0], feat_array[26:51, 1], color='red')
    plt.scatter(feat_array[51:, 0], feat_array[51:, 1], color='green')
    plt.xlabel('Contrast')
    plt.ylabel('Correlation')
    plt.savefig("./CONvsCOR.png")
    plt.close()

    # Contraste e Energia #
    plt.scatter(feat_array[:26, 0], feat_array[:26, 2], color='darkblue')
    plt.scatter(feat_array[26:51, 0], feat_array[26:51, 2], color='crimson')
    plt.scatter(feat_array[51:, 0], feat_array[51:, 2], color='darkgreen')
    plt.xlabel('Contrast')
    plt.ylabel('Energy')
    plt.savefig("./CONvsENE.png")
    plt.close()

    # Correlação e Energia #
    plt.scatter(feat_array[:26, 1], feat_array[:26, 2], color='lightblue')
    plt.scatter(feat_array[26:51, 1], feat_array[26:51, 2], color='coral')
    plt.scatter(feat_array[51:, 1], feat_array[51:, 2], color='lightgreen')
    plt.xlabel('Correlation')
    plt.ylabel('Energy')
    plt.savefig("./CORvsENE.png")
    plt.close()


import sys

sys.path.append("..")

from loguru import logger
from concurrent.futures import ThreadPoolExecutor, as_completed
from network_information_source.config import settings, FindRedisTaskTopic
from network_information_source.nis_utils import task_tools
from network_information_source.scheduler import deploy
from task_find_outbound import tsa
import traceback
RedisTaskTopic = FindRedisTaskTopic

task_topic = f'{RedisTaskTopic.test_task.value}_{settings.REGION.name}' if settings.DEPLOY_VERIFY_DEBUG else f'{RedisTaskTopic.task.value}_{settings.REGION.name}'

options = {
    'topic': 'topic_c1_original_site_dis',
    'tag_topic': RedisTaskTopic.tag_task.value,
    'push_debug': settings.DEPLOY_FIND_PUSH_DEBUG,
    'up_status_debug': settings.DEPLOY_FIND_PUSH_DEBUG,
    'extract_method': 'extract_find_data',
}

if __name__ == '__main__':
    with ThreadPoolExecutor(max_workers=settings.DEPLOY_FIND_THREAD_COUNT) as t:
        try:
            tasks = task_tools.pull(topic=task_topic, count=settings.DEPLOY_FIND_SINGLE_THREAD_TASK_COUNT,
                                    is_json=True)
            logger.info(f'正常运行')
            all_task = []
            for task in tasks:
                # deploy(
                #     task_obj=tsa,
                #     task=task,
                #     crawl_type_method=[
                #         'website_find'
                #     ],
                #     **options
                # )
                all_task.append(t.submit(
                    deploy,
                    task_obj=tsa,
                    task=task,
                    crawl_type_method=[
                        'website_find'
                    ],
                    **options
                ))

            for future in as_completed(all_task):
                try:
                    print(future.result())
                except Exception as ex:
                    logger.error(f'{ex}')
        except Exception as ex:
            logger.error(f'{traceback.format_exc()}')


import threading
import zipfile

class AsyncZip(threading.Thread):

    def __init__(self, arch_ent, arch_sal):
        threading.Thread.__init__(self)
        self.arch_ent = arch_ent
        self.arch_sal = arch_sal

    def run(self):
        f = zipfile.ZipFile(self.arch_sal, 'w', zipfile.ZIP_DEFLATED)
        f.write(self.arch_ent)
        f.close()
        print('Terminó zip en segundo plano de: ', self.arch_ent)

seg_plano = AsyncZip('large_lorem.txt', 'miarchivo.zip')
seg_plano.start()
print('El programa principal continúa la ejecución en primer plano.')
seg_plano.join()  # esperar que termine la tarea en segundo plano
print('El programa principal esperó hasta que el segundo plano terminara.')


import os

import matplotlib.pyplot as plt

from r2d2.plotting.analysis_func import *
from r2d2.plotting.misc import *
from r2d2.plotting.text import *

# Gather Graphical Data #
data_path = None  # "/Volumes/R2D2_Drive/MyDrive/R2D2: Weekly Lab Data/IPRL"
dir_path = os.path.dirname(os.path.realpath(__file__))
data_directory = os.path.join(dir_path, "../../", "data") if data_path is None else data_path
PLOT_FOLDERPATH = os.path.join(dir_path, "../../", "plots")
if not os.path.exists(PLOT_FOLDERPATH):
    os.makedirs(PLOT_FOLDERPATH)

# Run Data Crawler #
data_crawler(data_directory, func_list=[analysis_func])

# Prepare Cumulative Values #
cumulative_dicts = [traj_progress_dict, scene_progress_dict]
for curr_dict in cumulative_dicts:
    all_dict_keys = list(curr_dict.keys())
    for key in all_dict_keys:
        curr_dict[key] = np.cumsum(curr_dict[key])
        curr_dict["R2D2"] += curr_dict[key]

# Make Figure #
fig = plt.figure()

# Plot Trajectory Progress #
plt.xticks(all_month_timestamps, labels=all_month_names)
for key in traj_progress_dict:
    lab_progress = traj_progress_dict[key]
    plt.plot(DAY_TIMESTAMPS, lab_progress, label=key)

plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.title("Trajectories Collected", fontweight="bold")
plt.savefig(PLOT_FOLDERPATH + "/trajectory_progress.png", bbox_inches="tight")

# Plot Scene Progress #
plt.clf()
plt.xticks(all_month_timestamps, labels=all_month_names)
for key in scene_progress_dict:
    lab_progress = scene_progress_dict[key]
    plt.plot(DAY_TIMESTAMPS, lab_progress, label=key)

plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.title("Scenes Collected", fontweight="bold")
plt.savefig(PLOT_FOLDERPATH + "/scene_progress.png", bbox_inches="tight")

# Plot Week Summary #
plt.clf()
weekly_threshold = 150000
lab_names = list(weekly_progress_dict.keys())
lab_quantities = [weekly_progress_dict[key] for key in lab_names]
plt.bar(lab_names, lab_quantities)
plt.axhline(weekly_threshold, color="black", ls="dotted")
plt.title("Week Summary: Samples Collected", fontweight="bold")
plt.savefig(PLOT_FOLDERPATH + "/week_progress.png", bbox_inches="tight")

# Plot User Progress #
plt.clf()
user_threshold = 500000
user_names = list(user_progress_dict.keys())
user_quantities = [user_progress_dict[key] for key in user_names]
tick_names = ["{0}K".format(100 * i) if i < 10 else "1M" for i in range(6)]
tick_values = [1e5 * i for i in range(len(tick_names))]
plt.xticks(tick_values, labels=tick_names)
plt.barh(user_names, user_quantities)
plt.axvline(user_threshold, color="blue", ls="dotted", label="Standard Requirement")
plt.axvline(user_threshold // 2, color="green", ls="dotted", label="Lab Lead Requirement")
plt.title("Per Person Progress", fontweight="bold")
plt.xlabel("Data Points Collected")
plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.savefig(PLOT_FOLDERPATH + "/personal_progress.png", bbox_inches="tight")

# Plot Task Distribution #
plt.clf()
task_names = list(task_distribution_dict.keys())
task_quantities = [task_distribution_dict[key] for key in task_names]
sections, texts = plt.pie(task_quantities)
plt.legend(sections, task_names, loc="center left", bbox_to_anchor=(1, 0.5))
plt.title("Task Distribution", fontweight="bold")
plt.savefig(PLOT_FOLDERPATH + "/task_distribution.png", bbox_inches="tight")

# Plot Trajectory Length Distribution #
plt.clf()
plt.hist(all_traj_lengths, bins=100)
plt.xlim([0, 1000])
plt.title("Horizon Distribution", fontweight="bold")
plt.savefig(PLOT_FOLDERPATH + "/horizon_distribution.png", bbox_inches="tight")

# Plot Camera Pose Distribution #
plt.clf()
ax_pos = fig.add_subplot(1, 2, 1, projection="3d")
ax_ang = fig.add_subplot(1, 2, 2, projection="3d")
pos_values, pos_density, ang_values, ang_density = estimate_pos_angle_density(all_camera_poses)
ax_pos.scatter(pos_values[0], pos_values[1], pos_values[2], c=pos_density, cmap="viridis", linewidth=0.5)
ax_ang.scatter(ang_values[0], ang_values[1], ang_values[2], c=ang_density, cmap="viridis", linewidth=0.5)
ax_pos.set_title("Camera Positions", fontweight="bold")
ax_ang.set_title("Camera Angles", fontweight="bold")
ax_pos.view_init(elev=20, azim=-23)
ax_ang.view_init(elev=14, azim=-143)
plt.savefig(PLOT_FOLDERPATH + "/camera_poses.png", bbox_inches="tight")
plt.show()


from datetime import datetime, date
from matplotlib import pyplot as plt
import numpy as np
from  bcb import sgs

def checking_dates_and_frequency():

    capital = input(f"Digite o capital investido R$: ")
    frequency = input("Digite a frequência do periodo: ").lower()
    start = input("Digite a data inicial maior do que 1995/01/01 no seguinte formato YYYY/mm/dd: ")
    end = input(f"Digite a data final menor do que {date.today()} no seguinte formato YYYY/mm/dd: ")
    start = datetime.strptime(start, '%Y/%m/%d').date()
    end = datetime.strptime(end, '%Y/%m/%d').date()

    return capital, start, end, frequency

def challenge(selic, capital, start_date, end_date, frequency):

    selic_challenge = selic[(selic.index.date >= start_date ) & (selic.index.date < end_date)]

    selic_acum_return = (selic_challenge + 1).cumprod() - 1
    selic_acum_return = selic_acum_return.resample(f"{frequency.upper()}").last()

    selic_acum_return["capital"] = (selic_acum_return["juros"] * float(capital)) + float(capital)
    selic_acum_return["earned"] = selic_acum_return["capital"] - selic_acum_return["capital"].iloc[0]
    selic_acum_return.fillna(0,inplace=True)

    print(selic_acum_return)

def question(selic):

    initial_date = date(2000,1,1)
    final_date = date(2022,3,31)
    selic_question = selic[(selic.index.date >= initial_date ) & (selic.index.date < final_date)]

    windows = ((1 + selic_question).rolling(window=500).apply(np.prod) - 1)
    windows.plot()
    windows = windows.reset_index()
    windows.columns = ["end_date","returns"]
    windows.insert(0,"init_date",0)
    windows["init_date"] = windows["end_date"].shift(499)
    windows.dropna(inplace=True)
    plt.show()

    id_row = windows["returns"].idxmax()
    row = windows.loc[id_row]
    initial_return_date = row[0]
    final_return_date  = row[1]
    max_return = row[2]
    print(f'''O intervalo de datas de maior retorno foi:
          {initial_return_date} até {final_return_date} com retorno de {round(max_return,3)*100} %''')
    selic_range = selic_question[(selic_question.index >= initial_return_date) & (selic_question.index <= final_return_date)]
    acum_return_range = (selic_range + 1).cumprod() - 1
    acum_return_range.plot()
    plt.show()

capital, start_date, end_date, frequency = checking_dates_and_frequency()

date_min = date(1995,1,1)
selic = sgs.get({"juros":11}, start=date_min)/100

challenge(selic, capital, start_date, end_date, frequency)
question(selic)


from django.conf import settings
from django.core import validators
from django.db import models

class Tag(models.Model):
    name = models.CharField(
        'Название',
        unique=True,
        max_length=settings.NAME_MAX_LENGTH,
        help_text='Введите, пожалуйста, название',
    )
    color = models.CharField(
        'Цвет',
        unique=True,
        max_length=settings.COLOR_MAX_LENGTH,
        help_text='Введите цвет в формате RGB (#rrggbb)',
        validators=[
            validators.RegexValidator(
                r'^#[a-fA-F0-9]{6}$',
                'Используйте RGB-формат для указания цвета (#AABBCC)',
            )
        ],
    )
    slug = models.SlugField(
        'Slug',
        unique=True,
        max_length=settings.SLUG_MAX_LENGTH,
        help_text='Введите slug',
    )

    class Meta:
        ordering = ('name',)
        verbose_name = 'Тег'
        verbose_name_plural = 'Теги'

    def __str__(self) -> str:
        return f'{self.name} ({self.slug}).'


from flask import Flask
from flask import request
import datetime
import telebot
from telebot import types
import os
from database import db, cursor
import threading
from time import sleep

TOKEN = 'This is place for your Token'

APP_URL = f'https://name_your_app_on_heroku.herokuapp.com/{TOKEN}'

bot = telebot.TeleBot(TOKEN)

delay = 50

server = Flask(__name__)

@bot.message_handler(commands=['start'])
def start(message):
    cursor.execute("CREATE TABLE IF NOT EXISTS duty_days (id INT AUTO_INCREMENT PRIMARY KEY, date DATE, date_order SMALLINT)")
    cursor.execute("CREATE TABLE IF NOT EXISTS rescuers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), name_order SMALLINT)")
    cursor.execute("CREATE TABLE IF NOT EXISTS archive (id INT AUTO_INCREMENT PRIMARY KEY, date DATE, name VARCHAR(255))")
    bot.send_photo(message.chat.id, photo='https://i.postimg.cc/N0htGPMC/channels4-banner.jpg', caption="БУДЬТЕ ВНИМАТЕЛЬНЫ: СЕГОДНЯШНЯЯ ДАТА ДОЛЖНА СОВПАДАТЬ С ВАШЕЙ СМЕНОЙ!!!"
                                      "\nДОЖДИТЕСЬ СВОЕЙ СМЕНЫ и введите 'Начать' для начала работы телеграмм бота:")

# Эта функция нужна чтобы обдурить бесплатный ClearDb, который разрывает соединение с базой данных, если в течении 60 секунд нет запросов))
def send_reminder():
    while True:
        cursor.execute("SELECT COUNT(id) FROM rescuers")
        sleep(delay)

treading = threading.Thread(target=send_reminder)
treading.start()

def main_menu(message):
    global keyboard_main_menu
    keyboard_main_menu = types.InlineKeyboardMarkup()
    button_Add_Duty = types.InlineKeyboardButton(text='Добавить в список дежурных', callback_data='button_Add_Duty')
    keyboard_main_menu.add(button_Add_Duty)
    button_Minus_Duty = types.InlineKeyboardButton(text='Удалить из списка дежурных', callback_data='button_Minus_Duty')
    keyboard_main_menu.add(button_Minus_Duty)
    button_Check_Duty = types.InlineKeyboardButton(text='Узнать когда дежуришь', callback_data='button_Check_Duty')
    keyboard_main_menu.add(button_Check_Duty)
    button_Now_Duty = types.InlineKeyboardButton(text='Сегодня дежурит:', callback_data='button_Now_Duty')
    keyboard_main_menu.add(button_Now_Duty)
    bot.send_message(message.chat.id, 'ВЫБЕРИТЕ ДЕЙСТВИЕ \nИЗ ПРЕДЛОЖЕННЫХ :', reply_markup=keyboard_main_menu)

@bot.message_handler(func=lambda call: True)
def get_first_message(message):
    start_cmd = 'начать'
    if message.text.lower() == start_cmd:
        cursor.execute('SELECT COUNT(*) FROM duty_days')
        result = cursor.fetchone()
        len_duty_days = result[0]
        if len_duty_days >= 1:
            main_menu(message)
        else:
            start = datetime.date.today()
            end = start + datetime.timedelta(days=365)
            interval = datetime.timedelta(days=4)

            while start <= end:
                query = "INSERT INTO duty_days (date) VALUES (%s)"
                value = start
                cursor.execute(query, (value,))
                cursor.execute("SET @num=0")
                cursor.execute("UPDATE duty_days SET date_order =@num:= (@num +1)")
                db.commit()
                start += interval
            main_menu(message)

    else:
        bot.send_message(message.from_user.id, 'СЛЕДУЙТЕ УКАЗАНИЯМ ВЫШЕ')

@bot.callback_query_handler(func=lambda call: True)
def choose_operation(call):
    if call.data == 'button_main_menu':
        main_menu(call.message)

    elif call.data == 'button_Add_Duty':
        bot.send_message(call.message.chat.id, 'Введите имя человека, которого хотите добавить в список дежурных:')
        bot.register_next_step_handler(call.message, add_name_duty)

    elif call.data == 'button_Minus_Duty':
        cursor.execute("SELECT COUNT(name) FROM rescuers")
        result = cursor.fetchone()
        len_rescuers = result[0]
        if len_rescuers >= 1:
            bot.send_message(call.message.chat.id, 'Введите имя человека, которого хотите удалить из списка дежурных:')
            bot.register_next_step_handler(call.message, delete_duty)
        else:
            no_names(call)

    elif call.data == 'button_confirm_delete':
        query = "DELETE FROM rescuers WHERE name = %s"
        value = name_delete
        cursor.execute(query, (value,))
        cursor.execute("SET @num=0")
        cursor.execute("UPDATE rescuers SET name_order =@num:= (@num +1)")
        db.commit()

        keyboard_main_menu2 = types.InlineKeyboardMarkup()
        button_Add_Duty = types.InlineKeyboardButton(text='Добавить в список дежурных',
                                                             callback_data='button_Add_Duty')
        keyboard_main_menu2.add(button_Add_Duty)
        button_Minus_Duty = types.InlineKeyboardButton(text='Удалить из списка дежурных',
                                                               callback_data='button_Minus_Duty')
        keyboard_main_menu2.add(button_Minus_Duty)
        button_Check_Duty = types.InlineKeyboardButton(text='Узнать когда дежуришь',
                                                               callback_data='button_Check_Duty')
        keyboard_main_menu2.add(button_Check_Duty)
        button_Now_Duty = types.InlineKeyboardButton(text='Сегодня дежурит:', callback_data='button_Now_Duty')
        keyboard_main_menu2.add(button_Now_Duty)

        bot.send_message(call.message.chat.id, name_delete + ' удален из списка дежурных!')
        main_menu(call.message)

    elif call.data == 'button_cancel_delete':
        main_menu(call.message)

    elif call.data == 'button_Check_Duty':
        cursor.execute("SELECT COUNT(name) FROM rescuers")
        result = cursor.fetchone()
        len_rescuers = result[0]
        if len_rescuers >= 1:
            bot.send_message(call.message.chat.id, 'Введите имя человека чтобы узнать дату его дежурства: ')
            bot.register_next_step_handler(call.message, check_duty_date)
        else:
            no_names(call)

    elif call.data == 'button_Now_Duty':
        cursor.execute("SELECT COUNT(name) FROM rescuers")
        result = cursor.fetchone()
        len_rescuers = result[0]
        if len_rescuers >= 1:
            cursor.execute("SELECT COUNT(date) FROM duty_days WHERE date=CURDATE()")
            result = cursor.fetchone()
            result_value = result[0]
            if result_value >= 1:
                cursor.execute('CREATE TABLE IF NOT EXISTS counter (id INT AUTO_INCREMENT PRIMARY KEY, num SMALLINT)')
                query = 'INSERT INTO counter (num) VALUES (%s)'
                value = 1
                cursor.execute(query, (value,))
                db.commit()
                confirm_duty(call)

            else:
                not_today_duty_day(call)

        else:
            no_names(call)

    elif call.data == 'button_cancel_today_duty':
        query = 'INSERT INTO counter (num) VALUES (%s)'
        value = 1
        cursor.execute(query, (value,))
        db.commit()
        now_duty(call)

    elif call.data == 'button_confirm_today_duty':
        change_queue()
        delete_duty_day()
        cursor.execute("DELETE FROM counter WHERE id>0")
        db.commit()

        keyboard_go_to_main_menu = types.InlineKeyboardMarkup()
        button_go_to_main_menu = types.InlineKeyboardButton(text='Перейти в главное меню',
                                                            callback_data='button_main_menu')
        keyboard_go_to_main_menu.add(button_go_to_main_menu)

        bot.send_photo(call.message.chat.id, photo='https://i.postimg.cc/N0htGPMC/channels4-banner.jpg',
                        caption=f'{result_duty_name} - умывальников начальник \nи мочалок командир!',
                        reply_markup=keyboard_go_to_main_menu)

def confirm_duty(call):
    global keyboard_confirm_today_duty
    global result_duty_name

    cursor.execute(" SELECT COUNT(id) FROM counter")
    result_counter = cursor.fetchone()
    result_value = result_counter[0]

    query = "SELECT name FROM rescuers WHERE name_order= %s"
    cursor.execute(query, (result_value,))
    result = cursor.fetchone()
    if result is None:
        bot.send_message(call.message.chat.id, 'Еееййй... Очередь тю-тю, закончилась!!!')

    else:
        result_duty_name = result[0]
        bot.send_message(call.message.chat.id, f"Сегодня дежурит: {result_duty_name}")

        keyboard_confirm_today_duty = types.InlineKeyboardMarkup()
        button_confirm_today_duty = types.InlineKeyboardButton(text='Подтвердить',
                                                               callback_data='button_confirm_today_duty')
        button_cancel_today_duty = types.InlineKeyboardButton(text='Не дежурит',
                                                              callback_data='button_cancel_today_duty')
        keyboard_confirm_today_duty.add(button_confirm_today_duty, button_cancel_today_duty)
        bot.send_message(call.message.chat.id, f'Нажмите "Подтвердить", '
                                               f'\nесли {result_duty_name} дежурит сегодня \nИли "Не дежурит" - если нет!'
                                               f'\nБУДЬТЕ ВНИМАТЕЛЬНЫ : \nИзменить дежурного сегодня \nбудет уже НЕЛЬЗЯ!!!',
                         reply_markup=keyboard_confirm_today_duty)

def now_duty(call):
    cursor.execute("SELECT COUNT(name) FROM rescuers")
    result = cursor.fetchone()
    len_rescuers = result[0]
    if len_rescuers >= 1:
        cursor.execute("SELECT COUNT(date) FROM duty_days WHERE date=CURDATE()")
        result = cursor.fetchone()
        result_value = result[0]
        if result_value >= 1:
            confirm_duty(call)

        else:
            not_today_duty_day(call)

    else:
        no_names(call)

def not_today_duty_day(call):
    global keyboard_not_today_duty_day
    keyboard_not_today_duty_day = types.InlineKeyboardMarkup()
    button_go_to_main_menu3 = types.InlineKeyboardButton(text='Перейти в главное меню',
                                                         callback_data='button_main_menu')
    keyboard_not_today_duty_day.add(button_go_to_main_menu3)
    bot.send_message(call.message.chat.id, 'Дождитесь следующей смены!!! '
                                           '\nНазначить нового дежурного можно будет только в день смены!!!', reply_markup=keyboard_not_today_duty_day)

def change_queue():
    query_replica = "INSERT INTO rescuers(name) SELECT name FROM rescuers WHERE name = %s"
    value = result_duty_name
    cursor.execute(query_replica, (value,))

    query_insert = "INSERT INTO archive(name) SELECT name FROM rescuers WHERE name= %s LIMIT 1"
    cursor.execute(query_insert, (value,))

    query_delete = "DELETE FROM rescuers WHERE name= %s LIMIT 1"
    cursor.execute(query_delete, (value,))
    cursor.execute("SET @num=0")
    cursor.execute("UPDATE rescuers SET name_order =@num:= (@num +1)")
    db.commit()

def delete_duty_day():
    cursor.execute("SELECT date FROM duty_days WHERE date_order= 1")
    result = cursor.fetchone()
    value = result[0]
    query = "UPDATE archive SET date= %s WHERE name= %s"
    cursor.execute(query, (value, result_duty_name))

    cursor.execute("DELETE FROM duty_days WHERE date_order= 1")
    cursor.execute("SET @num=0")
    cursor.execute("UPDATE duty_days SET date_order =@num:= (@num +1)")
    db.commit()

def no_names(call):
    global keyboard_no_names
    keyboard_no_names = types.InlineKeyboardMarkup()
    button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
    button_add_duty = types.InlineKeyboardButton(text='Добавить в список', callback_data='button_Add_Duty')
    keyboard_no_names.add(button_main_menu, button_add_duty)
    bot.send_message(call.message.chat.id, 'Список дежурных пока еще пуст!!! \nДобавьте людей в список дежурных!',
                     reply_markup=keyboard_no_names)

def add_name_duty(message):
    global name1
    name1 = message.text
    query = "SELECT COUNT(name) FROM rescuers where name = %s"
    value = (name1)
    cursor.execute(query, (value,))
    result = cursor.fetchone()
    result_value = result[0]

    if result_value >=1:

        keyboard_mistake_add = types.InlineKeyboardMarkup()
        button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
        button_one_more = types.InlineKeyboardButton(text='Попробовать еще', callback_data='button_Add_Duty')
        keyboard_mistake_add.add(button_main_menu, button_one_more)
        bot.send_message(message.chat.id, f"Дежурный {name1} уже есть в базе!!! \nНельзя дважды добавлять одно и то же имя!",
                                reply_markup=keyboard_mistake_add)
    else:
        bot.send_message(message.chat.id, 'Повторно введите имя человека:')
        bot.register_next_step_handler(message, add_finish)

def add_finish(message):
    global name2
    name2 = message.text

    if name1 == name2:
        query = "INSERT INTO rescuers (name) VALUES (%s)"
        value = name2
        cursor.execute(query, (value,))
        cursor.execute("SET @num=0")
        cursor.execute("UPDATE rescuers SET name_order =@num:= (@num +1)")
        db.commit()

        keyboard_correct_add = types.InlineKeyboardMarkup()
        button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
        button_one_more = types.InlineKeyboardButton(text='Добавить еще', callback_data='button_Add_Duty')
        keyboard_correct_add.add(button_main_menu, button_one_more)
        bot.send_message(message.chat.id, name2 + ' добавлен в список дежурных!')
        main_menu(message)

    else:
        keyboard_mistake_add = types.InlineKeyboardMarkup()
        button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
        button_one_more = types.InlineKeyboardButton(text=' Ввести имя ', callback_data='button_Add_Duty')
        keyboard_mistake_add.add(button_main_menu, button_one_more)
        bot.send_message(message.chat.id, 'ИМЕНА НЕ СОВПАДАЮТ!!!', reply_markup=keyboard_mistake_add)

def delete_duty(message):
    global name_delete
    name_delete = message.text
    query = "SELECT COUNT(name) FROM rescuers WHERE name= %s"
    value = name_delete
    cursor.execute(query, (value,))
    result = cursor.fetchone()
    result_value = result[0]

    if result_value >= 1:
        keyboard_confirm_delete = types.InlineKeyboardMarkup()
        button_confirm_delete = types.InlineKeyboardButton(text='Удалить', callback_data='button_confirm_delete')
        button_cancel_delete = types.InlineKeyboardButton(text='Отмена', callback_data='button_cancel_delete')
        keyboard_confirm_delete.add(button_confirm_delete, button_cancel_delete)
        bot.send_message(message.chat.id, 'Вы хотите удалить ' + name_delete + ' из списка дежурных?',
                             reply_markup=keyboard_confirm_delete)

    else:
        keyboard_mistake_delete = types.InlineKeyboardMarkup()
        button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
        button_one_more = types.InlineKeyboardButton(text='Попробовать еще', callback_data='button_Minus_Duty')
        keyboard_mistake_delete.add(button_main_menu, button_one_more)
        bot.send_message(message.chat.id, name_delete + ' нет в списке дежурных! \nПопробуйте ввести имя еще раз!',
                                reply_markup=keyboard_mistake_delete)

def check_duty_date(message):
    global name_check
    name_check = message.text
    query = "SELECT COUNT(name) FROM rescuers WHERE name = %s"
    value = name_check
    cursor.execute(query, (value,))
    result = cursor.fetchone()
    result_value = result[0]

    if result_value >= 1:
        query1 = "SELECT name_order FROM rescuers WHERE name = %s LIMIT 0, 1"
        value1 = name_check
        cursor.execute(query1, (value1,))
        result = cursor.fetchone()
        result_value1 = result[0]

        query2 = "SELECT date FROM duty_days WHERE date_order = %s"
        value2 = result_value1
        cursor.execute(query2, (value2,))
        result = cursor.fetchone()
        result_value2 = result[0]

        bot.send_message(message.chat.id, f"{name_check}  дежурит:  {result_value2}")
        main_menu(message)

    else:
        keyboard_mistake_check_duty_date = types.InlineKeyboardMarkup()
        button_main_menu = types.InlineKeyboardButton(text='В главное меню', callback_data='button_main_menu')
        button_one_more = types.InlineKeyboardButton(text='Попробовать еще', callback_data='button_Check_Duty')
        keyboard_mistake_check_duty_date.add(button_main_menu, button_one_more)
        bot.send_message(message.chat.id, name_check + ' нет в списке дежурных! \nПопробуйте ввести имя еще раз!',
                         reply_markup=keyboard_mistake_check_duty_date)

@server.route('/' + TOKEN, methods=['POST'])
def get_message():
    json_string = request.get_data().decode('utf-8')
    update = telebot.types.Update.de_json(json_string)
    bot.process_new_updates([update])
    return '!', 200

@server.route("/")
def webhook():
    bot.remove_webhook()
    bot.set_webhook(url= APP_URL)
    return "!", 200

if __name__ == '__main__':
    server.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))


from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
  path(route='', view=views.post_list, name='post_list'),
  path(route='<slug:post>/', view=views.post_detail, name='post_detail'),
  path(route='comment/reply/', view=views.reply_page, name="reply"),
  path(route='tag/<slug:tag_slug>/', view=views.post_list, name="post_tag"),
]