openAccount.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "app.yhyue.com/moapp/jybase/log"
  7. "cmplatform/util"
  8. "database/sql"
  9. "fmt"
  10. "go.uber.org/zap"
  11. "regexp"
  12. "strconv"
  13. "time"
  14. )
  15. var (
  16. EncodeUtil = &encrypt.SimpleEncrypt{"qyfw2017jy"}
  17. strReg = regexp.MustCompile("^[0-9a-zA-Z]+$")
  18. )
  19. func (this *SecondPush) OpenAccountPage() {
  20. this.Render("openAccount/index.html")
  21. }
  22. func (this *SecondPush) OpenAccount() {
  23. startTime := this.GetString("startTime")
  24. endTime := this.GetString("endTime")
  25. companyName := this.GetString("companyName")
  26. phone := this.GetString("phone")
  27. fieldType := this.GetString("fieldType")
  28. openType := this.GetString("openType")
  29. rechargeNum, _ := strconv.Atoi(this.GetString("rechargeNum"))
  30. log.Debug(fmt.Sprint(rechargeNum))
  31. entId, userId, fieldTypes := 0, 0, 1
  32. if fieldType == "1" {
  33. fieldTypes = 1
  34. } else if fieldType == "2" {
  35. fieldTypes = 2
  36. }
  37. //
  38. entId, userId = EntnicheInit(companyName, phone, startTime, endTime)
  39. log.Debug("", zap.Int("entId", entId), zap.Int("userId", userId))
  40. if openType == "1" {
  41. if counts := util.JyMysqls.Count("entniche_export_limit", map[string]interface{}{"ent_id": entId}); counts > 0 {
  42. set := map[string]interface{}{
  43. "$inc": map[string]interface{}{
  44. "plan.current": rechargeNum,
  45. },
  46. }
  47. util.MgoSave.Update("user", map[string]interface{}{"username": companyName, "phone": phone}, set, false, false)
  48. } else {
  49. util.JyMysqls.Insert("entniche_export_limit", map[string]interface{}{
  50. "ent_id": entId,
  51. "data_limit": 200000,
  52. "user_id": userId,
  53. "remain_nums": 200000,
  54. "export_nums": 0,
  55. })
  56. NewUser(companyName, phone, startTime, endTime, rechargeNum)
  57. }
  58. } else if openType == "2" {
  59. if counts := util.Mgo.Count("datatag_export_config", map[string]interface{}{"ent_id": entId}); counts > 0 {
  60. datas := util.JyMysqls.FindOne("user_account", map[string]interface{}{"ent_id": entId, "user_id": userId}, "", "")
  61. before := common.IntAll((*datas)["left_num"])
  62. after := before + rechargeNum
  63. util.JyMysqls.Update("user_account", map[string]interface{}{"ent_id": entId, "user_id": userId}, map[string]interface{}{
  64. "left_num": after,
  65. "update_at": date.NowFormat(date.Date_Full_Layout),
  66. })
  67. util.JyMysqls.Insert("user_recharge_record", map[string]interface{}{
  68. "user_id": userId,
  69. "ent_id": entId,
  70. "before": 0,
  71. "after": before,
  72. "trade": after,
  73. "remark": "客户充值",
  74. "create_at": date.NowFormat(date.Date_Full_Layout),
  75. })
  76. } else {
  77. util.Mgo.Save("datatag_export_config", map[string]interface{}{"ent_id": entId, "dataType": fieldTypes})
  78. NewUser(companyName, phone, startTime, endTime, 0)
  79. util.JyMysqls.Insert("user_account", map[string]interface{}{
  80. "user_id": userId,
  81. "ent_id": entId,
  82. "left_num": rechargeNum,
  83. "create_at": date.NowFormat(date.Date_Full_Layout),
  84. "update_at": date.NowFormat(date.Date_Full_Layout),
  85. })
  86. util.JyMysqls.Insert("user_recharge_record", map[string]interface{}{
  87. "user_id": userId,
  88. "ent_id": entId,
  89. "`before`": 0,
  90. "`after`": rechargeNum,
  91. "trade": rechargeNum,
  92. "remark": "客户充值",
  93. "create_at": date.NowFormat(date.Date_Full_Layout),
  94. })
  95. }
  96. }
  97. this.ServeJson(map[string]interface{}{"entId": entId, "userId": userId, "msg": "开通成功!通知测试增加企业Id配置"})
  98. }
  99. func EntnicheInit(companyName, phone, startTime, endTime string) (int, int) {
  100. log.Debug("开始初始化企业信息。。。")
  101. ents := util.JyMysqls.SelectBySql(`select id,status from entniche_info where name=? and phone=? order by id desc`, companyName, phone)
  102. flag := false
  103. nowFormat := date.NowFormat(date.Date_Full_Layout)
  104. s, err := time.ParseInLocation(date.Date_Full_Layout, startTime, time.Local)
  105. if err != nil {
  106. log.Error("", zap.Error(err))
  107. }
  108. startdate := s.Unix()
  109. e, err := time.ParseInLocation(date.Date_Full_Layout, endTime, time.Local)
  110. if err != nil {
  111. log.Error("", zap.Error(err))
  112. }
  113. enddate := e.Unix()
  114. var entId int64
  115. var deptId int64
  116. var userId int64
  117. if ents != nil && len(*ents) > 0 {
  118. log.Debug("该企业已存在,执行修改操作!")
  119. for _, v := range *ents {
  120. if common.IntAll(v["status"]) == 1 {
  121. log.Fatal("该企业已购买过!")
  122. }
  123. }
  124. entId = common.Int64All((*ents)[0]["id"])
  125. depts := util.JyMysqls.SelectBySql(`select id from entniche_department where ent_id=? and pid=0`, entId)
  126. if depts == nil || len(*depts) == 0 {
  127. log.Fatal("没有该企业的部门信息")
  128. }
  129. deptId = common.Int64All((*depts)[0]["id"])
  130. } else {
  131. log.Debug("是个全新的企业,执行新增操作!")
  132. flag = util.JyMysqls.ExecTx("", func(tx *sql.Tx) bool {
  133. entId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_info (name,phone,model,status,quota,startdate,enddate,createtime,admin) values (?,?,?,?,?,?,?,?,?)`,
  134. companyName, phone, 1, 1, 0, startdate, enddate, nowFormat, "我")
  135. userId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_user (name,phone,mail,ent_id,power,createtime,timestamp,export_power) values (?,?,?,?,?,?,?,?)`,
  136. "我", phone, "", entId, 0, nowFormat, nowFormat, 1)
  137. deptId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_department (pid,name,ent_id,nodiff,subdis,createtime,timestamp) values (?,?,?,?,?,?,?)`,
  138. 0, companyName, entId, 1, 1, nowFormat, nowFormat)
  139. dept_user := util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_department_user (dept_id,user_id) values (?,?)`, deptId, userId)
  140. user_role := util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_user_role (user_id,role_id) values (?,?)`, userId, 1)
  141. return entId > 0 && userId > 0 && deptId > 0 && dept_user > -1 && user_role > -1
  142. })
  143. }
  144. if flag {
  145. log.Debug("开通成功!", zap.Int64("entId", entId), zap.Int64("deptId", deptId))
  146. } else {
  147. log.Debug("开通失败!")
  148. }
  149. return int(entId), int(userId)
  150. }
  151. // 创建新用户
  152. func NewUser(companyName, phone, startTime, endTime string, rechargeNum int) {
  153. t := time.Now()
  154. appid := GetAppid(t.Unix())
  155. key := common.GetComplexRandom(8, 3, 5)
  156. s, err := time.ParseInLocation(date.Date_Full_Layout, startTime, time.Local)
  157. if err != nil {
  158. log.Fatal("", zap.Error(err))
  159. }
  160. startdate := s.Unix()
  161. e, err := time.ParseInLocation(date.Date_Full_Layout, endTime, time.Local)
  162. if err != nil {
  163. log.Fatal("", zap.Error(err))
  164. }
  165. enddate := e.Unix()
  166. util.MgoSave.Save("user", &map[string]interface{}{
  167. "username": companyName,
  168. "createtime": t.Unix(),
  169. "appid": appid,
  170. "key": key,
  171. "bnormal": int32(1),
  172. "phone": phone,
  173. "plan": map[string]interface{}{
  174. "name": "A1",
  175. "starttime": startdate,
  176. "endtime": enddate,
  177. "current": rechargeNum,
  178. },
  179. })
  180. }
  181. func GetAppid(tn int64) (appid string) {
  182. for {
  183. randomstr := common.GetLetterRandom(5)
  184. str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:])
  185. appid = EncodeUtil.EncodeString(str)
  186. if strReg.MatchString(appid) {
  187. break
  188. }
  189. }
  190. appid = "jy" + appid
  191. return
  192. }