service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "time"
  9. . "bp.jydev.jianyu360.cn/BaseService/entManageApplication/entity"
  10. "bp.jydev.jianyu360.cn/BaseService/entManageApplication/rpc/entmanageapplication"
  11. // "github.com/zeromicro/go-zero/core/logx"
  12. "app.yhyue.com/moapp/jybase/common"
  13. "app.yhyue.com/moapp/jybase/redis"
  14. )
  15. func AddUsePerson(this *entmanageapplication.AddUsePersonReq) *entmanageapplication.AddUsePersonResp {
  16. timeStr, ok, id := time.Now().Format("2006-01-02 15:04:05"), false, int64(0)
  17. data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, "", "")
  18. if data != nil && len(*data) > 0 {
  19. ok = JyMysql.Update(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, map[string]interface{}{"status": 1, "update_time": timeStr})
  20. } else {
  21. id = JyMysql.Insert(EntnichePower, map[string]interface{}{
  22. "wait_empower_id": this.WaitEmpowerId,
  23. "ent_id": this.EntId,
  24. "ent_user_id": this.EntUserId,
  25. "status": 1,
  26. "create_time": timeStr,
  27. "update_time": timeStr,
  28. })
  29. }
  30. addPower(this.WaitEmpowerId)
  31. status := 0
  32. if ok || id > 0 {
  33. status = 1
  34. }
  35. return &entmanageapplication.AddUsePersonResp{
  36. ErrorCode: 0,
  37. Data: &entmanageapplication.AddUsePerson{Status: int64(status)},
  38. }
  39. }
  40. func DelUsePerson(this *entmanageapplication.DelUsePersonReq) *entmanageapplication.AddUsePersonResp {
  41. timeStr := time.Now().Format("2006-01-02 15:04:05")
  42. ok := JyMysql.Update(EntnichePower, map[string]interface{}{"id": this.EntnichePowerId},
  43. map[string]interface{}{"status": -1, "update_time": timeStr})
  44. delPower(this.EntnichePowerId)
  45. status := 0
  46. if ok {
  47. status = 1
  48. }
  49. return &entmanageapplication.AddUsePersonResp{
  50. ErrorCode: 0,
  51. Data: &entmanageapplication.AddUsePerson{Status: int64(status)},
  52. }
  53. }
  54. func UsePersonList(this *entmanageapplication.UsePersonListReq) *entmanageapplication.UsePersonListResp {
  55. data := JyMysql.SelectBySql(`select a.id,c.name,c.phone,c.mail,g.name as department,e.name as role from entniche_power a
  56. INNER JOIN entniche_user c on (a.ent_user_id=c.id and a.ent_id = c.ent_id)
  57. LEFT JOIN entniche_user_role d on (c.id=d.user_id)
  58. LEFT JOIN entniche_role e on (d.role_id=e.id)
  59. LEFT JOIN entniche_department_user f on (a.ent_user_id=f.user_id)
  60. LEFT JOIN entniche_department g on (g.id=f.dept_id)
  61. where a.wait_empower_id = ? and a.status = 1
  62. order by convert(c.name using gbk) COLLATE gbk_chinese_ci asc`, this.WaitEmpowerId)
  63. arr := []*entmanageapplication.UsePersonList{}
  64. if data != nil && len(*data) > 0 {
  65. for _, v := range *data {
  66. obj := &entmanageapplication.UsePersonList{
  67. EntnichePowerId: common.Int64All(v["id"]),
  68. Name: common.ObjToString(v["name"]),
  69. Phone: common.ObjToString(v["phone"]),
  70. Email: common.ObjToString(v["mail"]),
  71. Department: common.ObjToString(v["department"]),
  72. Role: common.ObjToString(v["role"]),
  73. }
  74. arr = append(arr, obj)
  75. }
  76. }
  77. return &entmanageapplication.UsePersonListResp{
  78. ErrorCode: 0,
  79. Data: arr,
  80. }
  81. }
  82. func BuyProductList(this *entmanageapplication.BuyProductListReq) *entmanageapplication.BuyProductListResp {
  83. data := JyMysql.Find(EntnicheWaitEmpower, map[string]interface{}{"ent_id": this.EntId}, "", "create_time desc", 0, 0)
  84. arr := []*entmanageapplication.BuyProductList{}
  85. if data != nil && len(*data) > 0 {
  86. for _, v := range *data {
  87. obj := &entmanageapplication.BuyProductList{
  88. WaitEmpowerId: common.Int64All(v["id"]),
  89. ProductType: common.ObjToString(v["product_type"]),
  90. UseCount: common.Int64All(v["use_count"]),
  91. EmpowerCount: common.Int64All(v["empower_count"]),
  92. ProvinceCount: common.Int64All(v["province_count"]),
  93. EndTime: common.ObjToString(v["end_time"]),
  94. }
  95. arr = append(arr, obj)
  96. }
  97. }
  98. return &entmanageapplication.BuyProductListResp{
  99. ErrorCode: 0,
  100. Data: arr,
  101. }
  102. }
  103. func addPower(waitEmpowerId int64) {
  104. data := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "")
  105. if data != nil && len(*data) > 0 {
  106. orderId := common.Int64All((*data)["order_id"])
  107. orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "")
  108. if orderData != nil && len(*orderData) > 0 {
  109. productType := common.ObjToString((*orderData)["product_type"])
  110. if productType == "VIP订阅" || productType == "超级订阅" {
  111. openPowerVip(*orderData)
  112. } else if productType == "大会员" {
  113. openPowerBig(*orderData)
  114. }
  115. }
  116. }
  117. }
  118. func openPowerBig(orderData map[string]interface{}) bool {
  119. filterMap := common.ObjToMap(orderData["filter"])
  120. startTime := common.ObjToString(orderData["vip_starttime"])
  121. endTime := common.ObjToString(orderData["vip_endtime"])
  122. userId := common.ObjToString(orderData["user_id"])
  123. tmp := "2006-01-02 15:04:05"
  124. startdate, _ := time.ParseInLocation(tmp, startTime, time.Local)
  125. enddate, _ := time.ParseInLocation(tmp, endTime, time.Local)
  126. level := common.IntAll((*filterMap)["level"])
  127. comboId := common.IntAll((*filterMap)["comboId"])
  128. payCycle := common.IntAll((*filterMap)["payCycle"])
  129. serversId := common.ObjToString((*filterMap)["serversId"])
  130. serverMap := GetServerPid()
  131. dataType := 1
  132. if startdate.Unix() > time.Now().Unix() {
  133. dataType = 2
  134. }
  135. sets := map[string]interface{}{
  136. "i_member_status": level,
  137. "i_member_starttime": startdate.Unix(),
  138. "i_member_endtime": enddate.Unix(),
  139. "i_mainaccount": 1,
  140. "o_member_jy.i_wxpush": 1,
  141. }
  142. set := map[string]interface{}{
  143. "$set": sets,
  144. }
  145. ok := Mgo.UpdateById("user", userId, set)
  146. if !ok {
  147. log.Println("大会员创建订单user表大会员状态更新失败")
  148. }
  149. if comboId != 0 {
  150. datas := JyMysql.FindOne("bigmember_combo", map[string]interface{}{"id": comboId}, "", "")
  151. if datas == nil || len(*datas) == 0 {
  152. log.Println("未找到此套餐")
  153. return false
  154. }
  155. s_servers := common.ObjToString((*datas)["s_servers"])
  156. for _, s := range strings.Split(s_servers, ",") {
  157. serverId, _ := strconv.Atoi(s)
  158. mainId := serverId
  159. if serverMap[serverId] != 0 {
  160. serverId = serverMap[serverId]
  161. }
  162. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  163. frequency := 0
  164. if serverData != nil && len(*serverData) > 0 {
  165. if serverId == 11 || serverId == 15 {
  166. if serverId == 15 {
  167. if payCycle >= 12 {
  168. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  169. } else {
  170. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  171. }
  172. } else {
  173. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  174. }
  175. } else {
  176. frequency = common.IntAll((*serverData)["s_count_month"])
  177. }
  178. }
  179. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status != -1"
  180. serverDatas := JyMysql.SelectBySql(sqls, serverId, userId)
  181. if serverDatas != nil && len(*serverDatas) > 0 {
  182. i_status := common.If(dataType == 1, 0, 1) //TODO
  183. sql := ""
  184. if serverId == 17 || serverId == 18 {
  185. sql = "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  186. } else {
  187. sql = "update bigmember_service_user set i_frequency = i_frequency + ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  188. }
  189. status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, userId)
  190. if status1 < 0 {
  191. log.Println("新建服务-更新服务表出错", userId)
  192. return false
  193. }
  194. } else {
  195. insert := map[string]interface{}{
  196. "s_smainid": mainId,
  197. "s_userid": userId,
  198. "s_serviceid": serverId,
  199. "i_frequency": frequency,
  200. "l_starttime": startdate.Format(tmp),
  201. "l_endtime": enddate.Format(tmp),
  202. "i_status": common.If(dataType == 1, 0, 1),
  203. "l_createtime": time.Now().Format(tmp),
  204. "l_updatetime": time.Now().Format(tmp),
  205. }
  206. order_id := JyMysql.Insert("bigmember_service_user", insert)
  207. if order_id > 0 {
  208. log.Println("用户服务表插入成功", userId)
  209. } else {
  210. log.Println("用户服务表插入失败", userId)
  211. return false
  212. }
  213. }
  214. }
  215. } else {
  216. for _, s := range strings.Split(serversId, ",") {
  217. serverId, _ := strconv.Atoi(s)
  218. mainId := serverId
  219. if serverMap[serverId] != 0 {
  220. serverId = serverMap[serverId]
  221. }
  222. serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "")
  223. frequency := 0
  224. if serverData != nil && len(*serverData) > 0 {
  225. if serverId == 11 || serverId == 15 {
  226. if serverId == 15 {
  227. if payCycle >= 12 {
  228. frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"]))
  229. } else {
  230. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  231. }
  232. } else {
  233. frequency = payCycle * common.IntAll((*serverData)["s_count_month"])
  234. }
  235. } else {
  236. frequency = common.IntAll((*serverData)["s_count_month"])
  237. }
  238. }
  239. sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status != -1"
  240. serverDatas := JyMysql.SelectBySql(sqls, serverId, userId)
  241. if serverDatas != nil && len(*serverDatas) > 0 {
  242. i_status := common.If(dataType == 1, 0, 1)
  243. sql := "update bigmember_service_user set i_frequency = i_frequency + ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?"
  244. status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, userId)
  245. if status1 < 0 {
  246. log.Println("新建服务-更新服务表出错")
  247. return false
  248. }
  249. } else {
  250. insert := map[string]interface{}{
  251. "s_userid": userId,
  252. "s_smainid": mainId,
  253. "s_serviceid": serverId,
  254. "i_frequency": frequency,
  255. "l_starttime": startdate.Format(tmp),
  256. "l_endtime": enddate.Format(tmp),
  257. "i_status": common.If(dataType == 1, 0, 1),
  258. "l_createtime": time.Now().Format(tmp),
  259. "l_updatetime": time.Now().Format(tmp),
  260. }
  261. order_id := JyMysql.Insert("bigmember_service_user", insert)
  262. if order_id > 0 {
  263. log.Println("用户服务表插入成功", userId)
  264. } else {
  265. log.Println("用户服务表插入失败")
  266. return false
  267. }
  268. }
  269. }
  270. RedisDel(userId)
  271. ClearBigVipUserPower(userId)
  272. }
  273. return true
  274. }
  275. func GetServerPid() map[int]int {
  276. pidMap := map[int]int{}
  277. datas := JyMysql.Find("bigmember_service", map[string]interface{}{"i_status": 0}, "id,i_pid", "", 0, 0)
  278. if datas != nil && len(*datas) > 0 {
  279. for _, v := range *datas {
  280. if v["i_pid"] != nil {
  281. pidMap[common.IntAll(v["id"])] = common.IntAll(v["i_pid"])
  282. }
  283. }
  284. }
  285. return pidMap
  286. }
  287. func RedisDel(userId string) {
  288. redis.Del("newother", BigKey+userId)
  289. cacheKey := fmt.Sprintf(PowerCacheKey, userId)
  290. baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId)
  291. redisMenuKeyPC := fmt.Sprintf(RedisMenuKeyPC, userId)
  292. redisMenuKeyWX := fmt.Sprintf(RedisMenuKeyWX, userId)
  293. redisMenuKeyAPP := fmt.Sprintf(RedisMenuKeyAPP, userId)
  294. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId)
  295. redis.Del(NewOther, cacheKey)
  296. redis.Del(NewOther, baseInfoCacheKey)
  297. redis.Del(NewOther, redisMenuKeyPC)
  298. redis.Del(NewOther, redisMenuKeyWX)
  299. redis.Del(NewOther, redisMenuKeyAPP)
  300. redis.Del(NewOther, userPowerRedisKey)
  301. }
  302. func openPowerVip(orderData map[string]interface{}) bool {
  303. vms := VipSimpleMsg{}
  304. json.Unmarshal([]byte(common.ObjToString(orderData["filter"])), &vms)
  305. startTime := common.ObjToString(orderData["vip_starttime"])
  306. endTime := common.ObjToString(orderData["vip_endtime"])
  307. Date_Full_Layout := "2006-01-02 15:04:05"
  308. user_phone := common.ObjToString(orderData["user_phone"])
  309. userId := common.ObjToString(orderData["user_id"])
  310. startdate, _ := time.ParseInLocation(Date_Full_Layout, startTime, time.Local)
  311. enddate, _ := time.ParseInLocation(Date_Full_Layout, endTime, time.Local)
  312. dataType := 1
  313. if startdate.Unix() > time.Now().Unix() {
  314. dataType = 2
  315. }
  316. // newBuySet := SubvipBuySet{
  317. // Upgrade: 1,
  318. // AreaCount: areaCount,
  319. // NewCitys: []int{},
  320. // BuyerclassCount: -1,
  321. // }
  322. // vms := VipSimpleMsg{
  323. // Area: &buyArea,
  324. // Industry: []string{},
  325. // Cyclecount: param.CycleCount,
  326. // Cycleunit: param.CycleUnit,
  327. // NewBuyset: &newBuySet,
  328. // OrderType: 1,
  329. // DisWord: "",
  330. // ContractStatus: contractStatus,
  331. // Source: "qmx",
  332. // Remark: param.Remark,
  333. // }
  334. //为用户开通超级订阅权限
  335. //是否开通超级订阅 -1 试用到期 -2 正式到期 1 试用 2 开通
  336. isTrial := false
  337. if dataType == 2 {
  338. isTrial = true
  339. }
  340. isOk := StartSubVip(user_phone, *vms.Area, *vms.NewBuyset, startdate, enddate, isTrial, userId)
  341. return isOk
  342. }
  343. func StartSubVip(phone string, area map[string]interface{}, newBuyset SubvipBuySet, startTime, endTime time.Time, isTrial bool, userId string) bool {
  344. set := map[string]interface{}{
  345. "o_vipjy.i_trial": -1, //已激活试用
  346. "o_vipjy.o_area": area, //设置地区
  347. "o_vipjy.o_buyset": newBuyset, //购买内容 城市、省份、行业数量
  348. "l_vip_starttime": startTime.Unix(), //开始时间
  349. "l_vip_endtime": endTime.Unix(), //结束时间
  350. "i_vip_status": common.If(isTrial, -2, 2), //1试用 2正式 -2 试用到期
  351. "i_vip_expire_tip": 0, //消息提示初始化
  352. "o_vipjy.a_buyerclass": []interface{}{}, //设置行业
  353. }
  354. isOk := Mgo.UpdateById("user", userId, map[string]interface{}{
  355. "$set": set,
  356. })
  357. go func() {
  358. MergeKws(userId)
  359. redis.Del(NewOther, "pl_indexMessage_"+userId)
  360. ClearBigVipUserPower(userId)
  361. }()
  362. if isOk {
  363. return true
  364. }
  365. return true
  366. }
  367. // 初始化vip订阅关键词
  368. func MergeKws(userId string) {
  369. if userId == "" { //11-11 取消此操作
  370. return
  371. }
  372. data, ok := Mgo.FindById("user", userId, `{"o_jy":1,"o_vipjy":1}`)
  373. var o_vipjy map[string]interface{}
  374. if ok && data != nil && len(*data) > 0 {
  375. o_vipjy, _ = (*data)["o_vipjy"].(map[string]interface{})
  376. a_items, _ := o_vipjy["a_items"].([]interface{})
  377. if a_items == nil { //首次
  378. Mgo.UpdateById("user", userId, map[string]interface{}{
  379. "$set": map[string]interface{}{"o_vipjy.i_matchway": 1, "o_vipjy.i_ratemode": 1, "o_vipjy.i_wxpush": 1, "o_vipjy.i_apppush": 1, "o_vipjy.i_projectmatch": 0, "o_vipjy.a_infotype": []string{}, "o_vipjy.a_items": []string{}, "o_vipjy.l_modifydate": time.Now().Unix()},
  380. })
  381. }
  382. }
  383. }
  384. func ClearBigVipUserPower(userId string) {
  385. cacheKey := fmt.Sprintf(PowerCacheKey, userId)
  386. baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId)
  387. redisMenuKeyPC := fmt.Sprintf(RedisMenuKeyPC, userId)
  388. redisMenuKeyWX := fmt.Sprintf(RedisMenuKeyWX, userId)
  389. redisMenuKeyAPP := fmt.Sprintf(RedisMenuKeyAPP, userId)
  390. userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId)
  391. redis.Del(NewOther, cacheKey)
  392. redis.Del(NewOther, baseInfoCacheKey)
  393. redis.Del(NewOther, redisMenuKeyPC)
  394. redis.Del(NewOther, redisMenuKeyWX)
  395. redis.Del(NewOther, redisMenuKeyAPP)
  396. redis.Del(NewOther, userPowerRedisKey)
  397. }
  398. func delPower(entnichePowerId int64) {
  399. data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"id": entnichePowerId}, "", "")
  400. if data != nil && len(*data) > 0 {
  401. waitEmpowerId := common.Int64All((*data)["wait_empower_id"])
  402. eOData := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "")
  403. if eOData != nil && len(*eOData) > 0 {
  404. orderId := common.Int64All((*eOData)["order_id"])
  405. orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "")
  406. if orderData != nil && len(*orderData) > 0 {
  407. productType := common.ObjToString((*orderData)["product_type"])
  408. userId := common.ObjToString((*orderData)["user_id"])
  409. if productType == "VIP订阅" || productType == "超级订阅" {
  410. Mgo.UpdateById("user", userId, map[string]interface{}{"$set": map[string]interface{}{"i_vip_status": -2}})
  411. ClearBigVipUserPower(userId)
  412. } else if productType == "大会员" {
  413. Mgo.UpdateById("user", userId, map[string]interface{}{"$set": map[string]interface{}{"i_member_status": -2}})
  414. RedisDel(userId)
  415. ClearBigVipUserPower(userId)
  416. DelBigPower(userId)
  417. }
  418. }
  419. }
  420. }
  421. }