service.go 28 KB

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