service.go 29 KB

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