package svc

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/svc"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"strings"
	"time"
)

func Notice(c *gin.Context) {
	var args map[string]string
	if err := c.ShouldBindJSON(&args); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	args["uid"] = utils.IntToStr(user.Info.Uid)
	data, total := db.GetBankNoticeAll(svc.MasterDb(c), args)
	list := make([]map[string]string, 0)
	if data != nil {
		for _, v := range *data {
			tmp := map[string]string{
				"id":          utils.IntToStr(v.Id),
				"title":       v.Title,
				"content":     v.Content,
				"create_time": v.CreateTime.Format("2006-01-02 15:04"),
			}
			list = append(list, tmp)
		}
	}
	res := map[string]interface{}{
		"list": list, "total": total,
	}
	e.OutSuc(c, res, nil)
	return
}
func NoticeSave(c *gin.Context) {
	var args map[string]string
	if err := c.ShouldBindJSON(&args); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	eg := svc.MasterDb(c)
	if args["id"] != "" {
		notice := db.GetNotice(eg, args["id"])
		notice.UpdateTime = time.Now()
		notice.Title = args["title"]
		notice.Content = args["content"]
	} else {
		notice := &model.CommunityTeamStoreNotice{
			Uid:        user.Info.Uid,
			CreateTime: time.Now(),
			UpdateTime: time.Now(),
			Title:      args["title"],
			Content:    args["content"],
		}
		eg.Insert(notice)
	}
	e.OutSuc(c, "success", nil)
	return

}

func NoticeDel(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	svc.MasterDb(c).In("id", strings.Split(arg["ids"], ",")).Delete(&model.CommunityTeamStoreNotice{})
	e.OutSuc(c, "success", nil)
	return
}
