service.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "bp.jydev.jianyu360.cn/BaseService/entManageApplication/entity"
  10. "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
  11. "context"
  12. . "app.yhyue.com/moapp/jybase/date"
  13. "app.yhyue.com/moapp/jybase/mongodb"
  14. . "bp.jydev.jianyu360.cn/BaseService/entManageApplication/entity"
  15. "bp.jydev.jianyu360.cn/BaseService/entManageApplication/rpc/entmanageapplication"
  16. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/powercheck"
  17. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  18. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  19. // "github.com/zeromicro/go-zero/core/logx"
  20. "app.yhyue.com/moapp/jybase/common"
  21. "app.yhyue.com/moapp/jybase/redis"
  22. )
  23. const (
  24. YYSSJDZYFX = "运营商数据定制及分析"
  25. )
  26. func AddUsePerson(this *entmanageapplication.AddUsePersonReq) *entmanageapplication.AddUsePersonResp {
  27. timeStr, ok, id, msg, code := time.Now().Format("2006-01-02 15:04:05"), false, int64(0), "", int64(0)
  28. if this.EntUserId == "" {
  29. return &entmanageapplication.AddUsePersonResp{
  30. ErrorCode: -1,
  31. ErrorMsg: "添加失败",
  32. Data: &entmanageapplication.AddUsePerson{Status: int64(-1)},
  33. }
  34. }
  35. UserArr := strings.Split(this.EntUserId, ",")
  36. sourceData := JyMysql.FindOne(EntnicheWaitEmpower, map[string]interface{}{"id": this.WaitEmpowerId}, "", "")
  37. if sourceData != nil && len(*sourceData) > 0 {
  38. empower_count := common.IntAll((*sourceData)["empower_count"])
  39. use_count := common.IntAll((*sourceData)["use_count"])
  40. if empower_count-use_count < len(UserArr) {
  41. msg = "购买数量不足"
  42. code = int64(-1)
  43. } else {
  44. for _, v := range UserArr {
  45. //过滤空值2023-01-04
  46. if v == "" {
  47. continue
  48. }
  49. //
  50. entUserId, _ := strconv.Atoi(v)
  51. if common.ObjToString((*sourceData)["product_type"]) != YYSSJDZYFX {
  52. datas := JyMysql.SelectBySql(`select a.end_time from `+EntnicheWaitEmpower+` a inner join `+EntnichePower+` b on (a.product_type<>? and b.ent_user_id=? and b.status=1 and a.id=b.wait_empower_id)`, YYSSJDZYFX, entUserId)
  53. if datas != nil && len(*datas) > 0 {
  54. end_time := common.ObjToString((*datas)[0]["end_time"])
  55. res, _ := time.ParseInLocation("2006-01-02 15:04:05", end_time, time.Local)
  56. if res.Unix() > time.Now().Unix() {
  57. msg = "人员已经拥有权限,请先删除原有权限"
  58. code = int64(-1)
  59. break
  60. }
  61. }
  62. }
  63. log.Println("------------1")
  64. if JyMysql.CountBySql(`select count(1) as count from `+EntnichePower+` where wait_empower_id=? and ent_id=? and ent_user_id=? and status=?`, this.WaitEmpowerId, this.EntId, entUserId, 1) == 0 {
  65. log.Println("------------2")
  66. id = JyMysql.Insert(EntnichePower, map[string]interface{}{
  67. "wait_empower_id": this.WaitEmpowerId,
  68. "ent_id": this.EntId,
  69. "ent_user_id": entUserId,
  70. "status": 1,
  71. "create_time": timeStr,
  72. "update_time": timeStr,
  73. })
  74. log.Println("------------3")
  75. JyMysql.UpdateOrDeleteBySql(`update `+EntnicheWaitEmpower+` set use_count = use_count + 1 where id = ?`, this.WaitEmpowerId)
  76. log.Println("------------4")
  77. addPower(this.WaitEmpowerId, entUserId, this.EntId)
  78. }
  79. }
  80. }
  81. }
  82. status := 0
  83. if ok || id > 0 {
  84. status = 1
  85. }
  86. return &entmanageapplication.AddUsePersonResp{
  87. ErrorCode: code,
  88. ErrorMsg: msg,
  89. Data: &entmanageapplication.AddUsePerson{Status: int64(status)},
  90. }
  91. }
  92. func DelUsePerson(this *entmanageapplication.DelUsePersonReq) *entmanageapplication.AddUsePersonResp {
  93. timeStr := time.Now().Format("2006-01-02 15:04:05")
  94. ok := JyMysql.Update(EntnichePower, map[string]interface{}{"id": this.EntnichePowerId},
  95. map[string]interface{}{"status": -1, "update_time": timeStr})
  96. delPower(this.EntnichePowerId)
  97. status := 0
  98. if ok {
  99. status = 1
  100. }
  101. return &entmanageapplication.AddUsePersonResp{
  102. ErrorCode: 0,
  103. Data: &entmanageapplication.AddUsePerson{Status: int64(status)},
  104. }
  105. }
  106. func UsePersonList(this *entmanageapplication.UsePersonListReq) *entmanageapplication.UsePersonListResp {
  107. sourceData := JyMysql.FindOne(EntnicheWaitEmpower, map[string]interface{}{"id": this.WaitEmpowerId}, "", "")
  108. nowTime := time.Now().Format("2006-01-02 15:04:05")
  109. q := `select h.product_type,a.id,a.wait_empower_id,c.name,c.phone,c.mail,g.name as department,e.name as role from entniche_power a
  110. INNER JOIN entniche_user c on (a.ent_user_id=c.id and a.ent_id = c.ent_id)
  111. LEFT JOIN entniche_user_role d on (c.id=d.user_id)
  112. LEFT JOIN entniche_role e on (d.role_id=e.id)
  113. LEFT JOIN entniche_department_user f on (a.ent_user_id=f.user_id)
  114. LEFT JOIN entniche_department g on (g.id=f.dept_id)
  115. LEFT JOIN entniche_wait_empower h on (h.id=a.wait_empower_id)
  116. where a.status = 1 and a.ent_id = ? and h.end_time >= ?`
  117. if common.ObjToString((*sourceData)["product_type"]) == YYSSJDZYFX {
  118. q += ` and h.product_type='` + YYSSJDZYFX + `'`
  119. } else {
  120. q += ` and h.product_type<>'` + YYSSJDZYFX + `'`
  121. }
  122. q += ` order by a.update_time,a.id desc`
  123. data := JyMysql.SelectBySql(q, this.EntId, nowTime)
  124. arr := []*entmanageapplication.UsePersonList{}
  125. if data != nil && len(*data) > 0 {
  126. for _, v := range *data {
  127. wait_empower_id := common.Int64All(v["wait_empower_id"])
  128. obj := &entmanageapplication.UsePersonList{
  129. EntnichePowerId: common.Int64All(v["id"]),
  130. Name: common.ObjToString(v["name"]),
  131. Phone: common.ObjToString(v["phone"]),
  132. Email: common.ObjToString(v["mail"]),
  133. Department: common.ObjToString(v["department"]),
  134. Role: common.ObjToString(v["role"]),
  135. }
  136. if wait_empower_id == this.WaitEmpowerId {
  137. obj.Status = 1
  138. } else {
  139. obj.Status = 2
  140. }
  141. arr = append(arr, obj)
  142. }
  143. }
  144. return &entmanageapplication.UsePersonListResp{
  145. ErrorCode: 0,
  146. Data: arr,
  147. }
  148. }
  149. func BuyProductList(this *entmanageapplication.BuyProductListReq) *entmanageapplication.BuyProductListResp {
  150. nowTime := time.Now().Format("2006-01-02 15:04:05")
  151. data := JyMysql.SelectBySql(`select * from `+EntnicheWaitEmpower+` where ent_id = ? and end_time >= ? order by create_time desc`, this.EntId, nowTime)
  152. arr := []*entmanageapplication.BuyProductList{}
  153. if data != nil && len(*data) > 0 {
  154. for _, v := range *data {
  155. product_type := common.ObjToString(v["product_type"])
  156. if product_type == "VIP订阅" {
  157. product_type = "超级订阅"
  158. }
  159. productTypeSuffix := ""
  160. if provinceCount := common.IntAllDef(v["province_count"], -1); provinceCount > 0 {
  161. switch provinceCount {
  162. case 1:
  163. productTypeSuffix = "单省版"
  164. default:
  165. productTypeSuffix = "省份版"
  166. }
  167. }
  168. obj := &entmanageapplication.BuyProductList{
  169. WaitEmpowerId: common.Int64All(v["id"]),
  170. ProductType: fmt.Sprintf("%s%s", product_type, productTypeSuffix),
  171. UseCount: common.Int64All(v["use_count"]),
  172. EmpowerCount: common.Int64All(v["empower_count"]),
  173. ProvinceCount: common.Int64All(v["province_count"]),
  174. EndTime: common.ObjToString(v["end_time"]),
  175. }
  176. arr = append(arr, obj)
  177. }
  178. }
  179. return &entmanageapplication.BuyProductListResp{
  180. ErrorCode: 0,
  181. Data: arr,
  182. }
  183. }
  184. func addPower(waitEmpowerId int64, entUserId int, entId int64) {
  185. identity := getPositionId(entUserId)
  186. ResourcePowerHandle(identity, waitEmpowerId, 1)
  187. data := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "")
  188. if data != nil && len(*data) > 0 {
  189. orderId := common.Int64All((*data)["order_id"])
  190. orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "")
  191. if orderData != nil && len(*orderData) > 0 {
  192. productType := common.ObjToString((*orderData)["product_type"])
  193. if productType == "VIP订阅" {
  194. log.Println("开通超级订阅", identity.PositionId)
  195. setRule(1, int64(entUserId), entId, *orderData, identity.PositionId)
  196. } else if productType == "大会员" {
  197. log.Println("开通大会员", identity.PositionId)
  198. setRule(2, int64(entUserId), entId, *orderData, identity.PositionId)
  199. }
  200. }
  201. }
  202. }
  203. func GetServerPid() map[int]int {
  204. pidMap := map[int]int{}
  205. datas := JyMysql.Find("bigmember_service", map[string]interface{}{"i_status": 0}, "id,i_pid", "", 0, 0)
  206. if datas != nil && len(*datas) > 0 {
  207. for _, v := range *datas {
  208. if v["i_pid"] != nil {
  209. pidMap[common.IntAll(v["id"])] = common.IntAll(v["i_pid"])
  210. }
  211. }
  212. }
  213. return pidMap
  214. }
  215. func RedisDel(userId string) {
  216. redis.Del(NewOther, "pl_indexMessage_"+userId)
  217. delRedisMenuByUserCenter(userId)
  218. delRedisByPowerCheck(userId)
  219. }
  220. // 初始化vip订阅关键词
  221. func MergeKws(entId, entUserId int64) {
  222. if entUserId == 0 || entId == 0 { //11-11 取消此操作
  223. return
  224. }
  225. data, ok := Mgo.FindOneByField("entniche_rule", map[string]interface{}{"i_entid": entId, "i_userid": entUserId}, `{"o_entniche":1,"_id":1}`)
  226. var o_entniche map[string]interface{}
  227. if ok && data != nil && len(*data) > 0 {
  228. o_entniche, _ = (*data)["o_entniche"].(map[string]interface{})
  229. a_items, _ := o_entniche["a_items"].([]interface{})
  230. if a_items == nil { //首次
  231. _id := mongodb.BsonIdToSId((*data)["_id"])
  232. Mgo.UpdateById("entniche_rule", _id, map[string]interface{}{
  233. "$set": map[string]interface{}{"o_entniche.i_matchway": 1, "o_entniche.i_ratemode": 1, "o_entniche.i_wxpush": 1, "o_entniche.i_apppush": 1, "o_entniche.i_projectmatch": 0, "o_entniche.a_infotype": []string{}, "o_entniche.a_items": []string{}, "o_entniche.l_modifydate": time.Now().Unix()},
  234. })
  235. }
  236. }
  237. }
  238. func delPower(id int64) {
  239. data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"id": id}, "", "")
  240. if data != nil && len(*data) > 0 {
  241. waitEmpowerId := common.Int64All((*data)["wait_empower_id"])
  242. entUserId := common.IntAll((*data)["ent_user_id"])
  243. JyMysql.UpdateOrDeleteBySql(`update `+EntnicheWaitEmpower+` set use_count = use_count - 1 where id = ? and use_count > 0`, waitEmpowerId)
  244. identity := getPositionId(entUserId)
  245. eOData := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "")
  246. if eOData != nil && len(*eOData) > 0 {
  247. orderId := common.Int64All((*eOData)["order_id"])
  248. orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "")
  249. set := map[string]interface{}{}
  250. if orderData != nil && len(*orderData) > 0 {
  251. productType := common.ObjToString((*orderData)["product_type"])
  252. entId := common.Int64All((*orderData)["ent_id"])
  253. if productType == "VIP订阅" {
  254. set["l_vip_endtime"] = time.Now().Unix()
  255. set["i_vip_status"] = 0
  256. } else if productType == "大会员" {
  257. set["i_member_endtime"] = time.Now().Unix()
  258. set["i_member_status"] = 0
  259. JyMysql.Update(BigServiceUser, map[string]interface{}{"s_userid": common.InterfaceToStr(identity.PositionId)}, map[string]interface{}{"i_status": -1})
  260. }
  261. Mgo.Update("ent_user", map[string]interface{}{
  262. "i_entid": entId,
  263. "i_userid": entUserId,
  264. }, map[string]interface{}{
  265. "$set": set,
  266. }, true, false)
  267. posititonIdStr := strconv.Itoa(int(identity.PositionId))
  268. RedisDel(posititonIdStr)
  269. }
  270. }
  271. ResourcePowerHandle(identity, waitEmpowerId, -1)
  272. }
  273. }
  274. // 获取用户信息
  275. // return 职位id
  276. func getPositionId(entUserId int) *usercenter.Identity {
  277. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  278. res, _ := UserCenterRpc.IdentityByEntUserId(ctx, &usercenter.IdentityReq{
  279. Id: int64(entUserId),
  280. })
  281. return res
  282. }
  283. // 订阅设置
  284. // types:1超级订阅 2大会员
  285. func setRule(types int64, entUserId, entId int64, orderData map[string]interface{}, posititonId int64) bool {
  286. //订阅设置
  287. query := map[string]interface{}{
  288. "i_entid": entId,
  289. "i_userid": entUserId,
  290. }
  291. //orderData相关权益
  292. set := map[string]interface{}{}
  293. setEntUser := map[string]interface{}{}
  294. //超级订阅
  295. if types == 1 {
  296. set, setEntUser = vipSetMap(orderData)
  297. } else if types == 2 {
  298. set, setEntUser = memberSetMap(posititonId, orderData)
  299. }
  300. ok1 := Mgo.Update("entniche_rule", query, map[string]interface{}{
  301. "$set": set,
  302. }, true, false)
  303. ok2 := Mgo.Update("ent_user", query, map[string]interface{}{
  304. "$set": setEntUser,
  305. }, true, false)
  306. go func() {
  307. MergeKws(entId, entUserId)
  308. posititonIdStr := strconv.Itoa(int(posititonId))
  309. RedisDel(posititonIdStr)
  310. if types == 1 {
  311. now1 := time.Now()
  312. currentYear, currentMonth, _ := now1.Date()
  313. currentLocation := now1.Location()
  314. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  315. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  316. endTime := lastOfMonth.Format("2006-01-02")
  317. vipTime := time.Unix(common.Int64All(setEntUser["l_vip_endtime"]), 0).Format("2006-01-02")
  318. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  319. ResourceCenterRpc.PurchaseUserBalance(ctx, &resourcesCenterclient.Resources{
  320. AppId: "10000",
  321. UserId: common.InterfaceToStr(posititonId),
  322. ResourceType: "附件下载包",
  323. AccountId: common.InterfaceToStr(posititonId),
  324. Name: "附件下载包",
  325. Number: 10,
  326. Spec: "个",
  327. EndTime: endTime,
  328. VipTime: vipTime,
  329. Model: 3,
  330. })
  331. }
  332. }()
  333. return ok1 && ok2
  334. }
  335. func vipSetMap(orderData map[string]interface{}) (set map[string]interface{}, setEntUser map[string]interface{}) {
  336. set, setEntUser = map[string]interface{}{}, map[string]interface{}{}
  337. startTime := common.ObjToString(orderData["vip_starttime"])
  338. endTime := common.ObjToString(orderData["vip_endtime"])
  339. Date_Full_Layout := "2006-01-02 15:04:05"
  340. startdate, _ := time.ParseInLocation(Date_Full_Layout, startTime, time.Local)
  341. enddate, _ := time.ParseInLocation(Date_Full_Layout, endTime, time.Local)
  342. dataType := 1
  343. if startdate.Unix() > time.Now().Unix() {
  344. dataType = 2
  345. }
  346. //为用户开通超级订阅权限
  347. //是否开通超级订阅 -1 试用到期 -2 正式到期 1 试用 2 开通
  348. isTrial := false
  349. if dataType == 2 {
  350. isTrial = true
  351. }
  352. setEntUser["l_createtime"] = time.Now().Unix()
  353. setEntUser["l_vip_starttime"] = startdate.Unix()
  354. setEntUser["l_vip_endtime"] = enddate.Unix()
  355. setEntUser["i_vip_status"] = common.If(isTrial, -2, 2) //1试用 2正式 -2 试用到期
  356. vms := VipSimpleMsg{}
  357. json.Unmarshal([]byte(common.ObjToString(orderData["filter"])), &vms)
  358. set = map[string]interface{}{
  359. "o_entniche": map[string]interface{}{
  360. "i_trial": -1, //已激活试用
  361. "o_area": *vms.Area, //设置地区
  362. "o_buyset": *vms.NewBuyset, //购买内容 城市、省份、行业数量
  363. "a_buyerclass": []interface{}{}, //设置行业
  364. },
  365. "i_vip_expire_tip": 0, //消息提示初始化
  366. "i_type": 1,
  367. }
  368. return
  369. }
  370. func memberSetMap(userId int64, orderData map[string]interface{}) (set map[string]interface{}, setEntUser map[string]interface{}) {
  371. set, setEntUser = map[string]interface{}{}, map[string]interface{}{}
  372. filterMap := common.ObjToMap(orderData["filter"])
  373. level := common.IntAll((*filterMap)["level"])
  374. comboId := common.IntAll((*filterMap)["comboId"])
  375. payCycle := common.IntAll((*filterMap)["cycle"])
  376. serversId := common.ObjToString((*filterMap)["serversId"])
  377. startTime := common.ObjToString(orderData["vip_starttime"])
  378. endTime := common.ObjToString(orderData["vip_endtime"])
  379. tmp := "2006-01-02 15:04:05"
  380. startdate, _ := time.ParseInLocation(tmp, startTime, time.Local)
  381. enddate, _ := time.ParseInLocation(tmp, endTime, time.Local)
  382. serverMap := GetServerPid()
  383. dataType := 1
  384. if startdate.Unix() > time.Now().Unix() {
  385. dataType = 2
  386. }
  387. var (
  388. areaCount = common.IntAllDef((*filterMap)["areaCount"], -1)
  389. area = common.If(areaCount > 0, map[string]interface{}{"北京": []string{}}, map[string]interface{}{}).(map[string]interface{})
  390. )
  391. areaCount = common.IntAllDef((*filterMap)["areaCount"], -1)
  392. set = map[string]interface{}{
  393. "i_type": 1,
  394. "o_member_jy": map[string]interface{}{
  395. "i_wxpush": 1,
  396. },
  397. "o_entniche.i_areacount": areaCount,
  398. "o_entniche.o_area": area, //默认北京
  399. }
  400. setEntUser = map[string]interface{}{
  401. "i_member_status": common.If(dataType == 1, level, -level),
  402. "i_member_starttime": startdate.Unix(),
  403. "i_member_endtime": enddate.Unix(),
  404. "i_mainaccount": 1,
  405. "l_createtime": time.Now().Unix(),
  406. }
  407. if comboId != 0 {
  408. datas := JyMysql.FindOne("bigmember_combo", map[string]interface{}{"id": comboId}, "", "")
  409. if datas == nil || len(*datas) == 0 {
  410. log.Println("未找到此套餐")
  411. return
  412. }
  413. s_servers := common.ObjToString((*datas)["s_servers"])
  414. for _, s := range strings.Split(s_servers, ",") {
  415. serverId, _ := strconv.Atoi(s)
  416. mainId := serverId
  417. if serverMap[serverId] != 0 {
  418. serverId = serverMap[serverId]
  419. }
  420. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  421. frequency := 0
  422. if serverData != nil && len(*serverData) > 0 {
  423. if serverId == 11 || serverId == 15 {
  424. if serverId == 15 {
  425. if payCycle >= 12 {
  426. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  427. } else {
  428. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  429. }
  430. } else {
  431. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  432. }
  433. } else {
  434. frequency = common.IntAll((*serverData)["s_count_month"])
  435. }
  436. }
  437. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status = -1"
  438. serverDatas := JyMysql.SelectBySql(sqls, serverId, common.InterfaceToStr(userId))
  439. if serverDatas != nil && len(*serverDatas) > 0 {
  440. i_status := common.If(dataType == 1, 0, 1)
  441. sql := "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  442. status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, common.InterfaceToStr(userId))
  443. if status1 < 0 {
  444. log.Println("新建服务-更新服务表出错", userId)
  445. return
  446. }
  447. } else {
  448. insert := map[string]interface{}{
  449. "s_smainid": mainId,
  450. "s_userid": userId,
  451. "s_serviceid": serverId,
  452. "i_frequency": frequency,
  453. "l_starttime": startdate.Format(tmp),
  454. "l_endtime": enddate.Format(tmp),
  455. "i_status": common.If(dataType == 1, 0, 1),
  456. "l_createtime": time.Now().Format(tmp),
  457. "l_updatetime": time.Now().Format(tmp),
  458. }
  459. order_id := JyMysql.Insert(BigServiceUser, insert)
  460. if order_id > 0 {
  461. log.Println("用户服务表插入成功", userId)
  462. } else {
  463. log.Println("用户服务表插入失败", userId)
  464. return
  465. }
  466. }
  467. }
  468. } else {
  469. for _, s := range strings.Split(serversId, ",") {
  470. serverId, _ := strconv.Atoi(s)
  471. mainId := serverId
  472. if serverMap[serverId] != 0 {
  473. serverId = serverMap[serverId]
  474. }
  475. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  476. frequency := 0
  477. if serverData != nil && len(*serverData) > 0 {
  478. if serverId == 11 || serverId == 15 {
  479. if serverId == 15 {
  480. if payCycle >= 12 {
  481. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  482. } else {
  483. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  484. }
  485. } else {
  486. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  487. }
  488. } else {
  489. frequency = common.IntAll((*serverData)["s_count_month"])
  490. }
  491. }
  492. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status = -1"
  493. serverDatas := JyMysql.SelectBySql(sqls, serverId, common.InterfaceToStr(userId))
  494. if serverDatas != nil && len(*serverDatas) > 0 {
  495. i_status := common.If(dataType == 1, 0, 1)
  496. sql := "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  497. status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, common.InterfaceToStr(userId))
  498. if status1 < 0 {
  499. log.Println("新建服务-更新服务表出错")
  500. return
  501. }
  502. } else {
  503. insert := map[string]interface{}{
  504. "s_userid": userId,
  505. "s_smainid": mainId,
  506. "s_serviceid": serverId,
  507. "i_frequency": frequency,
  508. "l_starttime": startdate.Format(tmp),
  509. "l_endtime": enddate.Format(tmp),
  510. "i_status": common.If(dataType == 1, 0, 1),
  511. "l_createtime": time.Now().Format(tmp),
  512. "l_updatetime": time.Now().Format(tmp),
  513. }
  514. order_id := JyMysql.Insert(BigServiceUser, insert)
  515. if order_id > 0 {
  516. log.Println("用户服务表插入成功", userId)
  517. } else {
  518. log.Println("用户服务表插入失败")
  519. return
  520. }
  521. }
  522. }
  523. }
  524. return
  525. }
  526. func EmpowerUserIds(ids []int64) *entmanageapplication.EmpowerUserIds {
  527. result := &entmanageapplication.EmpowerUserIds{}
  528. array := []string{}
  529. for _, v := range ids {
  530. array = append(array, fmt.Sprint(v))
  531. }
  532. list := JyMysql.SelectBySql(`SELECT a.ent_user_id from entniche_power a inner join entniche_wait_empower b on (a.ent_user_id in (`+strings.Join(array, ",")+`) and a.status=1 and b.end_time>? and a.wait_empower_id=b.id) ORDER BY a.create_time desc`, NowFormat(Date_Full_Layout))
  533. if list != nil {
  534. for _, v := range *list {
  535. result.Ids = append(result.Ids, common.Int64All(v["ent_user_id"]))
  536. }
  537. }
  538. return result
  539. }
  540. // 调用用户中台清除菜单缓存
  541. func delRedisMenuByUserCenter(positionId string) bool {
  542. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  543. res, err := UserCenterRpc.WorkDesktopClearUserInfo(ctx, &usercenter.WorkDesktopClearUserInfoReq{
  544. AppId: "10000",
  545. PositionId: positionId,
  546. })
  547. if err != nil || res == nil {
  548. log.Println("delRedisMenuByUserCenter err", err)
  549. }
  550. return true
  551. }
  552. // 清除权益中台缓存
  553. func delRedisByPowerCheck(positionId string) bool {
  554. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  555. i_positionId, _ := strconv.Atoi(positionId)
  556. res, err := PowerCheckCenterRpc.DelCheckRedis(ctx, &powercheck.CheckReq{
  557. PositionId: int64(i_positionId),
  558. Appid: "10000",
  559. })
  560. if err != nil || res == nil {
  561. log.Println("delRedisByPowerCheck err", err)
  562. }
  563. return true
  564. }
  565. //
  566. func ResourcePowerHandle(identity *usercenter.Identity, waitEmpowerId, t int64) {
  567. //如果是运营商数据定制及分析产品,调用资源中台,取消对应菜单权限
  568. log.Println("------------5")
  569. if JyMysql.CountBySql(`select count(1) as count from `+EntnicheWaitEmpower+` where id=? and product_type=?`, waitEmpowerId, YYSSJDZYFX) > 0 {
  570. log.Println("-------", identity.EntId, waitEmpowerId, t)
  571. if t == 1 {
  572. cc, err := entity.ResourceRpc.Empower(context.Background(), &pb.EmpowerReq{
  573. Appid: "10000",
  574. FunctionCode: "bi_sj_yyszs",
  575. EntId: identity.EntId,
  576. EntUserId: []int64{identity.EntUserId},
  577. })
  578. log.Println(err, fmt.Sprintf("%+v", cc))
  579. } else {
  580. entity.ResourceRpc.CancelEmpower(context.Background(), &pb.EmpowerReq{
  581. Appid: "10000",
  582. FunctionCode: "bi_sj_yyszs",
  583. EntId: identity.EntId,
  584. EntUserId: []int64{identity.EntUserId},
  585. })
  586. }
  587. }
  588. }