package services

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/services/alarm_msg"
	"eta/eta_api/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"time"
)

// PullPolicyReport 策略报告拉取
func PullPolicyReport() (ret models.PullEnglishPolicyDataResp, err error, msg string) {
	//调用列表接口，批量插入数据
	originData, err := getOriginPolicyReportList()
	if err != nil {
		msg = "调用英文策略报告列表接口失败"
		return
	}
	condition := ""
	var pars []interface{}
	startDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
	condition += ` AND publish_time >= ? `
	pars = append(pars, startDate)

	//获取已插入的报告ID，避免重复插入
	existData, err := models.GetEnglishPolicyReportByCondition(condition, pars)
	if err != nil {
		msg = "查询已存在的英文策略报告列表接口失败"
		return
	}
	existIdsMap := make(map[int]struct{}, 0)
	for _, v := range existData {
		existIdsMap[v.SourceReportId] = struct{}{}
	}
	//组装数据，插入到policy_report表中
	addItems := make([]*models.EnglishPolicyReport, 0)
	addIds := make([]int, 0)
	for _, v := range originData {
		if _, ok := existIdsMap[v.Id]; ok {
			continue
		}
		addIds = append(addIds, v.Id)
		tmp := &models.EnglishPolicyReport{
			ClassifyIdFirst:    v.Industry.Id,
			ClassifyNameFirst:  v.Industry.Description,
			ClassifyIdSecond:   v.FieldId,
			ClassifyNameSecond: v.Field.Description,
			Title:              v.TitleEn,
			Abstract:           v.Content.Abstract,
			Author:             v.Author.Name,
			Frequency:          v.Frequency,
			CreateTime:         time.Now(),
			ModifyTime:         time.Now(),
			State:              1,
			PublishStatus:      v.PublishStatus,
			PublishTime:        v.PublishDate,
			KeyTakeaways:       v.Content.Annotation,
			AuthorMobile:       v.Author.PhoneNumber,
			SourceReportId:     v.Id,
			ReportCoverUrl:     v.Cover,
		}
		addItems = append(addItems, tmp)
	}
	if len(addItems) > 0 {
		err = models.AddEnglishPolicyReportMulti(addItems)
		if err != nil {
			msg = "批量新增英文策略报告信息失败"
			return
		}
	}
	//根据原始ID调用详情接口，更新数据
	go setPolicyReportContentAndSync(addIds)
	ret = models.PullEnglishPolicyDataResp{
		Num: len(addItems),
	}
	return
}

type EnPolicyReportDataListResp struct {
	Code       int                          `json:"code"`
	Msg        string                       `json:"msg"`
	Data       []EnPolicyReportDataListItem `json:"data"`
	Pagination EnPolicyReportDataPage       `json:"pagination"`
}

type EnPolicyReportDataDetailResp struct {
	Code int                        `json:"code"`
	Msg  string                     `json:"msg"`
	Data EnPolicyReportDataListItem `json:"data"`
}

type EnPolicyReportDataPage struct {
	Total     int `json:"total"`
	Page      int `json:"page"`
	PageSize  int `json:"page_size"`
	PageTotal int `json:"page_total"`
}

type EnPolicyReportDataListItem struct {
	Id                int                                `json:"id"`
	Title             string                             `json:"title"`
	TitleEn           string                             `json:"title_en"`
	Frequency         string                             `json:"frequency"`
	CreateDate        time.Time                          `json:"create_date"`
	UpdateDate        time.Time                          `json:"update_date"`
	PublishDate       time.Time                          `json:"publish_date"`
	PublishStatus     int                                `json:"publish_status"`
	VerifyStatus      int                                `json:"verify_status"`
	PublishArea       string                             `json:"publish_area"`
	AccessLevel       int                                `json:"access_level"`
	IsActive          bool                               `json:"is_active"`
	AuthorPhoneNumber string                             `json:"author_phone_number"`
	Cover             string                             `json:"cover"`
	IndustryId        int                                `json:"industry_id"`
	ContentId         int                                `json:"content_id"`
	TypeId            int                                `json:"type_id"`
	FieldId           int                                `json:"field_id"`
	SeriesId          int                                `json:"series_id"`
	File              interface{}                        `json:"file"`
	Stock             []string                           `json:"stock"`
	IsFocused         int                                `json:"is_focused"`
	Content           EnPolicyReportDataListItemContent  `json:"content"`
	Industry          EnPolicyReportDataListItemInfo     `json:"industry"`
	Type              EnPolicyReportDataListItemInfo     `json:"type"`
	Field             EnPolicyReportDataListItemInfoMore `json:"field"`
	Series            EnPolicyReportDataListItemInfoMore `json:"series"`
	Author            EnPolicyReportDataListItemAuthor   `json:"author"`
}

