// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package suggestion import ( "context" "encoding/json" "fmt" "property-garden/errors" "property-garden/impl/v1/oss_utils" "property-garden/impl/v1/statistic" "property-garden/impl/v1/system_msg" dbmodel "property-garden/model" "property-garden/pb" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) const ( SuggestionOrderStatusCreate = 1 SuggestionOrderStatusSend = 2 SuggestionOrderStatusFinish = 3 ) const ( SuggestionPipelineStatusCreate = 1 SuggestionPipelineStatusSend = 2 SuggestionPipelineStatusFinish = 3 SuggestionPipelineStatusBack = 4 SuggestionPipelineStatusResend = 5 ) var SuggestionOrderStatusM = map[int32]string{ SuggestionOrderStatusCreate: "未派单", SuggestionOrderStatusSend: "已派单", SuggestionOrderStatusFinish: "已完结", } var SuggestionPipelineStatusM = map[int32]string{ SuggestionPipelineStatusCreate: "未派单", SuggestionPipelineStatusSend: "已派单", SuggestionPipelineStatusFinish: "已完结", SuggestionPipelineStatusBack: "退单", SuggestionPipelineStatusResend: "转单", } type SuggestionOrderPipelineData struct { // 处理意见 Feedback string `json:"feedback"` // 处理时间 HandleTime string `json:"handle_time"` LastUid int64 `json:"last_uid"` CurrentUid int64 `json:"current_uid"` // 上级处理人是公司账户 LastByCompany bool `json:"last_by_company"` Status int32 `json:"status"` } func checkSuggestionOrderAddParam(req *pb_v1.SuggestionOrderAddRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.SuggestionType < 1: return status.Error(10003, "投诉类型不能为空") case req.ApplyPeople == "": return status.Error(10003, "投诉人不能为空") case req.ApplyPeoplePhone != "": if !utils.VerifyMobileFormat(req.ApplyPeoplePhone) { return status.Error(10003, "手机格式错误") } case req.ApplyContent == "": return status.Error(10003, "投诉内容不能为空") } return nil } type UserInfo struct { Name string Phone string } func getUserInfos(ids []int64, gardenId int64) (map[int64]UserInfo, error) { ret := map[int64]UserInfo{} if len(ids) == 0 { return ret, nil } mreq := pb_v1.UserListRequest{ GardenId: gardenId, Ids: ids, PageSize: -1, Page: -1, } mreply, err := pb.System.UserList(context.Background(), &mreq) if err != nil { return ret, err } for _, v := range mreply.List { ret[v.Id] = UserInfo{Name: v.RealName, Phone: v.Phone} } for _, v := range ids { if _, ok := ret[v]; !ok { ret[v] = UserInfo{} } } return ret, nil } func SuggestionOrderAdd(ctx context.Context, req *pb_v1.SuggestionOrderAddRequest) (reply *pb_v1.SuggestionOrderAddReply, err error) { reply = &pb_v1.SuggestionOrderAddReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() // 参数检查 err = checkSuggestionOrderAddParam(req) if err != nil { return nil, err } now := time.Now() dbname := utils.GetGardenDbName(req.GardenId) pipeline := SuggestionOrderPipelineData{ LastUid: req.LastUid, HandleTime: now.Format("2006-01-02 15:04:05"), Status: SuggestionPipelineStatusCreate, LastByCompany: req.ByCompany, } pipeArray := []SuggestionOrderPipelineData{pipeline} bytes, _ := json.Marshal(pipeArray) p := dbmodel.TSuggestionOrder{ SuggestionType: req.SuggestionType, ApplyPeople: req.ApplyPeople, ApplyPeoplePhone: req.ApplyPeoplePhone, ApplyContent: req.ApplyContent, ApplyPic: utils.StringJoin(req.ApplyPic, ";"), LastUid: req.LastUid, Status: SuggestionOrderStatusCreate, Pipeline: string(bytes), CreatedAt: now, UpdatedAt: now, LastUidIsCompany: req.ByCompany, HouseholdUid: req.HouseholdUid, } p.SetTable(dbname) db := database.DB().Begin() err = p.Insert(db) if err != nil { db.Rollback() return nil, errors.DataBaseError } reply.Id = p.ID mreq := pb_v1.SystemMsgAddRequest{ GardenId: req.GardenId, Content: "新的投诉建议", Code: system_msg.SystemMsgCodeSuggestionOrder, } statisticReq := pb_v1.RepairStatisticSetRequest{ HandleType: statistic.HandleTypeSuggestion, TotalIncrease: int64(1), GardenId: req.GardenId, } err = statistic.RepairStatisticSetWithDb(&statisticReq, db) if err != nil { db.Rollback() return nil, err } if err := oss_utils.OssObjAdd(req.ApplyPic, nil); err != nil { db.Rollback() return nil, err } db.Commit() system_msg.SystemMsgAdd(ctx, &mreq) return reply, nil }