package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"hongze/hz_crm_api/utils"
	"time"
)

var (
	DayNewWeworkMsgRefreshUrl = utils.HongzeOpenApiHost + "/api/day_new/refresh"
	DayNewTranslateContentUrl = utils.HongzeOpenApiHost + "/api/day_new/translate"
)

type DayNewResponse struct {
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
	ErrMsg string `json:"err_msg"`
}

func DayNewWeworkMsgRefresh() (err error) {
	cacheKey := utils.CACHE_KEY_DAYNEW_REFRESH
	//设置redis 防止频繁操作
	if !utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) {
		err = errors.New(fmt.Sprintf("系统处理中,请1分钟之后再试"))
		return
	}
	ret, err := http.Get(DayNewWeworkMsgRefreshUrl)
	if err != nil {
		return
	}
	var resp DayNewResponse
	err = json.Unmarshal(ret, &resp)
	if err != nil {
		return
	}
	if resp.Code != 200 {
		err = errors.New(resp.Msg)
		return
	}
	return
}
func DayNewTranslateContent() (err error) {
	cacheKey := utils.CACHE_KEY_DAYNEW_TRANSLATE
	//设置redis 防止频繁操作,控制每分钟执行1次
	if !utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) {
		err = errors.New(fmt.Sprintf("系统处理中,请1分钟之后再试"))
		return
	}
	ret, err := http.Get(DayNewTranslateContentUrl)
	if err != nil {
		return
	}
	var resp DayNewResponse
	err = json.Unmarshal(ret, &resp)
	if err != nil {
		return
	}
	if resp.Code != 200 {
		err = errors.New(resp.Msg)
		return
	}
	return
}