作者 yangfu

评论,我的评论,机会评论列表

### 接口完成进度
|功能|完成状态|时间|接口路径|人员
|---|---|----|----|---|
|登录|完成|2019.10.20|/v1/auth/login|
|短信验证码|完成|2019.10.20|/v1/auth/smsCode|
|获取token|完成|2019.10.20|/v1/auth/accessToken|
|刷新token|完成|2019.10.20|/v1/auth/refreshToken|
|修改手机号-校验验证码|完成|2019.11.20|/v1/user/checkSmsCode|
|修改手机号|完成|2019.11.20|/v1/user/changePhone|
|修改密码|完成|2019.11.20|v1/user/changePassword|
|忘记密码|完成|2019.11.20|v1/user/resetPassword|
|机会评论列表| |2019.11.|/v1/chance/comments|
|我来评论| |2019.11.|/v1/chance/iComment|
|我的评论| |2019.11.|/v1/chance/iComments|
|机会评论列表|完成|2019.11.21|/v1/chance/comments|
|我来评论|完成|2019.11.21|/v1/chance/iComment|
|我的评论|完成|2019.11.21|/v1/chance/iComments|
|我的点赞| |2019.11.| |
|我的收藏| |2019.11.| |
|我提交的机会| |2019.11.| |
... ...
package v1
import (
"encoding/json"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/controllers"
"opp/protocol"
"opp/services/chance"
)
type ChanceController struct {
controllers.BaseController
}
//IComment
// @router /iComment [post]
func (this *ChanceController) IComment() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.ICommentRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(chance.IComment(header, request))
}
//IComments
// @router /iComments [post]
func (this *ChanceController) IComments() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.ICommentsRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(chance.IComments(header, request))
}
//Comments
// @router /comments [post]
func (this *ChanceController) Comments() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.CommentsRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(chance.Comments(header, request))
}
... ...
package repository
import "opp/models"
type IChanceRepository interface {
GetChanceById(id int64) (v *models.Chance, err error)
}
var _ IChanceRepository = (*ChanceRepository)(nil)
type ChanceRepository struct{}
func (r *ChanceRepository) GetChanceById(id int64) (v *models.Chance, err error) {
return models.GetChanceById(id)
}
... ...
package repository
import "opp/models"
type ICommentRepository interface {
GetCommentById(id int64) (v *models.Comment, err error)
AddComment(m *models.Comment) (id int64, err error)
GetComments(userId int64, sourceType int, sourceId int64, lastId int, pageSize int) (v []*models.Comment, total int, err error)
}
var _ ICommentRepository = (*CommentRepository)(nil)
type CommentRepository struct{}
func (r *CommentRepository) GetCommentById(id int64) (v *models.Comment, err error) {
return models.GetCommentById(id)
}
func (r *CommentRepository) AddComment(m *models.Comment) (id int64, err error) {
return models.AddComment(m)
}
func (r *CommentRepository) GetComments(userId int64, sourceType int, sourceId int64, lastId int, pageSize int) (v []*models.Comment, total int, err error) {
return models.GetComments(userId, sourceType, sourceId, lastId, pageSize)
}
... ...
... ... @@ -11,6 +11,8 @@ var (
Company ICompanyRepository
Department IDepartmentRepository
Position IPositionRepository
Chance IChanceRepository
Comment ICommentRepository
)
func init() {
... ... @@ -24,6 +26,8 @@ func InitRepository() {
Company = &CompanyRepository{}
Department = &DepartmentRepository{}
Position = &PositionRepository{}
Chance = &ChanceRepository{}
Comment = &CommentRepository{}
}
func InitRepositoryMock() {
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Chance struct {
Id int64 `orm:"column(id);pk" description:"id 主键"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户id"`
DepartmentId int64 `orm:"column(department_id)" description:"表department.id 部门id"`
ChanceTypeId int `orm:"column(chance_type_id)" description:"表cfg_chance_type.id 机会类型 1:产品 2:渠道 3.客户 4.区域 5.其他 "`
Content string `orm:"column(content)" description:"内容"`
CommentTotal int `orm:"column(comment_total)" description:"评论总数"`
FavoriteTotal int `orm:"column(favorite_total)" description:"点赞总数"`
ReviewStatus int8 `orm:"column(review_status)" description:"审核状态 0:待审核 1:被退回 2:已通过 3:草稿箱"`
EnableStatus int8 `orm:"column(enable_status)" description:"有效状态 0:无效 1:有效 "`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
}
func (t *Chance) TableName() string {
return "chance"
}
func init() {
orm.RegisterModel(new(Chance))
}
// AddChance insert a new Chance into database and returns
// last inserted Id on success.
func AddChance(m *Chance) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetChanceById retrieves Chance by Id. Returns error if
// Id doesn't exist
func GetChanceById(id int64) (v *Chance, err error) {
o := orm.NewOrm()
v = &Chance{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllChance retrieves all Chance matches certain condition. Returns empty list if
// no records exist
func GetAllChance(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Chance))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Chance
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateChance updates Chance by Id and returns error if
// the record to be updated doesn't exist
func UpdateChanceById(m *Chance) (err error) {
o := orm.NewOrm()
v := Chance{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteChance deletes Chance by Id and returns error if
// the record to be deleted doesn't exist
func DeleteChance(id int64) (err error) {
o := orm.NewOrm()
v := Chance{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Chance{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"reflect"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type Comment struct {
Id int64 `orm:"column(id);pk" description:"评论编号"`
UserId int64 `orm:"column(user_id)" description:"表user.id "`
Content string `orm:"column(content);size(1024)" description:"评论内容"`
ViewTotal int `orm:"column(view_total)" description:"浏览总数"`
ZanTotal int `orm:"column(zan_total)" description:"点赞总数"`
CommentTotal int `orm:"column(comment_total)" description:"评论总数"`
SourceType int8 `orm:"column(source_type)" description:"来源类型 1:机会 2:评论"`
SourceId int64 `orm:"column(source_id)" description:"来源唯一编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
}
func (t *Comment) TableName() string {
return "comment"
}
func init() {
orm.RegisterModel(new(Comment))
}
// AddComment insert a new Comment into database and returns
// last inserted Id on success.
func AddComment(m *Comment) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetCommentById retrieves Comment by Id. Returns error if
// Id doesn't exist
func GetCommentById(id int64) (v *Comment, err error) {
o := orm.NewOrm()
v = &Comment{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllComment retrieves all Comment matches certain condition. Returns empty list if
// no records exist
func GetAllComment(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Comment))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Comment
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdateComment updates Comment by Id and returns error if
// the record to be updated doesn't exist
func UpdateCommentById(m *Comment) (err error) {
o := orm.NewOrm()
v := Comment{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteComment deletes Comment by Id and returns error if
// the record to be deleted doesn't exist
func DeleteComment(id int64) (err error) {
o := orm.NewOrm()
v := Comment{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&Comment{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetComments(userId int64, sourceType int, sourceId int64, lastId int, pageSize int) (v []*Comment, total int, err error) {
sql := mybeego.NewSqlExutor().Table("comment").Order("create_at desc")
if userId > 0 {
sql.Where(fmt.Sprintf("user_id=%d", userId))
}
if sourceId > 0 {
sql.Where(fmt.Sprintf("source_id=%d", sourceId))
}
if sourceType > 0 {
sql.Where(fmt.Sprintf("source_type=%d", sourceType))
}
if pageSize > 0 {
sql.Limit(0, pageSize)
}
if lastId > 0 {
sql.Where(fmt.Sprintf("id>%d", lastId))
}
if total, err = sql.Querys(&v); err == nil {
return
}
return
}
... ...
... ... @@ -12,14 +12,14 @@ type CommendResponse struct {
}
type CommendInfo struct {
Id int64 `json:"cid"` //表彰编号
Content string `json:"content"` //表彰内容
Company string `json:"company"` //公司名
CommendAt int64 `json:"getTime"` //表彰时间
Honored HonoredUserInfo `json:"honored"` //证书获得者 信息
Id int64 `json:"cid"` //表彰编号
Content string `json:"content"` //表彰内容
Company string `json:"company"` //公司名
CommendAt int64 `json:"getTime"` //表彰时间
Honored BaseUserInfo `json:"honored"` //证书获得者 信息
}
type HonoredUserInfo struct {
type BaseUserInfo struct {
UserId int64 `json:"uid"` //用户id
NickName string `json:"uname"` //用户名
Department string `json:"department"` //部门
... ...
package protocol
const (
SourceType_Chance = 1
SourceType_Comment = 2
)
/*IComment */
type ICommentRequest struct {
Content string `json:"content" valid:"Required"`
SourceType int `json:"type" valid:"Required"`
Id int64 `json:"id" valid:"Required"`
}
type ICommentResponse struct {
Id int64 `json:"id"`
Provider *BaseUserInfo `json:"provider"`
CreateTime int64 `json:"createTime"`
Content string `json:"content"`
}
/*IComments */
type ICommentsRequest struct {
LastId int `json:"lastId"`
PageSize int `json:"pageSize" valid:"Required"`
}
type ICommentsResponse struct {
Comments []*IComments `json:"comments"`
Total int `json:"total"`
}
type IComments struct {
//question `json:"question"`
Comment *ICommentResponse `json:"comment"`
}
/*机会评论*/
type CommentsRequest struct {
LastId int `json:"lastId"`
PageSize int `json:"pageSize" valid:"Required"`
SourceId int64 `json:"id" valid:"Required"`
}
type CommentsResponse struct {
Total int `json:"total"`
Comments []*Comments `json:"comments"`
}
/*评论列表*/
type Comments struct {
Id int64 `json:"id"`
Provider *BaseUserInfo `json:"provider"`
Content string `json:"content"`
ViewTotal int `json:"pageView"`
CommentTotal int `json:"commentTotal"`
ZanTotal int `json:"zanTotal"`
CreateTime int64 `json:"createTime"`
}
... ...
... ... @@ -47,6 +47,30 @@ func init() {
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"],
beego.ControllerComments{
Method: "Comments",
Router: `/comments`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"],
beego.ControllerComments{
Method: "IComment",
Router: `/iComment`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:ChanceController"],
beego.ControllerComments{
Method: "IComments",
Router: `/iComments`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["opp/controllers/v1:CommendController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:CommendController"],
beego.ControllerComments{
Method: "Company",
... ...
... ... @@ -17,6 +17,7 @@ func init() {
beego.NSNamespace("version", beego.NSInclude(&v1.VersionController{})),
beego.NSNamespace("commend", beego.NSInclude(&v1.CommendController{})),
beego.NSNamespace("user", beego.NSInclude(&v1.UserController{})),
beego.NSNamespace("chance", beego.NSInclude(&v1.ChanceController{})),
)
beego.AddNamespace(nsV1)
beego.SetStaticPath("/file/ab", beego.AppConfig.String("source_path"))
... ...
... ... @@ -7,7 +7,7 @@ import (
"sync"
)
func GetUserBaseInfo(uid int64) (v *protocol.UserBaseInfoAggregation, err error) {
func GetUserBaseInfoAggregation(uid int64) (v *protocol.UserBaseInfoAggregation, err error) {
v = &protocol.UserBaseInfoAggregation{}
var wg sync.WaitGroup
if v.User, err = repository.User.GetUsersById(uid); err != nil {
... ... @@ -42,3 +42,19 @@ func GetUserBaseInfo(uid int64) (v *protocol.UserBaseInfoAggregation, err error)
wg.Wait()
return
}
func GetUserBaseInfo(uid int64) (v *protocol.BaseUserInfo, err error) {
var (
agg *protocol.UserBaseInfoAggregation
)
if agg, err = GetUserBaseInfoAggregation(uid); err != nil {
return
}
v = &protocol.BaseUserInfo{
UserId: agg.User.Id,
NickName: agg.User.NickName,
Department: agg.Department.Name,
Position: agg.Position.Name,
}
return
}
... ...
package chance
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/identity/idgen"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/internal/repository"
"opp/internal/utils"
"opp/models"
"opp/protocol"
"opp/services/agg"
"time"
)
//我来评论
func IComment(header *protocol.RequestHeader, request *protocol.ICommentRequest) (rsp *protocol.ICommentResponse, err error) {
var (
comment *models.Comment
baseUserInfo *protocol.BaseUserInfo
chance *models.Chance
)
switch request.SourceType {
case protocol.SourceType_Chance:
if chance, err = repository.Chance.GetChanceById(request.Id); err != nil {
log.Error(err)
return
}
case protocol.SourceType_Comment:
if _, err = repository.Comment.GetCommentById(request.Id); err != nil {
log.Error(err)
return
}
default:
err = fmt.Errorf("unknow source_type:%v", request.SourceType)
}
comment = &models.Comment{
Id: idgen.Next(),
UserId: header.Uid,
SourceType: int8(request.SourceType),
Content: request.Content,
CreateAt: time.Now(),
SourceId: request.Id,
}
if _, err = repository.Comment.AddComment(comment); err != nil {
log.Error(err)
return
}
if baseUserInfo, err = agg.GetUserBaseInfo(header.Uid); err != nil {
log.Error(err)
return
}
if chance != nil {
//TODO:sql更新
utils.UpdateTableByMap(chance, map[string]interface{}{"CommentTotal": chance.CommentTotal + 1})
}
rsp = &protocol.ICommentResponse{
Id: comment.Id,
Content: comment.Content,
CreateTime: comment.CreateAt.Unix(),
Provider: baseUserInfo,
}
return
}
//我的评论
func IComments(header *protocol.RequestHeader, request *protocol.ICommentsRequest) (rsp *protocol.ICommentsResponse, err error) {
var (
comments []*models.Comment
baseUserInfo *protocol.BaseUserInfo
total int
)
if comments, total, err = repository.Comment.GetComments(header.Uid, protocol.SourceType_Chance, 0, request.LastId, request.PageSize); err != nil {
log.Error(err)
return
}
rsp = &protocol.ICommentsResponse{
Total: total,
}
if baseUserInfo, err = agg.GetUserBaseInfo(header.Uid); err != nil {
log.Error(err)
return
}
rsp.Comments = make([]*protocol.IComments, len(comments))
for i := range comments {
comment := comments[i]
item := &protocol.IComments{
//TODO: 机会详情
//机会详情
//评论
Comment: &protocol.ICommentResponse{
Id: comment.Id,
Content: comment.Content,
CreateTime: comment.CreateAt.Unix(),
Provider: baseUserInfo,
},
}
rsp.Comments[i] = item
}
return
}
//机会评论列表
func Comments(header *protocol.RequestHeader, request *protocol.CommentsRequest) (rsp *protocol.CommentsResponse, err error) {
var (
comments []*models.Comment
baseUserInfo *protocol.BaseUserInfo
total int
)
if comments, total, err = repository.Comment.GetComments(header.Uid, protocol.SourceType_Chance, request.SourceId, request.LastId, request.PageSize); err != nil {
log.Error(err)
return
}
rsp = &protocol.CommentsResponse{
Total: total,
}
for i := range comments {
comment := comments[i]
if baseUserInfo, err = agg.GetUserBaseInfo(comment.UserId); err != nil {
log.Error(err)
//return
}
item := &protocol.Comments{
Id: comment.Id,
Provider: baseUserInfo,
Content: comment.Content,
CreateTime: comment.CreateAt.Unix(),
ViewTotal: comment.ViewTotal,
ZanTotal: comment.ZanTotal,
CommentTotal: comment.CommentTotal,
}
rsp.Comments = append(rsp.Comments, item)
}
return
}
... ...
... ... @@ -24,7 +24,7 @@ func Commend(header *protocol.RequestHeader, request *protocol.CommendRequest) (
}
for i := 0; i < len(commends); i++ {
c := commends[i]
userBaseInfo, err = agg.GetUserBaseInfo(c.UserId)
userBaseInfo, err = agg.GetUserBaseInfoAggregation(c.UserId)
if err != nil {
continue
}
... ... @@ -34,7 +34,7 @@ func Commend(header *protocol.RequestHeader, request *protocol.CommendRequest) (
Content: c.Content,
Company: userBaseInfo.Company.Name,
CommendAt: time.GetUnixTimeByNDayUnix(c.CommendAt.Unix(), 0),
Honored: protocol.HonoredUserInfo{
Honored: protocol.BaseUserInfo{
UserId: c.UserId,
NickName: user.NickName,
Department: userBaseInfo.Department.Name,
... ...