service.go 28 KB

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