bulletin.go 5.4 KB
package controllers

import (
	"encoding/json"
	"oppmg/common/log"
	"oppmg/protocol"
	"oppmg/services/bulletin"
)

//BulletinController 公告
type BulletinController struct {
	BaseController
}

//BulletinRelease
//@router /add [post]
func (this *BulletinController) BulletinRelease() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.BulletinReleaseRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if !(request.Type == 0 || request.Type == 1) {
		msg = protocol.BadRequestParam("1")
		log.Error("type error :%v", request.Type)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if len([]rune(request.Title)) > 30 {
		msg = protocol.BadRequestParam("10618")
		return
	}
	rsp, err := bulletin.BulletinRelease(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
	return
}

//BulletinList
//@router /list [post]
func (this *BulletinController) BulletinList() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.BulletinListRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rsp, err := bulletin.BulletinList(uid, companyId, request)
	msg = protocol.NewPageDataResponse(rsp, err)
}

//GetBulletin
//@router /get [post]
func (this *BulletinController) GetBulletin() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.GetBulletinRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rsp, err := bulletin.GetBulletin(request.Id, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//UpdateBulletin
//@router /update [post]
func (this *BulletinController) UpdateBulletin() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.UpdateBulletinRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rsp, err := bulletin.UpdateBulletin(companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//OperateBulletin
//@router /operateBulletin [post]
func (this *BulletinController) OperateBulletin() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.OperateBulletinRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rsp, err := bulletin.OperateBulletin(companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//BulletinFeedbacks 反馈详情
//@router /bulletinFeedbacks [post]
func (this *BulletinController) BulletinFeedbacks() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.BulletinFeedbacksRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rsp, err := bulletin.BulletinFeedbacks(companyId, request)
	msg = protocol.NewPageDataResponse(rsp, err)
}

//ExportFeedBacks
//@router /exportFeedBacks [post]
func (this *BulletinController) ExportFeedBacks() {
	var msg *protocol.ResponseMessage
	defer func() {
		if msg.Errno != 0 {
			this.ResposeJson(msg)
		}
	}()
	var request *protocol.ExportFeedBacksRequest
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	excel, err := bulletin.ExportFeedBacks(companyId, request)
	if err != nil {
		log.Error(err.Error())
		msg = protocol.BadRequestParam("1")
		return
	}
	//excel.FileName = fmt.Sprintf("商品列表%v.xlsx",time.Now().Format("2006-1-2 15:04:05"))
	if err = this.ResponseExcelByFile(this.Ctx, excel); err != nil {
		log.Error(err.Error())
		msg = protocol.BadRequestParam("1")
		return
	}
	msg = protocol.NewReturnResponse(nil, err)
}