service.go 29 KB

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