package quote

import (
	"fmt"
	"mtp2_if/global/app"
	"mtp2_if/global/e"
	"mtp2_if/logger"
	"mtp2_if/models"
	"mtp2_if/utils"
	"net/http"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
)

// QueryQuoteDayReq 获取商品盘面信息请求参数
type QueryQuoteDayReq struct {
	GoodsCodes string `form:"goodsCodes" binding:"required"`
}

// QueryQuoteDayRsp 获取商品盘面信息返回模型
type QueryQuoteDayRsp struct {
	Exchangedate         int64     `json:"exchangedate"`         // 交易日
	Goodscode            string    `json:"goodscode"`            // 商品代码
	Exchangecode         int       `json:"exchangecode"`         // 交易所代码
	Preclose             float64   `json:"preclose"`             // 昨收
	Opentime             time.Time `json:"opentime"`             // 开盘时间
	Opened               float64   `json:"opened"`               // 开盘价
	Highest              float64   `json:"highest"`              // 最高价
	Lowest               float64   `json:"lowest"`               // 最低价
	Lasttime             string    `json:"lasttime"`             // 行情时间（只有现价变化行情时间才变化）
	Utclasttime          time.Time `json:"utclasttime"`          // utc的行情时间
	Last                 float64   `json:"last"`                 // 最新价
	Lastvolume           int64     `json:"lastvolume"`           // 最新成交量
	Lastturnover         float64   `json:"lastturnover`          // 最新成交金额
	Totalbidvolume       int64     `json:"totalbidvolume"`       // 外盘
	Totalaskvolume       int64     `json:"totalaskvolume"`       // 内盘
	Totalvolume          int64     `json:"totalvolume"`          // 总量
	Totalturnover        float64   `json:"totalturnover"`        // 总金额
	Bid                  float64   `json:"bid"`                  // 买1
	Bid2                 float64   `json:"bid2"`                 // 买2
	Bid3                 float64   `json:"bid3"`                 // 买3
	Bid4                 float64   `json:"bid4"`                 // 买4
	Bid5                 float64   `json:"bid5"`                 // 买5
	Bidvolume            int64     `json:"bidvolume"`            // 买量1
	Bidvolume2           int64     `json:"bidvolume2"`           // 买量2
	Bidvolume3           int64     `json:"bidvolume3"`           // 买量3
	Bidvolume4           int64     `json:"bidvolume4"`           // 买量4
	Bidvolume5           int64     `json:"bidvolume5"`           // 买量5
	Ask                  float64   `json:"ask"`                  // 卖1
	Ask2                 float64   `json:"ask2"`                 // 卖2
	Ask3                 float64   `json:"ask3"`                 // 卖3
	Ask4                 float64   `json:"ask4"`                 // 卖4
	Ask5                 float64   `json:"ask5"`                 // 卖5
	Askvolume            int64     `json:"askvolume"`            // 卖量1
	Askvolume2           int64     `json:"askvolume2"`           // 卖量2
	Askvolume3           int64     `json:"askvolume3"`           // 卖量3
	Askvolume4           int64     `json:"askvolume4"`           // 卖量4
	Askvolume5           int64     `json:"askvolume5"`           // 卖量5
	Presettle            float64   `json:"presettle"`            // 昨结价
	Settle               float64   `json:"settle"`               // 结算价
	Preholdvolume        int64     `json:"preholdvolume"`        // 昨持仓
	Holdvolume           int64     `json:"holdvolume"`           // 持仓
	Averageprice         float64   `json:"averageprice"`         // 均价
	Orderid              int64     `json:"orderid"`              // 序号
	Limitup              float64   `json:"limitup"`              // 涨停价
	Limitdown            float64   `json:"limitdown"`            // 跌停价
	Inventory            int64     `json:"inventory"`            // 库存
	Holdincrement        int64     `json:"holdincrement"`        // 单笔持仓
	Iscleared            int       `json:"iscleared"`            // 是否清盘标志
	Issettled            int       `json:"issettled"`            // 是否结算标志
	Bidqueueinfo         string    `json:"bidqueueinfo"`         // 大利市买港股用
	Askqueueinfo         string    `json:"askqueueinfo"`         // 大利市卖港股用
	Bidorderid           int64     `json:"bidorderid"`           // 买单号1
	Bidorderid2          int64     `json:"bidorderid2"`          // 买单号2
	Bidorderid3          int64     `json:"bidorderid3"`          // 买单号3
	Bidorderid4          int64     `json:"bidorderid4"`          // 买单号4
	Bidorderid5          int64     `json:"bidorderid5"`          // 买单号5
	Askorderid           int64     `json:"askorderid"`           // 卖单号1
	Askorderid2          int64     `json:"askorderid2"`          // 卖单号2
	Askorderid3          int64     `json:"askorderid3"`          // 卖单号3
	Askorderid4          int64     `json:"askorderid4"`          // 卖单号4
	Askorderid5          int64     `json:"askorderid5"`          // 卖单号5
	Lastlot              int64     `json:"lastlot"`              // 最新成交手数
	Totallot             int64     `json:"totallot"`             // 总手数
	Strikeprice          float64   `json:"strikeprice"`          // 发行价
	Cleartime            int64     `json:"cleartime"`            // 清盘时间
	Calloptionpremiums   int64     `json:"calloptionpremiums"`   // 认购期权1
	Calloptionpremiums2  int64     `json:"calloptionpremiums2"`  // 认购期权2
	Calloptionpremiums3  int64     `json:"calloptionpremiums3"`  // 认购期权3
	Calloptionpremiums4  int64     `json:"calloptionpremiums4"`  // 认购期权4
	Calloptionpremiums5  int64     `json:"calloptionpremiums5"`  // 认购期权5
	Putoptionpremiums    int64     `json:"putoptionpremiums"`    // 认沽期权1
	Putoptionpremiums2   int64     `json:"putoptionpremiums2"`   // 认沽期权2
	Putoptionpremiums3   int64     `json:"putoptionpremiums3"`   // 认沽期权3
	Putoptionpremiums4   int64     `json:"putoptionpremiums4"`   // 认沽期权4
	Putoptionpremiums5   int64     `json:"putoptionpremiums5"`   // 认沽期权5
	Nontotalvolume       int64     `json:"nontotalvolume"`       // 非交易总量
	Nontotalholdervolume int64     `json:"nontotalholdervolume"` // 非交易持仓量
	Nontotalturnover     int64     `json:"nontotalturnover"`     // 非交易总金额
	Nontotallot          int64     `json:"nontotallot"`          // 非交易总手数
	Publictradetype      string    `json:"publictradetype"`      // 公共交易标志类型 港股专用
	Iep                  float64   `json:"iep"`                  // 平衡价 港股专用
	Iev                  float64   `json:"iev"`                  // 平衡量 港股专用
	Grepmarketprice      float64   `json:"grepmarketprice"`      // 暗盘价 港股专用
	Bid6                 float64   `json:"bid6"`                 // 买6
	Bid7                 float64   `json:"bid7"`                 // 买7
	Bid8                 float64   `json:"bid8"`                 // 买8
	Bid9                 float64   `json:"bid9"`                 // 买9
	Bid10                float64   `json:"bid10"`                // 买10
	Bidvolume6           int64     `json:"bidvolume6"`           // 买量6
	Bidvolume7           int64     `json:"bidvolume7"`           // 买量7
	Bidvolume8           int64     `json:"bidvolume8"`           // 买量8
	Bidvolume9           int64     `json:"bidvolume9"`           // 买量9
	Bidvolume10          int64     `json:"bidvolume10"`          // 买量10
	Ask6                 float64   `json:"ask6"`                 // 卖6
	Ask7                 float64   `json:"ask7"`                 // 卖7
	Ask8                 float64   `json:"ask8"`                 // 卖8
	Ask9                 float64   `json:"ask9"`                 // 卖9
	Ask10                float64   `json:"ask10"`                // 卖10
	Askvolume6           int64     `json:"askvolume6"`           // 卖量6
	Askvolume7           int64     `json:"askvolume7"`           // 卖量7
	Askvolume8           int64     `json:"askvolume8"`           // 卖量8
	Askvolume9           int64     `json:"askvolume9"`           // 卖量9
	Askvolume10          int64     `json:"askvolume10"`          // 卖量10
	Bidordervolume       int64     `json:"bidordervolume"`       // 买单量1
	Bidordervolume2      int64     `json:"bidordervolume2"`      // 买单量2
	Bidordervolume3      int64     `json:"bidordervolume3"`      // 买单量3
	Bidordervolume4      int64     `json:"bidordervolume4"`      // 买单量4
	Bidordervolume5      int64     `json:"bidordervolume5"`      // 买单量5
	Bidordervolume6      int64     `json:"bidordervolume6"`      // 买单量6
	Bidordervolume7      int64     `json:"bidordervolume7"`      // 买单量7
	Bidordervolume8      int64     `json:"bidordervolume8"`      // 买单量8
	Bidordervolume9      int64     `json:"bidordervolume9"`      // 买单量9
	Bidordervolume10     int64     `json:"bidordervolume10"`     // 买单量10
	Askordervolume       int64     `json:"askordervolume"`       // 卖单量1
	Askordervolume2      int64     `json:"askordervolume2"`      // 卖单量2
	Askordervolume3      int64     `json:"askordervolume3"`      // 卖单量3
	Askordervolume4      int64     `json:"askordervolume4"`      // 卖单量4
	Askordervolume5      int64     `json:"askordervolume5"`      // 卖单量5
	Askordervolume6      int64     `json:"askordervolume6"`      // 卖单量6
	Askordervolume7      int64     `json:"askordervolume7"`      // 卖单量7
	Askordervolume8      int64     `json:"askordervolume8"`      // 卖单量8
	Askordervolume9      int64     `json:"askordervolume9"`      // 卖单量9
	Askordervolume10     int64     `json:"askordervolume10"`     // 卖单量10
}

