task.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package task
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "time"
  9. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  10. . "app.yhyue.com/moapp/jybase/api"
  11. qu "app.yhyue.com/moapp/jybase/common"
  12. "app.yhyue.com/moapp/jybase/date"
  13. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  14. "app.yhyue.com/moapp/message/config"
  15. "app.yhyue.com/moapp/message/db"
  16. "app.yhyue.com/moapp/message/handler/activity"
  17. "app.yhyue.com/moapp/message/model"
  18. . "app.yhyue.com/moapp/message/rpc"
  19. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  20. "github.com/gogf/gf/v2/util/gconv"
  21. )
  22. type Task struct {
  23. *xweb.Action
  24. task xweb.Mapper `xweb:"/task"` //获取任务
  25. confirmChallenge xweb.Mapper `xweb:"/confirmChallenge"` //确认挑战
  26. }
  27. type TaskInfo struct {
  28. Name string `json:"name"`
  29. Desc string `json:"desc"`
  30. PcHref string `json:"pcHref"`
  31. AppHref string `json:"appHref"`
  32. WxHref string `json:"wxHref"`
  33. Point int `json:"point"`
  34. Icon string `json:"icon"`
  35. Type string `json:"type"`
  36. Status int `json:"status"`
  37. FinishTime string `json:"finishTime"`
  38. }
  39. //InitTask 初始化任务
  40. func (this *Task) InitTask(isNew bool) []*config.TaskStruct {
  41. if isNew {
  42. return config.TaskConf.NewTask
  43. }
  44. return config.TaskConf.OldTask
  45. }
  46. //Task 获取任务
  47. func (this *Task) Task() {
  48. sessVal := this.Session().GetMultiple()
  49. mgoUserId := gconv.String(sessVal["mgoUserId"])
  50. userId := gconv.String(sessVal["userId"])
  51. positionId := gconv.Int64(sessVal["positionId"])
  52. baseUserId := gconv.Int64(sessVal["base_user_id"])
  53. now := date.NowFormat(date.Date_Full_Layout)
  54. //
  55. rData, errMsg := func() (interface{}, error) {
  56. info := map[string]interface{}{}
  57. data := []*TaskInfo{}
  58. dataM := map[string]*TaskInfo{}
  59. //完成状态 0未开始/过期 1正在进行 2已完成
  60. status := 0
  61. //查询用户任务情况
  62. taskDetail := db.Tidb.SelectBySql(`select * from integral_task_detail where user_id =?`, baseUserId)
  63. if taskDetail != nil && len(*taskDetail) > 0 {
  64. for _, v := range *taskDetail {
  65. name := gconv.String(v["name"])
  66. desc := gconv.String(v["description"])
  67. pcHref := gconv.String(v["pc_href"])
  68. appHref := gconv.String(v["app_href"])
  69. wxHref := gconv.String(v["wx_href"])
  70. point := gconv.Int(v["point"])
  71. icon := gconv.String(v["icon"])
  72. types := gconv.String(v["type"])
  73. detailStatus := gconv.Int(v["status"])
  74. finishTime := gconv.String(v["finish_time"])
  75. tf := &TaskInfo{
  76. Name: name,
  77. Desc: desc,
  78. PcHref: pcHref,
  79. AppHref: appHref,
  80. WxHref: wxHref,
  81. Point: point,
  82. Icon: icon,
  83. Type: types,
  84. Status: detailStatus,
  85. FinishTime: finishTime,
  86. }
  87. dataM[types] = tf
  88. }
  89. }
  90. userMsg, ok := db.Mgo.FindById("user", mgoUserId, `{"l_registedate":1,"s_m_phone":1,"s_phone":1,"s_myemail":1,"s_nickname":1,"s_headimageurl":1,"s_password":1,"s_company":1,"s_unionid":1,"o_jy":1,"o_vipjy":1,"o_member_jy":1,"i_app_login_task":1}`)
  91. if userMsg == nil || len(*userMsg) == 0 || !ok {
  92. return nil, fmt.Errorf("未查询到用户")
  93. }
  94. //注册时间 判断是新手任务还是老用户限时任务
  95. l_registedate := gconv.Int64((*userMsg)["l_registedate"])
  96. isNew := l_registedate > config.TaskConf.TaskStartTime //是否注册时间处于新手任务开始时间
  97. ts := this.InitTask(isNew)
  98. //初始化任务
  99. for _, v := range ts {
  100. if dataM[v.Type] == nil {
  101. dataM[v.Type] = &TaskInfo{
  102. Name: v.Name,
  103. Desc: v.Desc,
  104. PcHref: v.PcHref,
  105. AppHref: v.AppHref,
  106. WxHref: v.WxHref,
  107. Point: v.Point,
  108. Icon: v.Icon,
  109. Type: v.Type,
  110. Status: 0,
  111. }
  112. }
  113. }
  114. mail := gconv.String((*userMsg)["s_myemail"])
  115. if !isNew {
  116. //邮箱是否绑定
  117. if mail != "" {
  118. dataM[model.BindMail].Status = 1
  119. dataM[model.BindMail].FinishTime = now
  120. activity.Task(&model.Message{
  121. E_code: "task",
  122. E_userId: userId,
  123. E_time: time.Now().Unix(),
  124. E_app: "jyweb_node2",
  125. E_body: map[string]interface{}{
  126. "code": 1009, //首次订阅
  127. "types": "bindMail",
  128. "baseUserId": baseUserId,
  129. "positionId": positionId,
  130. },
  131. })
  132. }
  133. }
  134. //判断是否已经创建任务
  135. integralTaskData := db.Tidb.SelectBySql(`select * from integral_task where user_id =? limit 1`, baseUserId)
  136. if integralTaskData == nil || len(*integralTaskData) <= 0 {
  137. db.Tidb.ExecTx("创建任务", func(tx *sql.Tx) bool {
  138. taskId := db.Tidb.InsertByTx(tx, "integral_task", map[string]interface{}{
  139. "user_id": baseUserId,
  140. "position_id": positionId,
  141. "type": qu.If(isNew, 2, 1),
  142. "create_time": now,
  143. })
  144. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  145. args := []interface{}{}
  146. //任务明细
  147. for _, v := range dataM {
  148. args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type)
  149. }
  150. db.Tidb.InsertBatchByTx(tx, "integral_task_detail", fields, args)
  151. return true
  152. })
  153. } else {
  154. //部分初始化
  155. taskId := gconv.Int64((*integralTaskData)[0]["id"])
  156. fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"}
  157. args := []interface{}{}
  158. for _, v := range dataM {
  159. if v.Status == 0 {
  160. if db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and type =?`, baseUserId, v.Type) > 0 {
  161. continue
  162. }
  163. //任务明细
  164. args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type)
  165. }
  166. }
  167. if len(args) > 0 {
  168. db.Tidb.InsertBatch("integral_task_detail", fields, args)
  169. }
  170. if (*integralTaskData)[0]["end_time"] != nil {
  171. et := gconv.String((*integralTaskData)[0]["end_time"])
  172. loc, _ := time.LoadLocation("Asia/Shanghai")
  173. endtime, _ := time.ParseInLocation(date.Date_Full_Layout, et, loc)
  174. if time.Now().Before(endtime) {
  175. //当前时间未超过结束时间
  176. status = 1
  177. } else {
  178. status = 3
  179. }
  180. info["endTime"] = endtime.Unix()
  181. }
  182. success_status := gconv.Int((*integralTaskData)[0]["success_status"])
  183. if success_status == 1 {
  184. //已完成挑战
  185. status = 2
  186. }
  187. }
  188. // for _, v := range dataM {
  189. for _, v := range ts {
  190. data = append(data, &TaskInfo{
  191. Name: dataM[v.Type].Name,
  192. Desc: dataM[v.Type].Desc,
  193. PcHref: dataM[v.Type].PcHref,
  194. AppHref: dataM[v.Type].AppHref,
  195. WxHref: dataM[v.Type].WxHref,
  196. Point: dataM[v.Type].Point,
  197. Icon: dataM[v.Type].Icon,
  198. Type: dataM[v.Type].Type,
  199. Status: dataM[v.Type].Status,
  200. FinishTime: dataM[v.Type].FinishTime,
  201. })
  202. }
  203. if isNew {
  204. info["newbieTask"] = data
  205. info["taskType"] = 2
  206. } else {
  207. info["limitedTask"] = data
  208. info["taskType"] = 1
  209. }
  210. //查询完成状态
  211. info["status"] = status
  212. return info, nil
  213. }()
  214. if errMsg != nil {
  215. log.Printf("%s Task %s异常:%s\n", userId, errMsg.Error())
  216. }
  217. this.ServeJson(NewResult(rData, errMsg))
  218. }
  219. //EntSubscribe 判断企业是否订阅
  220. func EntSubscribe(entUserId, entId int64) int {
  221. if entId > 0 {
  222. //企业
  223. data, ok := db.Mgo.FindOne("entniche_rule", map[string]interface{}{
  224. "i_entid": entId,
  225. "i_userid": entUserId,
  226. })
  227. if data == nil || len(*data) <= 0 || !ok {
  228. return 0
  229. }
  230. o_entniche := gconv.Map((*data)["o_entniche"])
  231. a_items := gconv.Maps(o_entniche["a_items"])
  232. for _, v := range a_items {
  233. a_key := gconv.Maps(v["a_key"])
  234. if len(a_key) > 0 {
  235. return 1
  236. }
  237. }
  238. }
  239. return 0
  240. }
  241. func (this *Task) ConfirmChallenge() {
  242. sessVal := this.Session().GetMultiple()
  243. positionId := gconv.Int64(sessVal["positionId"])
  244. baseUserId := gconv.Int64(sessVal["base_user_id"])
  245. rData, errMsg := func() (interface{}, error) {
  246. infoMap := map[string]interface{}{}
  247. if string(this.Body()) == "" {
  248. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1001)
  249. }
  250. body := xweb.FilterXSS(string(this.Body()))
  251. //接收参数
  252. json.Unmarshal([]byte(body), &infoMap)
  253. if len(infoMap) == 0 {
  254. return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1002)
  255. }
  256. if gconv.Int(infoMap["challenge"]) == 1 {
  257. dayLater := time.Now().Add(time.Duration(config.TaskConf.TaskDayTime-1) * time.Hour * 24)
  258. endTime := time.Date(dayLater.Year(), dayLater.Month(), dayLater.Day(), 23, 59, 59, 0, time.Local)
  259. if db.Tidb.Update("integral_task", map[string]interface{}{
  260. "position_id": positionId,
  261. "user_id": baseUserId,
  262. }, map[string]interface{}{
  263. "end_time": endTime.Format(date.Date_Full_Layout),
  264. }) {
  265. go func() {
  266. //用户点击“确认挑战”给用户发消息
  267. userId := gconv.String(sessVal["mgoUserId"])
  268. wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,identityKeep,") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  269. appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl)
  270. req := &message.MultipleSaveMsgReq{
  271. UserIds: userId,
  272. Title: config.PushConfig.Messages.ConfirmChallenge.Title,
  273. Content: config.PushConfig.Messages.ConfirmChallenge.Content,
  274. MsgType: config.PushConfig.Messages.ConfirmChallenge.MsgType,
  275. Link: config.PushConfig.Messages.ConfirmChallenge.PcUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl,
  276. Appid: config.PushConfig.Messages.ConfirmChallenge.Appid,
  277. AppPushUrl: appUrl,
  278. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  279. IosPushUrl: appUrl,
  280. }
  281. SendMsg("确认挑战", req)
  282. }()
  283. return map[string]interface{}{"status": 1}, nil
  284. }
  285. }
  286. return map[string]interface{}{"status": 1}, nil
  287. }()
  288. if errMsg != nil {
  289. log.Printf("%s UserAccount ConfirmChallenge 异常:%s\n", baseUserId, errMsg.Error())
  290. }
  291. this.ServeJson(NewResult(rData, errMsg))
  292. }