소스 검색

add:添加管理规则的增删改查接口

zqbao 6 달 전
부모
커밋
f5668124a0

+ 139 - 5
controllers/data_manage/excel/excel_info.go

@@ -3093,7 +3093,7 @@ func (c *ExcelInfoController) GetEdbSource() {
 	br.Success = true
 }
 
-// AddExcelRule
+// ExcelRule
 // @Title 添加表格规则
 // @Description 添加表格规则
 // @Param	request	body excel3.BatchRefreshExcelReq true "type json string"
@@ -3112,7 +3112,7 @@ func (c *ExcelInfoController) AddExcelRule() {
 		br.Ret = 408
 		return
 	}
-	var req request.AddExcelRuleReq
+	var req *request.ExcelRuleMappingReq
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
 	if err != nil {
 		br.Msg = "参数解析异常!"
@@ -3131,7 +3131,7 @@ func (c *ExcelInfoController) AddExcelRule() {
 		br.Msg = "字体颜色不能为空"
 		return
 	}
-	if req.BackGroundColor == "" {
+	if req.BackgroundColor == "" {
 		br.Msg = "背景颜色不能为空"
 		return
 	}
@@ -3139,13 +3139,147 @@ func (c *ExcelInfoController) AddExcelRule() {
 		br.Msg = "条件值不能为空"
 		return
 	}
-	err = excel2.AddExcelRule(req.LeftValue, req.RightValue, req.LeftValueType, req.RightValueType, req.RuleType, req.Scope, req.FontColor, req.BackGroundColor, req.Remark, c.Lang)
+	err = excel2.AddExcelRule(req, c.Lang)
 	if err != nil {
 		br.Msg = "规则添加失败"
 		br.ErrMsg = "规则添加失败,Err:" + err.Error()
 		return
 	}
-	br.Msg = "规则添加成功"
+	br.Msg = "添加成功"
+	br.Ret = 200
+	br.Success = true
+}
+
+// EditExcelRule
+// @Title 编辑表格规则
+// @Description 编辑表格规则
+// @Param	request	body request.ExcelRuleMappingReq true "type json string"
+// @Success Ret=200 刷新成功
+// @router /excel_info/rule/edit [post]
+func (c *ExcelInfoController) EditExcelRule() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		c.Data["json"] = br
+		c.ServeJSON()
+	}()
+	sysUser := c.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	var req *request.ExcelRuleMappingReq
+	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	if req.LeftValue == "" {
+		br.Msg = "条件值不能为空"
+		return
+	}
+	if req.Scope == "" {
+		br.Msg = "应用选区不能为空"
+		return
+	}
+	if req.FontColor == "" {
+		br.Msg = "字体颜色不能为空"
+		return
+	}
+	if req.BackgroundColor == "" {
+		br.Msg = "背景颜色不能为空"
+		return
+	}
+	if req.RuleType == 3 && req.RightValue == "" {
+		br.Msg = "条件值不能为空"
+		return
+	}
+	if req.ExcelRuleMappingId <= 0 {
+		br.Msg = "非法的管理规则ID"
+		return
+	}
+	err = excel2.ModifyExcelRule(req, c.Lang)
+	if err != nil {
+		br.Msg = "规则添加失败"
+		br.ErrMsg = "规则添加失败,Err:" + err.Error()
+		return
+	}
+	br.Msg = "添加成功"
+	br.Ret = 200
+	br.Success = true
+}
+
+// GetExcelRuleList
+// @Title 表格规则列表
+// @Description 表格规则列表
+// @Param   ExcelInfoId   query   int  true       "id"
+// @Success Ret=200 获取成功
+// @router /excel_info/rule/list [get]
+func (c *ExcelInfoController) GetExcelRuleList() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		c.Data["json"] = br
+		c.ServeJSON()
+	}()
+	sysUser := c.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	ExcelInfoId, _ := c.GetInt("ExcelInfoId")
+
+	if ExcelInfoId <= 0 {
+		br.Msg = "请选择表格"
+		return
+	}
+
+	items, err := excel2.GetExcelRuleList(ExcelInfoId)
+	if err != nil {
+		br.Msg = "管理规则编辑失败"
+		br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
+		return
+	}
+
+	br.Data = items
+	br.Msg = "获取成功"
+	br.Ret = 200
+	br.Success = true
+}
+
+// GetExcelRuleDetail
+// @Title 表格规则详情
+// @Description 表格规则详情
+// @Param   ExcelInfoRuleMappingId   query   int  true       "id"
+// @Success Ret=200 获取成功
+// @router /excel_info/rule/detail [get]
+func (c *ExcelInfoController) GetExcelRuleDetail() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		c.Data["json"] = br
+		c.ServeJSON()
+	}()
+	sysUser := c.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	excelInfoRuleMappingId, _ := c.GetInt("ExcelInfoRuleMappingId")
+
+	items, err := excel2.GetExcelRuleDetail(excelInfoRuleMappingId)
+	if err != nil {
+		br.Msg = "管理规则编辑失败"
+		br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
+		return
+	}
+
+	br.Data = items
+	br.Msg = "获取成功"
 	br.Ret = 200
 	br.Success = true
 }

