123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package application
- import (
- "context"
- "encoding/json"
- "fmt"
- "property-system/errors"
- dbmodel "property-system/model"
- "property-system/parser"
- "property-system/pb"
- pb_v1 "property-system/pb/v1"
- "property-system/utils"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func checkApplicationOrderAddParam(req *pb_v1.ApplicationOrderAddRequest) error {
- switch {
- case req.GardenId < 1:
- return status.Error(10003, "小区不能为空")
- case req.ApplicationId < 1:
- return status.Error(10003, "应用不能为空")
- }
- return nil
- }
- //
- func ApplicationOrderAdd(ctx context.Context, req *pb_v1.ApplicationOrderAddRequest) (reply *pb_v1.ApplicationOrderAddReply, err error) {
- reply = &pb_v1.ApplicationOrderAddReply{}
- // 捕获各个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 = checkApplicationOrderAddParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- p := dbmodel.TApplicationOrder{}
- where := map[string]interface{}{
- "garden_id": req.GardenId,
- "application_id": req.ApplicationId,
- "status": 2,
- }
- count, err := p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if count > 0 {
- return nil, status.Error(10003, "应用已存在")
- }
- mreq := pb_v1.ApplicationInfoRequest{Id: req.ApplicationId}
- mreply, err := pb.Common.ApplicationInfo(ctx, &mreq)
- if err != nil {
- return nil, err
- }
- p.CreatedAt = now
- p.UpdatedAt = now
- p.ApplicationId = req.ApplicationId
- p.GardenId = req.GardenId
- p.Amount = mreply.Info.Price
- p.Cid = req.Cid
- p.Status = 1
- p.PayType = 1
- bytes, _ := json.Marshal(mreply.Info)
- p.ApplicationContent = string(bytes)
- db := database.DB().Begin()
- err = p.Insert(db)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- // 价格为0 直接通过认证?
- /*if mreply.Info.Price == 0 {
- mreq := pb_v1.ApplicationOrderApproveRequest{OrderId: p.ID, Status: true, Feedback: "价格为零直接认证"}
- err = ApproveHandle(&mreq, db)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- }*/
- db.Commit()
- reply.OrderId = p.ID
- reply.ApplicationDesc = mreply.Info.Desc
- reply.Amount = p.Amount
- reply.ApplicationName = mreply.Info.Name
- reply.PayType = 1
- reply.BankName = parser.Conf.Bank.BankName
- reply.BankAccount = parser.Conf.Bank.BankAccount
- reply.BankUserName = parser.Conf.Bank.BankUserName
- utils.RobotMsg(fmt.Sprintf("有新的应用订单,请关注"))
- return reply, nil
- }
|