package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/utils"
)

// ReportChapterTypeController 报告章节配置
type ReportChapterTypeController struct {
	BaseAuthController
}

// List
// @Title 报告章节列表
// @Description 报告章节列表
// @Param   ClassifyId  query  int  true  "所属分类id"
// @Success 200 {object} models.ReportChapterTypePageListResp
// @router /chapter_type/list [get]
func (this *ReportChapterTypeController) List() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	classifyId, _ := this.GetInt("ClassifyId", 0)
	if classifyId <= 0 {
		br.Msg = "请选择分类"
		return
	}

	cond := ` AND report_classify_id = ?`
	pars := make([]interface{}, 0)
	pars = append(pars, classifyId)
	list, e := models.GetReportChapterTypePageList(cond, pars)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取报告章节列表失败, Err: " + e.Error()
		return
	}
	respList := make([]*models.ReportChapterTypeListItem, 0)
	for i := range list {
		respList = append(respList, &models.ReportChapterTypeListItem{
			ReportChapterTypeId:   list[i].ReportChapterTypeId,
			ReportChapterTypeName: list[i].ReportChapterTypeName,
			Sort:                  list[i].Sort,
			CreatedTime:           list[i].CreatedTime.Format(utils.FormatDateTime),
			ResearchType:          list[i].ResearchType,
			SelectedImage:         list[i].SelectedImage,
			UnselectedImage:       list[i].UnselectedImage,
			WordsImage:            list[i].YbBottomIcon, // 此处的不一样
			EditImgUrl:            list[i].EditImgUrl,
			IsShow:                list[i].IsShow,
			Enabled:               list[i].Enabled,
		})
	}

	resp := new(models.ReportChapterTypeListResp)
	resp.List = respList
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// Edit
// @Title 编辑报告章节
// @Description 编辑报告章节
// @Param	request  body  models.ReportChapterTypeEditReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /chapter_type/edit [post]
func (this *ReportChapterTypeController) Edit() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.ReportChapterTypeEditReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ReportChapterTypeId <= 0 {
		br.Msg = "章节ID有误"
		return
	}

	item, e := models.GetReportChapterTypeById(req.ReportChapterTypeId)
	if e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "获取报告章节失败, Err:" + e.Error()
		return
	}

	item.SelectedImage = req.SelectedImage
	item.UnselectedImage = req.UnselectedImage
	item.PcSelectedImage = req.SelectedImage
	item.PcUnselectedImage = req.UnselectedImage
	item.EditImgUrl = req.EditImgUrl
	item.YbIconUrl = req.UnselectedImage
	item.YbBottomIcon = req.WordsImage
	item.IsShow = req.IsShow
	item.ReportChapterTypeThumb = req.EditImgUrl
	item.BannerUrl = req.UnselectedImage

	updateCols := []string{"SelectedImage", "UnselectedImage",
		"PcSelectedImage", "PcUnselectedImage", "EditImgUrl", "YbIconUrl", "YbBottomIcon", "IsShow",
		"ReportChapterTypeThumb", "BannerUrl",
	}
	if e = item.Update(updateCols); e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "更新报告章节失败, Err:" + e.Error()
		return
	}

	// 清除小程序端的章节缓存
	{
		key := "hongze_yb:report_chapter_type:GetEffectTypeID"
		_ = utils.Rc.Delete(key)
	}
	// 同步eta系统的章节小程序配置
	go func() {
		var reqEta services.EditReportChapterTypeSyncReq
		reqEta.ReportChapterTypeId = req.ReportChapterTypeId
		_, _ = services.EditReportChapterTypeSync(&reqEta)
	}()

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
