task.go 8.6 KB

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