wx_account_apply_list.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package company
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-company/errors"
  11. dbmodel "property-company/model"
  12. "property-company/pb"
  13. pb_v1 "property-company/pb/v1"
  14. )
  15. /*
  16. 1、APPLYMENT_STATE_EDITTING(编辑中):提交申请发生错误导致,请尝试重新提交。
  17. 2、APPLYMENT_STATE_AUDITING(审核中):申请单正在审核中,超级管理员用微信打开“签约链接”,完成绑定微信号后,申请单进度将通过微信公众号通知超级管理员,引导完成后续步骤。
  18. 3、APPLYMENT_STATE_REJECTED(已驳回):请按照驳回原因修改申请资料,超级管理员用微信打开“签约链接”,完成绑定微信号,后续申请单进度将通过微信公众号通知超级管理员。
  19. 4、APPLYMENT_STATE_TO_BE_CONFIRMED(待账户验证):请超级管理员使用微信打开返回的“签约链接”,根据页面指引完成账户验证。
  20. 5、APPLYMENT_STATE_TO_BE_SIGNED(待签约):请超级管理员使用微信打开返回的“签约链接”,根据页面指引完成签约。
  21. 6、APPLYMENT_STATE_SIGNING(开通权限中):系统开通相关权限中,请耐心等待。
  22. 7、APPLYMENT_STATE_FINISHED(已完成):商户入驻申请已完成。
  23. 8、APPLYMENT_STATE_CANCELED(已作废):申请单已被撤销。
  24. 9 APPLYMENT_STATE_PREPARE 准备数据,还没调接口
  25. 示例值:APPLYMENT_STATE_FINISHED
  26. */
  27. var stateNeedSearch = map[string]bool{
  28. "APPLYMENT_STATE_CANCELED": false,
  29. "APPLYMENT_STATE_FINISHED": false,
  30. "APPLYMENT_STATE_SIGNING": true,
  31. "APPLYMENT_STATE_TO_BE_SIGNED": true,
  32. "APPLYMENT_STATE_TO_BE_CONFIRMED": true,
  33. "APPLYMENT_STATE_REJECTED": false,
  34. "APPLYMENT_STATE_AUDITING": true,
  35. "APPLYMENT_STATE_EDITTING": false,
  36. "APPLYMENT_STATE_PREPARE": true,
  37. }
  38. func searchState(applyId string, businessCode string) (*pb_v1.WxMerchantSearchReply, error) {
  39. mreq := pb_v1.WxMerchantSearchRequest{}
  40. mreq.ApplymentId = applyId
  41. mreq.BusinessCode = businessCode
  42. mreply, err := pb.Thirdparty.WxMerchantSearch(context.Background(), &mreq)
  43. return mreply, err
  44. }
  45. func updateState(data *pb_v1.WxMerchantSearchReply, businessCode string, item *pb_v1.CompanyWxAccountApplyListItem) error {
  46. p := &dbmodel.TWxMerchantInfo{}
  47. where := map[string]interface{}{
  48. "business_code": businessCode,
  49. }
  50. values := map[string]interface{}{
  51. "apply_id": data.ApplymentId,
  52. "state": data.ApplymentState,
  53. "state_msg": data.ApplymentStateMsg,
  54. "sign_url": data.SignUrl,
  55. "reason": data.AuditDetail,
  56. "mch_id": data.MchId,
  57. }
  58. err := p.Update(database.DB(), where, values)
  59. if err != nil {
  60. return errors.DataBaseError
  61. }
  62. item.State = data.ApplymentState
  63. item.StateMsg = data.ApplymentStateMsg
  64. item.SignUrl = data.SignUrl
  65. item.Reason = data.AuditDetail
  66. item.MchId = data.MchId
  67. return nil
  68. }
  69. func CompanyWxAccountApplyList(ctx context.Context, req *pb_v1.CompanyWxAccountApplyListRequest) (reply *pb_v1.CompanyWxAccountApplyListReply, err error) {
  70. reply = &pb_v1.CompanyWxAccountApplyListReply{}
  71. // 捕获各个task中的异常并返回给调用者
  72. defer func() {
  73. if r := recover(); r != nil {
  74. err = fmt.Errorf("%+v", r)
  75. e := &status.Status{}
  76. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  77. logger.Error("err",
  78. zap.String("system_err", err.Error()),
  79. zap.Stack("stacktrace"))
  80. }
  81. }
  82. }()
  83. if req.Cid == 0 && !req.Management {
  84. return nil, errors.ParamsError
  85. }
  86. if req.Page == 0 {
  87. req.Page = 1
  88. }
  89. if req.PageSize == 0 {
  90. req.PageSize = 10
  91. }
  92. p := &dbmodel.TWxMerchantInfo{}
  93. where := map[string]interface{}{}
  94. if req.Cid > 0 {
  95. where["cid"] = req.Cid
  96. }
  97. if req.MerchantName != "" {
  98. where["merchant_name"] = req.MerchantName
  99. }
  100. count, err := p.Count(database.DB(), where, nil)
  101. if err != nil {
  102. return nil, errors.DataBaseError
  103. }
  104. reply.Total = count
  105. reply.Page = req.Page
  106. if count == 0 {
  107. return reply, nil
  108. }
  109. list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  110. if err != nil {
  111. return nil, errors.DataBaseError
  112. }
  113. reply.List = make([]*pb_v1.CompanyWxAccountApplyListItem, len(list))
  114. for i, v := range list {
  115. reply.List[i] = &pb_v1.CompanyWxAccountApplyListItem{
  116. Id: v.ID,
  117. BusinessCode: v.BusinessCode,
  118. MerchantName: v.MerchantName,
  119. State: v.State,
  120. StateMsg: v.StateMsg,
  121. MchId: v.MchId,
  122. SignUrl: v.SignUrl,
  123. Reason: v.Reason,
  124. }
  125. // 不需要查询状态
  126. if !stateNeedSearch[v.State] {
  127. continue
  128. }
  129. // 因小概率出错事件导致该状态,通过businesscode 去查
  130. if v.State == "APPLYMENT_STATE_PREPARE" {
  131. mreply, err := searchState("", v.BusinessCode)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if err := updateState(mreply, v.BusinessCode, reply.List[i]); err != nil {
  136. return nil, err
  137. }
  138. continue
  139. }
  140. // 先用申请单号去查
  141. mreply, err := searchState(v.ApplyId, "")
  142. if err != nil {
  143. return nil, err
  144. }
  145. // 如果不一致再用businesscode去查
  146. if v.BusinessCode != mreply.BusinessCode {
  147. mreply, err = searchState("", v.BusinessCode)
  148. if err != nil {
  149. return nil, err
  150. }
  151. // todo 还不一致(这种情况按道理不应该出现)
  152. if v.ApplyId != mreply.ApplymentId {
  153. continue
  154. }
  155. }
  156. if mreply.ApplymentState != v.State || mreply.ApplymentStateMsg != v.StateMsg || mreply.SignUrl != v.SignUrl {
  157. if err := updateState(mreply, v.BusinessCode, reply.List[i]); err != nil {
  158. return nil, err
  159. }
  160. }
  161. }
  162. return reply, nil
  163. }