package controllers
|
|
import (
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/bhomedbapi.git"
|
"basic.com/valib/logger.git"
|
"github.com/satori/go.uuid"
|
"sort"
|
"vamicro/system-service/models"
|
)
|
|
type DictionaryController struct {
|
}
|
|
type DicTypeVo struct {
|
Type string `json:"type"`
|
Dics []DicWithChildren `json:"dics"`
|
}
|
|
type DicWithChildren struct {
|
models.Dictionary
|
Children []DicWithChildren `json:"children"`
|
}
|
|
type DicList []DicWithChildren
|
func (dl DicList) Len()int {
|
return len(dl)
|
}
|
func (dl DicList) Swap(i,j int) {
|
dl[i],dl[j] = dl[j],dl[i]
|
}
|
func (dl DicList) Less(i,j int) bool {
|
return dl[i].Sort < dl[j].Sort
|
}
|
|
func recursiveChildren(parentId string, allList *[]models.Dictionary) []DicWithChildren {
|
var children = make([]DicWithChildren, 0)
|
for _,d := range *allList {
|
if d.ParentId == parentId {
|
dwc := DicWithChildren{}
|
dwc.Dictionary = d
|
dwc.Children = []DicWithChildren{}
|
children = append(children, dwc)
|
}
|
}
|
var dArr DicList = children
|
sort.Sort(dArr)
|
return dArr
|
}
|
|
// @Summary 根据类型查找字典
|
// @Description 根据类型查找字典
|
// @Produce json
|
// @Tags 字典
|
// @Param type query string false "字典类型"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"成功信息"}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:"错误信息内容"}"
|
// @Router /data/api-v/dictionary/findByType [get]
|
func (controller DictionaryController) FindByType(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var model models.Dictionary
|
dicType := c.Query("type")
|
logger.Debug("dicType:", dicType)
|
allDics, err := model.FindAll()
|
if err != nil {
|
return &bhomeclient.Reply{ Msg: "查询失败"}
|
}
|
|
resMap := make(map[string]DicList, 0)
|
|
for _, dic := range allDics {
|
if dic.ParentId == "0" {
|
dwc := DicWithChildren{}
|
dwc.Dictionary = dic
|
dwc.Children = recursiveChildren(dwc.Id, &allDics)
|
resMap[dic.Type] = append(resMap[dic.Type], dwc)
|
}
|
}
|
//时间规则
|
var tApi bhomedbapi.TimePgnApi
|
tb,timeRules := tApi.FindAllTimeRules()
|
timeList := make(DicList, 0)
|
if tb && timeRules != nil {
|
for idx,t := range timeRules {
|
dwc := DicWithChildren{}
|
dwc.Dictionary = models.Dictionary{
|
Value: t.Id,
|
Name: t.Name,
|
Sort: idx+1,
|
}
|
timeList = append(timeList, dwc)
|
}
|
}
|
resMap["time_rule"] = timeList
|
//人员底库
|
personTableList := make(DicList, 0)
|
var tableApi bhomedbapi.DbTableApi
|
personTables, dtErr := tableApi.FindAllDbTablesByType("0", "person")
|
if dtErr == nil && personTables != nil {
|
for idx,t := range personTables {
|
dwc := DicWithChildren{}
|
dwc.Dictionary = models.Dictionary{
|
Value: t.Id,
|
Name: t.TableName,
|
Sort: idx +1,
|
}
|
personTableList = append(personTableList, dwc)
|
}
|
}
|
resMap["compareBase"] = personTableList
|
// 车辆底库
|
carTableList := make(DicList, 0)
|
carTables, _ := tableApi.FindAllDbTablesByType("0", "car")
|
if carTables != nil {
|
for idx,t := range carTables {
|
dwc := DicWithChildren{}
|
dwc.Dictionary = models.Dictionary{
|
Value: t.Id,
|
Name: t.TableName,
|
Sort: idx +1,
|
}
|
carTableList = append(carTableList, dwc)
|
}
|
}
|
resMap["compareCarBase"] = carTableList
|
for k,dList :=range resMap {
|
v := dList
|
sort.Sort(v)
|
resMap[k] = v
|
}
|
|
return &bhomeclient.Reply{ Success: true, Data: resMap}
|
}
|
|
// @Summary 根据父ID查找字典
|
// @Description 根据父ID查找字典
|
// @Produce json
|
// @Tags 字典
|
// @Param parentId query string false "parentId"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"成功信息"}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:"错误信息内容"}"
|
// @Router /data/api-v/dictionary/findByParentId [get]
|
func (controller DictionaryController) FindByParentId(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
parentId := c.Query("parentId")
|
if parentId == "" {
|
return &bhomeclient.Reply{ Msg: "参数有误"}
|
}
|
var model models.Dictionary
|
if flag, err := model.SelectById(parentId); !flag && err == nil {
|
|
if model.Value == models.EVENTRULETOPIC_DBTABLE || model.Value == models.EVENTRULETOPIC_TASK || model.Value == models.EVENTRULETOPIC_ALARMLEVEL {
|
empty := models.Dictionary{
|
Value: "",
|
Name: "空",
|
}
|
return &bhomeclient.Reply{ Success: true, Data: []models.Dictionary{empty} }
|
} else {
|
dics, err := model.FindByParentId(parentId)
|
if err != nil {
|
return &bhomeclient.Reply{ Msg: "查询失败"}
|
} else {
|
return &bhomeclient.Reply{ Success:true, Data: dics }
|
}
|
}
|
} else {
|
return &bhomeclient.Reply{ Msg: "查询失败"}
|
}
|
}
|
|
|
// @Summary 根据type查找字典列表
|
// @Description 根据type查找字典列表
|
// @Produce json
|
// @Tags 字典
|
// @Param type query string false "type"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
|
// @Router /data/api-v/dictionary/listByType [get]
|
func (controller DictionaryController) ListByType(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
typ := c.Query("type")
|
if typ == "" {
|
return &bhomeclient.Reply{ Msg: "参数有误"}
|
}
|
var model models.Dictionary
|
dics, err := model.FindByType(typ)
|
if err == nil {
|
return &bhomeclient.Reply{ Success:true, Data: dics }
|
} else {
|
return &bhomeclient.Reply{ Msg: "查询失败"}
|
}
|
}
|
|
// @Summary 保存字典
|
// @Description 保存字典
|
// @Accept json
|
// @Produce json
|
// @Tags 字典
|
// @Param dictionary body models.Dictionary true "字典结构"
|
// @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"成功信息"}"
|
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:"错误信息内容"}"
|
// @Router /data/api-v/dictionary/save [post]
|
func (controller DictionaryController) Save(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var model models.Dictionary
|
if err := c.BindJSON(&model); err != nil {
|
return &bhomeclient.Reply{ Msg: "参数有误"}
|
}
|
var flag bool
|
if model.Id == "" {
|
model.Id = uuid.NewV4().String()
|
flag, _ = model.Insert()
|
} else {
|
flag, _ = model.Update()
|
}
|
|
if !flag {
|
return &bhomeclient.Reply{ Msg: "保存失败"}
|
} else {
|
return &bhomeclient.Reply{ Success:true, Msg: "保存成功"}
|
}
|
}
|