task.go 9.7 KB

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