package services

import (
	"encoding/json"
	"fmt"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
)

type EditReportChapterTypeSyncReq struct {
	ReportChapterTypeId int `description:"报告章节类型id"`
}

func EditReportChapterTypeSync(pars *EditReportChapterTypeSyncReq) (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/eta/chapter_type/yb/sync")
	b, err := crmEtaPost(url, pars)
	if err != nil {
		errMsg = "更新品种失败"
		err = fmt.Errorf("url:%s err: %s", url, err.Error())
		return
	}
	result := new(CrmEtaBaseResp)
	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
}

type EditReportChapterTypeRuleSyncReq struct {
	ReportClassifyId int `description:"研报分类id"`
}

// EditReportChapterTypeRuleSync 同步章节更新设置
func EditReportChapterTypeRuleSync(pars *EditReportChapterTypeRuleSyncReq) (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/eta/chapter_type/rule/sync")
	b, err := crmEtaPost(url, pars)
	if err != nil {
		errMsg = "更新品种失败"
		err = fmt.Errorf("url:%s err: %s", url, err.Error())
		return
	}
	result := new(CrmEtaBaseResp)
	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
}
