package controllers import ( "Cold_Api/Nats/NatsServer" "Cold_Api/conf" "Cold_Api/controllers/lib" "Cold_Api/models/Account" "Cold_Api/models/Device" "Cold_Api/models/Product" "Cold_Api/models/System" beego "github.com/beego/beego/v2/server/web" "math" "strconv" ) type ProductController struct { beego.Controller Admin_r Account.Admin // 登陆的用户 } func (c *ProductController) Prepare() { GetCookie := c.Ctx.GetCookie("User_tokey") GetString := c.GetString("User_tokey") User_tokey := GetCookie if len(User_tokey) == 0 { User_tokey = GetString } c.Admin_r = *Account.Admin_r } func (c *ProductController) ProductUpgrade_List() { type R_JSONS struct { //必须的大写开头 Data []Product.ProductUpgrade_R Num int64 Page int Page_size int } var r_jsons R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_model := c.GetString("T_model") r_jsons.Data, r_jsons.Num = Product.Read_ProductUpgrade_List(T_model, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *ProductController) ProductUpgrade_T_model_List() { var list []string for _, v := range Product.Read_ProductType_All() { list = append(list, v.T_model) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: list} c.ServeJSON() return } func (c *ProductController) ProductUpgrade_Add() { T_model := c.GetString("T_model") T_version := c.GetString("T_version") T_file := c.GetString("T_file") T_remarks := c.GetString("T_remarks") T_carryout, _ := c.GetInt("T_carryout") var_ := Product.ProductUpgrade{ T_model: T_model, T_version: T_version, T_file: T_file, T_remarks: T_remarks, T_carryout: T_carryout, } Id, err := Product.Add_ProductUpgrade(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } if T_carryout == 1 { deviceList := Device.Read_Device_List_ByT_model(T_model) for _, v := range deviceList { device, _ := Device.Read_Device_ByT_sn(v.T_sn) NatsServer.Up_ProductUpgrade(v.T_sn, T_version, T_file, device.T_mqttid) } } System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备版本升级管理", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id} c.ServeJSON() return } func (c *ProductController) ProductUpgrade_Del() { id, _ := c.GetInt("T_id") _, err := Product.Read_ProductUpgrade_ById(id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if is := Product.Delete_ProductUpgrade_ById(id); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } System.Add_UserLogs(c.Admin_r.T_uuid, "设备版本升级管理", "删除", strconv.Itoa(id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return }