service.go 28 KB

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