123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167 |
- package taskManager
- import (
- ft "finishtime"
- "fmt"
- "io/ioutil"
- "log"
- "mongodb"
- qu "qfw/util"
- "sort"
- util "spiderutil"
- "strconv"
- "strings"
- "time"
- u "util"
- "github.com/go-xweb/xweb"
- "github.com/tealeg/xlsx"
- )
- type TaskM struct {
- *xweb.Action
- managerTask xweb.Mapper `xweb:"/center/managerTask"` //任务管理
- taskfile xweb.Mapper `xweb:"/center/taskfile"` //任务导入
- mytask xweb.Mapper `xweb:"/center/mytask"` //我的任务
- checkCode xweb.Mapper `xweb:"/center/task/checkCode"` //检验任务是否存在
- searchMintainer xweb.Mapper `xweb:"/center/searchMintainer"` //查询所有维护人员
- saveNewTask xweb.Mapper `xweb:"/center/task/saveNewTask"` //新建任务
- editTask xweb.Mapper `xweb:"/center/task/edit/(.*)"` //编辑任务
- updateTask xweb.Mapper `xweb:"/center/task/updateTask"` //修改任务
- updateTaskState xweb.Mapper `xweb:"/center/task/updateTaskState"` //修改状态为处理中
- saveRecord xweb.Mapper `xweb:"/center/task/saveRecord"` //保存记录提交审核
- audit xweb.Mapper `xweb:"/center/task/audit"` //审核任务
- assignChangeTaskState xweb.Mapper `xweb:"/center/task/assignChangeTaskState"` //分发任务
- closeChangeTaskState xweb.Mapper `xweb:"/center/task/closeChangeTaskState"` //关闭任务
- batchAssign xweb.Mapper `xweb:"/center/task/batchAssign"` //批量分发任务
- batchClose xweb.Mapper `xweb:"/center/task/batchClose"` //批量关闭任务
- batchDeal xweb.Mapper `xweb:"/center/task/batchDeal"` //批量处理任务
- getJumpMark xweb.Mapper `xweb:"/center/task/getJumpMark"` //跳转标记
- searchDataInfo xweb.Mapper `xweb:"/center/task/searchDataInfo"` //搜索信息详情
- createRelateTask xweb.Mapper `xweb:"/center/task/createrelatetask"` //创建关联任务
- //del xweb.Mapper `xweb:"/center/task/del"` //删除任务
- //searchTask xweb.Mapper `xweb:"/center/task/searchTask"` //查询任务
- }
- //session是否失效
- var SessionFailuer bool //检测每次登陆
- const role_admin, role_examine, role_dev = 3, 2, 1 //管理员,审核员,开发员
- //任务导入
- func (t *TaskM) Taskfile() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_admin {
- t.ServeJson("没有权限")
- return
- }
- if t.Method() == "POST" {
- mf, _, err := t.GetFile("xlsx")
- errorinfo := map[string]interface{}{}
- o := map[string]interface{}{}
- if err == nil {
- binary, _ := ioutil.ReadAll(mf)
- xls, _ := xlsx.OpenBinary(binary)
- sheet := xls.Sheets[0]
- rows := sheet.Rows
- for k, v := range rows {
- if k != 0 {
- cells := v.Cells
- if len(cells) == 5 {
- code := cells[0].Value
- if code == "" {
- errorinfo[cells[0].Value] = "第" + fmt.Sprint(k+1) + "行未指定爬虫"
- continue
- }
- query := map[string]interface{}{
- "s_code": code,
- "i_state": map[string]interface{}{
- "$in": []int{0, 1, 2, 3, 5},
- },
- }
- task, _ := u.MgoEB.FindOne("task", query)
- if len(*task) > 0 { //任务已存在
- errorinfo[cells[0].Value] = "第" + fmt.Sprint(k+1) + "行爬虫任务已存在"
- continue
- }
- urgency := cells[2].Value //紧急程度
- if urgency == "特别紧急" {
- urgency = "4"
- } else if urgency == "非常紧急" {
- urgency = "3"
- } else if urgency == "紧急" {
- urgency = "2"
- } else if urgency == "普通" {
- urgency = "1"
- }
- stype := cells[4].Value //任务类型
- if stype == "采集频率异常" {
- stype = "8"
- } else if stype == "列表页异常" {
- stype = "7"
- } else if stype == "附件异常" {
- stype = "6"
- } else if stype == "下载异常" {
- stype = "5"
- } else if stype == "运行异常" {
- stype = "4"
- } else if stype == "时间异常" {
- stype = "3"
- } else if stype == "数据异常" {
- stype = "2"
- } else if stype == "数量异常" {
- stype = "1"
- } else {
- errorinfo[cells[0].Value] = "第" + fmt.Sprint(k+1) + "行任务类型填写错误"
- continue
- }
- completeTime := ft.CompleteTime(urgency)
- if completeTimeStr := cells[3].Value; completeTimeStr != "" { //完成时间
- timeDate, _ := time.ParseInLocation(qu.Date_Full_Layout, completeTimeStr, time.Local)
- completeTime = timeDate.Unix()
- }
- //lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code})
- lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
- if len(*lua) > 0 {
- param := (*lua)["param_common"]
- o["s_site"] = param.([]interface{})[1] //取数组中的某个值
- o["s_channel"] = param.([]interface{})[2]
- o["i_state"] = 2
- o["s_modify"] = (*lua)["createuser"]
- o["s_modifyid"] = (*lua)["createuserid"]
- o["l_comeintime"] = time.Now().Unix()
- o["i_event"] = (*lua)["event"]
- o["i_times"] = 0
- //o["s_date"] = time.Now().Format("2006-01-02")
- o["s_source"] = "人工"
- o["s_code"] = code
- o["s_descript"] = cells[1].Value //描述
- o["s_urgency"] = urgency
- o["s_type"] = stype
- o["l_complete"] = completeTime
- o["i_pendstate"] = 0
- o["l_checktime"] = time.Now().Unix()
- u.MgoEB.Save("task", o)
- //清空map
- o = map[string]interface{}{}
- } else {
- errorinfo[cells[0].Value] = "第" + fmt.Sprint(k+1) + "行爬虫代码填写错误"
- }
- } else {
- break
- }
- }
- }
- t.ServeJson(errorinfo)
- } else {
- t.ServeJson(false)
- }
- }
- }
- func (t *TaskM) ManagerTask() {
- auth := qu.IntAll(t.GetSession("auth"))
- urgency, _ := t.GetInteger("state") //紧急程度
- taskState, _ := t.GetInteger("taskState") //任务状态
- event, _ := t.GetInteger("taskEvent") //节点
- stype, _ := t.GetInteger("taskStype") //任务类型
- userid := t.GetString("userid")
- searchStr := t.GetString("search[value]")
- //search := strings.Replace(searchStr, " ", "", -1)
- search := strings.TrimSpace(searchStr)
- draw, _ := t.GetInteger("draw")
- start, _ := t.GetInteger("start")
- limit, _ := t.GetInteger("length")
- if auth == role_admin {
- if t.Method() == "GET" {
- events := []string{}
- for k, _ := range util.Config.Uploadevents {
- events = append(events, k)
- }
- sort.Strings(events)
- t.T["events"] = events
- t.T["modifyusers"] = GetModifyUsers()
- t.Render("task.html", &t.T)
- } else {
- query := queryCriteria(userid, urgency, taskState, event, stype)
- if search != "" {
- query["$or"] = []interface{}{
- map[string]interface{}{"s_code": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_modify": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_site": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_channel": map[string]interface{}{"$regex": search}},
- }
- }
- sort := `{"%s":%d}`
- orderIndex := t.GetString("order[0][column]")
- orderName := t.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
- orderType := 1
- if t.GetString("order[0][dir]") != "asc" {
- orderType = -1
- }
- sort = fmt.Sprintf(sort, orderName, orderType)
- if orderIndex == "9" { //按下载/下限排序时 先按完成时间排序
- sorta := strings.Replace(sort, "{", "", -1)
- sortb := strings.Replace(sorta, "}", "", -1)
- //sort = `{"l_complete":1,` + sortb + `}`
- sort = `{` + sortb + `,"l_complete":1}`
- }
- qu.Debug("query:", query, sort)
- task, _ := u.MgoEB.Find("task", query, sort, nil, false, start, limit)
- count := u.MgoEB.Count("task", query)
- page := start / 10
- if len(*task) > 0 {
- for k, v := range *task {
- v["num"] = k + 1 + page*10
- v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["s_code"]))
- s_urgency := qu.IntAll(v["s_urgency"])
- if s_urgency == 1 {
- v["s_urgency"] = "普通"
- } else if s_urgency == 2 {
- v["s_urgency"] = "紧急"
- } else if s_urgency == 3 {
- v["s_urgency"] = "非常紧急"
- } else if s_urgency == 4 {
- v["s_urgency"] = "特别紧急"
- }
- state := qu.IntAll(v["i_state"])
- if state == 0 {
- v["i_state"] = "待确认"
- } else if state == 1 {
- v["i_state"] = "待处理"
- } else if state == 2 {
- v["i_state"] = "处理中"
- } else if state == 3 {
- v["i_state"] = "待审核"
- } else if state == 4 {
- v["i_state"] = "审核通过"
- } else if state == 5 {
- v["i_state"] = "未通过"
- } else if state == 6 {
- v["i_state"] = "关闭"
- }
- if v["i_event"] == nil { //节点
- v["i_event"] = 0
- }
- l_complete := qu.Int64All(v["l_complete"])
- v["l_complete"] = qu.FormatDateByInt64(&l_complete, qu.Date_Full_Layout)
- //v["l_complete"] = time.Unix(v["l_complete"].(int64), 0).Format("2006-01-02 15:04:05")
- v["_id"] = mongodb.BsonIdToSId(v["_id"])
- //根据code查询luaconfig
- param := findLua(v["s_code"].(string))
- if len(param) < 13 || param == nil {
- v["href"] = "javascript:void(0)"
- } else {
- v["href"] = param[11]
- }
- }
- }
- t.ServeJson(map[string]interface{}{"draw": draw, "data": task, "recordsFiltered": count, "recordsTotal": count})
- }
- } else {
- t.Write("您没有导入任务的权限")
- }
- }
- func findLua(code string) []interface{} {
- //lua := *mgdb.FindOneByField("luaconfig", `{"code":"`+code+`"}`, `{"param_common":1}`)
- lua, _ := u.MgoEB.FindOneByField("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"param_common": 1})
- param := (*lua)["param_common"].([]interface{})
- return param
- }
- //我的任务
- func (t *TaskM) Mytask() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_dev {
- t.ServeJson("没有权限")
- return
- }
- userid := qu.ObjToString(t.GetSession("userid"))
- query := map[string]interface{}{}
- if t.Method() == "POST" {
- start, _ := t.GetInteger("start")
- limit, _ := t.GetInteger("length")
- draw, _ := t.GetInteger("draw")
- //state, _ := t.GetInteger("state")
- urgency, _ := t.GetInteger("state")
- taskState, _ := t.GetInteger("taskState")
- stype, _ := t.GetInteger("taskStype") //任务类型
- event, _ := t.GetInteger("taskEvent") //节点
- searchStr := t.GetString("search[value]")
- //search := strings.Replace(searchStr, " ", "", -1)
- search := strings.TrimSpace(searchStr)
- //查询自己的任务
- query = queryCriteria(userid, urgency, taskState, event, stype)
- if search != "" {
- query["$or"] = []interface{}{
- map[string]interface{}{"s_code": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_site": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_channel": map[string]interface{}{"$regex": search}},
- }
- }
- sort := `{"%s":%d}`
- orderIndex := t.GetString("order[0][column]")
- orderName := t.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
- orderType := 1
- if t.GetString("order[0][dir]") != "asc" {
- orderType = -1
- }
- sort = fmt.Sprintf(sort, orderName, orderType)
- // sorta := strings.Replace(sort, "{", "", -1)
- // sortb := strings.Replace(sorta, "}", "", -1)
- // sortNew := `{"l_complete": 1,` + sortb + `}`
- qu.Debug(query, sort)
- task, _ := u.MgoEB.Find("task", query, sort, nil, false, start, limit)
- count := u.MgoEB.Count("task", query)
- if task != nil && len(*task) > 0 {
- for _, v := range *task {
- code := fmt.Sprint(v["s_code"])
- relatecode := ""
- //查询关联爬虫
- lua, _ := u.MgoEB.FindOneByField("luaconfig", map[string]interface{}{"code": code + u.Bu, "state": 5}, map[string]interface{}{"relatecode": 1})
- if len(*lua) > 0 {
- relatecode = qu.ObjToString((*lua)["relatecode"])
- }
- v["relatecode"] = relatecode
- v["encode"] = util.Se.Encode2Hex(code)
- urgency := qu.ObjToString(v["s_urgency"])
- if urgency == "1" {
- v["s_urgency"] = "普通"
- } else if urgency == "2" {
- v["s_urgency"] = "紧急"
- } else if urgency == "3" {
- v["s_urgency"] = "非常紧急"
- } else if urgency == "4" {
- v["s_urgency"] = "特别紧急"
- }
- state := qu.IntAll(v["i_state"])
- if state == 0 {
- v["i_state"] = "待确认"
- } else if state == 1 {
- v["i_state"] = "待处理"
- } else if state == 2 {
- v["i_state"] = "处理中"
- } else if state == 3 {
- v["i_state"] = "待审核"
- } else if state == 4 {
- v["i_state"] = "审核通过"
- } else if state == 5 {
- v["i_state"] = "未通过"
- } else if state == 6 {
- v["i_state"] = "关闭"
- }
- if v["i_event"] == nil { //节点
- v["i_event"] = 0
- }
- v["l_complete"] = time.Unix(v["l_complete"].(int64), 0).Format("2006-01-02 15:04:05")
- v["_id"] = mongodb.BsonIdToSId(v["_id"])
- }
- }
- t.ServeJson(map[string]interface{}{"draw": draw, "data": task, "recordsFiltered": count, "recordsTotal": count})
- } else {
- events := []string{}
- for k, _ := range util.Config.Uploadevents {
- events = append(events, k)
- }
- sort.Strings(events)
- t.T["events"] = events
- //查询当前未通过任务条数
- failedtasknum := 0
- if SessionFailuer {
- query["s_modifyid"] = userid
- query["i_state"] = 5
- task, _ := u.MgoEB.Find("task", query, nil, nil, false, -1, -1)
- failedtasknum = len(*task)
- SessionFailuer = false
- }
- //查询是否有错误爬虫
- modifyuser := qu.ObjToString(t.GetSession("loginuser"))
- errluanum := u.MgoS.Count("spider_loadfail",
- map[string]interface{}{
- "modifyuser": modifyuser,
- "modifytime": map[string]interface{}{
- "$exists": false,
- },
- },
- )
- t.T["failedtasknum"] = failedtasknum
- t.T["errluanum"] = errluanum
- t.Render("mytask.html", &t.T)
- }
- }
- //检验任务是否存在
- func (t *TaskM) CheckCode() {
- code := t.GetString("code")
- status := "notHasCode"
- lua := &map[string]interface{}{}
- if code != "" {
- query := map[string]interface{}{
- "s_code": code,
- "i_state": map[string]interface{}{
- "$in": []int{0, 1, 2, 3, 5},
- },
- }
- task, _ := u.MgoEB.FindOne("task", query)
- if task != nil && len(*task) > 0 {
- (*task)["l_complete"] = time.Unix((*task)["l_complete"].(int64), 0).Format("2006-01-02 15:04:05")
- status = "hasCode"
- } else {
- luaQuery := map[string]interface{}{
- "code": code,
- }
- //lua = *mgdb.FindOne("luaconfig", luaQuery)
- lua, _ = u.MgoEB.FindOne("luaconfig", luaQuery)
- }
- t.ServeJson(map[string]interface{}{
- "status": status,
- "task": task,
- "lua": *lua,
- })
- }
- }
- //查询维护人员
- func (t *TaskM) SearchMintainer() {
- users := GetModifyUsers()
- if len(users) > 0 {
- //t.SetSession("mintainer", mintainer)
- t.ServeJson(map[string]interface{}{
- "mintainer": users,
- })
- } else {
- log.Println("查询维护人员名单错误")
- }
- }
- func GetModifyUsers() []map[string]interface{} {
- query := map[string]interface{}{
- "i_auth": 1,
- "i_delete": 0,
- }
- user, _ := u.MgoEB.Find("user", query, nil, nil, false, -1, -1)
- return *user
- }
- //保存新建任务
- func (t *TaskM) SaveNewTask() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_admin {
- t.ServeJson("没有权限")
- return
- }
- site := t.GetString("site")
- code := t.GetString("code")
- channel := t.GetString("channel")
- modify := t.GetString("modify")
- descript := t.GetString("descript")
- urgency := t.GetString("urgency")
- complete := t.GetString("complete")
- stype := t.GetString("stype")
- comeintime := time.Now().Unix()
- var ug string = ""
- newTask := make(map[string]interface{})
- newTask["s_source"] = "人工"
- newTask["s_type"] = stype
- newTask["s_site"] = site //站点
- newTask["s_channel"] = channel //栏目
- newTask["s_code"] = code //代码
- newTask["i_state"] = 2 //完成状态
- newTask["l_comeintime"] = comeintime //创建时间
- //newTask["s_date"] = time.Now().Format("2006-01-02")
- newTask["i_times"] = 0
- newTask["i_pendstate"] = 0
- newTask["l_checktime"] = time.Now().Unix()
- newTask["i_frequencyerrtimes"] = 0 //采集频率异常次数
- newTask["s_platform"] = "golua平台"
- newTask["i_num"] = 0
- queryL := map[string]interface{}{
- "code": code,
- }
- //lua := *mgdb.FindOne("luaconfig", queryL)
- lua, _ := u.MgoEB.FindOne("luaconfig", queryL)
- if lua != nil {
- newTask["s_modifyid"] = (*lua)["createuserid"] //维护人员id
- newTask["i_event"] = (*lua)["event"] //节点
- }
- newTask["s_modify"] = modify //维护人员
- newTask["s_descript"] = descript //描述
- if urgency == "普通" {
- ug = "1"
- newTask["s_urgency"] = "1" //紧急度
- } else if urgency == "紧急" {
- ug = "2"
- newTask["s_urgency"] = "2"
- } else if urgency == "非常紧急" {
- ug = "3"
- newTask["s_urgency"] = "3"
- } else if urgency == "特别紧急" {
- ug = "4"
- newTask["s_urgency"] = "4"
- }
- //根据紧急度自动生成最终完成时间
- if complete == "" {
- //newTask["l_complete"] = ft.LastTime(timeHour) //最迟完成时间
- newTask["l_complete"] = ft.CompleteTime(ug)
- } else { //转成时间戳
- timeDate, err := time.ParseInLocation("2006-01-02 15:04:05", complete, time.Local)
- if err == nil {
- newTask["l_complete"] = timeDate.Unix()
- }
- }
- taskid := u.MgoEB.Save("task", newTask)
- if taskid != "" {
- t.ServeJson(map[string]interface{}{
- "state": "ok",
- })
- }
- }
- //编辑 查看任务
- func (t *TaskM) EditTask(ids string) error {
- auth := qu.IntAll(t.GetSession("auth"))
- //code := strings.Split(codes, "__")[0]
- id := strings.Split(ids, "__")[0]
- param := strings.Split(ids, "__")[1]
- if t.Method() == "GET" {
- query := map[string]interface{}{
- //"s_code": util.Se.Decode4Hex(code),
- "_id": mongodb.StringTOBsonId(id),
- }
- task, _ := u.MgoEB.FindOne("task", query)
- if task != nil && len(*task) > 0 {
- (*task)["l_comeintime"] = time.Unix((*task)["l_comeintime"].(int64), 0).Format("2006-01-02 15:04:05")
- (*task)["l_complete"] = time.Unix((*task)["l_complete"].(int64), 0).Format("2006-01-02 15:04:05")
- if (*task)["a_mrecord"] != nil {
- mrecord := qu.ObjArrToMapArr((*task)["a_mrecord"].([]interface{}))
- if mrecord != nil && len(mrecord) > 0 {
- for _, v := range mrecord {
- v["l_mrecord_comeintime"] = time.Unix(qu.Int64All(v["l_mrecord_comeintime"]), 0).Format("2006-01-02 15:04:05")
- v["l_mrecord_complete"] = time.Unix(qu.Int64All(v["l_mrecord_complete"]), 0).Format("2006-01-02 15:04:05")
- }
- }
- }
- if (*task)["a_check"] != nil {
- check := qu.ObjArrToMapArr((*task)["a_check"].([]interface{}))
- if check != nil && len(check) > 0 {
- for _, v := range check {
- v["l_check_checkTime"] = time.Unix(qu.Int64All(v["l_check_checkTime"]), 0).Format("2006-01-02 15:04:05")
- }
- }
- }
- t.T["encode"] = util.Se.Encode2Hex(fmt.Sprint((*task)["s_code"]))
- t.T["id"] = id
- t.T["task"] = *task
- t.T["param"] = param
- if t.GetSession(id) == "" || t.GetSession(id) == nil {
- t.T["xgTime"] = time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
- } else {
- t.T["xgTime"] = qu.ObjToString(t.GetSession(id))
- }
- t.DelSession(id)
- if auth == role_admin {
- return t.Render("taskedit.html", &t.T)
- } else if auth == role_dev {
- return t.Render("mytaskedit.html", &t.T)
- } else if auth == role_examine {
- return t.Render("auditedit.html", &t.T)
- }
- }
- }
- return nil
- }
- //删除任务
- func (t *TaskM) Del() {
- auth := qu.IntAll(t.GetSession("auth"))
- id := t.GetString("id")
- state := "no"
- if auth != role_admin {
- t.ServeJson("没有权限")
- return
- }
- del := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- ok := u.MgoEB.Del("task", del)
- if ok {
- state = "ok"
- }
- t.ServeJson(map[string]interface{}{
- "state": state,
- })
- }
- //修改任务
- func (t *TaskM) UpdateTask() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_admin {
- t.ServeJson("没有权限")
- return
- }
- modify := t.GetString("modify")
- id := t.GetString("id")
- descript := t.GetString("descript")
- urgency := t.GetString("urgency")
- complete := t.GetString("complete")
- completeChange := t.GetString("completeChange")
- urgencyChange := t.GetString("urgencyChange")
- var l_complete int64
- var state = "no"
- if "普通" == urgency {
- urgency = "1"
- } else if "紧急" == urgency {
- urgency = "2"
- } else if "非常紧急" == urgency {
- urgency = "3"
- } else if "特别紧急" == urgency {
- urgency = "4"
- }
- if "no" == completeChange { //时间格式未改变
- completeTime, _ := time.ParseInLocation("2006-01-02 15:04:05", complete, time.Local)
- l_complete = completeTime.Unix()
- } else if "yes" == completeChange { //时间格式改变
- timeDate, err := time.ParseInLocation("2006-01-02", complete, time.Local)
- if err == nil {
- l_complete = timeDate.Unix() + 64800
- }
- }
- if "yes" == urgencyChange { //紧急度改变 根据紧急度修改最迟完成时间
- l_complete = ft.CompleteTime(urgency)
- }
- queryU := map[string]interface{}{
- "s_name": modify,
- }
- task, _ := u.MgoEB.FindOne("user", queryU)
- queryT := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "s_modify": modify,
- "s_descript": descript,
- "s_urgency": urgency,
- "l_complete": l_complete,
- "s_modifyid": mongodb.BsonIdToSId((*task)["_id"]),
- },
- }
- ok := u.MgoEB.Update("task", queryT, update, false, false)
- if ok {
- state = "ok"
- t.SetSession("jumpMark", "y")
- }
- t.ServeJson(map[string]interface{}{
- "state": state,
- })
- }
- //修改任务状态为处理中
- func (t *TaskM) UpdateTaskState() {
- xgTime := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
- //判断之前是否已有
- id := t.GetString("id")
- if t.GetSession(id) == nil {
- t.SetSession(id, xgTime)
- }
- query := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- task, _ := u.MgoEB.FindOne("task", query)
- updateOk := false
- if len(*task) > 0 {
- state := qu.IntAll((*task)["i_state"])
- if state == 1 { //修改任务状态为待处理
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "i_state": 2,
- },
- }
- updateOk = u.MgoEB.Update("task", query, update, false, false) //更新任务状态
- code := qu.ObjToString((*task)["s_code"])
- //mgdb.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"ismodify": true}}, false, false)
- u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"ismodify": true}}, false, false)
- } else {
- updateOk = true
- }
- }
- t.ServeJson(map[string]interface{}{
- "state": updateOk,
- })
- }
- //保存记录
- func (t *TaskM) SaveRecord() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_dev {
- t.ServeJson("没有权限")
- return
- }
- id := t.GetString("id")
- startTime := t.GetString("startTime")
- endTime := t.GetString("endTime")
- remark := t.GetString("remark")
- rateremark := t.GetString("rateremark")
- comeintime, _ := time.ParseInLocation(qu.Date_Full_Layout, startTime, time.Local)
- l_comeintime := comeintime.Unix()
- completeTime, _ := time.ParseInLocation(qu.Date_Full_Layout, endTime, time.Local)
- l_complete := completeTime.Unix()
- tmp := map[string]interface{}{
- "l_mrecord_comeintime": l_comeintime,
- "l_mrecord_complete": l_complete,
- "s_mrecord_remark": remark,
- "s_mrecord_rateremark": rateremark,
- }
- set := map[string]interface{}{
- "$addToSet": map[string]interface{}{ //任务记录
- "a_mrecord": tmp,
- },
- "$set": map[string]interface{}{ //任务状态
- "i_state": 3,
- },
- }
- ok := u.MgoEB.UpdateById("task", id, set)
- t.ServeJson(map[string]interface{}{
- "state": ok,
- })
- }
- //审核任务
- func (t *TaskM) Audit() {
- auth := qu.IntAll(t.GetSession("auth"))
- if auth != role_examine {
- t.ServeJson("没有权限")
- return
- }
- if t.Method() == "POST" {
- start, _ := t.GetInteger("start")
- limit, _ := t.GetInteger("length")
- draw, _ := t.GetInteger("draw")
- state, _ := t.GetInteger("state") //紧急程度
- taskState, _ := t.GetInteger("taskState")
- searchStr := t.GetString("search[value]")
- //search := strings.Replace(searchStr, " ", "", -1)
- search := strings.TrimSpace(searchStr)
- //查询自己的任务
- query := map[string]interface{}{}
- query = map[string]interface{}{
- "i_state": map[string]interface{}{
- "$gte": 3,
- "$lte": 5,
- },
- }
- if state >= 0 && taskState >= 0 {
- query = map[string]interface{}{
- "s_urgency": strconv.Itoa(state),
- "i_state": taskState,
- }
- } else if state < 0 && taskState >= 0 {
- query = map[string]interface{}{
- "i_state": taskState,
- }
- } else if state >= 0 && taskState < 0 {
- query = map[string]interface{}{
- "s_urgency": strconv.Itoa(state),
- "i_state": map[string]interface{}{
- "$gte": 3,
- "$lte": 5,
- },
- }
- }
- if search != "" {
- query["$or"] = []interface{}{
- map[string]interface{}{"s_code": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_site": map[string]interface{}{"$regex": search}},
- map[string]interface{}{"s_channel": map[string]interface{}{"$regex": search}},
- }
- }
- sort := `{"%s":%d}`
- orderIndex := t.GetString("order[0][column]")
- orderName := t.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
- orderType := 1
- if t.GetString("order[0][dir]") != "asc" {
- orderType = -1
- }
- sort = fmt.Sprintf(sort, orderName, orderType)
- sorta := strings.Replace(sort, "{", "", -1)
- sortb := strings.Replace(sorta, "}", "", -1)
- sortNew := `{"l_complete": 1,` + sortb + `}`
- task, _ := u.MgoEB.Find("task", query, sortNew, nil, false, start, limit)
- count := u.MgoEB.Count("task", query)
- if task != nil && len(*task) > 0 {
- for _, v := range *task {
- v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["s_code"]))
- urgency := qu.ObjToString(v["s_urgency"])
- if urgency == "1" {
- v["s_urgency"] = "普通"
- } else if urgency == "2" {
- v["s_urgency"] = "紧急"
- } else if urgency == "3" {
- v["s_urgency"] = "非常紧急"
- } else if urgency == "4" {
- v["s_urgency"] = "特别紧急"
- }
- state := qu.IntAll(v["i_state"])
- if state == 0 {
- v["i_state"] = "待确认"
- } else if state == 1 {
- v["i_state"] = "待处理"
- } else if state == 2 {
- v["i_state"] = "处理中"
- } else if state == 3 {
- v["i_state"] = "待审核"
- } else if state == 4 {
- v["i_state"] = "审核通过"
- } else if state == 5 {
- v["i_state"] = "未通过"
- } else if state == 6 {
- v["i_state"] = "关闭"
- }
- v["l_complete"] = time.Unix(v["l_complete"].(int64), 0).Format("2006-01-02 15:04:05")
- v["_id"] = mongodb.BsonIdToSId(v["_id"])
- }
- }
- t.ServeJson(map[string]interface{}{"draw": draw, "data": task, "recordsFiltered": count, "recordsTotal": count})
- } else {
- t.Render("audit.html")
- }
- }
- //更新task
- func UpdateOldTask(id, descript, urgency string, complete int64) {
- queryT := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "s_descript": descript,
- "s_urgency": urgency,
- "l_complete": complete,
- "l_comeintime": time.Now().Unix(),
- },
- }
- ok := u.MgoEB.Update("task", queryT, update, false, false)
- if ok {
- log.Println("更新已有任务成功")
- } else {
- log.Println("更新已有任务失败")
- }
- }
- //分发任务
- func (t *TaskM) AssignChangeTaskState() {
- id := t.GetString("id")
- code := t.GetString("code")
- reason := t.GetString("reason") //有分发描述追加到任务问题描述中
- auth := qu.IntAll(t.GetSession("auth"))
- if auth == role_admin {
- //先根据code查有没有相关任务,再根据id修改任务状态
- query := map[string]interface{}{
- "s_code": code,
- "i_state": map[string]interface{}{
- "$in": []int{1, 2, 3, 5},
- },
- }
- task, _ := u.MgoEB.FindOne("task", query)
- if len(*task) > 0 { //有相关任务不能分发
- t.ServeJson("e")
- return
- }
- //没有相关任务,修改状态
- query = map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- set := map[string]interface{}{
- "i_state": 1,
- "l_checktime": time.Now().Unix(),
- }
- if reason != "" {
- tmp, _ := u.MgoEB.FindOne("task", query)
- descript := qu.ObjToString((*tmp)["s_descript"]) + "审核人员追加描述:------------------------------\n" + reason + "\n"
- set["s_descript"] = descript
- }
- update := map[string]interface{}{
- "$set": set,
- }
- flag := u.MgoEB.Update("task", query, update, false, false)
- if flag {
- t.ServeJson("y")
- t.SetSession("jumpMark", "y")
- } else {
- t.ServeJson("n")
- }
- } else {
- t.ServeJson("没有权限")
- }
- }
- //关闭任务
- func (t *TaskM) CloseChangeTaskState() {
- id := t.GetString("id")
- code := t.GetString("code")
- auth := qu.IntAll(t.GetSession("auth"))
- if auth == role_admin {
- //根据id关闭任务
- query := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "i_state": 6,
- "l_checktime": time.Now().Unix(),
- },
- }
- flag := u.MgoEB.Update("task", query, update, false, false)
- if flag {
- go updateClose(code) //更新closerate数据
- t.ServeJson("y")
- t.SetSession("jumpMark", "y")
- } else {
- t.ServeJson("n")
- }
- } else {
- t.ServeJson("没有权限")
- }
- }
- //批量分发任务
- func (t *TaskM) BatchAssign() {
- ids := strings.Split(t.GetString("ids"), ",")
- codes := strings.Split(t.GetString("codes"), ",")
- auth := qu.IntAll(t.GetSession("auth"))
- query := map[string]interface{}{}
- var existCode []string
- if auth == role_admin {
- for k, code := range codes {
- query = map[string]interface{}{
- "s_code": code,
- "i_state": map[string]interface{}{
- "$in": []int{1, 2, 3, 5},
- },
- }
- task, _ := u.MgoEB.FindOne("task", query)
- //log.Println("task", task)
- if len(*task) > 0 { //任务已经存在
- existCode = append(existCode, code)
- } else {
- id := ids[k]
- query = map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "i_state": 1,
- "l_checktime": time.Now().Unix(),
- },
- }
- flag := u.MgoEB.Update("task", query, update, false, false)
- log.Println("任务id:", id, " 更新:", flag)
- }
- }
- t.ServeJson(existCode)
- } else {
- t.ServeJson("没有权限")
- }
- }
- //批量关闭任务
- func (t *TaskM) BatchClose() {
- ids := strings.Split(t.GetString("ids"), ",")
- codes := strings.Split(t.GetString("codes"), ",")
- auth := qu.IntAll(t.GetSession("auth"))
- var falseCode []string
- if auth == role_admin {
- for k, id := range ids {
- query := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "i_state": 6,
- "l_checktime": time.Now().Unix(),
- },
- }
- flag := u.MgoEB.Update("task", query, update, false, false)
- log.Println("任务id:", id, " 关闭:", flag)
- if !flag {
- falseCode = append(falseCode, codes[k])
- } else {
- go updateClose(codes[k]) //更新closeRate
- }
- }
- t.ServeJson(falseCode)
- } else {
- t.ServeJson("没有权限")
- }
- }
- //批量处理任务
- func (t *TaskM) BatchDeal() {
- ids := strings.Split(t.GetString("ids"), ",")
- auth := qu.IntAll(t.GetSession("auth"))
- if auth == role_admin {
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "i_state": 2,
- "l_checktime": time.Now().Unix(),
- },
- }
- for _, id := range ids {
- query := map[string]interface{}{
- "_id": mongodb.StringTOBsonId(id),
- }
- flag := u.MgoEB.Update("task", query, update, false, false)
- log.Println("任务id:", id, " 更新为处理中:", flag)
- }
- t.ServeJson("处理成功")
- } else {
- t.ServeJson("没有权限")
- }
- }
- func (t *TaskM) GetJumpMark() {
- jumpMark := t.GetSession("jumpMark")
- if jumpMark == "y" {
- t.DelSession("jumpMark")
- t.ServeJson("y")
- } else {
- return
- }
- }
- //更新closerate
- func updateClose(code string) {
- defer qu.Catch()
- query := map[string]interface{}{
- "s_code": code,
- }
- data, _ := u.MgoEB.FindOne("closerate", query)
- if data != nil && len(*data) > 0 {
- arr := qu.ObjArrToStringArr((*data)["timeClose"].([]interface{}))
- last := arr[len(arr)-1] //更新最后一天的数据
- timeAndClose := strings.Split(last, ":")
- if len(timeAndClose) >= 2 {
- arr[len(arr)-1] = timeAndClose[0] + ":3"
- }
- //更新
- update := map[string]interface{}{
- "$set": map[string]interface{}{
- "timeClose": arr,
- },
- }
- flag := u.MgoEB.Update("closerate", query, update, false, false)
- fmt.Println("closerate关闭任务:code ", flag)
- }
- }
- func queryCriteria(userid string, urgency, taskState, event, stype int) (query map[string]interface{}) {
- query = map[string]interface{}{}
- if userid != "" && userid != "-1" {
- query["s_modifyid"] = userid
- }
- if urgency >= 0 {
- query["s_urgency"] = strconv.Itoa(urgency)
- }
- if taskState >= 0 {
- query["i_state"] = taskState
- }
- if event >= 0 {
- query["i_event"] = event
- }
- if stype >= 0 {
- query["s_type"] = strconv.Itoa(stype)
- }
- // if urgency >= 0 && taskState >= 0 && event >= 0 { //选择节点,状态和紧急度
- // query = map[string]interface{}{
- // "s_urgency": strconv.Itoa(urgency),
- // "i_state": taskState,
- // "i_event": event,
- // }
- // } else if event >= 0 && urgency < 0 && taskState >= 0 { //选择节点和状态,未选择紧急度
- // query = map[string]interface{}{
- // "i_state": taskState,
- // "i_event": event,
- // }
- // } else if event >= 0 && urgency >= 0 && taskState < 0 { //选择节点和紧急度,未选择状态
- // query = map[string]interface{}{
- // "i_event": event,
- // "s_urgency": strconv.Itoa(urgency),
- // }
- // } else if event >= 0 && urgency < 0 && taskState < 0 { //选择节点,未选择紧急度和状态
- // query = map[string]interface{}{
- // "i_event": event,
- // }
- // } else if event < 0 && urgency >= 0 && taskState >= 0 { //未选择节点,选择紧急度和状态
- // query = map[string]interface{}{
- // "s_urgency": strconv.Itoa(urgency),
- // "i_state": taskState,
- // }
- // } else if event < 0 && urgency < 0 && taskState >= 0 { //未选择节点和紧急度,选择状态
- // query = map[string]interface{}{
- // "i_state": taskState,
- // }
- // } else if event < 0 && urgency >= 0 && taskState < 0 { //未选择节点和状态,选择紧急度
- // query = map[string]interface{}{
- // "s_urgency": strconv.Itoa(urgency),
- // }
- // } else {
- // query = make(map[string]interface{})
- // }
- return
- }
- func (t *TaskM) SearchDataInfo() {
- href := t.GetString("href")
- stype := t.GetString("stype")
- event, _ := t.GetInteger("event")
- coll := "spider_warn"
- if stype == "5" { //下载异常查列表页数据
- if event < 7410 && event != 7000 {
- coll = "spider_highlistdata"
- } else {
- coll = "spider_listdata"
- }
- }
- qu.Debug(coll, stype, event, href)
- data, _ := u.MgoS.FindOne(coll, map[string]interface{}{"href": href})
- if data != nil && len(*data) > 0 {
- text := ""
- if coll == "spider_warn" {
- info := (*data)["data"].(map[string]interface{})
- publishtime := qu.Int64All(info["publishtime"])
- text = "publishtime:" + fmt.Sprint(publishtime) + "\ntitle:" + qu.ObjToString((*data)["title"]) + ";\ndetail:" + qu.ObjToString(info["detail"])
- } else {
- text = "publishtime:" + fmt.Sprint((*data)["publishtime"]) + "\ntitle:" + qu.ObjToString((*data)["title"]) + "; "
- }
- t.ServeJson(text)
- } else {
- t.ServeJson("无信息")
- }
- }
- func (t *TaskM) CreateRelateTask() {
- defer qu.Catch()
- taskid := t.GetString("id")
- relatecode := t.GetString("relatecode")
- task, _ := u.MgoEB.FindById("task", taskid, nil)
- (*task)["s_code"] = relatecode
- id := u.MgoEB.Save("task", task)
- t.ServeJson(map[string]interface{}{"ok": id != ""})
- }
|