service.go 29 KB

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