123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package repair
- import (
- "context"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/house"
- "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 (
- RepairOrderStatusCreate = 1
- RepairOrderStatusSend = 2
- RepairOrderStatusFinish = 3
- )
- const (
- RepairPipelineStatusCreate = 1
- RepairPipelineStatusSend = 2
- RepairPipelineStatusFinish = 3
- RepairPipelineStatusBack = 4
- RepairPipelineStatusResend = 5
- )
- var RepairOrderStatusM = map[int32]string{
- RepairOrderStatusCreate: "未派单",
- RepairOrderStatusSend: "已派单",
- RepairOrderStatusFinish: "已完结",
- }
- var RepairPipelineStatusM = map[int32]string{
- RepairPipelineStatusCreate: "未派单",
- RepairPipelineStatusSend: "已派单",
- RepairPipelineStatusFinish: "已完结",
- RepairPipelineStatusBack: "退单",
- RepairPipelineStatusResend: "转单",
- }
- type RepairOrderPipelineData 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 checkRepairOrderAddParam(req *pb_v1.RepairOrderAddRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.ClassId < 1:
- return status.Error(10003, "报修类型不能为空")
- case req.ApplyPeople == "":
- return status.Error(10003, "报修人不能为空")
- case req.ApplyPeoplePhone == "" || !utils.VerifyMobileFormat(req.ApplyPeoplePhone):
- return status.Error(10003, "手机格式错误")
- case req.ApplyContent == "":
- return status.Error(10003, "报修内容不能为空")
- }
- return nil
- }
- func getHouseInfo(id int64, gardenId int64) (houseInfo *pb_v1.HouseItem, err error) {
- // 获取房屋信息
- mreq := &pb_v1.HouseListRequest{
- GardenId: gardenId,
- HouseId: id,
- PageSize: -1,
- Page: -1,
- }
- mreply, err := house.HouseList(context.Background(), mreq)
- if err != nil {
- return nil, err
- }
- if len(mreply.List) == 0 {
- return nil, status.Error(10003, "房屋未录入")
- }
- return mreply.List[0], 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 RepairOrderAdd(ctx context.Context, req *pb_v1.RepairOrderAddRequest) (reply *pb_v1.RepairOrderAddReply, err error) {
- reply = &pb_v1.RepairOrderAddReply{}
- // 捕获各个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 = checkRepairOrderAddParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- dbname := utils.GetGardenDbName(req.GardenId)
- class := dbmodel.NewRepairClass(dbname)
- where := map[string]interface{}{
- "id": req.ClassId,
- }
- err = class.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if class.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if class.Enable == 2 {
- return nil, status.Error(10003, "报修类型未启用")
- }
- pipeline := RepairOrderPipelineData{
- LastUid: req.LastUid,
- HandleTime: now.Format("2006-01-02 15:04:05"),
- Status: RepairPipelineStatusCreate,
- LastByCompany: req.ByCompany,
- }
- pipeArray := []RepairOrderPipelineData{pipeline}
- bytes, _ := json.Marshal(pipeArray)
- p := dbmodel.TRepairOrder{
- ClassId: req.ClassId,
- ApplyPeople: req.ApplyPeople,
- ApplyPeoplePhone: req.ApplyPeoplePhone,
- Appointment: time.Unix(req.Appointment, 0),
- ApplyContent: req.ApplyContent,
- ApplyPic: utils.StringJoin(req.ApplyPic, ";"),
- LastUid: req.LastUid,
- Status: RepairOrderStatusCreate,
- Pipeline: string(bytes),
- CreatedAt: now,
- UpdatedAt: now,
- LastUidIsCompany: req.ByCompany,
- HouseholdUid: req.HouseholdUid,
- }
- if req.HouseId > 0 {
- houseInfo, err := getHouseInfo(req.HouseId, req.GardenId)
- if err != nil {
- return nil, err
- }
- p.HouseId = req.HouseId
- p.BuildingId = houseInfo.BuildingId
- p.UnitId = houseInfo.UnitId
- p.HouseName = houseInfo.HouseName
- }
- 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.SystemMsgCodeRepairOrder,
- }
- statisticReq := pb_v1.RepairStatisticSetRequest{
- HandleType: statistic.HandleTypeRepair,
- 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
- }
|