package services

import (
	"encoding/json"
	"eta/eta_mobile/services/alarm_msg"
	"eta/eta_mobile/utils"
	"fmt"
)

type ChapterTypeSyncReq struct {
	ReportChapterTypeId int    `description:"报告章节类型id"`
	ResearchType        string `description:"研报类型"`
}

type ChapterTypeBaseResp struct {
	Code   int    `json:"code" description:"状态码"`
	Msg    string `json:"msg" description:"提示信息"`
	ErrMsg string `json:"-" description:"错误信息，不用返回给前端，只是做日志记录"`
}

func ReportChapterTypeSync(pars *ChapterTypeSyncReq) (err error, errMsg string) {
	defer func() {
		if err != nil {
			utils.FileLog.Info("同步章节权限数据失败, Err: " + err.Error() + errMsg)
			alarm_msg.SendAlarmMsg("同步章节权限数据失败，Err："+err.Error(), 3)
		}
	}()
	if utils.CrmEtaServerUrl == "" {
		return
	}
	url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chapter_type/sync")
	b, err := crmEtaPost(url, pars)
	if err != nil {
		errMsg = "操作失败"
		err = fmt.Errorf("url:%s err: %s", url, err.Error())
		return
	}
	result := new(ChapterTypeBaseResp)
	if e := json.Unmarshal(b, &result); e != nil {
		errMsg = "操作失败"
		err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
		return
	}
	utils.FileLog.Info("%s", string(b))
	if result.Code != 200 {
		err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
		errMsg = result.Msg
		return
	}
	return
}
