// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package project import ( "fmt" "context" "encoding/json" "github.com/jaryhe/gopkgs/database" "github.com/jinzhu/gorm" "smart-site-management/errors" "smart-site-management/model" "smart-site-management/pb/v1" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" ) func makeProjectChangeInfoContent(data []byte)([]*v1.ProjectInfoItem) { m := map[string]interface{}{} if len(data) == 0 { return nil } json.Unmarshal(data, &m) ret := make([]*v1.ProjectInfoItem, len(m)) i := 0 for k, v := range m { item := &v1.ProjectInfoItem{ Name:k, Chinese:projectFieldMap[k], Value:fmt.Sprintf("%v", v), } ret[i] = item i++ } return ret } func ProjectChangeInfoList(ctx context.Context, req *v1.ProjectChangeInfoListRequest) (reply *v1.ProjectChangeInfoListReply, err error) { p := model.TProjectJob{} where := map[string]interface{}{ "project_id":req.Id, "type":model.ProjectJobTypeUpdateProject, } total, err := p.Count(database.DB(), where) if err != nil { logger.Error("ProjectChangeInfoList", zap.String("err", err.Error())) return nil, errors.DataBaseError } project := model.TProject{} err = project.Find(database.DB(), map[string]interface{}{"id":req.Id}) if err != nil { logger.Error("ProjectChangeInfoList", zap.String("err", err.Error())) return nil, errors.DataBaseError } if project.Id == 0 { return nil, errors.NoRecordError } reply = &v1.ProjectChangeInfoListReply{ Total:total, Page:req.Page, PageSize:int32(model.PageSize), } if total == 0 { return reply, nil } list, err := p.List(database.DB(), where, int(req.Page)) if err != nil && err != gorm.ErrRecordNotFound { logger.Error("ProjectChangeInfoList", zap.String("err", err.Error())) return nil, errors.DataBaseError } reply.List = make([]*v1.ProjectChangeInfoItem, len(list)) for i, v := range list { reply.List[i] = &v1.ProjectChangeInfoItem{} reply.List[i].Status = v.Status reply.List[i].ApplyTime = v.CreatedAt.Format(layout) if v.Status > 0 { reply.List[i].ApproveTime = v.UpdatedAt.Format(layout) } reply.List[i].Origin = makeProjectChangeInfoContent([]byte(v.Origin)) reply.List[i].Dst = makeProjectChangeInfoContent([]byte(v.Content)) reply.List[i].Name = project.Name reply.List[i].SafetyRecordNo = project.SafetyRecordNo reply.List[i].Addr = project.Location } return reply, nil }