+ 42 - 0
models/data_manage/excel/excel_info_rule_mapping.go

@@ -26,8 +26,50 @@ type ExcelInfoRuleMapping struct {
 	CreateTime             time.Time `description:"创建时间"`
 }
 
+type ExcelInfoRuleMappingView struct {
+	ExcelInfoRuleMappingId int    `orm:"pk" description:"主键"`
+	ExcelInfoId            int    `description:"Excel信息ID"`
+	RuleType               int    `description:"规则类型"`
+	LeftValue              string `description:"左值"`
+	LeftValueShow          string `description:"左值前端显示"`
+	LeftValueType          int    `description:"左值类型"`
+	RightValue             string `description:"右值"`
+	RightValueShow         string `description:"右值前端显示"`
+	RightValueType         int    `description:"右值类型"`
+	FontColor              string `description:"字体颜色"`
+	BackgroundColor        string `description:"背景颜色"`
+	Remark                 string `description:"预设颜色说明"`
+	RemarkEn               string `description:"预设颜色英文说明"`
+	Scope                  string `description:"作用范围"`
+	ScopeCoord             string `description:"作用范围坐标"`
+	ScopeShow              string `description:"作用范围坐标前端显示"`
+	CreateTime             string `description:"创建时间"`
+}
+
 func (e *ExcelInfoRuleMapping) Insert() (insertId int64, err error) {
 	o := orm.NewOrmUsingDB("data")
 	insertId, err = o.Insert(e)
 	return
 }
+
+func (e *ExcelInfoRuleMapping) Update(cols []string) (err error) {
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.Update(e, cols...)
+	return
+}
+
+// GetExcelRuleMappingByExcelInfoId 根据excelInfoId获取规则映射信息
+func GetExcelRuleMappingByExcelInfoId(id int) (items []*ExcelInfoRuleMappingView, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM excel_info_rule_mapping WHERE excel_info_id = ?`
+	_, err = o.Raw(sql, id).QueryRows(&items)
+	return
+}
+
+// GetExcelRuleMappingById 根据主键Id获取规则映射信息
+func GetExcelRuleMappingById(id int) (item *ExcelInfoRuleMappingView, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM excel_info_rule_mapping WHERE excel_info_rule_mapping_id = ?`
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}

+ 12 - 11
models/data_manage/excel/request/excel_info.go

@@ -168,15 +168,16 @@ type ShareExcelInfoReq struct {
 	EditUserIds []int `description:"编辑权限用户IDs"`
 }
 
