package logic

import (
	"context"
	"fmt"
	IC "jyBXBase/rpc/init"
	"jyBXBase/rpc/internal/svc"
	"jyBXBase/rpc/model"
	"jyBXBase/rpc/type/bxbase"

	"app.yhyue.com/moapp/jybase/common"
	"app.yhyue.com/moapp/jybase/redis"

	"github.com/zeromicro/go-zero/core/logx"
)

type IncludedLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewIncludedLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IncludedLogic {
	return &IncludedLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 剑鱼网站收录情况
func (l *IncludedLogic) Included(in *bxbase.AppIdReq) (*bxbase.IncludedResp, error) {
	resp := &bxbase.IncludedResp{}
	include := model.Included{
		MysqlDb: IC.BaseServiceMysql,
		Mgo:     IC.Mgo,
	}
	data := &model.IncludedData{}
	if jyIncludedData := redis.Get("other", "jyIncludedData"); jyIncludedData != nil {
		data, _ = common.JsonUnmarshal(jyIncludedData, &model.IncludedData{}).(*model.IncludedData)
	} else {
		includeData, err := include.GetIncludedData()
		if err != nil || includeData == nil {
			l.Error(fmt.Sprintf("%+v", in), err.Error())
			resp.ErrorMsg = err.Error()
			resp.ErrorCode = -1
			return resp, nil
		}
		data = includeData
		redis.Put("other", "jyIncludedData", data, 7200)
	}

	unitAppend := IC.C.UnitAppend
	resp.Bid = float32(data.Bid)
	resp.BidUnit = data.BidUnit
	resp.BidUnitAppend = unitAppend
	resp.BidDayUpdate = float32(data.BidDayUpdate)
	resp.BidDayUpdateUnit = data.BidDayUpdateUnit
	resp.BidDayUpdateUnitAppend = unitAppend
	resp.BidField = float32(data.BidField)
	resp.BidFieldUnit = data.BidFieldUnit
	resp.BidFieldUnitAppend = unitAppend
	resp.Buyer = float32(data.Buyer)
	resp.BuyerUnit = data.BuyerUnit
	resp.BuyerUnitAppend = unitAppend
	resp.Year = data.Year
	resp.Month = data.Month
	resp.Day = data.Day
	resp.Ent = float32(data.Ent)
	resp.EntUnit = data.EntUnit
	resp.EntUnitAppend = unitAppend
	resp.FieldAccuracy = float32(data.FieldAccuracy)
	resp.FieldAccuracyUnit = data.FieldAccuracyUnit
	resp.FieldAccuracyUnitAppend = unitAppend
	resp.Push = float32(data.Push)
	resp.PushUnit = data.PushUnit
	resp.PushUnitAppend = unitAppend
	resp.Project = float32(data.Project)
	resp.ProjectUnit = data.ProjectUnit
	resp.ProjectUnitAppend = unitAppend
	return resp, nil
}
