/* * @Author: lilig * @Date: 2023-10-10 15:22:58 * @LastEditors: lilifor * @LastEditTime: 2023-10-10 15:22:58 * @FilePath: \SCD\controllers\TaskController.go * @Description: * * Copyright (c) 2023 by lilig/jujutong, All Rights Reserved. */ package controllers import ( "encoding/json" "fmt" "scd_check_tools/models/bo" "scd_check_tools/tools" "strings" "time" ) //检测任务服务 type TaskController struct { BaseController } func init() { } // @Summary 创建新的检测任务 // @Description 创建新的检测任务 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id formData int false "任务ID。指定id值大于0时为编辑操作;否则为新增操作" // @Param name formData string true "任务名称" // @Param station_id formData int true "变电站ID" // @Param memo formData string false "任务说明" // @Param report_id formData int true "报告模板ID" // @Param scd_id formData int true "SCD文件ID" // @Param modelids formData string true "模型ID。多个模型ID之间逗号分隔" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/save [post] func (c *TaskController) SaveTaskInfo() { id, _ := c.GetInt("id") obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{Id: id} obj.Model.Name = c.GetString("name") obj.Model.StationId, _ = c.GetInt("station_id") obj.Model.Memo = c.GetString("memo") obj.Model.ReportId, _ = c.GetInt("report_id") obj.Model.ScdId, _ = c.GetInt64("scd_id") obj.Model.State = 0 if obj.Model.Id == 0 { obj.Model.Code = fmt.Sprintf("T-%d-%s", obj.Model.StationId, time.Now().Format("20060102150405")) } else { obj.Model.Code = c.GetString("code") } modelids := c.GetString("modelids") if obj.Model.Name == "" { c.Data["json"] = c.ResultError("检测任务名称不能为空") c.ServeJSON() return } if obj.Model.ReportId == 0 { c.Data["json"] = c.ResultError("检测报告模型不能为空") c.ServeJSON() return } if obj.Model.StationId == 0 { c.Data["json"] = c.ResultError("检测变电站不能为空") c.ServeJSON() return } if obj.Model.ScdId == 0 { c.Data["json"] = c.ResultError("检测的SCD文件不能为空") c.ServeJSON() return } if modelids == "" || modelids == "[]" { c.Data["json"] = c.ResultError("检测任务的模型不能为空") c.ServeJSON() return } area_models := []string{} err1 := json.Unmarshal([]byte(`["`+strings.ReplaceAll(modelids, ",", "\",\"")+`"]`), &area_models) if err1 != nil { c.Data["json"] = c.ResultError(err1.Error()) c.ServeJSON() return } taskid, err := obj.Save() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } modelObj := new(bo.TaskModelMgr) modelObj.SetUserInfo(c.GetCurrentUserInfo()) modelObj.Model.TaskId = taskid err = modelObj.Save(area_models) if err != nil { obj.Delete() c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 删除指定的检测任务 // @Description 删除指定的检测任务 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id formData int true "检测任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/delete [post] func (c *TaskController) DeleteTask() { id, _ := c.GetInt("id") if id == 0 { c.Data["json"] = c.ResultError("任务编号不能为空!") c.ServeJSON() return } obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id = id err := obj.Delete() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 开始进行检测 // @Description 开始进行检测 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id formData int true "检测任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/start [post] func (c *TaskController) StartTask() { id, _ := c.GetInt("id") if id == 0 { c.Data["json"] = c.ResultError("任务编号不能为空!") c.ServeJSON() return } obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id = id err := obj.SetActive(1) if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 取消或者终止检测 // @Description 取消或者终止检测 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id formData int true "检测任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/stop [post] func (c *TaskController) SetTaskActiveStop() { id, _ := c.GetInt("id") if id == 0 { c.Data["json"] = c.ResultError("任务编号不能为空!") c.ServeJSON() return } obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id = id err := obj.SetActive(3) if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 任务重新检测 // @Description 任务重新检测 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id formData int true "检测任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/reset [post] func (c *TaskController) ReSetTaskActive() { id, _ := c.GetInt("id") if id == 0 { c.Data["json"] = c.ResultError("任务编号不能为空!") c.ServeJSON() return } obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id = id err := obj.SetActive(0) if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 查询检测任务 // @Description 查询检测任务。支持任务名称、检测状态等过滤条件 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageno query int true "当前页码。默认为1" // @Param pagesize query int true "每页显示数据数。默认为20" // @Param id query int false "任务ID" // @Param station_id query int false "变电站ID" // @Param name query string false "名称" // @Param start_time query string false "检测开始日期。格式:yyyy-mm-dd" // @Param end_time query string false "检测结束日期。格式:yyyy-mm-dd" // @Param state query int false "检测状态。空:全部 0:未检查 1:检测中 2:检测结束 3:异常中断" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/list [get] func (c *TaskController) GetTaskList() { obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id, _ = c.GetInt("id") obj.Model.StationId, _ = c.GetInt("station_id") obj.Model.Name = c.GetString("name") obj.Model.State, _ = c.GetInt("state", -1) obj.Model.StartTime = c.GetString("start_time") obj.Model.EndTime = c.GetString("end_time") pi, _ := c.GetInt("pagesize", 20) po, _ := c.GetInt("pageno", 0) if po == 0 { po, _ = c.GetInt("pageindex", 0) } if po == 0 { po = 1 } lst, cnt, err := obj.List(po, pi) if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK(lst, cnt) c.ServeJSON() } // @Summary 获取指定检测任务的详细信息 // @Description 获取指定检测任务的详细信息 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/info [get] func (c *TaskController) GetTaskInfo() { obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id, _ = c.GetInt("id") lst, err := obj.One() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } scdmgr := new(bo.ScdMgr) scdinfo, err := scdmgr.One(tools.IsEmpty(lst.ScdId)) if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } models, _, err := obj.GetModels() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } rep := new(bo.TaskReportMgr) report, _ := rep.One(lst.Id) result := map[string]interface{}{} result["id"] = lst.Id result["code"] = lst.Code result["cr"] = lst.Cr result["ct"] = lst.Ct result["end_time"] = lst.EndTime result["memo"] = lst.Memo result["name"] = lst.Name result["report_id"] = lst.ReportId //报告模板ID result["scd_id"] = lst.ScdId result["scd_info"] = scdinfo result["start_time"] = lst.StartTime result["models"] = models result["report_info"] = report //生成的报告信息 c.Data["json"] = c.ResultOK(result, 1) c.ServeJSON() } // @Summary 获取指定检测任务步骤的实时检测状态 // @Description 获取指定检测任务步骤的实时检测状态。该接口需要周期性调用,建议每2秒调用一次。 // @Tags 检测任务服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "任务ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /task/check/step_info [get] func (c *TaskController) GetTaskMonitorInfo() { obj := new(bo.TaskMgr) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model = bo.T_data_task{} obj.Model.Id, _ = c.GetInt("id") lst, err := obj.One() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } scdmgr := new(bo.ScdMgr) _, err = scdmgr.One(tools.IsEmpty(lst.ScdId)) if err != nil { c.Data["json"] = c.ResultError("任务还未开始检测") c.ServeJSON() return } info, cnt, err := obj.GetCheckStepInfo() if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK(info, cnt) c.ServeJSON() }