enterprise.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. "time"
  8. "youngee_b_api/db"
  9. "youngee_b_api/model/gorm_model"
  10. "youngee_b_api/model/http_model"
  11. "github.com/issue9/conv"
  12. log "github.com/sirupsen/logrus"
  13. )
  14. var Enterprise *enterprise
  15. type enterprise struct {
  16. }
  17. func (*enterprise) CreateEnterpriseUser(ctx context.Context, newEnterprise http_model.RegisterRequest) (*http_model.RegisterData, error) {
  18. user := gorm_model.YounggeeUser{
  19. Phone: newEnterprise.Phone,
  20. User: "1001",
  21. Username: newEnterprise.BusinessName,
  22. Password: "1001",
  23. RealName: newEnterprise.RealName,
  24. Role: "3",
  25. Email: newEnterprise.Email,
  26. LastLoginTime: time.Now().UTC().Local(),
  27. }
  28. userId, err := db.CreateUser(ctx, user)
  29. if err != nil {
  30. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  31. return nil, err
  32. } else {
  33. rand.Seed(time.Now().UnixNano())
  34. th := strconv.Itoa(time.Now().Hour())
  35. tm := strconv.Itoa(time.Now().Minute())
  36. if len(tm) < 2 {
  37. tm = "0" + tm
  38. }
  39. if len(th) < 2 {
  40. th = "0" + th
  41. }
  42. //ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000
  43. ShowEnterpriseID := "1" + th + tm + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
  44. enterprise := &gorm_model.Enterprise{
  45. EnterpriseID: ShowEnterpriseID,
  46. Industry: newEnterprise.Industry,
  47. BusinessName: newEnterprise.BusinessName,
  48. UserID: *userId,
  49. Balance: 0,
  50. FrozenBalance: 0,
  51. AvailableBalance: 0,
  52. }
  53. enterpriseId, err := db.CreateEnterprise(ctx, *enterprise)
  54. if err != nil {
  55. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  56. return nil, err
  57. }
  58. res := &http_model.RegisterData{
  59. EnterpriseId: *enterpriseId,
  60. UserID: *userId,
  61. }
  62. return res, nil
  63. }
  64. }
  65. func (*enterprise) CreateEnterprise(ctx context.Context, phone string) (*http_model.RegisterData, error) {
  66. userInfo := gorm_model.YounggeeUser{
  67. Phone: phone,
  68. User: "1001",
  69. Username: phone,
  70. Password: "1001",
  71. RealName: "",
  72. Role: "3",
  73. Email: "",
  74. LastLoginTime: time.Now().UTC().Local(),
  75. }
  76. userId, createUserErr := db.CreateUser(ctx, userInfo)
  77. if createUserErr != nil {
  78. log.Infof("[CreateEnterpriseUser] fail,err:%+v", createUserErr)
  79. return nil, createUserErr
  80. } else {
  81. rand.Seed(time.Now().UnixNano())
  82. th := conv.MustString(time.Now().Hour())
  83. tm := conv.MustString(time.Now().Minute())
  84. if len(tm) < 2 {
  85. tm = "0" + tm
  86. }
  87. if len(tm) < 2 {
  88. th = "0" + th
  89. }
  90. ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000)
  91. enterpriseInfo := &gorm_model.Enterprise{
  92. EnterpriseID: ShowEnterpriseID,
  93. Industry: 1,
  94. BusinessName: phone,
  95. UserID: *userId,
  96. Balance: 0,
  97. FrozenBalance: 0,
  98. AvailableBalance: 0,
  99. Phone: phone,
  100. }
  101. enterpriseId, createEnterpriseErr := db.CreateEnterprise(ctx, *enterpriseInfo)
  102. if createEnterpriseErr != nil {
  103. log.Infof("[CreateEnterpriseUser] fail,err:%+v", createEnterpriseErr)
  104. return nil, createEnterpriseErr
  105. }
  106. res := &http_model.RegisterData{
  107. EnterpriseId: *enterpriseId,
  108. UserID: *userId,
  109. }
  110. return res, nil
  111. }
  112. }
  113. // GetEnterpriseAccountInfo 查询商家账号信息
  114. func (*enterprise) GetEnterpriseAccountInfo(ctx context.Context, req *http_model.GetAccountInfoRequest) (*http_model.GetAccountInfoData, error) {
  115. var enterpriseUserInfo *http_model.GetAccountInfoData
  116. enterpriseUserInfo = &http_model.GetAccountInfoData{}
  117. if req.SubAccountId == 0 {
  118. // 1. 商家主账号
  119. enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
  120. if enterpriseInfoErr != nil {
  121. log.Infof("[GetSupplierAccountInfo] fail,err:%+v", enterpriseInfoErr)
  122. return nil, enterpriseInfoErr
  123. }
  124. if enterpriseInfo != nil {
  125. enterpriseUserInfo.EnterpriseName = enterpriseInfo.EnterpriseName
  126. enterpriseUserInfo.Type = 1
  127. enterpriseUserInfo.Avatar = enterpriseInfo.Avatar
  128. enterpriseUserInfo.Phone = enterpriseInfo.Phone
  129. }
  130. } else {
  131. // 2. 商家子账号
  132. subAccountInfo, subAccountInfoErr := db.FindSubAccountById(ctx, req.SubAccountId)
  133. if subAccountInfoErr != nil {
  134. log.Infof("[GetSupplierAccountInfo] fail,err:%+v", subAccountInfoErr)
  135. return nil, subAccountInfoErr
  136. }
  137. if subAccountInfo != nil {
  138. enterpriseUserInfo.SubAccountName = subAccountInfo.SubAccountName
  139. enterpriseUserInfo.Type = 2
  140. enterpriseUserInfo.Avatar = subAccountInfo.Avatar
  141. enterpriseUserInfo.Phone = subAccountInfo.PhoneNumber
  142. jobInfo, jobInfoErr := db.FindJobByJobId(ctx, subAccountInfo.JobId)
  143. if jobInfoErr != nil {
  144. log.Infof("[GetSupplierAccountInfo] fail,err:%+v", jobInfoErr)
  145. return nil, subAccountInfoErr
  146. }
  147. if jobInfo != nil {
  148. enterpriseUserInfo.JobName = jobInfo.JobName
  149. }
  150. }
  151. }
  152. return enterpriseUserInfo, nil
  153. }
  154. // GetEnterpriseReviewInfo 查询商家认证信息
  155. func (*enterprise) GetEnterpriseReviewInfo(ctx context.Context, req *http_model.GetReviewInfoRequest) (*http_model.GetReviewInfoData, error) {
  156. var enterpriseUserInfo *http_model.GetReviewInfoData
  157. enterpriseUserInfo = &http_model.GetReviewInfoData{}
  158. enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
  159. if enterpriseInfoErr != nil {
  160. log.Infof("[GetEnterpriseByEnterpriseID] fail,err:%+v", enterpriseInfoErr)
  161. return nil, enterpriseInfoErr
  162. }
  163. if enterpriseInfo != nil {
  164. enterpriseUserInfo.ReviewType = 1
  165. if enterpriseInfo.AuthStatus == 1 {
  166. enterpriseUserInfo.ReviewStatus = 2
  167. } else {
  168. enterpriseUserInfo.ReviewStatus = 1
  169. }
  170. enterpriseUserInfo.BusinessLicense = enterpriseInfo.BusinessLicense
  171. enterpriseUserInfo.USCI = enterpriseInfo.Usci
  172. enterpriseUserInfo.CompanyName = enterpriseInfo.BusinessName
  173. }
  174. return enterpriseUserInfo, nil
  175. }
  176. // GetEnterpriseContactInfo 查询商家联系方式
  177. func (*enterprise) GetEnterpriseContactInfo(ctx context.Context, req *http_model.GetContactInfoRequest) (*http_model.GetContactInfoData, error) {
  178. var contactInfo *http_model.GetContactInfoData
  179. contactInfo = &http_model.GetContactInfoData{}
  180. // 1. 商家主账号
  181. if req.SubAccountId == 0 {
  182. enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
  183. if enterpriseInfoErr != nil {
  184. log.Infof("[enterpriseInfoErr] fail,err:%+v", enterpriseInfoErr)
  185. return nil, enterpriseInfoErr
  186. }
  187. if enterpriseInfo != nil {
  188. contactInfo.ContactPhone = enterpriseInfo.ContactPhone
  189. contactInfo.WechatQRCode = enterpriseInfo.WechatQrCode
  190. contactInfo.WechatNumber = enterpriseInfo.WechatNumber
  191. }
  192. } else {
  193. // 2. 商家子账号
  194. subAccountInfo, subAccountInfoErr := db.FindSubAccountById(ctx, req.SubAccountId)
  195. if subAccountInfoErr != nil {
  196. log.Infof("[GetSupplierContactInfo] fail,err:%+v", subAccountInfoErr)
  197. return nil, subAccountInfoErr
  198. }
  199. if subAccountInfo != nil {
  200. contactInfo.ContactPhone = subAccountInfo.ContactPhone
  201. contactInfo.WechatQRCode = subAccountInfo.WechatQRCode
  202. contactInfo.WechatNumber = subAccountInfo.WechatNumber
  203. }
  204. }
  205. return contactInfo, nil
  206. }