123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package company
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-company/errors"
- dbmodel "property-company/model"
- "property-company/pb"
- pb_v1 "property-company/pb/v1"
- )
- /*
- 1、APPLYMENT_STATE_EDITTING(编辑中):提交申请发生错误导致,请尝试重新提交。
- 2、APPLYMENT_STATE_AUDITING(审核中):申请单正在审核中,超级管理员用微信打开“签约链接”,完成绑定微信号后,申请单进度将通过微信公众号通知超级管理员,引导完成后续步骤。
- 3、APPLYMENT_STATE_REJECTED(已驳回):请按照驳回原因修改申请资料,超级管理员用微信打开“签约链接”,完成绑定微信号,后续申请单进度将通过微信公众号通知超级管理员。
- 4、APPLYMENT_STATE_TO_BE_CONFIRMED(待账户验证):请超级管理员使用微信打开返回的“签约链接”,根据页面指引完成账户验证。
- 5、APPLYMENT_STATE_TO_BE_SIGNED(待签约):请超级管理员使用微信打开返回的“签约链接”,根据页面指引完成签约。
- 6、APPLYMENT_STATE_SIGNING(开通权限中):系统开通相关权限中,请耐心等待。
- 7、APPLYMENT_STATE_FINISHED(已完成):商户入驻申请已完成。
- 8、APPLYMENT_STATE_CANCELED(已作废):申请单已被撤销。
- 9 APPLYMENT_STATE_PREPARE 准备数据,还没调接口
- 示例值:APPLYMENT_STATE_FINISHED
- */
- var stateNeedSearch = map[string]bool{
- "APPLYMENT_STATE_CANCELED": false,
- "APPLYMENT_STATE_FINISHED": false,
- "APPLYMENT_STATE_SIGNING": true,
- "APPLYMENT_STATE_TO_BE_SIGNED": true,
- "APPLYMENT_STATE_TO_BE_CONFIRMED": true,
- "APPLYMENT_STATE_REJECTED": false,
- "APPLYMENT_STATE_AUDITING": true,
- "APPLYMENT_STATE_EDITTING": false,
- "APPLYMENT_STATE_PREPARE": true,
- }
- func searchState(applyId string, businessCode string) (*pb_v1.WxMerchantSearchReply, error) {
- mreq := pb_v1.WxMerchantSearchRequest{}
- mreq.ApplymentId = applyId
- mreq.BusinessCode = businessCode
- mreply, err := pb.Thirdparty.WxMerchantSearch(context.Background(), &mreq)
- return mreply, err
- }
- func updateState(data *pb_v1.WxMerchantSearchReply, businessCode string, item *pb_v1.CompanyWxAccountApplyListItem) error {
- p := &dbmodel.TWxMerchantInfo{}
- where := map[string]interface{}{
- "business_code": businessCode,
- }
- values := map[string]interface{}{
- "apply_id": data.ApplymentId,
- "state": data.ApplymentState,
- "state_msg": data.ApplymentStateMsg,
- "sign_url": data.SignUrl,
- "reason": data.AuditDetail,
- "mch_id": data.MchId,
- }
- err := p.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- item.State = data.ApplymentState
- item.StateMsg = data.ApplymentStateMsg
- item.SignUrl = data.SignUrl
- item.Reason = data.AuditDetail
- item.MchId = data.MchId
- return nil
- }
- func CompanyWxAccountApplyList(ctx context.Context, req *pb_v1.CompanyWxAccountApplyListRequest) (reply *pb_v1.CompanyWxAccountApplyListReply, err error) {
- reply = &pb_v1.CompanyWxAccountApplyListReply{}
- // 捕获各个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"))
- }
- }
- }()
- if req.Cid == 0 && !req.Management {
- return nil, errors.ParamsError
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- p := &dbmodel.TWxMerchantInfo{}
- where := map[string]interface{}{}
- if req.Cid > 0 {
- where["cid"] = req.Cid
- }
- if req.MerchantName != "" {
- where["merchant_name"] = req.MerchantName
- }
- count, err := p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Total = count
- reply.Page = req.Page
- if count == 0 {
- return reply, nil
- }
- list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.CompanyWxAccountApplyListItem, len(list))
- for i, v := range list {
- reply.List[i] = &pb_v1.CompanyWxAccountApplyListItem{
- Id: v.ID,
- BusinessCode: v.BusinessCode,
- MerchantName: v.MerchantName,
- State: v.State,
- StateMsg: v.StateMsg,
- MchId: v.MchId,
- SignUrl: v.SignUrl,
- Reason: v.Reason,
- }
- // 不需要查询状态
- if !stateNeedSearch[v.State] {
- continue
- }
- // 因小概率出错事件导致该状态,通过businesscode 去查
- if v.State == "APPLYMENT_STATE_PREPARE" {
- mreply, err := searchState("", v.BusinessCode)
- if err != nil {
- return nil, err
- }
- if err := updateState(mreply, v.BusinessCode, reply.List[i]); err != nil {
- return nil, err
- }
- continue
- }
- // 先用申请单号去查
- mreply, err := searchState(v.ApplyId, "")
- if err != nil {
- return nil, err
- }
- // 如果不一致再用businesscode去查
- if v.BusinessCode != mreply.BusinessCode {
- mreply, err = searchState("", v.BusinessCode)
- if err != nil {
- return nil, err
- }
- // todo 还不一致(这种情况按道理不应该出现)
- if v.ApplyId != mreply.ApplymentId {
- continue
- }
- }
- if mreply.ApplymentState != v.State || mreply.ApplymentStateMsg != v.StateMsg || mreply.SignUrl != v.SignUrl {
- if err := updateState(mreply, v.BusinessCode, reply.List[i]); err != nil {
- return nil, err
- }
- }
- }
- return reply, nil
- }
|