package knowledge

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/weixinapi"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/weixinmsg"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/globals/api"
	"mime/multipart"
	"net/http"
)

func GetKnowledgeDepot(ctx *jxcontext.Context, keyword string, offset, pageSize int) (page *model.PagedInfo, err error) {
	return dao.GetKnowledgeDepot(dao.GetDB(), keyword, offset, pageSize)
}
func AddKnowledgeDepot(ctx *jxcontext.Context, title, content string) (err error) {
	var (
		db = dao.GetDB()
	)
	know := &model.KnowledgeDepot{
		Title:   title,
		Content: content,
	}
	dao.WrapAddIDCULDEntity(know, ctx.GetUserName())
	dao.CreateEntity(db, know)
	return err
}

func UpdateKnowledgeDepot(ctx *jxcontext.Context, id int, content string, isDel bool) (err error) {
	var (
		db   = dao.GetDB()
		know = &model.KnowledgeDepot{}
	)
	know.ID = id
	err = dao.GetEntity(db, know)
	if isDel {
		dao.DeleteEntity(db, know)
	} else {
		if content != know.Content {
			know.Content = content
			dao.UpdateEntity(db, know, "Content")
		}
	}
	return err
}

type SNSUploadImgResult struct {
	URL     string `json:"url"`
	MediaID string `json:"mediaID"`
}

func SNSUploadImg(ctx *jxcontext.Context, files []*multipart.FileHeader, isThumb bool) (snsUploadImgResult *SNSUploadImgResult, err error) {
	var (
		fileHeader = files[0]
		fileName   = fileHeader.Filename
		data       = make([]byte, fileHeader.Size)
	)
	snsUploadImgResult = &SNSUploadImgResult{}
	file, err := fileHeader.Open()
	file.Read(data)
	if len(data) == 0 {
		return snsUploadImgResult, fmt.Errorf("未读取到文件！")
	}
	//如果不是缩略图，只会返url
	//如果是封面缩略图，则还会返mediaID
	if !isThumb {
		if url, err := api.WeixinAPI.CBUploadImg(data, fileName, http.DetectContentType(data)); err == nil && url != "" {
			snsUploadImgResult.URL = url
		}
	} else {
		if mediaID, url, err := api.WeixinAPI.CBUploadThumb(data, fileName, http.DetectContentType(data)); err == nil && url != "" && mediaID != "" {
			snsUploadImgResult.URL = url
			snsUploadImgResult.MediaID = mediaID
		}
	}
	file.Close()
	return snsUploadImgResult, err
}

func AddMaterial(ctx *jxcontext.Context, knowIDs []int, param *weixinapi.CBAddNewsParam) (err error) {
	var (
		db      = dao.GetDB()
		content = ""
	)
	if len(knowIDs) == 0 {
		return fmt.Errorf("请至少勾选一条！")
	}
	knows, err := dao.GetKnowledgeDepotNoPage(db, knowIDs)
	for k, v := range knows {
		if k != 0 {
			content += weixinapi.SNSCutLine
		}
		content += v.Content
	}
	if content != "" {
		param.Content = content
		api.WeixinAPI.CBAddNews(param)
	} else {
		return fmt.Errorf("读取内容错误！")
	}
	return err
}

func SendSNSMediaMsg(ctx *jxcontext.Context, storeIDs []int, mediaID string) (err error) {
	var (
		openIDs []string
	)
	for _, v := range storeIDs {
		if results := weixinmsg.GetWeixinOpenIDsFromStoreID(v); len(results) > 0 {
			openIDs = append(openIDs, results...)
		}
	}
	if len(openIDs) > 0 {
		return weixinmsg.MediaMessageSendGroup(openIDs, mediaID)
	}
	return err
}
