approve.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package provider
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jaryhe/gopkgs/tasker/transtasker"
  9. "github.com/jinzhu/gorm"
  10. "github.com/tidwall/gjson"
  11. "go.uber.org/zap"
  12. "smart-government-management/consts"
  13. "smart-government-management/errors"
  14. dbmodel "smart-government-management/model"
  15. "smart-government-management/pb"
  16. "smart-government-management/pb/v1"
  17. pb_v1 "smart-government-management/pb/v1"
  18. "time"
  19. )
  20. const (
  21. // 0 供应商入驻审批 1 设备对接审批 3 拆机审批
  22. ApproveTypeProvider = iota
  23. ApproveTypeDeviceDocking
  24. ApproveTypeDeviceDel
  25. )
  26. func providerApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
  27. p := &dbmodel.TProvider{}
  28. where := map[string]interface{}{
  29. "id": req.Id,
  30. }
  31. // 状态判断
  32. err = p.Find(database.DB(), where)
  33. if err != nil && err != gorm.ErrRecordNotFound {
  34. logger.Error("providerApprove",
  35. zap.String("err", err.Error()))
  36. return nil, errors.DataBaseError
  37. }
  38. if p.Id == 0 {
  39. return nil, errors.NoRecordError
  40. }
  41. if p.Status != consts.ProviderStatusAddAuditting {
  42. return nil, errors.CanNotApproveWithStatus
  43. }
  44. // 更新
  45. where = map[string]interface{}{
  46. "id": req.Id,
  47. "status": consts.ProviderStatusAddAuditting,
  48. }
  49. value := map[string]interface{}{
  50. "status": consts.ProviderStatusAddAuditted,
  51. "approve_time": time.Now(),
  52. }
  53. if !req.Status {
  54. value["status"] = consts.ProviderStatusAddAudittedFail
  55. }
  56. value["feedback"] = req.Feedback
  57. err = p.Update(database.DB(), where, value)
  58. if err != nil {
  59. logger.Error("providerApprove",
  60. zap.String("err", err.Error()))
  61. return nil, errors.DataBaseError
  62. }
  63. go func() {
  64. mreq := pb_v1.SendThroughVerifyRequest{
  65. Company: p.Name,
  66. CompanyType: 1,
  67. PhoneNumber: p.BusinessContactPhone,
  68. Status: req.Status,
  69. ApproveTime: time.Now().Format("2006-01-02 15:04:05"),
  70. }
  71. pb.Thirdparty.SendThroughVerify(context.Background(), &mreq)
  72. }()
  73. return &pb_v1.ProviderApproveReply{}, nil
  74. }
  75. // 设备类型对接开发者审批
  76. func developDockingApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
  77. p := &dbmodel.TProviderDevice{}
  78. // 更新
  79. where := map[string]interface{}{
  80. "id": req.Id,
  81. "status": consts.DeviceTypeStatusAddTesting,
  82. }
  83. value := map[string]interface{}{
  84. "status": consts.DeviceTypeStatusAddTestted,
  85. "comment": req.Feedback,
  86. "develop_approve_time": time.Now(),
  87. }
  88. if !req.Status {
  89. value["status"] = consts.DeviceTypeStatusAddTesttedFail
  90. }
  91. err = p.Update(database.DB(), where, value)
  92. if err != nil {
  93. logger.Error("develop DockingApprove",
  94. zap.String("err", err.Error()))
  95. return nil, errors.DataBaseError
  96. }
  97. return &pb_v1.ProviderApproveReply{}, nil
  98. }
  99. // 设配类型对接政府审批
  100. func deviceDockingApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
  101. p := &dbmodel.TProviderDevice{}
  102. where := map[string]interface{}{
  103. "id": req.Id,
  104. }
  105. // 状态判断
  106. err = p.Find(database.DB(), where)
  107. if err != nil && err != gorm.ErrRecordNotFound {
  108. logger.Error("deviceDockingApprove",
  109. zap.String("err", err.Error()))
  110. return nil, errors.DataBaseError
  111. }
  112. if p.Id == 0 {
  113. return nil, errors.NoRecordError
  114. }
  115. // 已审批未通过
  116. if p.Status == consts.DeviceTypeStatusAddTesttedFail || p.Status == consts.DeviceTypeStatusAddAudittedFail {
  117. return nil, errors.CanNotApproveWithStatus
  118. }
  119. // 开发者审批
  120. if req.IsDevelop {
  121. if p.Status != consts.DeviceTypeStatusAddTesting {
  122. return nil, errors.CanNotApproveWithStatus
  123. }
  124. return developDockingApprove(req)
  125. }
  126. // 政府审批
  127. if p.Status != consts.DeviceTypeStatusAddTestted {
  128. return nil, errors.CanNotApproveWithStatus
  129. }
  130. // 更新
  131. where = map[string]interface{}{
  132. "id": req.Id,
  133. "status": consts.DeviceTypeStatusAddTestted,
  134. }
  135. value := map[string]interface{}{
  136. "status": consts.DeviceTypeStatusAddAuditted,
  137. "approve_time": time.Now(),
  138. }
  139. if !req.Status {
  140. value["status"] = consts.DeviceTypeStatusAddAudittedFail
  141. }
  142. value["feedback"] = req.Feedback
  143. err = p.Update(database.DB(), where, value)
  144. if err != nil {
  145. logger.Error("deviceDockingApprove",
  146. zap.String("err", err.Error()))
  147. return nil, errors.DataBaseError
  148. }
  149. return &pb_v1.ProviderApproveReply{}, nil
  150. return nil, nil
  151. }
  152. /*
  153. func deviceAddApprove(req *pb_v1.ProviderApproveRequest)(reply *pb_v1.ProviderApproveReply, err error) {
  154. p := &dbmodel.TDevice{}
  155. where := map[string]interface{}{
  156. "id":req.Id,
  157. }
  158. // 状态判断
  159. err = p.Find(database.DB(), where)
  160. if err != nil && err != gorm.ErrRecordNotFound {
  161. return nil, errors.DataBaseError
  162. }
  163. if p.Id == 0 {
  164. return nil, errors.NoRecordError
  165. }
  166. if p.VerifyStatus != consts.DeviceStatusAddProjectAuditted {
  167. return nil, errors.CanNotApproveWithStatus
  168. }
  169. // 更新
  170. where = map[string]interface{}{
  171. "id":req.Id,
  172. "verify_status":consts.DeviceStatusAddProjectAuditted,
  173. }
  174. value := map[string]interface{}{
  175. "verify_status":consts.DeviceStatusAddAuditted,
  176. "goverment_approve_time":time.Now(),
  177. }
  178. err = p.Update(database.DB(), where, value)
  179. if err != nil {
  180. return nil, errors.DataBaseError
  181. }
  182. return &pb_v1.ProviderApproveReply{}, nil
  183. }
  184. */
  185. func deviceDelApprove(req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
  186. p := &dbmodel.TProjectJob{}
  187. where := map[string]interface{}{
  188. "id": req.Id,
  189. }
  190. // 状态判断
  191. err = p.Find(database.DB(), where)
  192. if err != nil && err != gorm.ErrRecordNotFound {
  193. logger.Error("deviceDelApprove",
  194. zap.String("err", err.Error()))
  195. return nil, errors.DataBaseError
  196. }
  197. if p.Id == 0 {
  198. return nil, errors.NoRecordError
  199. }
  200. if p.Status != consts.DeviceStatusDelAuditting {
  201. return nil, errors.CanNotApproveWithStatus
  202. }
  203. jobTask := func(db *gorm.DB) error {
  204. job := &dbmodel.TProjectJob{}
  205. where := map[string]interface{}{
  206. "id": req.Id,
  207. }
  208. status := 2
  209. if req.Status {
  210. status = 1
  211. }
  212. value := map[string]interface{}{
  213. "feedback": req.Feedback,
  214. "status": status,
  215. "updated_at": time.Now(),
  216. }
  217. err := job.Update(db, where, value)
  218. if err != nil {
  219. logger.Error("deviceDelApprove",
  220. zap.String("err", err.Error()))
  221. return errors.DataBaseError
  222. }
  223. return nil
  224. }
  225. delTask := func(db *gorm.DB) error {
  226. device := dbmodel.TDevice{}
  227. if !req.Status {
  228. return nil
  229. }
  230. where = map[string]interface{}{
  231. "id": p.DeviceId,
  232. }
  233. err = device.Del(db, where)
  234. if err != nil {
  235. logger.Error("deviceDelApprove",
  236. zap.String("err", err.Error()))
  237. return errors.DataBaseError
  238. }
  239. if gjson.GetBytes([]byte(p.Origin), "device_code").Int() == dbmodel.DeviceTypeVedio {
  240. video := dbmodel.Vssdevtbl{}
  241. where := map[string]interface{}{
  242. "DevPubID": gjson.GetBytes([]byte(p.Origin), "sn").String(),
  243. }
  244. err = video.Del(db, where)
  245. if err != nil {
  246. logger.Error("deviceDelApprove",
  247. zap.String("err", err.Error()))
  248. return errors.DataBaseError
  249. }
  250. channel := dbmodel.Vsschanneltbl{}
  251. err = channel.Del(db, where)
  252. if err != nil {
  253. logger.Error("deviceDelApprove",
  254. zap.String("err", err.Error()))
  255. return errors.DataBaseError
  256. }
  257. }
  258. return nil
  259. }
  260. notifyTask := func(db *gorm.DB) error {
  261. mreq := v1.DeviceChangeNotifyRequest{
  262. DeviceCode: int32(gjson.GetBytes([]byte(p.Origin), "device_code").Int()),
  263. Sn: gjson.GetBytes([]byte(p.Origin), "sn").String(),
  264. }
  265. _, err := pb.Auth.DeviceChangeNotify(context.Background(), &mreq)
  266. if err != nil {
  267. logger.Error("deviceDelApprove notify",
  268. zap.String("err", err.Error()))
  269. return errors.DataBaseError
  270. return err
  271. }
  272. return nil
  273. }
  274. err = transtasker.Exec(database.DB(),
  275. transtasker.NewTask(jobTask),
  276. transtasker.NewTask(delTask), transtasker.NewTask(notifyTask))
  277. // 更新
  278. return &pb_v1.ProviderApproveReply{ProjectId: p.ProjectId}, err
  279. }
  280. func ProviderApprove(ctx context.Context, req *pb_v1.ProviderApproveRequest) (reply *pb_v1.ProviderApproveReply, err error) {
  281. switch req.ApproveType {
  282. case ApproveTypeProvider:
  283. return providerApprove(req)
  284. case ApproveTypeDeviceDocking:
  285. return deviceDockingApprove(req)
  286. case ApproveTypeDeviceDel:
  287. return deviceDelApprove(req)
  288. }
  289. return nil, errors.ParamsError
  290. }