123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- // Copyright 2019 githup.com. All rights reserved.
- // Use of this source code is governed by githup.com.
- package provider
- import (
- "context"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/transtasker"
- "github.com/jinzhu/gorm"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- "smart-government-management/consts"
- "smart-government-management/errors"
- dbmodel "smart-government-management/model"
- "smart-government-management/pb"
- "smart-government-management/pb/v1"
- pb_v1 "smart-government-management/pb/v1"
- "time"
- )
- const (
- // 0 供应商入驻审批 1 设备对接审批 3 拆机审批
- ApproveTypeProvider = iota
- ApproveTypeDeviceDocking
- ApproveTypeDeviceDel
- )
- func providerApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
- p := &dbmodel.TProvider{}
- where := map[string]interface{}{
- "id": req.Id,
- }
- // 状态判断
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- logger.Error("providerApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- if p.Status != consts.ProviderStatusAddAuditting {
- return nil, errors.CanNotApproveWithStatus
- }
- // 更新
- where = map[string]interface{}{
- "id": req.Id,
- "status": consts.ProviderStatusAddAuditting,
- }
- value := map[string]interface{}{
- "status": consts.ProviderStatusAddAuditted,
- "approve_time": time.Now(),
- }
- if !req.Status {
- value["status"] = consts.ProviderStatusAddAudittedFail
- }
- value["feedback"] = req.Feedback
- err = p.Update(database.DB(), where, value)
- if err != nil {
- logger.Error("providerApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- go func() {
- mreq := pb_v1.SendThroughVerifyRequest{
- Company: p.Name,
- CompanyType: 1,
- PhoneNumber: p.BusinessContactPhone,
- Status: req.Status,
- ApproveTime: time.Now().Format("2006-01-02 15:04:05"),
- }
- pb.Thirdparty.SendThroughVerify(context.Background(), &mreq)
- }()
- return &pb_v1.ProviderApproveReply{}, nil
- }
- // 设备类型对接开发者审批
- func developDockingApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
- p := &dbmodel.TProviderDevice{}
- // 更新
- where := map[string]interface{}{
- "id": req.Id,
- "status": consts.DeviceTypeStatusAddTesting,
- }
- value := map[string]interface{}{
- "status": consts.DeviceTypeStatusAddTestted,
- "comment": req.Feedback,
- "develop_approve_time": time.Now(),
- }
- if !req.Status {
- value["status"] = consts.DeviceTypeStatusAddTesttedFail
- }
- err = p.Update(database.DB(), where, value)
- if err != nil {
- logger.Error("develop DockingApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- return &pb_v1.ProviderApproveReply{}, nil
- }
- // 设配类型对接政府审批
- func deviceDockingApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
- p := &dbmodel.TProviderDevice{}
- where := map[string]interface{}{
- "id": req.Id,
- }
- // 状态判断
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- logger.Error("deviceDockingApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- // 已审批未通过
- if p.Status == consts.DeviceTypeStatusAddTesttedFail || p.Status == consts.DeviceTypeStatusAddAudittedFail {
- return nil, errors.CanNotApproveWithStatus
- }
- // 开发者审批
- if req.IsDevelop {
- if p.Status != consts.DeviceTypeStatusAddTesting {
- return nil, errors.CanNotApproveWithStatus
- }
- return developDockingApprove(req)
- }
- // 政府审批
- if p.Status != consts.DeviceTypeStatusAddTestted {
- return nil, errors.CanNotApproveWithStatus
- }
- // 更新
- where = map[string]interface{}{
- "id": req.Id,
- "status": consts.DeviceTypeStatusAddTestted,
- }
- value := map[string]interface{}{
- "status": consts.DeviceTypeStatusAddAuditted,
- "approve_time": time.Now(),
- }
- if !req.Status {
- value["status"] = consts.DeviceTypeStatusAddAudittedFail
- }
- value["feedback"] = req.Feedback
- err = p.Update(database.DB(), where, value)
- if err != nil {
- logger.Error("deviceDockingApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- return &pb_v1.ProviderApproveReply{}, nil
- return nil, nil
- }
- /*
- func deviceAddApprove(req *pb_v1.ProviderApproveRequest)(reply *pb_v1.ProviderApproveReply, err error) {
- p := &dbmodel.TDevice{}
- where := map[string]interface{}{
- "id":req.Id,
- }
- // 状态判断
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- if p.VerifyStatus != consts.DeviceStatusAddProjectAuditted {
- return nil, errors.CanNotApproveWithStatus
- }
- // 更新
- where = map[string]interface{}{
- "id":req.Id,
- "verify_status":consts.DeviceStatusAddProjectAuditted,
- }
- value := map[string]interface{}{
- "verify_status":consts.DeviceStatusAddAuditted,
- "goverment_approve_time":time.Now(),
- }
- err = p.Update(database.DB(), where, value)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return &pb_v1.ProviderApproveReply{}, nil
- }
- */
- func deviceDelApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
- p := &dbmodel.TProjectJob{}
- where := map[string]interface{}{
- "id": req.Id,
- }
- // 状态判断
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- logger.Error("deviceDelApprove",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- if p.Status != consts.DeviceStatusDelAuditting {
- return nil, errors.CanNotApproveWithStatus
- }
- jobTask := func(db *gorm.DB) error {
- job := &dbmodel.TProjectJob{}
- where := map[string]interface{}{
- "id": req.Id,
- }
- status := 2
- if req.Status {
- status = 1
- }
- value := map[string]interface{}{
- "feedback": req.Feedback,
- "status": status,
- "updated_at": time.Now(),
- }
- err := job.Update(db, where, value)
- if err != nil {
- logger.Error("deviceDelApprove",
- zap.String("err", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
- delTask := func(db *gorm.DB) error {
- device := dbmodel.TDevice{}
- if !req.Status {
- return nil
- }
- where = map[string]interface{}{
- "id": p.DeviceId,
- }
- err = device.Del(db, where)
- if err != nil {
- logger.Error("deviceDelApprove",
- zap.String("err", err.Error()))
- return errors.DataBaseError
- }
- if gjson.GetBytes([]byte(p.Origin), "device_code").Int() == dbmodel.DeviceTypeVedio {
- video := dbmodel.Vssdevtbl{}
- where := map[string]interface{}{
- "DevPubID": gjson.GetBytes([]byte(p.Origin), "sn").String(),
- }
- err = video.Del(db, where)
- if err != nil {
- logger.Error("deviceDelApprove",
- zap.String("err", err.Error()))
- return errors.DataBaseError
- }
- channel := dbmodel.Vsschanneltbl{}
- err = channel.Del(db, where)
- if err != nil {
- logger.Error("deviceDelApprove",
- zap.String("err", err.Error()))
- return errors.DataBaseError
- }
- }
- return nil
- }
- notifyTask := func(db *gorm.DB) error {
- mreq := v1.DeviceChangeNotifyRequest{
- DeviceCode: int32(gjson.GetBytes([]byte(p.Origin), "device_code").Int()),
- Sn: gjson.GetBytes([]byte(p.Origin), "sn").String(),
- }
- _, err := pb.Auth.DeviceChangeNotify(context.Background(), &mreq)
- if err != nil {
- logger.Error("deviceDelApprove notify",
- zap.String("err", err.Error()))
- return errors.DataBaseError
- return err
- }
- return nil
- }
- err = transtasker.Exec(database.DB(),
- transtasker.NewTask(jobTask),
- transtasker.NewTask(delTask), transtasker.NewTask(notifyTask))
- // 更新
- return &pb_v1.ProviderApproveReply{ProjectId: p.ProjectId}, err
- }
- func ProviderApprove(ctx context.Context, req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
- switch req.ApproveType {
- case ApproveTypeProvider:
- return providerApprove(req)
- case ApproveTypeDeviceDocking:
- return deviceDockingApprove(req)
- case ApproveTypeDeviceDel:
- return deviceDelApprove(req)
- }
- return nil, errors.ParamsError
- }
|