// QueryQuoteDay 获取商品盘面信息
// @Summary 获取商品盘面信息
// @Produce json
// @Security ApiKeyAuth
// @Param goodsCodes query string true "商品代码列表，格式：CU2102,CU2103,AL2107"
// @Success 200 {object} QueryQuoteDayRsp
// @Failure 500 {object} app.Response
// @Router /Quote/QueryQuoteDay [get]
// @Tags 行情服务
func QueryQuoteDay(c *gin.Context) {
	appG := app.Gin{C: c}

	// 获取请求参数
	var req QueryQuoteDayReq
	if err := appG.C.ShouldBindQuery(&req); err != nil {
		logger.GetLogger().Errorf("QueryQuoteDay failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	rsp := make([]QueryQuoteDayRsp, 0)
	// 构建查询GoodsCodes
	goodsCodes := fmt.Sprintf("'%s'", req.GoodsCodes)
	goodsCodes = strings.Replace(goodsCodes, ",", "','", -1)
	quoteDays, err := models.GetQuoteDays(goodsCodes)
	if err != nil {
		logger.GetLogger().Errorf("QueryQuoteDay failed: %s", err.Error())
		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
		return
	}
	for _, v := range quoteDays {
		// 获取对应的商品信息
		goods, _ := models.GetGoodsByGoodsCode(v.Goodscode)
		if goods != nil {
			rsp = append(rsp, QueryQuoteDayRsp{
				Exchangedate:         v.Exchangedate,
				Goodscode:            v.Goodscode,
				Exchangecode:         v.Exchangecode,
				Preclose:             utils.IntToFloat64(int(v.Preclose), int(goods.Decimalplace)),
				Opentime:             time.Unix(int64(v.Opentime), 0),
				Opened:               utils.IntToFloat64(int(v.Opened), int(goods.Decimalplace)),
				Highest:              utils.IntToFloat64(int(v.Highest), int(goods.Decimalplace)),
				Lowest:               utils.IntToFloat64(int(v.Lowest), int(goods.Decimalplace)),
				Lasttime:             v.Lasttime,
				Utclasttime:          time.Unix(int64(v.Utclasttime), 0),
				Last:                 utils.IntToFloat64(int(v.Last), int(goods.Decimalplace)),
				Lastvolume:           v.Lastvolume,
				Lastturnover:         float64(v.Lastturnover),
				Totalbidvolume:       v.Totalbidvolume,
				Totalaskvolume:       v.Totalaskvolume,
				Totalvolume:          v.Totalvolume,
				Totalturnover:        float64(v.Totalturnover),
				Bid:                  utils.IntToFloat64(int(v.Bid), int(goods.Decimalplace)),
				Bid2:                 utils.IntToFloat64(int(v.Bid2), int(goods.Decimalplace)),
				Bid3:                 utils.IntToFloat64(int(v.Bid3), int(goods.Decimalplace)),
				Bid4:                 utils.IntToFloat64(int(v.Bid4), int(goods.Decimalplace)),
				Bid5:                 utils.IntToFloat64(int(v.Bid5), int(goods.Decimalplace)),
				Bidvolume:            v.Bidvolume,
				Bidvolume2:           v.Bidvolume2,
				Bidvolume3:           v.Bidvolume3,
				Bidvolume4:           v.Bidvolume4,
				Bidvolume5:           v.Bidvolume5,
				Ask:                  utils.IntToFloat64(int(v.Ask), int(goods.Decimalplace)),
				Ask2:                 utils.IntToFloat64(int(v.Ask2), int(goods.Decimalplace)),
				Ask3:                 utils.IntToFloat64(int(v.Ask3), int(goods.Decimalplace)),
				Ask4:                 utils.IntToFloat64(int(v.Ask4), int(goods.Decimalplace)),
				Ask5:                 utils.IntToFloat64(int(v.Ask5), int(goods.Decimalplace)),
				Askvolume:            v.Askvolume,
				Askvolume2:           v.Askvolume2,
				Askvolume3:           v.Askvolume3,
				Askvolume4:           v.Askvolume4,
				Askvolume5:           v.Askvolume5,
				Presettle:            utils.IntToFloat64(int(v.Presettle), int(goods.Decimalplace)),
				Settle:               utils.IntToFloat64(int(v.Settle), int(goods.Decimalplace)),
				Preholdvolume:        v.Preholdvolume,
				Holdvolume:           v.Holdvolume,
				Averageprice:         utils.IntToFloat64(int(v.Averageprice), int(goods.Decimalplace)),
				Orderid:              v.Orderid,
				Limitup:              utils.IntToFloat64(int(v.Limitup), int(goods.Decimalplace)),
				Limitdown:            utils.IntToFloat64(int(v.Limitdown), int(goods.Decimalplace)),
				Inventory:            v.Inventory,
				Holdincrement:        v.Holdincrement,
				Iscleared:            v.Iscleared,
				Issettled:            v.Issettled,
				Bidqueueinfo:         v.Bidqueueinfo,
				Askqueueinfo:         v.Askqueueinfo,
				Bidorderid:           v.Bidorderid,
				Bidorderid2:          v.Bidorderid2,
				Bidorderid3:          v.Bidorderid3,
				Bidorderid4:          v.Bidorderid4,
				Bidorderid5:          v.Bidorderid5,
				Askorderid:           v.Askorderid,
				Askorderid2:          v.Askorderid2,
				Askorderid3:          v.Askorderid3,
				Askorderid4:          v.Askorderid4,
				Askorderid5:          v.Askorderid5,
				Lastlot:              v.Lastlot,
				Totallot:             v.Totallot,
				Strikeprice:          utils.IntToFloat64(int(v.Strikeprice), int(goods.Decimalplace)),
				Cleartime:            v.Cleartime,
				Calloptionpremiums:   v.Calloptionpremiums,
				Calloptionpremiums2:  v.Calloptionpremiums2,
				Calloptionpremiums3:  v.Calloptionpremiums3,
				Calloptionpremiums4:  v.Calloptionpremiums4,
				Calloptionpremiums5:  v.Calloptionpremiums5,
				Putoptionpremiums:    v.Putoptionpremiums,
				Putoptionpremiums2:   v.Putoptionpremiums2,
				Putoptionpremiums3:   v.Putoptionpremiums3,
				Putoptionpremiums4:   v.Putoptionpremiums4,
				Putoptionpremiums5:   v.Putoptionpremiums5,
				Nontotalvolume:       v.Nontotalvolume,
				Nontotalholdervolume: v.Nontotalholdervolume,
				Nontotalturnover:     v.Nontotalturnover,
				Nontotallot:          v.Nontotallot,
				Publictradetype:      v.Publictradetype,
				Iep:                  utils.IntToFloat64(int(v.Iep), int(goods.Decimalplace)),
				Iev:                  utils.IntToFloat64(int(v.Iev), int(goods.Decimalplace)),
				Grepmarketprice:      utils.IntToFloat64(int(v.Grepmarketprice), int(goods.Decimalplace)),
				Bid6:                 utils.IntToFloat64(int(v.Bid6), int(goods.Decimalplace)),
				Bid7:                 utils.IntToFloat64(int(v.Bid7), int(goods.Decimalplace)),
				Bid8:                 utils.IntToFloat64(int(v.Bid8), int(goods.Decimalplace)),
				Bid9:                 utils.IntToFloat64(int(v.Bid9), int(goods.Decimalplace)),
				Bid10:                utils.IntToFloat64(int(v.Bid10), int(goods.Decimalplace)),
				Bidvolume6:           v.Bidvolume6,
				Bidvolume7:           v.Bidvolume7,
				Bidvolume8:           v.Bidvolume8,
				Bidvolume9:           v.Bidvolume9,
				Bidvolume10:          v.Bidvolume10,
				Ask6:                 utils.IntToFloat64(int(v.Ask6), int(goods.Decimalplace)),
				Ask7:                 utils.IntToFloat64(int(v.Ask7), int(goods.Decimalplace)),
				Ask8:                 utils.IntToFloat64(int(v.Ask8), int(goods.Decimalplace)),
				Ask9:                 utils.IntToFloat64(int(v.Ask9), int(goods.Decimalplace)),
				Ask10:                utils.IntToFloat64(int(v.Ask10), int(goods.Decimalplace)),
				Askvolume6:           v.Askvolume6,
				Askvolume7:           v.Askvolume7,
				Askvolume8:           v.Askvolume8,
				Askvolume9:           v.Askvolume9,
				Askvolume10:          v.Askvolume10,
				Bidordervolume:       v.Bidordervolume,
				Bidordervolume2:      v.Bidordervolume2,
				Bidordervolume3:      v.Bidordervolume3,
				Bidordervolume4:      v.Bidordervolume4,
				Bidordervolume5:      v.Bidordervolume5,
				Bidordervolume6:      v.Bidordervolume6,
				Bidordervolume7:      v.Bidordervolume7,
				Bidordervolume8:      v.Bidordervolume8,
				Bidordervolume9:      v.Bidordervolume9,
				Bidordervolume10:     v.Bidordervolume10,
				Askordervolume:       v.Askordervolume,
				Askordervolume2:      v.Askordervolume2,
				Askordervolume3:      v.Askordervolume3,
				Askordervolume4:      v.Askordervolume4,
				Askordervolume5:      v.Askordervolume5,
				Askordervolume6:      v.Askordervolume6,
				Askordervolume7:      v.Askordervolume7,
				Askordervolume8:      v.Askordervolume8,
				Askordervolume9:      v.Askordervolume9,
				Askordervolume10:     v.Askordervolume10,
			})
		}
	}

	// 查询成功
	logger.GetLogger().Debugln("QueryQuoteDay successed: %v", rsp)
	appG.Response(http.StatusOK, e.SUCCESS, rsp)
}