type EnPolicyReportDataListItemContent struct {
	Id         int    `json:"id"`
	Body       string `json:"body"`
	Abstract   string `json:"abstract"`
	Annotation string `json:"annotation"`
}

type EnPolicyReportDataListItemInfo struct {
	Id          int    `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

type EnPolicyReportDataListItemInfoMore struct {
	Id          int    `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	IndustryId  int    `json:"industry_id"`
}

type EnPolicyReportDataListItemAuthor struct {
	PhoneNumber string `json:"phone_number"`
	Name        string `json:"name"`
}

func getOriginPolicyReportList() (list []EnPolicyReportDataListItem, err error) {
	//组装请求字段
	typeId := 9
	//field_id=1 表示 Daily Check-in
	fieldId := 1
	take := 1000
	skip := 0
	publishStatus := "1,2"
	mode := "all"
	startDt := ""
	// 获取当天的报告
	startDt = time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
	//设置接口地址
	//处理返回值
	url := utils.EnPolicyReportUrl + `articles/index?type_id=%d&field_id=%d&take=%d&skip=%d&publish_status=%s&mode=%s&start_dt=%s`
	url = fmt.Sprintf(url, typeId, fieldId, take, skip, publishStatus, mode, startDt)
	utils.FileLog.Info("url:%s", url)
	body, err := http.Get(url)
	fmt.Println("getOriginPolicyReportList body:")
	fmt.Println(string(body))
	item := new(EnPolicyReportDataListResp)
	err = json.Unmarshal(body, &item)
	if err != nil {
		return
	}
	if item.Code != 0 {
		err = fmt.Errorf("getOriginPolicyReportDetail ErrCode: %d, ErrMsg: %s", item.Code, item.Msg)
		return
	}

	list = item.Data
	return
}

func getOriginPolicyReportDetail(id int) (detail EnPolicyReportDataListItem, err error) {
	//设置接口地址
	//处理返回值
	url := utils.EnPolicyReportUrl + `articles/en/%d`
	url = fmt.Sprintf(url, id)
	utils.FileLog.Info("url:%s", url)
	body, err := http.Get(url)
	fmt.Println("getOriginPolicyReportDetail body:")
	fmt.Println(string(body))
	item := new(EnPolicyReportDataDetailResp)
	err = json.Unmarshal(body, &item)
	if err != nil {
		return
	}
	if item.Code != 0 {
		err = fmt.Errorf("getOriginPolicyReportDetail ErrCode: %d, ErrMsg: %s", item.Code, item.Msg)
		return
	}

	detail = item.Data
	return
}

func setPolicyReportContentAndSync(addIds []int) (err error, msg string) {
	defer func() {
		if err != nil {
			go alarm_msg.SendAlarmMsg("设置英文策略报告内容失败，Err："+err.Error()+";msg:"+msg, 3)
			utils.FileLog.Info(fmt.Sprintf("设置英文策略报告内容失败 setPolicyReportContentAndSync,Err:%s,%s", err.Error(), msg))
		}
	}()
	if len(addIds) > 0 {
		for _, v := range addIds {
			originDetail, e := getOriginPolicyReportDetail(v)
			if e != nil {
				msg = "拉取英文策略报告详情失败"
				err = e
				return
			}
			err = models.SetEnglishPolicyReportContentBySourceReportId(v, originDetail.Content.Body)
			if err != nil {
				msg = "更新英文策略报告内容失败"
				return
			}
		}
	}
	//同步到英文研报
	err, msg = MultiEnglishPolicyReportSync()
	return
}
