service.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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,a.product_key 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. Key: common.ObjToString(v["product_key"]),
  198. }
  199. arr = append(arr, obj)
  200. }
  201. }
  202. return &entmanageapplication.BuyProductListResp{
  203. ErrorCode: 0,
  204. Data: arr,
  205. }
  206. }
  207. func addPower(waitEmpowerId int64, identity *usercenter.Identity) {
  208. data := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "")
  209. if data != nil && len(*data) > 0 {
  210. orderId := common.Int64All((*data)["order_id"])
  211. orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "")
  212. if orderData != nil && len(*orderData) > 0 {
  213. productType := common.ObjToString((*orderData)["product_type"])
  214. if productType == "VIP订阅" {
  215. log.Println("开通超级订阅", identity.PositionId)
  216. set, setEntUser := vipSetMap(*orderData)
  217. setRule(identity, set, setEntUser)
  218. now1 := time.Now()
  219. currentYear, currentMonth, _ := now1.Date()
  220. currentLocation := now1.Location()
  221. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  222. lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
  223. endTime := lastOfMonth.Format("2006-01-02")
  224. vipTime := time.Unix(common.Int64All(setEntUser["l_vip_endtime"]), 0).Format("2006-01-02")
  225. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  226. ResourceCenterRpc.PurchaseUserBalance(ctx, &resourcesCenterclient.Resources{
  227. AppId: "10000",
  228. UserId: fmt.Sprint(identity.PositionId),
  229. ResourceType: "附件下载包",
  230. AccountId: fmt.Sprint(identity.PositionId),
  231. Name: "附件下载包",
  232. Number: 10,
  233. Spec: "个",
  234. EndTime: endTime,
  235. VipTime: vipTime,
  236. Model: 3,
  237. })
  238. } else if productType == "大会员" {
  239. log.Println("开通大会员", identity.PositionId)
  240. set, setEntUser := memberSetMap(identity.PositionId, waitEmpowerId, *orderData)
  241. setRule(identity, set, setEntUser)
  242. }
  243. }
  244. }
  245. }
  246. func GetServerPid() map[int]int {
  247. pidMap := map[int]int{}
  248. datas := JyMysql.Find("bigmember_service", map[string]interface{}{"i_status": 0}, "id,i_pid", "", 0, 0)
  249. if datas != nil && len(*datas) > 0 {
  250. for _, v := range *datas {
  251. if v["i_pid"] != nil {
  252. pidMap[common.IntAll(v["id"])] = common.IntAll(v["i_pid"])
  253. }
  254. }
  255. }
  256. return pidMap
  257. }
  258. func RedisDel(userId string) {
  259. redis.Del(NewOther, "pl_indexMessage_"+userId)
  260. delRedisMenuByUserCenter(userId)
  261. delRedisByPowerCheck(userId)
  262. }
  263. // 初始化vip订阅关键词
  264. func MergeKws(entId, entUserId int64) {
  265. if entUserId == 0 || entId == 0 { //11-11 取消此操作
  266. return
  267. }
  268. data, ok := Mgo.FindOneByField("entniche_rule", map[string]interface{}{"i_entid": entId, "i_userid": entUserId}, `{"o_entniche":1,"_id":1}`)
  269. var o_entniche map[string]interface{}
  270. if ok && data != nil && len(*data) > 0 {
  271. o_entniche, _ = (*data)["o_entniche"].(map[string]interface{})
  272. a_items, _ := o_entniche["a_items"].([]interface{})
  273. if a_items == nil { //首次
  274. _id := mongodb.BsonIdToSId((*data)["_id"])
  275. Mgo.UpdateById("entniche_rule", _id, map[string]interface{}{
  276. "$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()},
  277. })
  278. }
  279. }
  280. }
  281. func delPower(id int64) {
  282. 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)
  283. if datas != nil && len(*datas) > 0 {
  284. entUserId := common.IntAll((*datas)[0]["ent_user_id"])
  285. productType := common.ObjToString((*datas)[0]["product_type"])
  286. entId := common.Int64All((*datas)[0]["ent_id"])
  287. identity := getPositionId(entUserId)
  288. if productType == YYSZSYY {
  289. ResourcePowerHandle(identity, -1, "", "")
  290. }
  291. set := map[string]interface{}{}
  292. if strings.Contains(productType, "VIP订阅") {
  293. set["l_vip_endtime"] = time.Now().Unix()
  294. set["i_vip_status"] = 0
  295. } else if strings.Contains(productType, "大会员") || productType == YYSZSYY {
  296. go DelPdfPack(common.InterfaceToStr(identity.PositionId), entId)
  297. set["i_member_endtime"] = time.Now().Unix()
  298. set["i_member_status"] = 0
  299. JyMysql.Update(BigServiceUser, map[string]interface{}{"s_userid": common.InterfaceToStr(identity.PositionId)}, map[string]interface{}{"i_status": -1, "l_endtime": NowFormat(Date_Full_Layout)})
  300. }
  301. if len(set) > 0 {
  302. Mgo.Update("ent_user", map[string]interface{}{
  303. "i_entid": entId,
  304. "i_userid": entUserId,
  305. }, map[string]interface{}{
  306. "$set": set,
  307. }, true, false)
  308. RedisDel(strconv.Itoa(int(identity.PositionId)))
  309. }
  310. }
  311. }
  312. // 获取用户信息
  313. // return 职位id
  314. func getPositionId(entUserId int) *usercenter.Identity {
  315. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  316. res, _ := UserCenterRpc.IdentityByEntUserId(ctx, &usercenter.IdentityReq{
  317. Id: int64(entUserId),
  318. })
  319. return res
  320. }
  321. // 订阅设置
  322. // types:1超级订阅 2大会员
  323. func setRule(identity *usercenter.Identity, set, setEntUser map[string]interface{}) bool {
  324. //订阅设置
  325. query := map[string]interface{}{
  326. "i_entid": identity.EntId,
  327. "i_userid": identity.EntUserId,
  328. }
  329. //orderData相关权益
  330. ok1 := Mgo.Update("entniche_rule", query, map[string]interface{}{
  331. "$set": set,
  332. }, true, false)
  333. ok2 := Mgo.Update("ent_user", query, map[string]interface{}{
  334. "$set": setEntUser,
  335. }, true, false)
  336. go func() {
  337. MergeKws(identity.EntId, identity.EntUserId)
  338. RedisDel(strconv.Itoa(int(identity.PositionId)))
  339. }()
  340. return ok1 && ok2
  341. }
  342. func vipSetMap(orderData map[string]interface{}) (set map[string]interface{}, setEntUser map[string]interface{}) {
  343. set, setEntUser = map[string]interface{}{}, map[string]interface{}{}
  344. startTime := common.ObjToString(orderData["vip_starttime"])
  345. endTime := common.ObjToString(orderData["vip_endtime"])
  346. Date_Full_Layout := "2006-01-02 15:04:05"
  347. startdate, _ := time.ParseInLocation(Date_Full_Layout, startTime, time.Local)
  348. enddate, _ := time.ParseInLocation(Date_Full_Layout, endTime, time.Local)
  349. dataType := 1
  350. if startdate.Unix() > time.Now().Unix() {
  351. dataType = 2
  352. }
  353. //为用户开通超级订阅权限
  354. //是否开通超级订阅 -1 试用到期 -2 正式到期 1 试用 2 开通
  355. isTrial := false
  356. if dataType == 2 {
  357. isTrial = true
  358. }
  359. setEntUser["l_createtime"] = time.Now().Unix()
  360. setEntUser["l_vip_starttime"] = startdate.Unix()
  361. setEntUser["l_vip_endtime"] = enddate.Unix()
  362. setEntUser["i_vip_status"] = common.If(isTrial, -2, 2) //1试用 2正式 -2 试用到期
  363. vms := VipSimpleMsg{}
  364. json.Unmarshal([]byte(common.ObjToString(orderData["filter"])), &vms)
  365. set = map[string]interface{}{
  366. "o_entniche": map[string]interface{}{
  367. "i_trial": -1, //已激活试用
  368. "o_area": *vms.Area, //设置地区
  369. "o_buyset": *vms.NewBuyset, //购买内容 城市、省份、行业数量
  370. "a_buyerclass": []interface{}{}, //设置行业
  371. },
  372. "i_vip_expire_tip": 0, //消息提示初始化
  373. "i_type": 1,
  374. }
  375. return
  376. }
  377. func memberSetMap(userId, waitEmpowerId int64, orderData map[string]interface{}) (set map[string]interface{}, setEntUser map[string]interface{}) {
  378. set, setEntUser = map[string]interface{}{}, map[string]interface{}{}
  379. filterMap := common.ObjToMap(orderData["filter"])
  380. level := common.IntAll((*filterMap)["level"])
  381. comboId := common.IntAll((*filterMap)["comboId"])
  382. payCycle := common.IntAll((*filterMap)["cycle"])
  383. serversId := common.ObjToString((*filterMap)["serversId"])
  384. startTime := common.ObjToString(orderData["vip_starttime"])
  385. endTime := common.ObjToString(orderData["vip_endtime"])
  386. tmp := "2006-01-02 15:04:05"
  387. startdate, _ := time.ParseInLocation(tmp, startTime, time.Local)
  388. enddate, _ := time.ParseInLocation(tmp, endTime, time.Local)
  389. serverMap := GetServerPid()
  390. dataType := 1
  391. if startdate.Unix() > time.Now().Unix() {
  392. dataType = 2
  393. }
  394. var (
  395. areaCount = common.IntAllDef((*filterMap)["areaCount"], -1)
  396. area = common.If(areaCount > 0, map[string]interface{}{"北京": []string{}}, map[string]interface{}{}).(map[string]interface{})
  397. )
  398. areaCount = common.IntAllDef((*filterMap)["areaCount"], -1)
  399. set = map[string]interface{}{
  400. "i_type": 1,
  401. "o_member_jy": map[string]interface{}{
  402. "i_wxpush": 1,
  403. },
  404. "o_entniche.i_areacount": areaCount,
  405. "o_entniche.o_area": area, //默认北京
  406. }
  407. setEntUser = map[string]interface{}{
  408. "i_member_status": common.If(dataType == 1, level, -level),
  409. "i_member_starttime": startdate.Unix(),
  410. "i_member_endtime": enddate.Unix(),
  411. "i_mainaccount": 1,
  412. "l_createtime": time.Now().Unix(),
  413. }
  414. if comboId != 0 {
  415. datas := JyMysql.FindOne("bigmember_combo", map[string]interface{}{"id": comboId}, "", "")
  416. if datas == nil || len(*datas) == 0 {
  417. log.Println("未找到此套餐")
  418. return
  419. }
  420. s_servers := common.ObjToString((*datas)["s_servers"])
  421. for _, s := range strings.Split(s_servers, ",") {
  422. serverId, _ := strconv.Atoi(s)
  423. mainId := serverId
  424. if serverMap[serverId] != 0 {
  425. serverId = serverMap[serverId]
  426. }
  427. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  428. frequency := 0
  429. if serverData != nil && len(*serverData) > 0 {
  430. var (
  431. resourceType string
  432. )
  433. if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "市场分析定制报告pdf下载") {
  434. resourceType = "市场分析定制报告下载包"
  435. } else if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "业主采购分析报告pdf下载") {
  436. resourceType = "业主采购分析报告下载包"
  437. } else if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "企业中标分析报告pdf下载") {
  438. resourceType = "企业中标分析报告下载包"
  439. }
  440. if resourceType != "" {
  441. number := common.IntAll((*serverData)["s_count_year"])
  442. cycle := common.IntAll((*filterMap)["cycle"]) //购买时长
  443. cycleType := common.IntAll((*filterMap)["cycleType"]) //时间类型
  444. cycleFmt := common.If(cycleType == 0, 12, 366).(int)
  445. number = number * (cycle / cycleFmt)
  446. frequency = number
  447. if number > 0 {
  448. if err := AddPdfPack(common.InterfaceToStr(userId), resourceType, common.Int64All(orderData["ent_id"]), waitEmpowerId); err != nil {
  449. log.Println("AddPdfPack err ", err)
  450. }
  451. }
  452. }
  453. if frequency == 0 {
  454. if serverId == 11 || serverId == 15 {
  455. if serverId == 15 {
  456. if payCycle >= 12 {
  457. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  458. } else {
  459. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  460. }
  461. } else {
  462. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  463. }
  464. } else {
  465. frequency = common.IntAll((*serverData)["s_count_month"])
  466. }
  467. }
  468. }
  469. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status = -1"
  470. serverDatas := JyMysql.SelectBySql(sqls, serverId, fmt.Sprint(userId))
  471. if serverDatas != nil && len(*serverDatas) > 0 {
  472. i_status := common.If(dataType == 1, 0, 1)
  473. sql := "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  474. 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))
  475. if status1 < 0 {
  476. log.Println("新建服务-更新服务表出错", userId)
  477. return
  478. }
  479. } else {
  480. insert := map[string]interface{}{
  481. "s_smainid": mainId,
  482. "s_userid": userId,
  483. "s_serviceid": serverId,
  484. "i_frequency": frequency,
  485. "l_starttime": startdate.Format(tmp),
  486. "l_endtime": enddate.Format(tmp),
  487. "i_status": common.If(dataType == 1, 0, 1),
  488. "l_createtime": time.Now().Format(tmp),
  489. "l_updatetime": time.Now().Format(tmp),
  490. }
  491. order_id := JyMysql.Insert(BigServiceUser, insert)
  492. if order_id > 0 {
  493. log.Println("用户服务表插入成功", userId)
  494. } else {
  495. log.Println("用户服务表插入失败", userId)
  496. return
  497. }
  498. }
  499. }
  500. } else {
  501. for _, s := range strings.Split(serversId, ",") {
  502. serverId, _ := strconv.Atoi(s)
  503. mainId := serverId
  504. if serverMap[serverId] != 0 {
  505. serverId = serverMap[serverId]
  506. }
  507. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  508. frequency := 0
  509. if serverData != nil && len(*serverData) > 0 {
  510. var (
  511. resourceType string
  512. )
  513. if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "市场分析定制报告pdf下载") {
  514. resourceType = "市场分析定制报告下载包"
  515. } else if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "业主采购分析报告pdf下载") {
  516. resourceType = "业主采购分析报告下载包"
  517. } else if strings.Contains(common.InterfaceToStr((*serverData)["s_name"]), "企业中标分析报告pdf下载") {
  518. resourceType = "企业中标分析报告下载包"
  519. }
  520. if resourceType != "" {
  521. frequency = common.IntAll((*serverData)["s_count_year"])
  522. if err := AddPdfPack(common.InterfaceToStr(userId), resourceType, common.Int64All(orderData["ent_id"]), waitEmpowerId); err != nil {
  523. log.Println("AddPdfPack err ", err)
  524. }
  525. }
  526. if frequency == 0 {
  527. if serverId == 11 || serverId == 15 {
  528. if serverId == 15 {
  529. if payCycle >= 12 {
  530. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  531. } else {
  532. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  533. }
  534. } else {
  535. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  536. }
  537. } else {
  538. frequency = common.IntAll((*serverData)["s_count_month"])
  539. }
  540. }
  541. }
  542. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status = -1"
  543. serverDatas := JyMysql.SelectBySql(sqls, serverId, common.InterfaceToStr(userId))
  544. if serverDatas != nil && len(*serverDatas) > 0 {
  545. i_status := common.If(dataType == 1, 0, 1)
  546. sql := "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  547. 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))
  548. if status1 < 0 {
  549. log.Println("新建服务-更新服务表出错")
  550. return
  551. }
  552. } else {
  553. insert := map[string]interface{}{
  554. "s_userid": userId,
  555. "s_smainid": mainId,
  556. "s_serviceid": serverId,
  557. "i_frequency": frequency,
  558. "l_starttime": startdate.Format(tmp),
  559. "l_endtime": enddate.Format(tmp),
  560. "i_status": common.If(dataType == 1, 0, 1),
  561. "l_createtime": time.Now().Format(tmp),
  562. "l_updatetime": time.Now().Format(tmp),
  563. }
  564. order_id := JyMysql.Insert(BigServiceUser, insert)
  565. if order_id > 0 {
  566. log.Println("用户服务表插入成功", userId)
  567. } else {
  568. log.Println("用户服务表插入失败")
  569. return
  570. }
  571. }
  572. }
  573. }
  574. return
  575. }
  576. // AddPdfPack 增加pdf下载包权限
  577. // resourceType "企业中标分析报告下载包","业主采购分析报告下载包","市场分析定制报告下载包"
  578. func AddPdfPack(userId, resourceType string, entId, waitEmpowerId int64) error {
  579. log.Println("AddPdfPack==", userId, resourceType, entId, waitEmpowerId)
  580. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  581. _, err := ResourceCenterRpc.EntAccountGiven(ctx, &resourcesCenterclient.EntOperateReq{
  582. OperateAccountId: userId,
  583. EmpowerId: waitEmpowerId,
  584. CompanyId: entId,
  585. ResourceType: resourceType,
  586. })
  587. return err
  588. }
  589. // DelPdfPack 增加pdf下载包权限
  590. // resourceType "企业中标分析报告下载包","业主采购分析报告下载包","市场分析定制报告下载包"
  591. func DelPdfPack(userId string, entId int64) {
  592. log.Println("DelPdfPack===", userId, entId)
  593. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  594. res, err := ResourceCenterRpc.FindAccountBalance(ctx, &resourcesCenterclient.ResourcesReq{
  595. AccountId: userId,
  596. ResourceName: "pdf下载包",
  597. })
  598. if err != nil {
  599. log.Println("FindAccountBalance err", err.Error())
  600. return
  601. }
  602. for _, datum := range res.Data {
  603. if datum.Number > 0 {
  604. _, err = ResourceCenterRpc.EntAccountRecovery(ctx, &resourcesCenterclient.EntOperateReq{
  605. OperateAccountId: userId,
  606. CompanyId: entId,
  607. ResourceType: datum.ResourceType,
  608. })
  609. if err != nil {
  610. log.Println("EntAccountRecovery err", err.Error())
  611. }
  612. }
  613. }
  614. }
  615. func EmpowerUserIds(ids []int64) *entmanageapplication.EmpowerUserIds {
  616. result := &entmanageapplication.EmpowerUserIds{}
  617. array := []string{}
  618. for _, v := range ids {
  619. array = append(array, fmt.Sprint(v))
  620. }
  621. 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))
  622. if list != nil {
  623. for _, v := range *list {
  624. result.Ids = append(result.Ids, common.Int64All(v["ent_user_id"]))
  625. }
  626. }
  627. return result
  628. }
  629. // 调用用户中台清除菜单缓存
  630. func delRedisMenuByUserCenter(positionId string) bool {
  631. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  632. res, err := UserCenterRpc.WorkDesktopClearUserInfo(ctx, &usercenter.WorkDesktopClearUserInfoReq{
  633. AppId: "10000",
  634. PositionId: positionId,
  635. })
  636. if err != nil || res == nil {
  637. log.Println("delRedisMenuByUserCenter err", err)
  638. }
  639. return true
  640. }
  641. // 清除权益中台缓存
  642. func delRedisByPowerCheck(positionId string) bool {
  643. ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
  644. i_positionId, _ := strconv.Atoi(positionId)
  645. res, err := PowerCheckCenterRpc.DelCheckRedis(ctx, &powercheck.CheckReq{
  646. PositionId: int64(i_positionId),
  647. Appid: "10000",
  648. })
  649. if err != nil || res == nil {
  650. log.Println("delRedisByPowerCheck err", err)
  651. }
  652. return true
  653. }
  654. func ResourcePowerHandle(identity *usercenter.Identity, t int64, startTime, endTime string) {
  655. if t == 1 {
  656. resp, err := entity.ResourceRpc.Empower(context.Background(), &pb.EmpowerReq{
  657. Appid: "10000",
  658. FunctionCode: "bi_sj_yyszs",
  659. EntId: identity.EntId,
  660. EntUserId: []int64{identity.EntUserId},
  661. })
  662. if err == nil || resp.Status == 1 {
  663. nowFormat := NowFormat(Date_Full_Layout)
  664. positionId := fmt.Sprint(identity.PositionId)
  665. 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{}{
  666. positionId, 1, 0, startTime, endTime, 0, nowFormat, nowFormat,
  667. positionId, 3, 0, startTime, endTime, 0, nowFormat, nowFormat,
  668. positionId, 4, 500, startTime, endTime, 0, nowFormat, nowFormat,
  669. positionId, 5, 0, startTime, endTime, 0, nowFormat, nowFormat,
  670. positionId, 10, 0, startTime, endTime, 0, nowFormat, nowFormat,
  671. positionId, 12, 500, startTime, endTime, 0, nowFormat, nowFormat,
  672. positionId, 13, 500, startTime, endTime, 0, nowFormat, nowFormat,
  673. positionId, 14, 500, startTime, endTime, 0, nowFormat, nowFormat,
  674. })
  675. if r1 > 0 && r2 > 0 {
  676. start, err1 := time.ParseInLocation(Date_Full_Layout, startTime, time.Local)
  677. end, err2 := time.ParseInLocation(Date_Full_Layout, endTime, time.Local)
  678. if err1 == nil && err2 == nil {
  679. setRule(identity, map[string]interface{}{"i_type": 1}, map[string]interface{}{
  680. "i_member_status": 5,
  681. "i_member_starttime": start.Unix(),
  682. "i_member_endtime": end.Unix(),
  683. "l_createtime": time.Now().Unix(),
  684. })
  685. }
  686. }
  687. }
  688. } else {
  689. entity.ResourceRpc.CancelEmpower(context.Background(), &pb.EmpowerReq{
  690. Appid: "10000",
  691. FunctionCode: "bi_sj_yyszs",
  692. EntId: identity.EntId,
  693. EntUserId: []int64{identity.EntUserId},
  694. })
  695. }
  696. }