package taskManager import ( "fmt" "io/ioutil" "log" ft "luaweb/finishtime" sp "luaweb/front" qu "qfw/util" mgdb "qfw/util/mongodb" mgu "qfw/util/mongodbutil" "sort" util "spiderutil" "strconv" "strings" "time" "github.com/go-xweb/xweb" "github.com/tealeg/xlsx" "gopkg.in/mgo.v2/bson" ) 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"` //批量关闭任务 getJumpMark xweb.Mapper `xweb:"/center/task/getJumpMark"` //跳转标记 searchErrLog xweb.Mapper `xweb:"/center/task/searchErrLog"` //搜索错误日志 searchDataInfo xweb.Mapper `xweb:"/center/task/searchDataInfo"` //搜索数据的标题和发布时间 //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 := make(map[string]interface{}) o["s_date"] = time.Now().Format("2006-01-02") o["s_source"] = "人工" o["s_type"] = "0" var ug string = "" 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) == 0 { continue } if cells[0].Value == "" { //没有code continue } else { //有code o["s_code"] = cells[0].Value queryT := bson.M{ "s_code": cells[0].Value, "i_state": bson.M{ "$in": []int{1, 2, 5}, }, } task := *mgdb.FindOne("task", queryT) //if len(task) != 0 { //任务已存在 追加描述 并修改紧急度和最迟完成时间 //errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行数据已导入" //continue //} else { //任务不存在 if len(cells) == 4 && cells[3].Value != "" { //必有code和最终完成时间 if cells[1].Value == "" || cells[2].Value == "" { //没有描述或者紧急度 errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行数据不完整" continue } else { if !strings.Contains(cells[3].Value, "-") { //日期格式不对 errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行日期格式错误" continue } else { if len(task) != 0 { //有该任务 追加描述 比较紧急度和最迟完成时间 //追加描述 text := time.Now().Format(qu.Date_Short_Layout) + "追加描述:------------------------------\n" + cells[1].Value + "\n" s_descript := task["s_descript"].(string) + text //比较、更新最迟完成时间 var l_complete int64 var s_urgency string s_urgency = isUrgency(cells) if s_urgency == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } timeDate, _ := time.ParseInLocation("2006-01-02", cells[3].Value, time.Local) complete := timeDate.Unix() + 64800 taskComplete := qu.Int64All(task["l_complete"]) if complete <= taskComplete { //取excel中的最迟完成时间和紧急度 l_complete = complete } else { //取task中的最迟完成时间和紧急度 l_complete = taskComplete s_urgency = task["s_urgency"].(string) } if task["i_state"].(int) == 2 { errorinfo[cells[0].Value] = "任务已存在" } //更新task UpdateOldTask(task["_id"].(bson.ObjectId).Hex(), s_descript, s_urgency, l_complete) } else { timeDate, err := time.ParseInLocation("2006-01-02", cells[3].Value, time.Local) if err == nil { o["l_complete"] = timeDate.Unix() + 64800 } o["s_descript"] = cells[1].Value + "\n" ug = isUrgency(cells) if ug == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } o["s_urgency"] = ug } } } } else if len(cells) == 4 && cells[3].Value == "" { //避免添加最终时间后又手动删除 if len(task) != 0 { //追加描述 text := time.Now().Format(qu.Date_Short_Layout) + "追加描述:------------------------------\n" + cells[1].Value + "\n" s_descript := task["s_descript"].(string) + text //比较、更新最迟完成时间 var l_complete int64 var s_urgency string s_urgency = isUrgency(cells) if s_urgency == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } complete := ft.CompleteTime(s_urgency) taskComplete := qu.Int64All(task["l_complete"]) if complete <= taskComplete { //取excel中的最迟完成时间和紧急度 l_complete = complete } else { //取task中的最迟完成时间和紧急度 l_complete = taskComplete s_urgency = task["s_urgency"].(string) } if task["i_state"].(int) == 2 { errorinfo[cells[0].Value] = "任务已存在" } //更新task UpdateOldTask(task["_id"].(bson.ObjectId).Hex(), s_descript, s_urgency, l_complete) } else { if cells[1].Value != "" { ug = isUrgency(cells) if ug == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } o["l_complete"] = ft.CompleteTime(ug) o["s_descript"] = cells[1].Value + "\n" o["s_urgency"] = ug } else { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行数据不完整" } } } else if len(cells) == 3 { //必没有最终完成时间 自动生成 if len(task) != 0 { //追加描述 text := time.Now().Format(qu.Date_Short_Layout) + "追加描述:------------------------------\n" + cells[1].Value + "\n" s_descript := task["s_descript"].(string) + text //比较、更新最迟完成时间 var l_complete int64 var s_urgency string s_urgency = isUrgency(cells) if s_urgency == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } complete := ft.CompleteTime(s_urgency) taskComplete := qu.Int64All(task["l_complete"]) if complete <= taskComplete { //取excel中的最迟完成时间和紧急度 l_complete = complete } else { //取task中的最迟完成时间和紧急度 l_complete = taskComplete s_urgency = task["s_urgency"].(string) } if task["i_state"].(int) == 2 { errorinfo[cells[0].Value] = "任务已存在" } //更新task UpdateOldTask(task["_id"].(bson.ObjectId).Hex(), s_descript, s_urgency, l_complete) } else { if cells[1].Value != "" { ug = isUrgency(cells) if ug == "0" { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行紧急度填写错误" continue } o["l_complete"] = ft.CompleteTime(ug) o["s_descript"] = cells[1].Value + "\n" o["s_urgency"] = ug } else { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行数据不完整" } } } else { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行数据不完整" continue } if len(task) == 0 { queryL := bson.M{"code": cells[0].Value} lua := *mgdb.FindOne("luaconfig", queryL) if lua != nil { param := lua["param_common"] o["s_site"] = param.([]interface{})[1] //取数组中的某个值 o["s_channel"] = param.([]interface{})[2] o["i_state"] = 1 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 } else { errorinfo[cells[0].Value] = "第" + strconv.Itoa(k+1) + "行爬虫代码填写错误" continue } mgdb.Save("task", o) //清空map o = map[string]interface{}{} } //} } } } 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") //任务类型 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.Render("task.html", &t.T) } else { query := queryCriteria(urgency, taskState, event, stype) if search != "" { query["$or"] = []interface{}{ bson.M{"s_code": bson.M{"$regex": search}}, bson.M{"s_modify": bson.M{"$regex": search}}, bson.M{"s_site": bson.M{"$regex": search}}, bson.M{"s_channel": bson.M{"$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}` } task := *mgdb.Find("task", query, sort, nil, false, start, limit) count := mgdb.Count("task", query) page := start / 10 if len(task) > 0 { for k, v := range task { // if v["f_num"] == nil { // v["f_num"] = 0 // } // if v["f_min"] == nil { // v["f_min"] = 0 // } // if v["i_minNum"] == nil { // v["i_minNum"] = 0 // } 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"] = "特别紧急" } if v["i_state"] == 0 { v["i_state"] = "待确认" } else if v["i_state"] == 1 { v["i_state"] = "待处理" } else if v["i_state"] == 2 { v["i_state"] = "处理中" } else if v["i_state"] == 3 { v["i_state"] = "待审核" } else if v["i_state"] == 4 { v["i_state"] = "审核通过" } else if v["i_state"] == 5 { v["i_state"] = "未通过" } else if v["i_state"] == 6 { v["i_state"] = "关闭" } if v["i_event"] == nil { //节点 v["i_event"] = 0 } // if v["continueTimes"] == nil { //特别紧急任务连续报错次数 // v["continueTimes"] = 0 // } else { // for _, times := range v["continueTimes"].(map[string]interface{}) { // v["continueTimes"] = times // } // } 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"] = v["_id"].(bson.ObjectId).Hex() //根据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}`) 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 := t.GetSession("userid") query := bson.M{} 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(urgency, taskState, event, stype) query["s_modifyid"] = userid if search != "" { query["$or"] = []interface{}{ bson.M{"s_code": bson.M{"$regex": search}}, bson.M{"s_site": bson.M{"$regex": search}}, bson.M{"s_channel": bson.M{"$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 := *mgdb.Find("task", query, sort, nil, false, start, limit) count := mgdb.Count("task", query) if task != nil { for _, v := range task { v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["s_code"])) if v["s_urgency"] == "1" { v["s_urgency"] = "普通" } else if v["s_urgency"] == "2" { v["s_urgency"] = "紧急" } else if v["s_urgency"] == "3" { v["s_urgency"] = "非常紧急" } else if v["s_urgency"] == "4" { v["s_urgency"] = "特别紧急" } if v["i_state"] == 0 { v["i_state"] = "待确认" } else if v["i_state"] == 1 { v["i_state"] = "待处理" } else if v["i_state"] == 2 { v["i_state"] = "处理中" } else if v["i_state"] == 3 { v["i_state"] = "待审核" } else if v["i_state"] == 4 { v["i_state"] = "审核通过" } else if v["i_state"] == 5 { v["i_state"] = "未通过" } else if v["i_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"] = v["_id"].(bson.ObjectId).Hex() } } 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 := *mgdb.Find("task", query, nil, nil, false, -1, -1) failedtasknum = len(task) SessionFailuer = false } t.T["failedtasknum"] = failedtasknum t.Render("mytask.html", &t.T) } } //检验任务是否存在 func (t *TaskM) CheckCode() { code := t.GetString("code") status := "notHasCode" var lua map[string]interface{} if code != "" { query := bson.M{ "s_code": code, "i_state": bson.M{ "$in": []int{1, 2, 5}, }, } task := *mgdb.FindOne("task", query) if task != nil { task["l_complete"] = time.Unix(task["l_complete"].(int64), 0).Format("2006-01-02 15:04:05") status = "hasCode" } else { luaQuery := bson.M{ "code": code, } lua = *mgdb.FindOne("luaconfig", luaQuery) } t.ServeJson(map[string]interface{}{ "status": status, "task": task, "lua": lua, }) } } //查询维护人员 func (t *TaskM) SearchMintainer() { query := bson.M{ "i_auth": 1, } mintainer := *mgdb.Find("user", query, nil, nil, false, -1, -1) if len(mintainer) > 0 { t.SetSession("mintainer", mintainer) t.ServeJson(map[string]interface{}{ "mintainer": mintainer, }) } else { log.Println("查询维护人员名单错误") } } //保存新建任务 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") comeintime := time.Now().Unix() var ug string = "" newTask := make(map[string]interface{}) newTask["s_source"] = "人工" newTask["s_type"] = "0" newTask["s_site"] = site //站点 newTask["s_channel"] = channel //栏目 newTask["s_code"] = code //代码 newTask["i_state"] = 1 //完成状态 newTask["l_comeintime"] = comeintime //创建时间 newTask["s_date"] = time.Now().Format("2006-01-02") newTask["i_times"] = 0 queryL := bson.M{ "code": code, } lua := *mgdb.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() } } task := mgdb.Save("task", newTask) if task != "" { t.ServeJson(map[string]interface{}{ "state": "ok", }) } } //编辑 查看任务 func (t *TaskM) EditTask(ids string) error { auth := t.GetSession("auth") //code := strings.Split(codes, "__")[0] id := strings.Split(ids, "__")[0] param := strings.Split(ids, "__")[1] if t.Method() == "GET" { query := bson.M{ //"s_code": util.Se.Decode4Hex(code), "_id": bson.ObjectIdHex(id), } task := *mgdb.FindOne("task", query) if task != nil { 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 == 3 { return t.Render("taskedit.html", &t.T) } else if auth == 1 { return t.Render("mytaskedit.html", &t.T) } else if auth == 2 { 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 := bson.M{ "_id": bson.ObjectIdHex(id), } ok := mgdb.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 := bson.M{ "s_name": modify, } task := *mgdb.FindOne("user", queryU) queryT := bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "s_modify": modify, "s_descript": descript, "s_urgency": urgency, "l_complete": l_complete, "s_modifyid": task["_id"].(bson.ObjectId).Hex(), }, } ok := mgdb.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 := bson.M{ "_id": bson.ObjectIdHex(id), } task := *mgdb.FindOne("task", query) updateOk := false if len(task) > 0 { state := qu.IntAll(task["i_state"]) if state == 1 { //修改任务状态为待处理 update := bson.M{ "$set": bson.M{ "i_state": 2, }, } updateOk = mgdb.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) // code := qu.ObjToString(task["s_code"]) // lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code}) // if len(lua) > 0 { // lua_state := qu.IntAll(lua["state"]) // if lua_state == sp.Sp_state_5 { // b, err := sp.UpStateAndUpSpider(code, "", "", "", sp.Sp_state_6) //点击修改爬虫时,线上爬虫下架 // if b && err == nil { //下架成功 // log.Println("爬虫下架成功", code, ",任务id:", id) // ok := mgdb.Update("task", query, update, false, false) //更新任务状态 // if ok { // updateOk = true // } // } else { // log.Println("修改任务失败", id) // } // } else { // ok := mgdb.Update("task", query, update, false, false) //更新任务状态 // if ok { // updateOk = true // } // } // } } 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 } state := "no" id := t.GetString("id") startTime := t.GetString("startTime") endTime := t.GetString("endTime") remark := t.GetString("remark") query := bson.M{ "_id": bson.ObjectIdHex(id), } task := *mgdb.FindOne("task", query) mrecordData := task["a_mrecord"] var mreArr []map[string]interface{} newData := make(map[string]interface{}) comeintime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local) l_comeintime := comeintime.Unix() completeTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local) l_complete := completeTime.Unix() newData["l_mrecord_comeintime"] = l_comeintime newData["l_mrecord_complete"] = l_complete newData["s_mrecord_remark"] = remark if mrecordData != nil { myArr := qu.ObjArrToMapArr(mrecordData.([]interface{})) if myArr != nil && len(myArr) > 0 { for _, v := range myArr { mreArr = append(mreArr, v) } } } mreArr = append(mreArr, newData) task["a_mrecord"] = mreArr //保存记录 ok := mgdb.Update("task", query, map[string]interface{}{ "$set": task, }, false, false) var codeArr = []string{id} if ok { //修改状态 sp.UpTaskState(codeArr, 1, "", int64(0)) state = "yes" } t.ServeJson(map[string]interface{}{ "state": state, }) } //审核任务 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 := bson.M{} query = bson.M{ "i_state": bson.M{ "$gte": 3, "$lte": 5, }, } if state >= 0 && taskState >= 0 { query = bson.M{ "s_urgency": strconv.Itoa(state), "i_state": taskState, } } else if state < 0 && taskState >= 0 { query = bson.M{ "i_state": taskState, } } else if state >= 0 && taskState < 0 { query = bson.M{ "s_urgency": strconv.Itoa(state), "i_state": bson.M{ "$gte": 3, "$lte": 5, }, } } if search != "" { query["$or"] = []interface{}{ bson.M{"s_code": bson.M{"$regex": search}}, bson.M{"s_site": bson.M{"$regex": search}}, bson.M{"s_channel": bson.M{"$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 := *mgdb.Find("task", query, sortNew, nil, false, start, limit) count := mgdb.Count("task", query) if task != nil { for _, v := range task { v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["s_code"])) if v["s_urgency"] == "1" { v["s_urgency"] = "普通" } else if v["s_urgency"] == "2" { v["s_urgency"] = "紧急" } else if v["s_urgency"] == "3" { v["s_urgency"] = "非常紧急" } else if v["s_urgency"] == "4" { v["s_urgency"] = "特别紧急" } if v["i_state"] == 0 { v["i_state"] = "待确认" } else if v["i_state"] == 1 { v["i_state"] = "待处理" } else if v["i_state"] == 2 { v["i_state"] = "处理中" } else if v["i_state"] == 3 { v["i_state"] = "待审核" } else if v["i_state"] == 4 { v["i_state"] = "审核通过" } else if v["i_state"] == 5 { v["i_state"] = "未通过" } else if v["i_state"] == 6 { v["i_state"] = "关闭" } v["l_complete"] = time.Unix(v["l_complete"].(int64), 0).Format("2006-01-02 15:04:05") v["_id"] = v["_id"].(bson.ObjectId).Hex() } } t.ServeJson(map[string]interface{}{"draw": draw, "data": task, "recordsFiltered": count, "recordsTotal": count}) } else { t.Render("audit.html") } } //判断紧急度 func isUrgency(cells []*xlsx.Cell) string { ug := "0" if cells[2].Value == "普通" { //不紧急 ug = "1" //5天 } else if cells[2].Value == "紧急" { ug = "2" //2天 } else if cells[2].Value == "非常紧急" { ug = "3" //6小时 } else if cells[2].Value == "特别紧急" { //非常紧急 ug = "4" //2小时 } return ug } //更新task func UpdateOldTask(id, descript, urgency string, complete int64) { queryT := bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "s_descript": descript, "s_urgency": urgency, "l_complete": complete, "l_comeintime": time.Now().Unix(), }, } ok := mgdb.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") auth := qu.IntAll(t.GetSession("auth")) if auth == role_admin { //先根据code查有没有相关任务,再根据id修改任务状态 query := bson.M{ "s_code": code, "i_state": bson.M{ "$in": []int{1, 2, 3, 5}, }, } task := *mgdb.FindOne("task", query) if len(task) > 0 { //有相关任务不能分发 t.ServeJson("e") return } //没有相关任务,修改状态 query = bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "i_state": 1, }, } flag := mgdb.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 := bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "i_state": 6, }, } flag := mgdb.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 := bson.M{} var existCode []string if auth == role_admin { for k, code := range codes { query = bson.M{ "s_code": code, "i_state": bson.M{ "$in": []int{1, 2, 3, 5}, }, } task := *mgdb.FindOne("task", query) //log.Println("task", task) if len(task) > 0 { //任务已经存在 existCode = append(existCode, code) } else { id := ids[k] query = bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "i_state": 1, }, } flag := mgdb.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 := bson.M{ "_id": bson.ObjectIdHex(id), } update := bson.M{ "$set": bson.M{ "i_state": 6, }, } flag := mgdb.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) 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 := bson.M{ "s_code": code, } data := *mgdb.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 := bson.M{ "$set": bson.M{ "timeClose": arr, }, } flag := mgdb.Update("closerate", query, update, false, false) fmt.Println("closerate关闭任务:code ", flag) } } func queryCriteria(urgency, taskState, event, stype int) (query bson.M) { query = bson.M{} 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 = bson.M{ // "s_urgency": strconv.Itoa(urgency), // "i_state": taskState, // "i_event": event, // } // } else if event >= 0 && urgency < 0 && taskState >= 0 { //选择节点和状态,未选择紧急度 // query = bson.M{ // "i_state": taskState, // "i_event": event, // } // } else if event >= 0 && urgency >= 0 && taskState < 0 { //选择节点和紧急度,未选择状态 // query = bson.M{ // "i_event": event, // "s_urgency": strconv.Itoa(urgency), // } // } else if event >= 0 && urgency < 0 && taskState < 0 { //选择节点,未选择紧急度和状态 // query = bson.M{ // "i_event": event, // } // } else if event < 0 && urgency >= 0 && taskState >= 0 { //未选择节点,选择紧急度和状态 // query = bson.M{ // "s_urgency": strconv.Itoa(urgency), // "i_state": taskState, // } // } else if event < 0 && urgency < 0 && taskState >= 0 { //未选择节点和紧急度,选择状态 // query = bson.M{ // "i_state": taskState, // } // } else if event < 0 && urgency >= 0 && taskState < 0 { //未选择节点和状态,选择紧急度 // query = bson.M{ // "s_urgency": strconv.Itoa(urgency), // } // } else { // query = make(map[string]interface{}) // } return } func (t *TaskM) SearchErrLog() { href := t.GetString("href") data := *mgu.FindOne("regatherdata", "spider", "spider", map[string]interface{}{"href": href}) if data != nil && len(data) > 0 { t.ServeJson("日志:" + qu.ObjToString(data["error"])) } else { t.ServeJson("无信息") } } func (t *TaskM) SearchDataInfo() { href := t.GetString("href") data := *mgu.FindOne("spider_highlistdata", "spider", "spider", map[string]interface{}{"href": href}) if data != nil && len(data) > 0 { t.ServeJson("标题:" + qu.ObjToString(data["title"]) + "\n" + "发布时间:" + qu.ObjToString(data["publishtime"])) } else { t.ServeJson("无信息") } }