service.go 30 KB

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