作者 yangfu

反馈详情导出

... ... @@ -5,9 +5,11 @@ import (
"fmt"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/utils/exceltool"
"strconv"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
)
... ... @@ -85,3 +87,16 @@ func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.R
return
}
func (this *BaseController) ResponseExcelByFile(ctx *context.Context, excelMaker *exceltool.ExcelMaker) error {
ctx.Output.Header("Content-Disposition", "attachment; filename="+excelMaker.FileName)
ctx.Output.Header("Content-Description", "File Transfer")
ctx.Output.Header("Content-Type", "application/octet-stream")
ctx.Output.Header("Content-Transfer-Encoding", "binary")
ctx.Output.Header("Expires", "0")
ctx.Output.Header("Cache-Control", "must-revalidate")
ctx.Output.Header("Pragma", "public")
//跳过保存文件,直接写入ctx.ResponseWriter
excelMaker.Xlsx.Write(ctx.ResponseWriter)
return nil
}
... ...
... ... @@ -175,3 +175,39 @@ func (this *BulletinController) BulletinFeedbacks() {
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() {
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
}
if err = this.ResponseExcelByFile(this.Ctx, excel); err != nil {
log.Error(err.Error())
msg = protocol.BadRequestParam("1")
return
}
msg = protocol.NewReturnResponse(nil, err)
}
... ...
... ... @@ -86,17 +86,16 @@ select answer,update_at,uid from bulletin_question_answer where bulletin_id =?
sqlCount += where
sql += where
sql += ` limit ?,?`
if err = utils.ExecuteQueryOne(&total, sqlCount, id); err != nil {
return
}
if page > 0 {
sql += fmt.Sprintf(` limit %v,%v`, (page-1)*pageSize, pageSize)
}
if v != nil {
if err = utils.ExecuteQueryOne(&total, sqlCount, id); err != nil {
if err = utils.ExecuteQueryAll(v, sql, id); err != nil {
return
}
}
if page == 0 {
page = 1
}
if err = utils.ExecuteQueryAll(v, sql, id, (page-1)*pageSize, pageSize); err != nil {
return
}
return
}
... ...
... ... @@ -128,3 +128,10 @@ type Answer struct {
VoteResults []int `json:"voteResults"`
EditContent string `json:"editContent"`
}
/*ExportFeedBacks */
type ExportFeedBacksRequest struct {
Id int `json:"id"` //公告编号
}
type ExportFeedBacksResponse struct {
}
... ...
... ... @@ -53,6 +53,7 @@ func init() {
beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"),
beego.NSRouter("/operate", &controllers.BulletinController{}, "post:OperateBulletin"),
beego.NSRouter("/feedbacks", &controllers.BulletinController{}, "post:BulletinFeedbacks"),
beego.NSRouter("/exportFeedBacks", &controllers.BulletinController{}, "post:ExportFeedBacks"),
),
beego.NSNamespace("/common",
beego.NSRouter("/department", &controllers.CommonController{}, "post:SelectorDepartment"),
... ...
... ... @@ -9,6 +9,7 @@ import (
"oppmg/protocol"
"oppmg/services/agg"
"oppmg/utils"
"oppmg/utils/exceltool"
"strconv"
"strings"
"time"
... ... @@ -452,3 +453,37 @@ func BulletinFeedbacks(companyId int64, request *protocol.BulletinFeedbacksReque
}
return
}
//导出反馈详情
func ExportFeedBacks(companyId int64, request *protocol.ExportFeedBacksRequest) (excel *exceltool.ExcelMaker, err error) {
var (
feedbackRsp *protocol.BulletinFeedbacksResponse
sourceData []map[string]string
)
if feedbackRsp, err = BulletinFeedbacks(companyId, &protocol.BulletinFeedbacksRequest{Id: request.Id}); err != nil {
//if err == orm2.ErrNoRows {
// err = nil
//}
//无数据
return
}
excel = exceltool.NewExcelMaker()
headTitle := [][]string{
[]string{"time", "时间"},
[]string{"name", "姓名"},
[]string{"content", "选项"},
}
for i := range feedbackRsp.List {
item := feedbackRsp.List[i]
row := make(map[string]string)
row["time"] = time.Unix(item.Time, 0).Format("2006-01-02 15:04:05")
row["name"] = item.Name
row["content"] = item.Content
sourceData = append(sourceData, row)
}
if err = excel.MakeListExcel(sourceData, headTitle); err != nil {
log.Error(err.Error())
return
}
return
}
... ...
... ... @@ -39,10 +39,11 @@ func (e *ExcelMaker) MakeListExcel(sourData []map[string]string, xlsxHeader [][]
}
headEn = append(headEn, xlsxHeader[key][0])
headCn = append(headCn, xlsxHeader[key][1])
alpha, err := excelize.ColumnNumberToName(key)
if err != nil {
return err
}
//alpha, err := excelize.ColumnNumberToName(key)
//if err != nil {
// return err
//}
alpha := ToAlphaString(key)
alphaSlice = append(alphaSlice, alpha)
}
... ... @@ -119,3 +120,16 @@ func GetRandomString(lenght int) string {
}
return string(result)
}
func ToAlphaString(value int) string {
if value < 0 {
return ""
}
var ans string
i := value + 1
for i > 0 {
ans = string((i-1)%26+65) + ans
i = (i - 1) / 26
}
return ans
}
... ...