htgj.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func CheckHtgj(companyCodeHt, companyNameHt, email, sign string) (errMsg string, errHt error, user *models.WxUser, companyName string) {
  11. errMsg = CheckSign(companyCodeHt+companyNameHt+email, sign)
  12. if errMsg != "" {
  13. if errMsg == "机构下账号个数已达上限" {
  14. return
  15. }
  16. errMsg = "签名方式错误!"
  17. return
  18. }
  19. var wxuUserName, tripartiteCompanyCode string
  20. var startDate, endDate string
  21. var companyId int64
  22. companyName = companyCodeHt[0:4] + "_" + companyNameHt
  23. tripartiteCompanyCode = companyCodeHt + "_" + companyNameHt
  24. wxUser := new(models.WxUser)
  25. productId := 2
  26. productName := "权益"
  27. itemCompany := new(models.Company)
  28. sellerInfo, err := models.GetSellerByName(utils.HtgjDefaultSaleName)
  29. if err != nil {
  30. errHt = err
  31. return
  32. }
  33. wxuUserName = email
  34. countCompay, err := models.GetCompanyCountByThirdName(tripartiteCompanyCode)
  35. if err != nil {
  36. errHt = err
  37. return
  38. }
  39. if countCompay == 0 {
  40. defaultPermissionIds, err := models.GetCompanyPermissionIdWithHtgj()
  41. if err != nil {
  42. errHt = err
  43. return
  44. }
  45. companySourceInfo, err := models.GetCompanySourceByName(utils.HtgjName)
  46. if err != nil {
  47. errHt = err
  48. return
  49. }
  50. companyCode, err := models.GetCompanyCode()
  51. if err != nil {
  52. errHt = err
  53. return
  54. }
  55. startDate = time.Now().Format(utils.FormatDate)
  56. endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  57. itemCompany.TripartiteCompanyCode = tripartiteCompanyCode
  58. itemCompany.CompanyName = companyName
  59. itemCompany.CreditCode = "HZ" + strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
  60. itemCompany.CompanyCode = companyCode
  61. itemCompany.StartDate = startDate
  62. itemCompany.EndDate = endDate
  63. itemCompany.CreatedTime = time.Now()
  64. itemCompany.LastUpdatedTime = time.Now()
  65. itemCompany.Enabled = 1
  66. itemCompany.CompanyType = 2
  67. itemCompany.RegionType = "海外"
  68. companyId, err = models.AddCompany(itemCompany)
  69. if err != nil {
  70. errHt = err
  71. return
  72. }
  73. //更新三方Code
  74. itemCompany.OpenCompanyCode = GenerateOpenCompanyCode(int(companyId))
  75. itemCompany.CompanyId = int(companyId)
  76. itemCompany.Update([]string{"OpenCompanyCode"})
  77. companyProduct := new(models.CompanyProduct)
  78. companyProduct.CompanyId = int(companyId)
  79. companyProduct.ProductId = productId
  80. companyProduct.ProductName = productName
  81. companyProduct.CompanyName = companyName
  82. companyProduct.Source = companySourceInfo.SourceName
  83. companyProduct.Reasons = ""
  84. companyProduct.Status = "试用"
  85. companyProduct.StartDate = startDate
  86. companyProduct.EndDate = endDate
  87. companyProduct.IndustryId = 11 //11是海通国际的ID
  88. companyProduct.IndustryName = "其他类型"
  89. companyProduct.SellerId = sellerInfo.AdminId
  90. companyProduct.SellerName = sellerInfo.RealName
  91. companyProduct.GroupId = sellerInfo.GroupId
  92. companyProduct.DepartmentId = sellerInfo.DepartmentId
  93. companyProduct.IsSuspend = 0
  94. companyProduct.CreateTime = time.Now()
  95. companyProduct.ModifyTime = time.Now()
  96. companyProduct.CompanyType = "权益"
  97. companyProduct.OpenCode = GenerateOpenCompanyProductCode(int(companyId), productId)
  98. companyProductId, err := models.AddCompanyProduct(companyProduct)
  99. if err != nil {
  100. errHt = err
  101. return
  102. }
  103. err = AddCompanyPermission(int(companyId), sellerInfo.AdminId, productId, productName, sellerInfo.RealName, defaultPermissionIds, startDate, endDate)
  104. if err != nil {
  105. errHt = err
  106. return
  107. }
  108. {
  109. record := new(models.CompanyOperationRecord)
  110. record.CompanyId = int(companyId)
  111. record.CompanyName = companyName
  112. record.SellerId = sellerInfo.AdminId
  113. record.SysUserId = sellerInfo.AdminId
  114. record.SysRealName = sellerInfo.RealName
  115. record.Remark = "新增客户"
  116. record.Operation = "add"
  117. record.CreateTime = time.Now()
  118. record.ProductId = productId
  119. record.ProductName = productName
  120. record.Status = "试用"
  121. _, err = models.AddCompanyOperationRecord(record)
  122. if err != nil {
  123. errHt = err
  124. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "海通国际添加客户失败"+err.Error(), utils.EmailSendToUsers)
  125. }
  126. }
  127. //新增客户日志
  128. {
  129. companyLog := new(models.CompanyLog)
  130. companyLog.CompanyId = int(companyId)
  131. companyLog.CompanyName = companyName
  132. companyLog.CreditCode = ""
  133. companyLog.CompanyCode = companyCode
  134. companyLog.Sort = itemCompany.Sort
  135. companyLog.IsFeeCustomer = itemCompany.IsFeeCustomer
  136. companyLog.Country = itemCompany.Country
  137. companyLog.Address = itemCompany.Address
  138. companyLog.City = itemCompany.City
  139. companyLog.Province = itemCompany.Province
  140. companyLog.Enabled = itemCompany.Enabled
  141. companyLog.CreatedTime = itemCompany.CreatedTime
  142. companyLog.LastUpdatedTime = itemCompany.LastUpdatedTime
  143. companyLog.Seller = itemCompany.Seller
  144. companyLog.SellsId = itemCompany.SellsId
  145. companyLog.CompanyBelong = itemCompany.CompanyBelong
  146. companyLog.StartDate = itemCompany.StartDate
  147. companyLog.EndDate = itemCompany.EndDate
  148. companyLog.FirstStartDate = itemCompany.FirstStartDate
  149. companyLog.FirstEndDate = itemCompany.FirstEndDate
  150. companyLog.DateType = itemCompany.DateType
  151. companyLog.AdminId = sellerInfo.AdminId
  152. companyLog.LogType = "add"
  153. companyLog.LogCreateTime = time.Now()
  154. go models.AddCompanyLog(companyLog)
  155. }
  156. //新增客户产品日志
  157. {
  158. productLog := new(models.CompanyProductLog)
  159. productLog.CompanyProductId = int(companyProductId)
  160. productLog.CompanyId = companyProduct.CompanyId
  161. productLog.ProductId = companyProduct.ProductId
  162. productLog.ProductName = companyProduct.ProductName
  163. productLog.CompanyName = companyProduct.CompanyName
  164. productLog.CompanyName = companyProduct.CompanyName
  165. productLog.Source = companyProduct.Source
  166. productLog.Reasons = companyProduct.Reasons
  167. productLog.Status = companyProduct.Status
  168. productLog.IndustryId = companyProduct.IndustryId
  169. productLog.IndustryName = companyProduct.IndustryName
  170. productLog.SellerId = companyProduct.SellerId
  171. productLog.SellerName = companyProduct.SellerName
  172. productLog.GroupId = companyProduct.GroupId
  173. productLog.DepartmentId = companyProduct.DepartmentId
  174. productLog.IsSuspend = companyProduct.IsSuspend
  175. productLog.SuspendTime = companyProduct.SuspendTime
  176. productLog.ApproveStatus = companyProduct.ApproveStatus
  177. productLog.FreezeTime = companyProduct.FreezeTime
  178. productLog.Remark = companyProduct.Remark
  179. productLog.StartDate = companyProduct.StartDate
  180. productLog.EndDate = companyProduct.EndDate
  181. productLog.CreateTime = companyProduct.CreateTime
  182. productLog.ModifyTime = companyProduct.ModifyTime
  183. productLog.LoseReason = companyProduct.LoseReason
  184. productLog.LossTime = companyProduct.LossTime
  185. productLog.LogType = "add"
  186. productLog.LogCreateTime = time.Now()
  187. productLog.AdminId = sellerInfo.AdminId
  188. go models.AddCompanyProductLog(productLog)
  189. }
  190. } else {
  191. companyInfo, err := models.GetCompanyByThirdName(tripartiteCompanyCode)
  192. if err != nil {
  193. errHt = err
  194. return
  195. }
  196. companyId = int64(companyInfo.CompanyId)
  197. }
  198. countUser, err := models.GetUserCountByThirdName(int(companyId), wxuUserName)
  199. if countUser == 0 {
  200. countCompanyUser, err := models.GetUserCountByCompanyId(int(companyId))
  201. if err != nil {
  202. errHt = err
  203. return
  204. }
  205. if countCompanyUser >= 15 {
  206. errMsg = "机构下账号个数已达上限"
  207. return
  208. }
  209. wxUser.CompanyId = int(companyId)
  210. wxUser.RealName = wxuUserName
  211. wxUser.Email = wxuUserName
  212. wxUser.TripartiteCode = wxuUserName
  213. wxUser.Source = 7
  214. wxUser.CreatedTime = time.Now()
  215. newUserId, err := models.AddWxUser(wxUser)
  216. if err != nil {
  217. errHt = err
  218. return
  219. }
  220. md5Mobile := utils.Get16MD5Encode(strconv.Itoa(int(newUserId)))
  221. wxUser.Mobile = md5Mobile
  222. wxUser.UserId = int(newUserId)
  223. err = models.UpdateUserMobile(int(newUserId), md5Mobile)
  224. if err != nil {
  225. errHt = err
  226. return
  227. }
  228. models.AddUserSellerRelation(newUserId, int(companyId), sellerInfo.AdminId, productId, sellerInfo.RealName, wxUser.Mobile, wxuUserName)
  229. } else {
  230. wxUser, err = models.GetUserByThirdName(int(companyId), wxuUserName)
  231. if err != nil {
  232. errHt = err
  233. return
  234. }
  235. }
  236. user = wxUser
  237. return
  238. }
  239. // GenerateOpenCompanyCode 生成第三方code编号
  240. func GenerateOpenCompanyCode(companyId int) (openCode string) {
  241. openCode = utils.MD5(fmt.Sprint(companyId, "hzyj20210902"))
  242. return
  243. }
  244. // GenerateOpenCompanyProductCode 生成客户产品第三方code编号
  245. func GenerateOpenCompanyProductCode(companyId, productId int) (openCode string) {
  246. openCode = utils.MD5(fmt.Sprint(companyId, "_", productId, "hzyj20210902"))
  247. return
  248. }
  249. //新增客户权限
  250. func AddCompanyPermission(companyId, sysUserId, productId int, productName, sysUserName, permissionStr, startDate, endDate string) (err error) {
  251. if permissionStr == "" {
  252. return
  253. }
  254. permissionArr := strings.Split(permissionStr, ",")
  255. count, _ := models.GetCompanyReportPermissionCount(companyId, productId)
  256. //删除权限
  257. if count > 0 {
  258. err = models.DeleteCompanyReportPermission(companyId, productId)
  259. if err != nil {
  260. return
  261. }
  262. }
  263. for _, pv := range permissionArr {
  264. permissionId, err := strconv.Atoi(pv)
  265. if err != nil {
  266. return err
  267. }
  268. permission := new(models.CompanyReportPermission)
  269. permission.CompanyId = companyId
  270. permission.ReportPermissionId = permissionId
  271. permission.CreatedTime = time.Now()
  272. permission.LastUpdatedTime = time.Now()
  273. permission.ChartPermissionId = permissionId
  274. permission.StartDate = startDate
  275. permission.EndDate = endDate
  276. permission.ProductId = productId
  277. permission.ProductName = productName
  278. permission.ModifyTime = time.Now().Format(utils.FormatDateTime)
  279. permission.Status = "试用"
  280. err = models.AddCompanyReportPermission(permission)
  281. if err != nil {
  282. return err
  283. }
  284. cpLog := new(models.CompanyPermissionLog)
  285. cpLog.CompanyId = companyId
  286. cpLog.ChartPermissionId = permissionId
  287. cpLog.CreateTime = time.Now()
  288. cpLog.SysUserId = sysUserId
  289. cpLog.SysUserName = sysUserName
  290. cpLog.StartDate = startDate
  291. cpLog.EndDate = endDate
  292. cpLog.ProductId = productId
  293. cpLog.ProductName = productName
  294. go models.AddCompanyPermissionLog(cpLog)
  295. }
  296. return
  297. }
  298. func CheckSign(userName, sign string) (errMsg string) {
  299. var pwdSign string
  300. pwdSign = userName + utils.HtgjSalt
  301. fmt.Println("pwdSign", pwdSign)
  302. pwdSign = strings.ToUpper(utils.MD5(pwdSign))
  303. fmt.Println(pwdSign)
  304. if sign != pwdSign {
  305. errMsg = "签名错误"
  306. }
  307. return
  308. }