service.go 23 KB

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