gitee.com/quant1x/engine@v1.8.4/datasource/dfcf/finance_analysis.go (about)

     1  package dfcf
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/exchange"
     6  	"gitee.com/quant1x/gox/api"
     7  	"gitee.com/quant1x/gox/http"
     8  	"gitee.com/quant1x/gox/logger"
     9  	"gitee.com/quant1x/pkg/fastjson"
    10  	urlpkg "net/url"
    11  )
    12  
    13  // 数据来源: https://data.eastmoney.com/bbsj/yjbb/301381.html
    14  const (
    15  	urlQuarterlyReportAll               = "https://datacenter-web.eastmoney.com/api/data/v1/get"
    16  	EastmoneyQuarterlyReportAllPageSize = 50 // 一页最大50条
    17  )
    18  
    19  // QuarterlyReport 财报
    20  type QuarterlyReport struct {
    21  	SecurityCode       string  `json:"CODE"`                 // 证券代码
    22  	ReportDate         string  `json:"REPORTDATE"`           // 报告日期
    23  	NoticeDate         string  `json:"NOTICE_DATE"`          // 最新公告日期
    24  	UpdateDate         string  `json:"UPDATE_DATE"`          // 更新日期
    25  	SecuCode           string  `json:"SECUCODE"`             // 证券代码
    26  	BasicEPS           float64 `json:"BASIC_EPS"`            // 每股收益
    27  	DeductBasicEPS     float64 `json:"DEDUCT_BASIC_EPS"`     // 每股收益(扣除)
    28  	TotalOperateIncome float64 `json:"TOTAL_OPERATE_INCOME"` // 营业总收入
    29  	ParentNetprofit    float64 `json:"PARENT_NETPROFIT"`     // 净利润
    30  	WeightAvgRoe       float64 `json:"WEIGHTAVG_ROE"`        // 净资产收益率
    31  	YSTZ               float64 `json:"YSTZ"`                 // 营业总收入同比增长
    32  	SJLTZ              float64 `json:"SJLTZ"`                // 净利润同比增长
    33  	BPS                float64 `json:"BPS"`                  // 每股净资产
    34  	MGJYXJJE           float64 `json:"MGJYXJJE"`             // 每股经营现金流量(元)
    35  	XSMLL              float64 `json:"XSMLL"`                // 销售毛利率(%)
    36  	YSHZ               float64 `json:"YSHZ"`
    37  	SJLHZ              float64 `json:"SJLHZ"`
    38  	ASSIGNDSCRPT       float64 `json:"ASSIGNDSCRPT"`
    39  	PAYYEAR            float64 `json:"PAYYEAR"`
    40  	PUBLISHNAME        float64 `json:"PUBLISHNAME"`
    41  	ZXGXL              float64 `json:"ZXGXL"`
    42  	ORGCODE            string  `json:"ORG_CODE"`
    43  	TRADEMARKETZJG     string  `json:"TRADE_MARKET_ZJG"`
    44  	IsNew              string  `json:"ISNEW"`
    45  	QDATE              string  `name:"报告期" json:"QDATE"` // 季报期
    46  	DATATYPE           string  `json:"DATATYPE"`
    47  	DATAYEAR           string  `json:"DATAYEAR"`
    48  	DATEMMDD           string  `json:"DATEMMDD"`
    49  	EITIME             string  `json:"EITIME"`
    50  	TRADEMARKETCODE    string  `json:"TRADE_MARKET_CODE"`
    51  	TRADEMARKET        string  `json:"TRADE_MARKET"` //市场
    52  	SECURITYTYPECODE   string  `json:"SECURITY_TYPE_CODE"`
    53  	SECURITYTYPE       string  `json:"SECURITY_TYPE"`
    54  	SECURITYCODE       string  `json:"SECURITY_CODE"`      // 证券代码
    55  	SECURITYNAMEABBR   string  `json:"SECURITY_NAME_ABBR"` // 证券名称
    56  }
    57  
    58  // GetQuarterlyReports 分页获取季报数据
    59  func GetQuarterlyReports(pageNumber ...int) (reports []QuarterlyReport, pages int, err error) {
    60  	pageNo := 1
    61  	if len(pageNumber) > 0 {
    62  		pageNo = pageNumber[0]
    63  	}
    64  	_, qEnd := api.GetQuarterDay(4)
    65  	beginDate := exchange.FixTradeDate(qEnd)
    66  	params := urlpkg.Values{
    67  		//"callback":    {"jQuery1123043614175387302234_1685785566671"},
    68  		//"sortColumns": {"UPDATE_DATE,SECURITY_CODE"},
    69  		"sortColumns": {"REPORTDATE,SECURITY_CODE"},
    70  		"sortTypes":   {"-1,1"},
    71  		"pageSize":    {fmt.Sprint(EastmoneyQuarterlyReportAllPageSize)},
    72  		"pageNumber":  {fmt.Sprintf("%d", pageNo)},
    73  		"reportName":  {"RPT_LICO_FN_CPD"},
    74  		"columns":     {"ALL"},
    75  		//"filter":      {"(REPORTDATE>='2023-03-31')"},
    76  		"filter": {fmt.Sprintf("(REPORTDATE>='%s')", beginDate)},
    77  		//"filter": {fmt.Sprintf("(REPORTDATE<='%s')(SECURITY_CODE=\"301381\")", beginDate)},
    78  		//"filter": {fmt.Sprintf("(REPORTDATE>='%s')(REPORTDATE<'%s')", beginDate, "2023-03-31")},
    79  	}
    80  
    81  	url := urlQuarterlyReportAll + "?" + params.Encode()
    82  	data, err := http.Get(url)
    83  	//fmt.Println(api.Bytes2String(data))
    84  	obj, err := fastjson.ParseBytes(data)
    85  	if err != nil {
    86  		logger.Errorf("%+v\n", err)
    87  		return
    88  	}
    89  
    90  	result := obj.Get("result")
    91  	list := result.GetArray("data")
    92  	pages = result.GetInt("pages")
    93  	if len(list) > 0 {
    94  		for _, v := range list {
    95  			v.GetStringBytes()
    96  			report := QuarterlyReport{
    97  				SecuCode:           v.GetString("SECUCODE"),
    98  				UpdateDate:         v.GetString("UPDATE_DATE"),
    99  				ReportDate:         v.GetString("REPORTDATE"),
   100  				BasicEPS:           v.GetFloat64("BASIC_EPS"),
   101  				DeductBasicEPS:     v.GetFloat64("DEDUCT_BASIC_EPS"),
   102  				BPS:                v.GetFloat64("BPS"),
   103  				NoticeDate:         v.GetString("NOTICE_DATE"),
   104  				IsNew:              v.GetString("ISNEW"),
   105  				ORGCODE:            v.GetString("ORG_CODE"),
   106  				TRADEMARKETZJG:     v.GetString("TRADE_MARKET_ZJG"),
   107  				QDATE:              v.GetString("QDATE"),
   108  				DATATYPE:           v.GetString("DATATYPE"),
   109  				DATAYEAR:           v.GetString("DATAYEAR"),
   110  				DATEMMDD:           v.GetString("DATEMMDD"),
   111  				EITIME:             v.GetString("EITIME"),
   112  				SECURITYCODE:       v.GetString("SECURITY_CODE"),
   113  				SECURITYNAMEABBR:   v.GetString("SECURITY_NAME_ABBR"),
   114  				TRADEMARKETCODE:    v.GetString("TRADE_MARKET_CODE"),
   115  				TRADEMARKET:        v.GetString("TRADE_MARKET"),
   116  				SECURITYTYPECODE:   v.GetString("SECURITY_TYPE_CODE"),
   117  				SECURITYTYPE:       v.GetString("SECURITY_TYPE"),
   118  				TotalOperateIncome: v.GetFloat64("TOTAL_OPERATE_INCOME"),
   119  				ParentNetprofit:    v.GetFloat64("PARENT_NETPROFIT"),
   120  				WeightAvgRoe:       v.GetFloat64("WEIGHTAVG_ROE"),
   121  				YSTZ:               v.GetFloat64("YSTZ"),
   122  				SJLTZ:              v.GetFloat64("SJLTZ"),
   123  				MGJYXJJE:           v.GetFloat64("MGJYXJJE"),
   124  				XSMLL:              v.GetFloat64("XSMLL"),
   125  				YSHZ:               v.GetFloat64("YSHZ"),
   126  				SJLHZ:              v.GetFloat64("SJLHZ"),
   127  				ASSIGNDSCRPT:       v.GetFloat64("ASSIGNDSCRPT"),
   128  				PAYYEAR:            v.GetFloat64("PAYYEAR"),
   129  				PUBLISHNAME:        v.GetFloat64("PUBLISHNAME"),
   130  				ZXGXL:              v.GetFloat64("ZXGXL"),
   131  			}
   132  			// 截取市场编码,截取股票编码,市场编码+股票编码拼接作为主键
   133  			securityCode := exchange.CorrectSecurityCode(report.SecuCode)
   134  			report.SecurityCode = securityCode
   135  			reports = append(reports, report)
   136  		}
   137  	}
   138  	return
   139  }