123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package service
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/encrypt"
- "app.yhyue.com/moapp/jybase/log"
- "cmplatform/util"
- "database/sql"
- "fmt"
- "go.uber.org/zap"
- "regexp"
- "strconv"
- "time"
- )
- var (
- EncodeUtil = &encrypt.SimpleEncrypt{"qyfw2017jy"}
- strReg = regexp.MustCompile("^[0-9a-zA-Z]+$")
- )
- func (this *SecondPush) OpenAccountPage() {
- this.Render("openAccount/index.html")
- }
- func (this *SecondPush) OpenAccount() {
- startTime := this.GetString("startTime")
- endTime := this.GetString("endTime")
- companyName := this.GetString("companyName")
- phone := this.GetString("phone")
- fieldType := this.GetString("fieldType")
- openType := this.GetString("openType")
- rechargeNum, _ := strconv.Atoi(this.GetString("rechargeNum"))
- log.Debug(fmt.Sprint(rechargeNum))
- entId, userId, fieldTypes := 0, 0, 1
- if fieldType == "1" {
- fieldTypes = 1
- } else if fieldType == "2" {
- fieldTypes = 2
- }
- //
- entId, userId = EntnicheInit(companyName, phone, startTime, endTime)
- log.Debug("", zap.Int("entId", entId), zap.Int("userId", userId))
- if openType == "1" {
- if counts := util.JyMysqls.Count("entniche_export_limit", map[string]interface{}{"ent_id": entId}); counts > 0 {
- set := map[string]interface{}{
- "$inc": map[string]interface{}{
- "plan.current": rechargeNum,
- },
- }
- util.MgoSave.Update("user", map[string]interface{}{"username": companyName, "phone": phone}, set, false, false)
- } else {
- util.JyMysqls.Insert("entniche_export_limit", map[string]interface{}{
- "ent_id": entId,
- "data_limit": 200000,
- "user_id": userId,
- "remain_nums": 200000,
- "export_nums": 0,
- })
- NewUser(companyName, phone, startTime, endTime, rechargeNum)
- }
- } else if openType == "2" {
- if counts := util.Mgo.Count("datatag_export_config", map[string]interface{}{"ent_id": entId}); counts > 0 {
- datas := util.JyMysqls.FindOne("user_account", map[string]interface{}{"ent_id": entId, "user_id": userId}, "", "")
- before := common.IntAll((*datas)["left_num"])
- after := before + rechargeNum
- util.JyMysqls.Update("user_account", map[string]interface{}{"ent_id": entId, "user_id": userId}, map[string]interface{}{
- "left_num": after,
- "update_at": date.NowFormat(date.Date_Full_Layout),
- })
- util.JyMysqls.Insert("user_recharge_record", map[string]interface{}{
- "user_id": userId,
- "ent_id": entId,
- "before": 0,
- "after": before,
- "trade": after,
- "remark": "客户充值",
- "create_at": date.NowFormat(date.Date_Full_Layout),
- })
- } else {
- util.Mgo.Save("datatag_export_config", map[string]interface{}{"ent_id": entId, "dataType": fieldTypes})
- NewUser(companyName, phone, startTime, endTime, 0)
- util.JyMysqls.Insert("user_account", map[string]interface{}{
- "user_id": userId,
- "ent_id": entId,
- "left_num": rechargeNum,
- "create_at": date.NowFormat(date.Date_Full_Layout),
- "update_at": date.NowFormat(date.Date_Full_Layout),
- })
- util.JyMysqls.Insert("user_recharge_record", map[string]interface{}{
- "user_id": userId,
- "ent_id": entId,
- "`before`": 0,
- "`after`": rechargeNum,
- "trade": rechargeNum,
- "remark": "客户充值",
- "create_at": date.NowFormat(date.Date_Full_Layout),
- })
- }
- }
- this.ServeJson(map[string]interface{}{"entId": entId, "userId": userId, "msg": "开通成功!通知测试增加企业Id配置"})
- }
- func EntnicheInit(companyName, phone, startTime, endTime string) (int, int) {
- log.Debug("开始初始化企业信息。。。")
- ents := util.JyMysqls.SelectBySql(`select id,status from entniche_info where name=? and phone=? order by id desc`, companyName, phone)
- flag := false
- nowFormat := date.NowFormat(date.Date_Full_Layout)
- s, err := time.ParseInLocation(date.Date_Full_Layout, startTime, time.Local)
- if err != nil {
- log.Error("", zap.Error(err))
- }
- startdate := s.Unix()
- e, err := time.ParseInLocation(date.Date_Full_Layout, endTime, time.Local)
- if err != nil {
- log.Error("", zap.Error(err))
- }
- enddate := e.Unix()
- var entId int64
- var deptId int64
- var userId int64
- if ents != nil && len(*ents) > 0 {
- log.Debug("该企业已存在,执行修改操作!")
- for _, v := range *ents {
- if common.IntAll(v["status"]) == 1 {
- log.Fatal("该企业已购买过!")
- }
- }
- entId = common.Int64All((*ents)[0]["id"])
- depts := util.JyMysqls.SelectBySql(`select id from entniche_department where ent_id=? and pid=0`, entId)
- if depts == nil || len(*depts) == 0 {
- log.Fatal("没有该企业的部门信息")
- }
- deptId = common.Int64All((*depts)[0]["id"])
- } else {
- log.Debug("是个全新的企业,执行新增操作!")
- flag = util.JyMysqls.ExecTx("", func(tx *sql.Tx) bool {
- entId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_info (name,phone,model,status,quota,startdate,enddate,createtime,admin) values (?,?,?,?,?,?,?,?,?)`,
- companyName, phone, 1, 1, 0, startdate, enddate, nowFormat, "我")
- userId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_user (name,phone,mail,ent_id,power,createtime,timestamp,export_power) values (?,?,?,?,?,?,?,?)`,
- "我", phone, "", entId, 0, nowFormat, nowFormat, 1)
- deptId = util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_department (pid,name,ent_id,nodiff,subdis,createtime,timestamp) values (?,?,?,?,?,?,?)`,
- 0, companyName, entId, 1, 1, nowFormat, nowFormat)
- dept_user := util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_department_user (dept_id,user_id) values (?,?)`, deptId, userId)
- user_role := util.JyMysqls.InsertBySqlByTx(tx, `insert into entniche_user_role (user_id,role_id) values (?,?)`, userId, 1)
- return entId > 0 && userId > 0 && deptId > 0 && dept_user > -1 && user_role > -1
- })
- }
- if flag {
- log.Debug("开通成功!", zap.Int64("entId", entId), zap.Int64("deptId", deptId))
- } else {
- log.Debug("开通失败!")
- }
- return int(entId), int(userId)
- }
- // 创建新用户
- func NewUser(companyName, phone, startTime, endTime string, rechargeNum int) {
- t := time.Now()
- appid := GetAppid(t.Unix())
- key := common.GetComplexRandom(8, 3, 5)
- s, err := time.ParseInLocation(date.Date_Full_Layout, startTime, time.Local)
- if err != nil {
- log.Fatal("", zap.Error(err))
- }
- startdate := s.Unix()
- e, err := time.ParseInLocation(date.Date_Full_Layout, endTime, time.Local)
- if err != nil {
- log.Fatal("", zap.Error(err))
- }
- enddate := e.Unix()
- util.MgoSave.Save("user", &map[string]interface{}{
- "username": companyName,
- "createtime": t.Unix(),
- "appid": appid,
- "key": key,
- "bnormal": int32(1),
- "phone": phone,
- "plan": map[string]interface{}{
- "name": "A1",
- "starttime": startdate,
- "endtime": enddate,
- "current": rechargeNum,
- },
- })
- }
- func GetAppid(tn int64) (appid string) {
- for {
- randomstr := common.GetLetterRandom(5)
- str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:])
- appid = EncodeUtil.EncodeString(str)
- if strReg.MatchString(appid) {
- break
- }
- }
- appid = "jy" + appid
- return
- }
|