from __future__ import annotations

from xfintech.data.common.paginate import Paginate
from xfintech.fabric.column.info import ColumnInfo
from xfintech.fabric.column.kind import ColumnKind
from xfintech.fabric.table.info import TableInfo

PROVIDER = "tushare"
SOURCE_NAME = "income_vip"
URL = "https://tushare.pro/document/2?doc_id=33"
ARGS = {
    "ts_code": {
        "type": ColumnKind.STRING,
        "required": "Y",
        "desc": "股票代码",
    },
    "start_date": {
        "type": ColumnKind.STRING,
        "required": "N",
        "desc": "开始日期(YYYYMMDD)",
    },
    "end_date": {
        "type": ColumnKind.STRING,
        "required": "N",
        "desc": "结束日期(YYYYMMDD)",
    },
    "year": {
        "type": ColumnKind.STRING,
        "required": "N",
        "desc": "年份(YYYY)",
    },
    "period": {
        "type": ColumnKind.STRING,
        "required": "N",
        "desc": "报告期(每个季度最后一天的日期)",
    },
}

# Exported constants
NAME = "companyprofit"
KEY = "/tushare/companyprofit"
PAGINATE = Paginate(
    pagesize=1000,
    pagelimit=1000,
)
SOURCE = TableInfo(
    desc="A股利润表数据（tushare格式）",
    meta={
        "provider": PROVIDER,
        "source": SOURCE_NAME,
        "url": URL,
        "args": ARGS,
        "type": "partitioned",
        "scale": "crosssection",
    },
    columns=[
        ColumnInfo(name="ts_code", kind=ColumnKind.STRING, desc="TS代码"),
        ColumnInfo(name="ann_date", kind=ColumnKind.STRING, desc="公告日期"),
        ColumnInfo(name="f_ann_date", kind=ColumnKind.STRING, desc="实际公告日期"),
        ColumnInfo(name="end_date", kind=ColumnKind.STRING, desc="报告期"),
        ColumnInfo(name="report_type", kind=ColumnKind.STRING, desc="报告类型 见底部表"),
        ColumnInfo(name="comp_type", kind=ColumnKind.STRING, desc="公司类型(1一般工商业2银行3保险4证券)"),
        ColumnInfo(name="end_type", kind=ColumnKind.STRING, desc="报告期类型"),
        ColumnInfo(name="update_flag", kind=ColumnKind.STRING, desc="更新标识"),
        ColumnInfo(name="basic_eps", kind=ColumnKind.FLOAT, desc="基本每股收益"),
        ColumnInfo(name="diluted_eps", kind=ColumnKind.FLOAT, desc="稀释每股收益"),
        ColumnInfo(name="total_revenue", kind=ColumnKind.FLOAT, desc="营业总收入"),
        ColumnInfo(name="revenue", kind=ColumnKind.FLOAT, desc="营业收入"),
        ColumnInfo(name="int_income", kind=ColumnKind.FLOAT, desc="利息收入"),
        ColumnInfo(name="prem_earned", kind=ColumnKind.FLOAT, desc="已赚保费"),
        ColumnInfo(name="comm_income", kind=ColumnKind.FLOAT, desc="手续费及佣金收入"),
        ColumnInfo(name="n_commis_income", kind=ColumnKind.FLOAT, desc="手续费及佣金净收入"),
        ColumnInfo(name="n_oth_income", kind=ColumnKind.FLOAT, desc="其他经营净收益"),
        ColumnInfo(name="n_oth_b_income", kind=ColumnKind.FLOAT, desc="加:其他业务净收益"),
        ColumnInfo(name="prem_income", kind=ColumnKind.FLOAT, desc="保险业务收入"),
        ColumnInfo(name="out_prem", kind=ColumnKind.FLOAT, desc="减:分出保费"),
        ColumnInfo(name="une_prem_reser", kind=ColumnKind.FLOAT, desc="提取未到期责任准备金"),
        ColumnInfo(name="reins_income", kind=ColumnKind.FLOAT, desc="其中:分保费收入"),
        ColumnInfo(name="n_sec_tb_income", kind=ColumnKind.FLOAT, desc="代理买卖证券业务净收入"),
        ColumnInfo(name="n_sec_uw_income", kind=ColumnKind.FLOAT, desc="证券承销业务净收入"),
        ColumnInfo(name="n_asset_mg_income", kind=ColumnKind.FLOAT, desc="受托客户资产管理业务净收入"),
        ColumnInfo(name="oth_b_income", kind=ColumnKind.FLOAT, desc="其他业务收入"),
        ColumnInfo(name="fv_value_chg_gain", kind=ColumnKind.FLOAT, desc="加:公允价值变动净收益"),
        ColumnInfo(name="invest_income", kind=ColumnKind.FLOAT, desc="加:投资净收益"),
        ColumnInfo(name="ass_invest_income", kind=ColumnKind.FLOAT, desc="其中:对联营企业和合营企业的投资收益"),
        ColumnInfo(name="forex_gain", kind=ColumnKind.FLOAT, desc="加:汇兑净收益"),
        ColumnInfo(name="total_cogs", kind=ColumnKind.FLOAT, desc="营业总成本"),
        ColumnInfo(name="oper_cost", kind=ColumnKind.FLOAT, desc="减:营业成本"),
        ColumnInfo(name="int_exp", kind=ColumnKind.FLOAT, desc="减:利息支出"),
        ColumnInfo(name="comm_exp", kind=ColumnKind.FLOAT, desc="减:手续费及佣金支出"),
        ColumnInfo(name="biz_tax_surchg", kind=ColumnKind.FLOAT, desc="减:营业税金及附加"),
        ColumnInfo(name="sell_exp", kind=ColumnKind.FLOAT, desc="减:销售费用"),
        ColumnInfo(name="admin_exp", kind=ColumnKind.FLOAT, desc="减:管理费用"),
        ColumnInfo(name="fin_exp", kind=ColumnKind.FLOAT, desc="减:财务费用"),
        ColumnInfo(name="assets_impair_loss", kind=ColumnKind.FLOAT, desc="减:资产减值损失"),
        ColumnInfo(name="prem_refund", kind=ColumnKind.FLOAT, desc="退保金"),
        ColumnInfo(name="compens_payout", kind=ColumnKind.FLOAT, desc="赔付总支出"),
        ColumnInfo(name="reser_insur_liab", kind=ColumnKind.FLOAT, desc="提取保险责任准备金"),
        ColumnInfo(name="div_payt", kind=ColumnKind.FLOAT, desc="保户红利支出"),
        ColumnInfo(name="reins_exp", kind=ColumnKind.FLOAT, desc="分保费用"),
        ColumnInfo(name="oper_exp", kind=ColumnKind.FLOAT, desc="营业支出"),
        ColumnInfo(name="compens_payout_refu", kind=ColumnKind.FLOAT, desc="减:摊回赔付支出"),
        ColumnInfo(name="insur_reser_refu", kind=ColumnKind.FLOAT, desc="减:摊回保险责任准备金"),
        ColumnInfo(name="reins_cost_refund", kind=ColumnKind.FLOAT, desc="减:摊回分保费用"),
        ColumnInfo(name="other_bus_cost", kind=ColumnKind.FLOAT, desc="其他业务成本"),
        ColumnInfo(name="operate_profit", kind=ColumnKind.FLOAT, desc="营业利润"),
        ColumnInfo(name="non_oper_income", kind=ColumnKind.FLOAT, desc="加:营业外收入"),
        ColumnInfo(name="non_oper_exp", kind=ColumnKind.FLOAT, desc="减:营业外支出"),
        ColumnInfo(name="nca_disploss", kind=ColumnKind.FLOAT, desc="其中:减:非流动资产处置净损失"),
        ColumnInfo(name="total_profit", kind=ColumnKind.FLOAT, desc="利润总额"),
        ColumnInfo(name="income_tax", kind=ColumnKind.FLOAT, desc="所得税费用"),
        ColumnInfo(name="n_income", kind=ColumnKind.FLOAT, desc="净利润(含少数股东损益)"),
        ColumnInfo(name="n_income_attr_p", kind=ColumnKind.FLOAT, desc="净利润(不含少数股东损益)"),
        ColumnInfo(name="minority_gain", kind=ColumnKind.FLOAT, desc="少数股东损益"),
        ColumnInfo(name="oth_compr_income", kind=ColumnKind.FLOAT, desc="其他综合收益"),
        ColumnInfo(name="t_compr_income", kind=ColumnKind.FLOAT, desc="综合收益总额"),
        ColumnInfo(name="compr_inc_attr_p", kind=ColumnKind.FLOAT, desc="归属于母公司(或股东)的综合收益总额"),
        ColumnInfo(name="compr_inc_attr_m_s", kind=ColumnKind.FLOAT, desc="归属于少数股东的综合收益总额"),
        ColumnInfo(name="ebit", kind=ColumnKind.FLOAT, desc="息税前利润"),
        ColumnInfo(name="ebitda", kind=ColumnKind.FLOAT, desc="息税折旧摊销前利润"),
        ColumnInfo(name="insurance_exp", kind=ColumnKind.FLOAT, desc="保险业务支出"),
        ColumnInfo(name="undist_profit", kind=ColumnKind.FLOAT, desc="年初未分配利润"),
        ColumnInfo(name="distable_profit", kind=ColumnKind.FLOAT, desc="可分配利润"),
        ColumnInfo(name="rd_exp", kind=ColumnKind.FLOAT, desc="研发费用"),
        ColumnInfo(name="fin_exp_int_exp", kind=ColumnKind.FLOAT, desc="财务费用:利息费用"),
        ColumnInfo(name="fin_exp_int_inc", kind=ColumnKind.FLOAT, desc="财务费用:利息收入"),
        ColumnInfo(name="transfer_surplus_rese", kind=ColumnKind.FLOAT, desc="盈余公积转入"),
        ColumnInfo(name="transfer_housing_imprest", kind=ColumnKind.FLOAT, desc="住房周转金转入"),
        ColumnInfo(name="transfer_oth", kind=ColumnKind.FLOAT, desc="其他转入"),
        ColumnInfo(name="adj_lossgain", kind=ColumnKind.FLOAT, desc="调整以前年度损益"),
        ColumnInfo(name="withdra_legal_surplus", kind=ColumnKind.FLOAT, desc="提取法定盈余公积"),
        ColumnInfo(name="withdra_legal_pubfund", kind=ColumnKind.FLOAT, desc="提取法定公益金"),
        ColumnInfo(name="withdra_biz_devfund", kind=ColumnKind.FLOAT, desc="提取企业发展基金"),
        ColumnInfo(name="withdra_rese_fund", kind=ColumnKind.FLOAT, desc="提取储备基金"),
        ColumnInfo(name="withdra_oth_ersu", kind=ColumnKind.FLOAT, desc="提取任意盈余公积金"),
        ColumnInfo(name="workers_welfare", kind=ColumnKind.FLOAT, desc="职工奖金福利"),
        ColumnInfo(name="distr_profit_shrhder", kind=ColumnKind.FLOAT, desc="可供股东分配的利润"),
        ColumnInfo(name="prfshare_payable_dvd", kind=ColumnKind.FLOAT, desc="应付优先股股利"),
        ColumnInfo(name="comshare_payable_dvd", kind=ColumnKind.FLOAT, desc="应付普通股股利"),
        ColumnInfo(name="capit_comstock_div", kind=ColumnKind.FLOAT, desc="转作股本的普通股股利"),
        ColumnInfo(name="net_after_nr_lp_correct", kind=ColumnKind.FLOAT, desc="扣除非经常性损益后的净利润（更正前）"),
        ColumnInfo(name="credit_impa_loss", kind=ColumnKind.FLOAT, desc="信用减值损失"),
        ColumnInfo(name="net_expo_hedging_benefits", kind=ColumnKind.FLOAT, desc="净敞口套期收益"),
        ColumnInfo(name="oth_impair_loss_assets", kind=ColumnKind.FLOAT, desc="其他资产减值损失"),
        ColumnInfo(name="total_opcost", kind=ColumnKind.FLOAT, desc="营业总成本（二）"),
        ColumnInfo(name="amodcost_fin_assets", kind=ColumnKind.FLOAT, desc="以摊余成本计量的金融资产终止确认收益"),
        ColumnInfo(name="oth_income", kind=ColumnKind.FLOAT, desc="其他收益"),
        ColumnInfo(name="asset_disp_income", kind=ColumnKind.FLOAT, desc="资产处置收益"),
        ColumnInfo(name="continued_net_profit", kind=ColumnKind.FLOAT, desc="持续经营净利润"),
        ColumnInfo(name="end_net_profit", kind=ColumnKind.FLOAT, desc="终止经营净利润"),
    ],
)
TARGET = TableInfo(
    desc="A股利润表数据（xfintech格式）",
    meta={
        "key": KEY,
        "name": NAME,
        "type": "partitioned",
        "scale": "crosssection",
    },
    columns=[
        ColumnInfo(name="code", kind=ColumnKind.STRING, desc="TS代码"),
        ColumnInfo(name="date", kind=ColumnKind.STRING, desc="报告期日期(YYYY-MM-DD)"),
        ColumnInfo(name="datecode", kind=ColumnKind.STRING, desc="报告期日期码(YYYYMMDD)"),
        ColumnInfo(name="ann_date", kind=ColumnKind.STRING, desc="公告日期"),
        ColumnInfo(name="f_ann_date", kind=ColumnKind.STRING, desc="实际公告日期"),
        ColumnInfo(name="end_date", kind=ColumnKind.STRING, desc="报告期"),
        ColumnInfo(name="report_type", kind=ColumnKind.STRING, desc="报告类型 见底部表"),
        ColumnInfo(name="comp_type", kind=ColumnKind.STRING, desc="公司类型(1一般工商业2银行3保险4证券)"),
        ColumnInfo(name="end_type", kind=ColumnKind.STRING, desc="报告期类型"),
        ColumnInfo(name="update_flag", kind=ColumnKind.STRING, desc="更新标识"),
        ColumnInfo(name="basic_eps", kind=ColumnKind.FLOAT, desc="基本每股收益"),
        ColumnInfo(name="diluted_eps", kind=ColumnKind.FLOAT, desc="稀释每股收益"),
        ColumnInfo(name="total_revenue", kind=ColumnKind.FLOAT, desc="营业总收入"),
        ColumnInfo(name="revenue", kind=ColumnKind.FLOAT, desc="营业收入"),
        ColumnInfo(name="int_income", kind=ColumnKind.FLOAT, desc="利息收入"),
        ColumnInfo(name="prem_earned", kind=ColumnKind.FLOAT, desc="已赚保费"),
        ColumnInfo(name="comm_income", kind=ColumnKind.FLOAT, desc="手续费及佣金收入"),
        ColumnInfo(name="n_commis_income", kind=ColumnKind.FLOAT, desc="手续费及佣金净收入"),
        ColumnInfo(name="n_oth_income", kind=ColumnKind.FLOAT, desc="其他经营净收益"),
        ColumnInfo(name="n_oth_b_income", kind=ColumnKind.FLOAT, desc="加:其他业务净收益"),
        ColumnInfo(name="prem_income", kind=ColumnKind.FLOAT, desc="保险业务收入"),
        ColumnInfo(name="out_prem", kind=ColumnKind.FLOAT, desc="减:分出保费"),
        ColumnInfo(name="une_prem_reser", kind=ColumnKind.FLOAT, desc="提取未到期责任准备金"),
        ColumnInfo(name="reins_income", kind=ColumnKind.FLOAT, desc="其中:分保费收入"),
        ColumnInfo(name="n_sec_tb_income", kind=ColumnKind.FLOAT, desc="代理买卖证券业务净收入"),
        ColumnInfo(name="n_sec_uw_income", kind=ColumnKind.FLOAT, desc="证券承销业务净收入"),
        ColumnInfo(name="n_asset_mg_income", kind=ColumnKind.FLOAT, desc="受托客户资产管理业务净收入"),
        ColumnInfo(name="oth_b_income", kind=ColumnKind.FLOAT, desc="其他业务收入"),
        ColumnInfo(name="fv_value_chg_gain", kind=ColumnKind.FLOAT, desc="加:公允价值变动净收益"),
        ColumnInfo(name="invest_income", kind=ColumnKind.FLOAT, desc="加:投资净收益"),
        ColumnInfo(name="ass_invest_income", kind=ColumnKind.FLOAT, desc="其中:对联营企业和合营企业的投资收益"),
        ColumnInfo(name="forex_gain", kind=ColumnKind.FLOAT, desc="加:汇兑净收益"),
        ColumnInfo(name="total_cogs", kind=ColumnKind.FLOAT, desc="营业总成本"),
        ColumnInfo(name="oper_cost", kind=ColumnKind.FLOAT, desc="减:营业成本"),
        ColumnInfo(name="int_exp", kind=ColumnKind.FLOAT, desc="减:利息支出"),
        ColumnInfo(name="comm_exp", kind=ColumnKind.FLOAT, desc="减:手续费及佣金支出"),
        ColumnInfo(name="biz_tax_surchg", kind=ColumnKind.FLOAT, desc="减:营业税金及附加"),
        ColumnInfo(name="sell_exp", kind=ColumnKind.FLOAT, desc="减:销售费用"),
        ColumnInfo(name="admin_exp", kind=ColumnKind.FLOAT, desc="减:管理费用"),
        ColumnInfo(name="fin_exp", kind=ColumnKind.FLOAT, desc="减:财务费用"),
        ColumnInfo(name="assets_impair_loss", kind=ColumnKind.FLOAT, desc="减:资产减值损失"),
        ColumnInfo(name="prem_refund", kind=ColumnKind.FLOAT, desc="退保金"),
        ColumnInfo(name="compens_payout", kind=ColumnKind.FLOAT, desc="赔付总支出"),
        ColumnInfo(name="reser_insur_liab", kind=ColumnKind.FLOAT, desc="提取保险责任准备金"),
        ColumnInfo(name="div_payt", kind=ColumnKind.FLOAT, desc="保户红利支出"),
        ColumnInfo(name="reins_exp", kind=ColumnKind.FLOAT, desc="分保费用"),
        ColumnInfo(name="oper_exp", kind=ColumnKind.FLOAT, desc="营业支出"),
        ColumnInfo(name="compens_payout_refu", kind=ColumnKind.FLOAT, desc="减:摊回赔付支出"),
        ColumnInfo(name="insur_reser_refu", kind=ColumnKind.FLOAT, desc="减:摊回保险责任准备金"),
        ColumnInfo(name="reins_cost_refund", kind=ColumnKind.FLOAT, desc="减:摊回分保费用"),
        ColumnInfo(name="other_bus_cost", kind=ColumnKind.FLOAT, desc="其他业务成本"),
        ColumnInfo(name="operate_profit", kind=ColumnKind.FLOAT, desc="营业利润"),
        ColumnInfo(name="non_oper_income", kind=ColumnKind.FLOAT, desc="加:营业外收入"),
        ColumnInfo(name="non_oper_exp", kind=ColumnKind.FLOAT, desc="减:营业外支出"),
        ColumnInfo(name="nca_disploss", kind=ColumnKind.FLOAT, desc="其中:减:非流动资产处置净损失"),
        ColumnInfo(name="total_profit", kind=ColumnKind.FLOAT, desc="利润总额"),
        ColumnInfo(name="income_tax", kind=ColumnKind.FLOAT, desc="所得税费用"),
        ColumnInfo(name="n_income", kind=ColumnKind.FLOAT, desc="净利润(含少数股东损益)"),
        ColumnInfo(name="n_income_attr_p", kind=ColumnKind.FLOAT, desc="净利润(不含少数股东损益)"),
        ColumnInfo(name="minority_gain", kind=ColumnKind.FLOAT, desc="少数股东损益"),
        ColumnInfo(name="oth_compr_income", kind=ColumnKind.FLOAT, desc="其他综合收益"),
        ColumnInfo(name="t_compr_income", kind=ColumnKind.FLOAT, desc="综合收益总额"),
        ColumnInfo(name="compr_inc_attr_p", kind=ColumnKind.FLOAT, desc="归属于母公司(或股东)的综合收益总额"),
        ColumnInfo(name="compr_inc_attr_m_s", kind=ColumnKind.FLOAT, desc="归属于少数股东的综合收益总额"),
        ColumnInfo(name="ebit", kind=ColumnKind.FLOAT, desc="息税前利润"),
        ColumnInfo(name="ebitda", kind=ColumnKind.FLOAT, desc="息税折旧摊销前利润"),
        ColumnInfo(name="insurance_exp", kind=ColumnKind.FLOAT, desc="保险业务支出"),
        ColumnInfo(name="undist_profit", kind=ColumnKind.FLOAT, desc="年初未分配利润"),
        ColumnInfo(name="distable_profit", kind=ColumnKind.FLOAT, desc="可分配利润"),
        ColumnInfo(name="rd_exp", kind=ColumnKind.FLOAT, desc="研发费用"),
        ColumnInfo(name="fin_exp_int_exp", kind=ColumnKind.FLOAT, desc="财务费用:利息费用"),
        ColumnInfo(name="fin_exp_int_inc", kind=ColumnKind.FLOAT, desc="财务费用:利息收入"),
        ColumnInfo(name="transfer_surplus_rese", kind=ColumnKind.FLOAT, desc="盈余公积转入"),
        ColumnInfo(name="transfer_housing_imprest", kind=ColumnKind.FLOAT, desc="住房周转金转入"),
        ColumnInfo(name="transfer_oth", kind=ColumnKind.FLOAT, desc="其他转入"),
        ColumnInfo(name="adj_lossgain", kind=ColumnKind.FLOAT, desc="调整以前年度损益"),
        ColumnInfo(name="withdra_legal_surplus", kind=ColumnKind.FLOAT, desc="提取法定盈余公积"),
        ColumnInfo(name="withdra_legal_pubfund", kind=ColumnKind.FLOAT, desc="提取法定公益金"),
        ColumnInfo(name="withdra_biz_devfund", kind=ColumnKind.FLOAT, desc="提取企业发展基金"),
        ColumnInfo(name="withdra_rese_fund", kind=ColumnKind.FLOAT, desc="提取储备基金"),
        ColumnInfo(name="withdra_oth_ersu", kind=ColumnKind.FLOAT, desc="提取任意盈余公积金"),
        ColumnInfo(name="workers_welfare", kind=ColumnKind.FLOAT, desc="职工奖金福利"),
        ColumnInfo(name="distr_profit_shrhder", kind=ColumnKind.FLOAT, desc="可供股东分配的利润"),
        ColumnInfo(name="prfshare_payable_dvd", kind=ColumnKind.FLOAT, desc="应付优先股股利"),
        ColumnInfo(name="comshare_payable_dvd", kind=ColumnKind.FLOAT, desc="应付普通股股利"),
        ColumnInfo(name="capit_comstock_div", kind=ColumnKind.FLOAT, desc="转作股本的普通股股利"),
        ColumnInfo(name="net_after_nr_lp_correct", kind=ColumnKind.FLOAT, desc="扣除非经常性损益后的净利润（更正前）"),
        ColumnInfo(name="credit_impa_loss", kind=ColumnKind.FLOAT, desc="信用减值损失"),
        ColumnInfo(name="net_expo_hedging_benefits", kind=ColumnKind.FLOAT, desc="净敞口套期收益"),
        ColumnInfo(name="oth_impair_loss_assets", kind=ColumnKind.FLOAT, desc="其他资产减值损失"),
        ColumnInfo(name="total_opcost", kind=ColumnKind.FLOAT, desc="营业总成本（二）"),
        ColumnInfo(name="amodcost_fin_assets", kind=ColumnKind.FLOAT, desc="以摊余成本计量的金融资产终止确认收益"),
        ColumnInfo(name="oth_income", kind=ColumnKind.FLOAT, desc="其他收益"),
        ColumnInfo(name="asset_disp_income", kind=ColumnKind.FLOAT, desc="资产处置收益"),
        ColumnInfo(name="continued_net_profit", kind=ColumnKind.FLOAT, desc="持续经营净利润"),
        ColumnInfo(name="end_net_profit", kind=ColumnKind.FLOAT, desc="终止经营净利润"),
    ],
)