-type AddExcelRuleReq struct {
-	ExcelInfoId     int    `description:"ETA表格ID"`
-	RuleType        int    `description:"规则类型:1-大于,2-小于,3-介于,4-等于,5-发生日期"`
-	LeftValue       string `description:"条件值"`
-	LeftValueType   int    `description:"条件值的类型,1:数值,2:单元格uid,3:坐标"`
-	RightValue      string `description:"条件值, 用于介于的条件"`
-	RightValueType  int    `description:"条件值的类型,1:数值,2:单元格uid,3:坐标"`
-	FontColor       string `description:"字体颜色"`
-	BackGroundColor string `description:"背景颜色"`
-	Remark          string `description:"预设的单元格样式名称"`
-	Scope           string `description:"应用选区"`
+type ExcelRuleMappingReq struct {
+	ExcelRuleMappingId int    `description:"规则映射ID"`
+	ExcelInfoId        int    `description:"ETA表格ID"`
+	RuleType           int    `description:"规则类型:1-大于,2-小于,3-介于,4-等于,5-发生日期"`
+	LeftValue          string `description:"条件值"`
+	LeftValueType      int    `description:"条件值的类型,1:数值,2:坐标"`
+	RightValue         string `description:"条件值, 用于介于的条件"`
+	RightValueType     int    `description:"条件值的类型,1:数值,2:坐标"`
+	FontColor          string `description:"字体颜色"`
+	BackgroundColor    string `description:"背景颜色"`
+	Remark             string `description:"预设的单元格样式名称"`
+	Scope              string `description:"应用选区"`
 }

+ 6 - 1
models/data_manage/excel/response/excel_info.go

@@ -4,8 +4,9 @@ import (
 	excel2 "eta/eta_api/models/data_manage/excel"
 	"eta/eta_api/models/data_manage/excel/request"
 	"eta/eta_api/services/excel"
-	"github.com/rdlucklib/rdluck_tools/paging"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
 // AddExcelInfoResp 添加excel表格的返回
@@ -118,3 +119,7 @@ type ShareExcelInfoDetail struct {
 	ViewUserIds []int `description:"查看权限用户IDs"`
 	EditUserIds []int `description:"编辑权限用户IDs"`
 }
+
+type ExcelRuleListResp struct {
+	List []*excel2.ExcelInfoRuleMappingView
+}

+ 10 - 9
models/db.go

@@ -502,15 +502,16 @@ func initChartFramework() {
 // initExcel 初始化EXCEL
 func initExcel() {
 	orm.RegisterModel(
-		new(excel.ExcelClassify),   //ETA excel表格分类
-		new(excel.ExcelInfo),       //ETA excel表格
-		new(excel.ExcelDraft),      //ETA excel表格草稿
-		new(excel.ExcelSheet),      //ETA excel sheet
-		new(excel.ExcelSheetData),  //ETA excel sheet data
-		new(excel.ExcelEdbMapping), //ETA excel 与 指标 的关系表
-		new(excel.ExcelWorker),     // 平衡表协作人表格
-		new(excel.ExcelChartEdb),   // 平衡表做图指标
-		new(excel.ExcelChartData),  // 平衡表作图数据
+		new(excel.ExcelClassify),        //ETA excel表格分类
+		new(excel.ExcelInfo),            //ETA excel表格
+		new(excel.ExcelDraft),           //ETA excel表格草稿
+		new(excel.ExcelSheet),           //ETA excel sheet
+		new(excel.ExcelSheetData),       //ETA excel sheet data
+		new(excel.ExcelEdbMapping),      //ETA excel 与 指标 的关系表
+		new(excel.ExcelWorker),          // 平衡表协作人表格
+		new(excel.ExcelChartEdb),        // 平衡表做图指标
+		new(excel.ExcelChartData),       // 平衡表作图数据
+		new(excel.ExcelInfoRuleMapping), //表格的管理规则
 	)
 }
 

+ 36 - 0
routers/commentsRouter.go

@@ -1276,6 +1276,42 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"],
+        beego.ControllerComments{
+            Method: "AddExcelRule",
+            Router: `/excel_info/rule/add`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"],
+        beego.ControllerComments{
+            Method: "GetExcelRuleDetail",
+            Router: `/excel_info/rule/detail`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"],
+        beego.ControllerComments{
+            Method: "EditExcelRule",
+            Router: `/excel_info/rule/edit`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"],
+        beego.ControllerComments{
+            Method: "GetExcelRuleList",
+            Router: `/excel_info/rule/list`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:ExcelInfoController"],
         beego.ControllerComments{
             Method: "GetBatchEdbData",

+ 175 - 37
services/data/excel/excel_info.go

@@ -14,7 +14,10 @@ import (
 	"eta/eta_api/utils"
 	"fmt"
 	"sort"
+	"strings"
 	"time"
+
+	"github.com/xuri/excelize/v2"
 )
 
 // GetExcelDetailInfoByExcelInfoId 根据表格id获取表格详情
@@ -650,66 +653,201 @@ func GetCustomAnalysisOpButton(sysUser *system.Admin, belongUserId int, permissi
 	return
 }
 
-func AddExcelRule(leftValue, rightValue string, leftValueType, rightValueType, ruleType int, scope, fontColor, backgroundColor, remark, lang string) (err error) {
-	// 发生日期
-	tmpExcelRule := new(excel.ExcelInfoRuleMapping)
-	switch ruleType {
-	// 大于
-	case 1:
-		tmpExcelRule.LeftValueShow = "大于" + leftValue
-	// 小于
-	case 2:
-		tmpExcelRule.LeftValueShow = "小于" + leftValue
-	// 介于
-	case 3:
-		tmpExcelRule.LeftValueShow = "介于" + leftValue + "到" + rightValue
-		tmpExcelRule.RightValueShow = rightValue
-	// 等于
-	case 4:
-		tmpExcelRule.LeftValueShow = "等于" + leftValue
-	// 发生日期
-	case 5:
-		switch leftValue {
+func parseExcelScopeCoord(scopeList []string) (scopeCoord string, err error) {
+	if len(scopeList) == 2 {
+		x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
+		if er != nil {
+			return "", er
+		}
+		x2, y2, er := excelize.CellNameToCoordinates(scopeList[1])
+		if er != nil {
+			return "", er
+		}
+		scopeCoord = fmt.Sprintf("%d,%d,%d,%d", x1, y1, x2, y2)
+	}
+	if len(scopeCoord) == 1 {
+		x1, y1, er := excelize.CellNameToCoordinates(scopeList[0])
+		if er != nil {
+			return "", er
+		}
+		scopeCoord = fmt.Sprintf("%d,%d", x1, y1)
+	}
+	return
+}
+
+func ExcelRuleFormat(req *request.ExcelRuleMappingReq, lang string) (res *excel.ExcelInfoRuleMapping, err error) {
+	res = new(excel.ExcelInfoRuleMapping)
+	if req.RuleType == 5 {
+		switch req.LeftValue {
+		// 今天
 		case "today":
-			tmpExcelRule.LeftValueShow = "今天"
+			res.LeftValueShow = "今天"
 		// 明天
 		case "tomorrow":
-			tmpExcelRule.LeftValueShow = "明天"
+			res.LeftValueShow = "明天"
 		// 最近7天
 		case "last7days":
-			tmpExcelRule.LeftValueShow = "最近7天"
+			res.LeftValueShow = "最近7天"
 		// 上周
 		case "lastweek":
-			tmpExcelRule.LeftValueShow = "上周"
+			res.LeftValueShow = "上周"
 		// 本周
 		case "thisweek":
-			tmpExcelRule.LeftValueShow = "本周"
+			res.LeftValueShow = "本周"
 		// 下周
 		case "nextweek":
-			tmpExcelRule.LeftValueShow = "下周"
+			res.LeftValueShow = "下周"
 		// 上月
 		case "lastmonth":
-			tmpExcelRule.LeftValueShow = "上月"
+			res.LeftValueShow = "上月"
 		// 本月
 		case "thismonth":
-			tmpExcelRule.LeftValueShow = "本月"
+			res.LeftValueShow = "本月"
 		// 下月
 		case "nextmonth":
-			tmpExcelRule.LeftValueShow = "下月"
+			res.LeftValueShow = "下月"
 		default:
 			err = errors.New("发生日期规则错误")
 			return
 		}
+	}
+	// 格式化条件值
+	switch req.LeftValueType {
+	// 坐标
+	case 2:
+		x, y, err := excelize.CellNameToCoordinates(req.LeftValue)
+		if err != nil {
+			return nil, err
+		}
+		res.LeftValue = fmt.Sprintf("%d,%d", x, y)
+		res.LeftValueShow = req.LeftValue
+	default:
+		res.LeftValue = req.LeftValue
+		res.LeftValueShow = req.LeftValue
+	}
+	switch req.RightValueType {
+	// 坐标
+	case 2:
+		x, y, err := excelize.CellNameToCoordinates(req.RightValue)
+		if err != nil {
+			return nil, err
+		}
+		res.RightValue = fmt.Sprintf("%d,%d", x, y)
+		res.RightValueShow = req.RightValue
+	default:
+		res.RightValue = req.LeftValue
+		res.RightValueShow = req.RightValue
+	}
+
+	res.ExcelInfoId = req.ExcelInfoId
+	res.ExcelInfoRuleMappingId = req.ExcelRuleMappingId
+	res.LeftValueType = req.LeftValueType
+	res.RightValueType = req.RightValueType
+	res.FontColor = req.FontColor
+	res.BackgroundColor = req.BackgroundColor
+	res.Remark = req.Remark
+	res.RemarkEn = req.Remark
+	res.ScopeShow = req.Scope
+	scopeList := strings.Split(req.Scope, ":")
+	res.ScopeCoord, err = parseExcelScopeCoord(scopeList)
+	return
+}
+
+func AddExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
+	excelRule, err := ExcelRuleFormat(req, lang)
+	if err != nil {
+		return
+	}
+	if excelRule.ExcelInfoRuleMappingId != 0 {
+		return errors.New("规则已存在")
+	}
+	excelRule.CreateTime = time.Now()
+	_, err = excelRule.Insert()
+	return
+}
 
+func ModifyExcelRule(req *request.ExcelRuleMappingReq, lang string) (err error) {
+	excelInfo, err := excel.GetExcelRuleMappingById(req.ExcelRuleMappingId)
+	if err != nil {
+		return
+	}
+	editExcelInfo, err := ExcelRuleFormat(req, lang)
+	if err != nil {
+		return
+	}
+	var updateCols []string
+	if excelInfo.LeftValue != editExcelInfo.LeftValue {
+		updateCols = append(updateCols, "LeftValue")
+		updateCols = append(updateCols, "LeftValueShow")
+	}
+	if excelInfo.LeftValueType != editExcelInfo.LeftValueType {
+		updateCols = append(updateCols, "LeftValueType")
+	}
+	if excelInfo.RightValue != editExcelInfo.RightValue {
+		updateCols = append(updateCols, "RightValue")
+		updateCols = append(updateCols, "RightValueShow")
+	}
+	if excelInfo.RightValueType != editExcelInfo.RightValueType {
+		updateCols = append(updateCols, "RightValueType")
+	}
+	if excelInfo.FontColor != editExcelInfo.FontColor {
+		updateCols = append(updateCols, "FontColor")
+	}
+	if excelInfo.BackgroundColor != editExcelInfo.BackgroundColor {
+		updateCols = append(updateCols, "BackgroundColor")
+	}
+	if excelInfo.Remark != editExcelInfo.Remark {
+		updateCols = append(updateCols, "Remark")
+	}
+	if excelInfo.RemarkEn != editExcelInfo.RemarkEn {
+		updateCols = append(updateCols, "RemarkEn")
+	}
+	if excelInfo.Scope != editExcelInfo.Scope {
+		updateCols = append(updateCols, "ScopeCoord")
+		updateCols = append(updateCols, "ScopeShow")
+	}
+	if len(updateCols) > 0 {
+		err = editExcelInfo.Update(updateCols)
 	}
-	tmpExcelRule.LeftValueType = leftValueType
-	tmpExcelRule.FontColor = fontColor
-	tmpExcelRule.BackgroundColor = backgroundColor
-	tmpExcelRule.Remark = remark
-	tmpExcelRule.RemarkEn = remark
-	tmpExcelRule.ScopeShow = scope
 
-	tmpExcelRule.CreateTime = time.Now()
-	_, err = tmpExcelRule.Insert()
+	return
+}
+
+// GetExcelRuleList 获取规则列表
+func GetExcelRuleList(excelInfoId int) (resp *response.ExcelRuleListResp, err error) {
+	resp = new(response.ExcelRuleListResp)
+	excelInfoList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
+	if err != nil {
+		return
+	}
+	for _, v := range excelInfoList {
+		switch v.RuleType {
+		// 大于
+		case 1:
+			v.LeftValueShow = "大于" + v.LeftValueShow
+		// 小于
+		case 2:
+			v.LeftValueShow = "小于" + v.LeftValueShow
+		// 介于
+		case 3:
+			v.LeftValueShow = "介于" + v.LeftValueShow + "到" + v.RightValueShow
+		// 等于
+		case 4:
+			v.LeftValueShow = "等于" + v.LeftValueShow
+		}
+	}
+
+	resp.List = excelInfoList
+	return
+}
+
+// GetExcelRuleDetail 获取规则列表
+func GetExcelRuleDetail(excelInfoMappingId int) (resp *excel.ExcelInfoRuleMappingView, err error) {
+	resp = new(excel.ExcelInfoRuleMappingView)
+	excelInfoDetail, err := excel.GetExcelRuleMappingById(excelInfoMappingId)
+	if err != nil {
+		return
+	}
+	resp = excelInfoDetail
 	return
 }