package controllers import ( "ERP_storage/Nats/NatsServer" "ERP_storage/conf" "ERP_storage/models/Account" "ERP_storage/models/Property" userlibs "git.baozhida.cn/ERP_libs/User" "git.baozhida.cn/ERP_libs/lib" "github.com/beego/beego/v2/adapter/orm" beego "github.com/beego/beego/v2/server/web" "math" "strconv" ) type PropertyController struct { beego.Controller User userlibs.User } func (c *PropertyController) Prepare() { c.User = *Account.User_r } func (c *PropertyController) IotCard_List() { // 分页参数 初始化 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_name := c.GetString("T_name") T_state, _ := c.GetInt("T_state") R_List, R_cnt := Property.Read_IotCard_List(T_name, T_state, page, page_z) var r_jsons lib.R_JSONS r_jsons.Num = R_cnt r_jsons.Data = R_List r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *PropertyController) IotCard_Add() { T_iccid := c.GetString("T_iccid") T_type := c.GetString("T_type") T_sn := c.GetString("T_sn") T_state, _ := c.GetInt("T_state") var_ := Property.IotCard{ T_iccid: T_iccid, T_type: T_type, T_sn: T_sn, T_State: T_state, } iotCard, err := Property.Read_IotCard_ByT_iccid(T_iccid) if err != nil && err.Error() != orm.ErrNoRows.Error() { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } if iotCard.Id > 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "物联网卡号重复!"} c.ServeJSON() return } o := orm.NewOrm() IotCardDao := Property.NewIotCard(&o) Id, err := IotCardDao.Add_IotCard(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id} c.ServeJSON() return } func (c *PropertyController) IotCard_Edit() { T_id, _ := c.GetInt("T_id") T_iccid := c.GetString("T_iccid") T_type := c.GetString("T_type") T_sn := c.GetString("T_sn") T_State, _ := c.GetInt("T_State") IotCard_r, err := Property.Read_IotCard_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"} c.ServeJSON() return } if len(T_iccid) > 0 { IotCard_r.T_iccid = T_iccid } if len(T_type) > 0 { IotCard_r.T_type = T_type } if len(T_sn) > 0 { IotCard_r.T_sn = T_sn } if T_State > 0 { IotCard_r.T_State = T_State } o := orm.NewOrm() IotCardDao := Property.NewIotCard(&o) if err = IotCardDao.Update_IotCard(IotCard_r, "T_iccid", "T_type", "T_sn", "T_State"); err != nil { c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "修改", IotCard_r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *PropertyController) IotCard_Del() { T_id, _ := c.GetInt("T_id") IotCard_r, err := Property.Read_IotCard_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if IotCard_r.T_State == 2 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "已使用物联网卡禁止删除!"} c.ServeJSON() return } o := orm.NewOrm() IotCardDao := Property.NewIotCard(&o) if err = IotCardDao.Delete_IotCard(IotCard_r); err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "物联网卡", "删除", strconv.Itoa(T_id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id} c.ServeJSON() return }