package equipment import ( "encoding/json" "fmt" "strconv" "strings" "time" "dashoo.cn/backend/api/business/authcode" "dashoo.cn/backend/api/business/currboxcapacity" "dashoo.cn/backend/api/business/equipment" "dashoo.cn/backend/api/business/lastordernum" "dashoo.cn/backend/api/business/role" "dashoo.cn/backend/api/business/samplesinfo" "dashoo.cn/backend/api/business/shelfset" . "dashoo.cn/backend/api/controllers" "dashoo.cn/business2/permission" "dashoo.cn/utils" . "dashoo.cn/utils/db" ) // 设备异常报警接口说明 type EquipmentController struct { BaseController } type Equipments struct { CurrBoxCapacity []currboxcapacity.CurrBoxCapacity Equipment equipment.Equipment } type EquipmentInfo struct { Equipment equipment.EquipmentView SampleCount int ShelfCount int64 BoxCount int64 SensorData []SensorsShowData SensorCount int } type SensorsShowData struct { AuthCode string SensorNo string Temperature string Time time.Time } type EquipmentModel struct { Code string `json:"Code,omitempty"` // 设备编码 Name string `json:"Name,omitempty"` // 设备名 Brand string `json:"Brand,omitempty"` // 品牌 ModelVersion string `json:"ModelVersion,omitempty"` // 型号 ModelId int `json:"ModelId,omitempty"` // 型号ID RowNum int `json:"RowNum,omitempty"` // 名称 ColumnNum int `json:"ColumnNum,omitempty"` // 接收日期 Width string `json:"Width,omitempty"` // 可用容量 Height string `json:"Height,omitempty"` // 单位(容量) Depth string `json:"Depth,omitempty"` // 有效日期 TCode []string `json:"TCode,omitempty"` // 样本内码 Shelf_X int `json:"shelf_x,omitempty"` // 样本来源 Shelf_y int `json:"shelf_y,omitempty"` // 备注 Box_X int `json:"box_x,omitempty"` // 样本来源 Box_y int `json:"box_y,omitempty"` // 备注 ValidityDate int64 `json:"ValidityDate,omitempty"` // 备注 UseDate int64 `json:"UseDate,omitempty"` // 备注 ProduceDate int64 `json:"ProduceDate,omitempty"` // 备注 Checkedstr string `json:"checkedstr,omitempty"` // 样本类型 Remark string `json:"Remark,omitempty"` // 位置信息 DItem int `json:"DItem,omitempty"` // 设备类型19:贝尔Cryobiobank13K液氮罐;20:贝尔Cryobiobank13K_BloodBag液氮罐 } // @Title 报警项目列表 // @Description 报警项目列表 // @Success 200 {object} business.device.DeviceChannels // @router /list [get] func (this *EquipmentController) List() { page := this.GetPageInfoForm() svc := equipment.GetEquipmentService(utils.DBE) svcPermission := permission.GetPermissionService(utils.DBE) eids := svcPermission.GetEquipmentIdById(utils.ToStr(this.User.Id)) where := " a.AccCode = '" + this.User.AccCode + "'" if eids != "" { where = where + " and (a.CreateUserId= " + utils.ToStr(this.User.Id) + " or a.Id in (" + eids + "))" } else { where = where + "and a.CreateUserId= " + utils.ToStr(this.User.Id) } searchkey := this.GetString("searchkey") if searchkey != "" { where = where + " and (a.Code like '%" + searchkey + "%' or a.Name like '%" + searchkey + "%') " } total, list := svc.GetEquipmenViewtList(page.CurrentPage, page.Size, "a.Id desc", where) var equipmentsinfos []EquipmentInfo for i := 0; i < len(list); i++ { var equipmentsinfo EquipmentInfo equipmentsinfo.Equipment = list[i] //样本总数 svcsample := samplesinfo.GetSamplesInfoService(utils.DBE) tblsample := this.User.AccCode + SamplesDetailtbName wheresample := " IState=1 and DeletionStateCode=0 and EquipmentId = " + utils.ToStr(list[i].Id) samplenum := svcsample.GetSampleOwnCount(tblsample, wheresample) equipmentsinfo.SampleCount = samplenum //冻存架总数 whereshelf := " AccCode='" + this.User.AccCode + "' and EquipmentId = " + utils.ToStr(list[i].Id) svcshelf := shelfset.GetshelfsetService(utils.DBE) equipmentsinfo.ShelfCount = svcshelf.GetShelfCount(whereshelf) //冻存盒总数 equipmentsinfo.BoxCount = svcshelf.GetBoxCount(whereshelf) //Sensor序列号 var esentitylist []equipment.EquipmentSensors wheresensor := "where AccCode='" + this.User.AccCode + "' and EquipmentId=" + utils.ToStr(list[i].Id) esentitylist = svc.GetSensorsList(wheresensor) var sensorshowdatalist []SensorsShowData for j := 0; j < len(esentitylist); j++ { var sensorshowdata SensorsShowData sensorshowdata.AuthCode = esentitylist[j].AuthCode sensorshowdata.SensorNo = esentitylist[j].SensorNo sensorshowdatalist = append(sensorshowdatalist, sensorshowdata) } equipmentsinfo.SensorCount = len(esentitylist) equipmentsinfo.SensorData = sensorshowdatalist equipmentsinfos = append(equipmentsinfos, equipmentsinfo) } var datainfo DataInfo datainfo.Items = equipmentsinfos datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 容器列表 // @Description 容器列表所有容器数据 // @Success 200 {object} business.device.DeviceChannels // @router /equipalllist [get] func (this *EquipmentController) EquipAllList() { svc := equipment.GetEquipmentService(utils.DBE) svcrole := role.GetRoleService(utils.DBE) roleids := svcrole.GetRoleidsByuid(utils.ToStr(this.User.Id)) where := " a.AccCode = '" + this.User.AccCode + "'" where = where + " and (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (select EquipmentId Id FROM Base_RoleEquipment where RoleId in (" + strings.Join(roleids, ",") + ")))" Name := this.GetString("Name") if Name != "" { where = where + " and Name like '%" + Name + "%' " } total, list := svc.GetEquipmenViewtList(0, 0, "a.Id desc", where) var datainfo DataInfo datainfo.Items = list datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 名字id列表 // @Description 获取设备的名字与id列表 // @Success 200 {object} business.device.DeviceChannels // @router /elist [get] func (this *EquipmentController) NameIdList() { svc := equipment.GetEquipmentService(utils.DBE) svcPermission := permission.GetPermissionService(utils.DBE) eids := svcPermission.GetEquipmentIdById(utils.ToStr(this.User.Id)) where := " a.AccCode = '" + this.User.AccCode + "'" if eids != "" { where = where + " and (a.CreateUserId= " + utils.ToStr(this.User.Id) + " or a.Id in (" + eids + "))" } else { where = where + "and a.CreateUserId= " + utils.ToStr(this.User.Id) } var list []string _, entities := svc.GetEquipmenViewtList(0, 0, "a.Id desc", where) for i := 0; i < len(entities); i++ { list = append(list, entities[i].Name) list = append(list, utils.ToStr(entities[i].Id)) } var datainfo DataInfo datainfo.Items = list this.Data["json"] = &datainfo this.ServeJSON() } // @Title 获取品牌数据 // @Description 获取品牌数据 // @Success 200 {object} business.device.DeviceChannels // @router /brands [get] func (this *EquipmentController) GetBrands() { svc := equipment.GetEquipmentService(utils.DBE) // 获得所有的品牌 brands := svc.GetAllBrands(this.User.AccCode) var datainfo DataInfo datainfo.Items = brands this.Data["json"] = &datainfo this.ServeJSON() } // @Title 根据品牌取型号 // @Description 设备列表 // @Success 200 {object} business.device.DeviceChannels // @router /dmodels/:brand [get] func (this *EquipmentController) Dmodels() { brand := this.Ctx.Input.Param(":brand") svc := equipment.GetEquipmentService(utils.DBE) // 获得所有的品牌 dmodels := svc.GetModelsByBrand(brand, this.User.AccCode) var datainfo DataInfo datainfo.Items = dmodels this.Data["json"] = &datainfo this.ServeJSON() } // @Title 报警器列表 // @Description 设备列表 // @Success 200 {object} business.device.DeviceChannels // @router /photo/:id [get] func (this *EquipmentController) GetPhoto() { id := this.Ctx.Input.Param(":id") svc := equipment.GetEquipmentService(utils.DBE) // 获得对应型号的照片数据 var entity equipment.EquipMentModel where := "DModel='" + id + "'" svc.GetEntity(&entity, where) var datainfo DataInfo realArr := [2]string{entity.Photo, entity.SpareData} datainfo.Items = realArr this.Data["json"] = &datainfo this.ServeJSON() } // @Title 根据型号获取设备信息 // @Description 根据型号获取设备信息 // @Success 200 {object} business.device.DeviceChannels // @router /getbydmodel/:dmodel [get] func (this *EquipmentController) GetBydModel() { dmodel := this.Ctx.Input.Param(":dmodel") svc := equipment.GetEquipmentService(utils.DBE) var entity equipment.EquipMentModel where := "Id='" + dmodel + "'" svc.GetEntity(&entity, where) this.Data["json"] = &entity this.ServeJSON() } // @Title 根据Id获取设备信息 // @Description 根据型号获取设备信息 // @Success 200 {object} business.device.DeviceChannels // @router /getmodel/:id [get] func (this *EquipmentController) GetModel() { id := this.Ctx.Input.Param(":id") svc := equipment.GetEquipmentService(utils.DBE) entity := svc.GetEquipmentViewById(id) this.Data["json"] = &entity this.ServeJSON() } // @Title 根据ID判断是否有冻存架使用 // @Description 根据ID判断是否有冻存架使用 // @Success 200 {object} business.device.DeviceChannels // @router /getisusedbyshelf/:id [get] func (this *EquipmentController) GetisUsedByShelf() { id := this.Ctx.Input.Param(":id") var shelf shelfset.Shelf svcshelf := shelfset.GetshelfsetService(utils.DBE) has := svcshelf.GetShelfByEquipment(id, &shelf) var errinfo ErrorInfo if has { errinfo.Code = 0 errinfo.Message = "有冻存架" } else { errinfo.Code = -1 errinfo.Message = "无冻存架" } this.Data["json"] = &errinfo this.ServeJSON() } // @Title 创建报警项目 // @Description 创建报警项目 // @Param body body business.device.DeviceChannels "报警项目信息" // @Success 200 {object} controllers.Request // @router /editequip/:id [put] func (this *EquipmentController) EditEquipment() { id := this.Ctx.Input.Param(":id") // 获得前端传输的model var model EquipmentModel var jsonblob = this.Ctx.Input.RequestBody json.Unmarshal(jsonblob, &model) //赋值 var equip equipment.Equipment equip.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int() equip.ModifiedBy = this.User.Realname equip.Remark = model.Remark equip.Code = model.Code equip.ModelId = model.ModelId equip.Name = model.Name equip.Brand = model.Brand equip.ModelVersion = model.ModelVersion equip.Item = 1 // 立式 取消了这项选择 equip.RowNum = model.RowNum equip.ColumnNum = model.ColumnNum equip.Width, _ = utils.StrTo(model.Width).Float32() equip.Height, _ = utils.StrTo(model.Height).Float32() equip.Depth, _ = utils.StrTo(model.Depth).Float32() equip.SampleType = model.Checkedstr equip.ProduceDate = time.Unix(model.ProduceDate/1000, 0) equip.UseDate = time.Unix(model.UseDate/1000, 0) equip.ValidityDate = time.Unix(model.ValidityDate/1000, 0) // 初始化服务 var errinfo ErrorInfo var equipempty equipment.Equipment svc := equipment.GetEquipmentService(utils.DBE) // 处理数据, 保存设备 var cols []string = []string{"ModelId", "ModifiedUserId", "ModifiedBy", "Remark", "Code", "Name", "Brand", "ModelVersion", "Item", "RowNum", "ColumnNum", "Width", "Height", "Depth", "SampleType", "ProduceDate", "UseDate", "ValidityDate"} opdesc := "修改设备-" + equip.Name err := svc.UpdateEntityAndWriteLog(id, &equip, &equipempty, cols, utils.ToStr(this.User.Id), this.User.Username, opdesc, this.User.AccCode, "设备管理") //删除Sensors wheresensor := " where AccCode='" + this.User.AccCode + "' and EquipmentId=" + id svc.DeleteSensors(wheresensor) //添加Sensors //添加Sensors if len(model.TCode) > 0 { sensorsinfoarr := model.TCode for i := 0; i < len(sensorsinfoarr); i++ { // sensordata := strings.Split(sensorsinfoarr[i], ";") var esentity equipment.EquipmentSensors esentity.AccCode = this.User.AccCode esentity.EquipmentId, _ = utils.StrTo(id).Int() // 授权码暂时取消 // esentity.AuthCode = sensordata[0] esentity.SensorNo = sensorsinfoarr[i] esentity.CreateBy = this.User.Realname esentity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() _, _ = svc.InsertEntity(&esentity) } } // 错误检测 if err != nil { errinfo.Message = "编辑设备失败!" errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() return } else { errinfo.Message = "编辑设备成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() return } } // @Title 复制所选设备 // @Description 复制所选设备 // @Param body body business.device.DeviceChannels "报警项目信息" // @Success 200 {object} controllers.Request // @router /copyequip/:id [put] func (this *EquipmentController) CopyEquipment() { id := this.Ctx.Input.Param(":id") var entity equipment.Equipment svc := equipment.GetEquipmentService(utils.DBE) svc.GetEntityById(id, &entity) entity.Id = 0 entity.Name = entity.Name + utils.ToStr(time.Now())[7:19] entity.Code = entity.Code + utils.ToStr(time.Now())[7:19] entity.HCode = "" entity.ModifiedUserId = 0 entity.ModifiedBy = "" entity.CreateBy = this.User.Realname entity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() _, err := svc.InsertEntity(&entity) new_id := entity.Id var shelf_list []shelfset.Shelf where := "EquipmentId=" + id svc.GetEntities(&shelf_list, where) svcshelf := shelfset.GetshelfsetService(utils.DBE) lastdata := svcshelf.GetLastShelfNum(this.User.AccCode) num_lastdata, _ := utils.StrTo(lastdata[1:]).Int64() for i := 0; i < len(shelf_list); i++ { old_shelf_id := shelf_list[i].Id shelf_list[i].Id = 0 shelf_list[i].Code = "F" + utils.ToStr(num_lastdata) shelf_list[i].BarCode = "F" + utils.ToStr(num_lastdata) shelf_list[i].EquipmentId = new_id shelf_list[i].ModifiedBy = "" shelf_list[i].ModifiedUserId = 0 shelf_list[i].CreateBy = this.User.Realname shelf_list[i].CreateUserId, _ = utils.StrTo(this.User.Id).Int() num_lastdata = num_lastdata + 1 _, err = svc.InsertEntity(&shelf_list[i]) new_shelf_id := shelf_list[i].Id var box_list []shelfset.Box where1 := "EquipmentId=" + id + " and ShelfId=" + utils.ToStr(old_shelf_id) svc.GetEntities(&box_list, where1) lastdata_box := svcshelf.GetLastBoxNum(this.User.AccCode) num_lastdata_box, _ := utils.StrTo(lastdata_box[1:]).Int64() for j := 0; j < len(box_list); j++ { box_list[j].Id = 0 box_list[j].Code = "B" + utils.ToStr(num_lastdata_box) box_list[j].BarCode = "B" + utils.ToStr(num_lastdata_box) box_list[j].EquipmentId = new_id box_list[j].ShelfId = new_shelf_id box_list[j].ModifiedBy = "" box_list[j].ModifiedUserId = 0 box_list[j].CreateBy = this.User.Realname box_list[j].CreateUserId, _ = utils.StrTo(this.User.Id).Int() num_lastdata_box = num_lastdata_box + 1 _, err = svc.InsertEntity(&box_list[j]) } num_lastdata_box = num_lastdata_box - 1 var lastnum lastordernum.LastOrderNum lastnum.LastNum = "B" + utils.ToStr(num_lastdata_box) svcshelf.UpdateBoxNum(lastnum, this.User.AccCode) var currboxcapacitys currboxcapacity.CurrBoxCapacity svc.GetEntity(&currboxcapacitys, where1) currboxcapacitys.Id = 0 currboxcapacitys.EquipmentId = new_id currboxcapacitys.ShelfId = new_shelf_id _, err = svc.InsertEntity(&currboxcapacitys) } num_lastdata = num_lastdata - 1 var lastnum_shelf lastordernum.LastOrderNum lastnum_shelf.LastNum = "F" + utils.ToStr(num_lastdata) svcshelf.UpdateShelfNum(lastnum_shelf, this.User.AccCode) var errinfo ErrorInfo // 错误检测 if err != nil { errinfo.Message = "复制设备失败!" errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() return } else { errinfo.Message = "复制设备成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() return } } // @Title 创建容器设备数据 // @Description 创建容器设备数据 // @Param body body business.device.DeviceChannels "报警项目信息" // @Success 200 {object} controllers.Request // @router / [post] func (this *EquipmentController) AddEquipment() { // 获得前端传输的model var model EquipmentModel var jsonblob = this.Ctx.Input.RequestBody json.Unmarshal(jsonblob, &model) //赋值 var equip equipment.Equipment equip.AccCode = this.User.AccCode equip.Code = model.Code equip.ModelId = model.ModelId equip.Name = model.Name equip.Brand = model.Brand equip.ModelVersion = model.ModelVersion equip.Item = 1 // 立式 取消了这项选择 equip.RowNum = model.RowNum equip.ColumnNum = model.ColumnNum equip.Width, _ = utils.StrTo(model.Width).Float32() equip.Height, _ = utils.StrTo(model.Height).Float32() equip.Depth, _ = utils.StrTo(model.Depth).Float32() equip.SampleType = model.Checkedstr equip.State = 1 //默认启用 equip.CreateUserId, _ = utils.StrTo(this.User.Id).Int() equip.CreateBy = this.User.Realname equip.Remark = model.Remark equip.ProduceDate = time.Unix(model.ProduceDate/1000, 0) equip.UseDate = time.Unix(model.UseDate/1000, 0) equip.ValidityDate = time.Unix(model.ValidityDate/1000, 0) //添加设备 svc := equipment.GetEquipmentService(utils.DBE) _, err := svc.InsertEntity(&equip) var errinfo ErrorInfo if err != nil { errinfo.Message = "添加设备失败!" + err.Error() errinfo.Code = -2 this.Data["json"] = &errinfo this.ServeJSON() return } //同步添加冻存架 // 19:贝尔Cryobiobank13K液氮罐;20:贝尔Cryobiobank13K_BloodBag液氮罐 if model.DItem == 19 { shelf_x_int := 1 shelf_y_int := 1 box_x_int := 1 box_y_int := 1 svcshelf := shelfset.GetshelfsetService(utils.DBE) lastdata := svcshelf.GetLastShelfNum(this.User.AccCode) num_lastdata, _ := utils.StrTo(lastdata[1:]).Int64() lastdata_box := svcshelf.GetLastBoxNum(this.User.AccCode) num_lastdata_box, _ := utils.StrTo(lastdata_box[1:]).Int64() for i := 1; i <= equip.RowNum; i++ { for j := 1; j <= equip.ColumnNum; j++ { if j == 4 { //第4个为小型格4行5x5冻存盒 shelf_x_int = 1 shelf_y_int = 10 box_x_int = 5 box_y_int = 5 } else { //其他为大型格12行10x10冻存盒 shelf_x_int = 1 shelf_y_int = 10 box_x_int = 10 box_y_int = 10 } var shelfempty shelfset.Shelf shelfempty.AccCode = this.User.AccCode shelfempty.EquipmentId = equip.Id shelfempty.Code = "F" + utils.ToStr(num_lastdata) shelfempty.BarCode = "F" + utils.ToStr(num_lastdata) shelfempty.RowNum = shelf_x_int shelfempty.ColumnNum = shelf_y_int shelfempty.XStation = j shelfempty.YStation = i shelfempty.SampleType = equip.SampleType shelfempty.CreateBy = this.User.Realname shelfempty.CreateUserId, _ = utils.StrTo(this.User.Id).Int() num_lastdata = num_lastdata + 1 _, err = svcshelf.InsertEntity(&shelfempty) sqlfield := " AccCode,EquipmentId,ShelfId,XStation,YStation,RowNum,ColumnNum," sqlvalue := "'" + this.User.AccCode + "'," + utils.ToStr(equip.Id) + "," + utils.ToStr(shelfempty.Id) + "," + utils.ToStr(j) + "," + utils.ToStr(i) + "," + utils.ToStr(shelf_x_int) + "," + utils.ToStr(shelf_y_int) + "," ii, jj := shelf_x_int, shelf_y_int //添加冻存盒 if box_x_int > 0 && box_y_int > 0 { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-1" } else { sqlfield += fieldname + "," sqlvalue += "-1," } } } for a := 1; a <= shelf_x_int; a++ { for b := 1; b <= shelf_y_int; b++ { var boxempty shelfset.Box boxempty.AccCode = this.User.AccCode boxempty.EquipmentId = equip.Id boxempty.ShelfId = shelfempty.Id boxempty.Code = "B" + utils.ToStr(num_lastdata_box) boxempty.BarCode = "B" + utils.ToStr(num_lastdata_box) boxempty.YStation = a boxempty.XStation = b boxempty.RowNum = box_x_int boxempty.ColumnNum = box_y_int boxempty.SampleType = equip.SampleType boxempty.CreateBy = this.User.Realname boxempty.CreateBy = this.User.Realname num_lastdata_box = num_lastdata_box + 1 _, err = svc.InsertEntity(&boxempty) } } } else { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-2" } else { sqlfield += fieldname + "," sqlvalue += "-2," } } } } svc1 := currboxcapacity.GetCurrboxcapacityService(utils.DBE) svc1.Add(sqlfield, sqlvalue) } } num_lastdata_box = num_lastdata_box - 1 var lastnum lastordernum.LastOrderNum lastnum.LastNum = "B" + utils.ToStr(num_lastdata_box) svcshelf.UpdateBoxNum(lastnum, this.User.AccCode) num_lastdata = num_lastdata - 1 var lastnum_shelf lastordernum.LastOrderNum lastnum_shelf.LastNum = "F" + utils.ToStr(num_lastdata) svcshelf.UpdateShelfNum(lastnum_shelf, this.User.AccCode) } else if model.DItem == 20 { shelf_x_int := 1 shelf_y_int := 6 box_x_int := 1 box_y_int := 1 svcshelf := shelfset.GetshelfsetService(utils.DBE) lastdata := svcshelf.GetLastShelfNum(this.User.AccCode) num_lastdata, _ := utils.StrTo(lastdata[1:]).Int64() lastdata_box := svcshelf.GetLastBoxNum(this.User.AccCode) num_lastdata_box, _ := utils.StrTo(lastdata_box[1:]).Int64() for i := 1; i <= equip.RowNum; i++ { for j := 1; j <= equip.ColumnNum; j++ { var shelfempty shelfset.Shelf shelfempty.AccCode = this.User.AccCode shelfempty.EquipmentId = equip.Id shelfempty.Code = "F" + utils.ToStr(num_lastdata) shelfempty.BarCode = "F" + utils.ToStr(num_lastdata) shelfempty.RowNum = shelf_x_int shelfempty.ColumnNum = shelf_y_int shelfempty.XStation = j shelfempty.YStation = i shelfempty.SampleType = equip.SampleType shelfempty.CreateBy = this.User.Realname shelfempty.CreateUserId, _ = utils.StrTo(this.User.Id).Int() num_lastdata = num_lastdata + 1 _, err = svcshelf.InsertEntity(&shelfempty) sqlfield := " AccCode,EquipmentId,ShelfId,XStation,YStation,RowNum,ColumnNum," sqlvalue := "'" + this.User.AccCode + "'," + utils.ToStr(equip.Id) + "," + utils.ToStr(shelfempty.Id) + "," + utils.ToStr(j) + "," + utils.ToStr(i) + "," + utils.ToStr(shelf_x_int) + "," + utils.ToStr(shelf_y_int) + "," ii, jj := shelf_x_int, shelf_y_int //添加冻存盒 if box_x_int > 0 && box_y_int > 0 { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-1" } else { sqlfield += fieldname + "," sqlvalue += "-1," } } } for a := 1; a <= shelf_x_int; a++ { for b := 1; b <= shelf_y_int; b++ { var boxempty shelfset.Box boxempty.AccCode = this.User.AccCode boxempty.EquipmentId = equip.Id boxempty.ShelfId = shelfempty.Id boxempty.Code = "B" + utils.ToStr(num_lastdata_box) boxempty.BarCode = "B" + utils.ToStr(num_lastdata_box) boxempty.YStation = a boxempty.XStation = b boxempty.RowNum = box_x_int boxempty.ColumnNum = box_y_int boxempty.SampleType = equip.SampleType boxempty.CreateBy = this.User.Realname boxempty.CreateBy = this.User.Realname num_lastdata_box = num_lastdata_box + 1 _, err = svc.InsertEntity(&boxempty) } } } else { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-2" } else { sqlfield += fieldname + "," sqlvalue += "-2," } } } } svc1 := currboxcapacity.GetCurrboxcapacityService(utils.DBE) svc1.Add(sqlfield, sqlvalue) } } num_lastdata_box = num_lastdata_box - 1 var lastnum lastordernum.LastOrderNum lastnum.LastNum = "B" + utils.ToStr(num_lastdata_box) svcshelf.UpdateBoxNum(lastnum, this.User.AccCode) num_lastdata = num_lastdata - 1 var lastnum_shelf lastordernum.LastOrderNum lastnum_shelf.LastNum = "F" + utils.ToStr(num_lastdata) svcshelf.UpdateShelfNum(lastnum_shelf, this.User.AccCode) } else { if model.Shelf_X > 0 && model.Shelf_y > 0 { shelf_x_int := model.Shelf_X shelf_y_int := model.Shelf_y box_x_int := model.Box_X box_y_int := model.Box_y svcshelf := shelfset.GetshelfsetService(utils.DBE) lastdata := svcshelf.GetLastShelfNum(this.User.AccCode) num_lastdata, _ := utils.StrTo(lastdata[1:]).Int64() lastdata_box := svcshelf.GetLastBoxNum(this.User.AccCode) num_lastdata_box, _ := utils.StrTo(lastdata_box[1:]).Int64() for i := 1; i <= equip.RowNum; i++ { for j := 1; j <= equip.ColumnNum; j++ { var shelfempty shelfset.Shelf shelfempty.AccCode = this.User.AccCode shelfempty.EquipmentId = equip.Id shelfempty.Code = "F" + utils.ToStr(num_lastdata) shelfempty.BarCode = "F" + utils.ToStr(num_lastdata) shelfempty.RowNum = shelf_x_int shelfempty.ColumnNum = shelf_y_int shelfempty.XStation = j shelfempty.YStation = i shelfempty.SampleType = equip.SampleType shelfempty.CreateBy = this.User.Realname shelfempty.CreateUserId, _ = utils.StrTo(this.User.Id).Int() num_lastdata = num_lastdata + 1 _, err = svcshelf.InsertEntity(&shelfempty) sqlfield := " AccCode,EquipmentId,ShelfId,XStation,YStation,RowNum,ColumnNum," sqlvalue := "'" + this.User.AccCode + "'," + utils.ToStr(equip.Id) + "," + utils.ToStr(shelfempty.Id) + "," + utils.ToStr(j) + "," + utils.ToStr(i) + "," + utils.ToStr(shelf_x_int) + "," + utils.ToStr(shelf_y_int) + "," ii, jj := shelf_x_int, shelf_y_int //添加冻存盒 if box_x_int > 0 && box_y_int > 0 { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-1" } else { sqlfield += fieldname + "," sqlvalue += "-1," } } } for a := 1; a <= shelf_x_int; a++ { for b := 1; b <= shelf_y_int; b++ { var boxempty shelfset.Box boxempty.AccCode = this.User.AccCode boxempty.EquipmentId = equip.Id boxempty.ShelfId = shelfempty.Id boxempty.Code = "B" + utils.ToStr(num_lastdata_box) boxempty.BarCode = "B" + utils.ToStr(num_lastdata_box) boxempty.YStation = a boxempty.XStation = b boxempty.RowNum = box_x_int boxempty.ColumnNum = box_y_int boxempty.SampleType = equip.SampleType boxempty.CreateBy = this.User.Realname boxempty.CreateBy = this.User.Realname num_lastdata_box = num_lastdata_box + 1 _, err = svc.InsertEntity(&boxempty) } } } else { for e := 0; e < ii; e++ { charname := Boxlinename(e + 1) for f := 0; f < jj; f++ { fieldname := fmt.Sprintf("%v%v", charname, f+1) if f == jj-1 && e == ii-1 { sqlfield += fieldname sqlvalue += "-2" } else { sqlfield += fieldname + "," sqlvalue += "-2," } } } } svc1 := currboxcapacity.GetCurrboxcapacityService(utils.DBE) svc1.Add(sqlfield, sqlvalue) } } num_lastdata_box = num_lastdata_box - 1 var lastnum lastordernum.LastOrderNum lastnum.LastNum = "B" + utils.ToStr(num_lastdata_box) svcshelf.UpdateBoxNum(lastnum, this.User.AccCode) num_lastdata = num_lastdata - 1 var lastnum_shelf lastordernum.LastOrderNum lastnum_shelf.LastNum = "F" + utils.ToStr(num_lastdata) svcshelf.UpdateShelfNum(lastnum_shelf, this.User.AccCode) } } //添加Sensors if len(model.TCode) > 0 { sensorsinfoarr := model.TCode for i := 0; i < len(sensorsinfoarr); i++ { // sensordata := strings.Split(sensorsinfoarr[i], ";") var esentity equipment.EquipmentSensors esentity.AccCode = this.User.AccCode esentity.EquipmentId = equip.Id // 授权码暂时取消 // esentity.AuthCode = sensordata[0] esentity.SensorNo = sensorsinfoarr[i] esentity.CreateBy = this.User.Realname esentity.CreateUserId, _ = utils.StrTo(this.User.Id).Int() _, _ = svc.InsertEntity(&esentity) } } errinfo.Message = "添加设备成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } // @Title 删除容器设备 // @Description 删除容器设备 // @Param code path string true "需要删除的传感器编号" // @Success 200 {object} ErrorInfo // @Failure 403 :id 为空 // @router delequip/:id [delete] func (this *EquipmentController) EquipmentDelete() { id := this.Ctx.Input.Param(":id") name := this.GetString("name") var entity equipment.Equipment var entityempty equipment.Equipment svc := equipment.GetEquipmentService(utils.DBE) opdesc := "删除设备-" + name err := svc.DeleteEntityAndWriteLog(id, &entity, &entityempty, utils.ToStr(this.User.Id), this.User.Username, opdesc, this.User.AccCode, "设备管理") //删除样本 svc1 := shelfset.GetshelfsetService(utils.DBE) where_shelf := "EquipmentId =" + id var shelfempty shelfset.Shelf svc1.DeleteEntity(&shelfempty, where_shelf) var currboxcapacitys currboxcapacity.CurrBoxCapacity svc1.DeleteEntity(&currboxcapacitys, where_shelf) var boxempty shelfset.Box svc1.DeleteEntity(&boxempty, where_shelf) //删除Sensors wheresensor := "where AccCode='" + this.User.AccCode + "' and EquipmentId=" + id svc.DeleteSensors(wheresensor) var errinfo ErrorInfo // 错误检测 if err != nil { errinfo.Message = "删除设备失败!" errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() return } else { errinfo.Message = "删除设备成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } } // @Title 更新所有冻存盒颜色值 // @Description 更新所有冻存盒颜色值 // @Param code path string true "需要删除的传感器编号" // @Success 200 {object} ErrorInfo // @Failure 403 :id 为空 // @router /refresh [put] func (this *EquipmentController) RefreshCapacity() { s := currboxcapacity.GetCurrboxcapacityService(utils.DBE) var acccodes []Id_Str //账户AccCode列表 curracccode := this.User.AccCode if curracccode == "" { sqltables := "select distinct AccCode Id from Equipment " s.DBE.Sql(sqltables).Find(&acccodes) } else { //账户内部刷新 acccodes = append(acccodes, Id_Str{curracccode}) } var tablename string //表名 var boxdata []samplesinfo.SampleBoxCount for _, v := range acccodes { tablename = string(v.Id) + "SamplesDetail" //样本表名 var fieldname, updatesql string whereshelf := " where AccCode = '" + v.Id + "'" var shelflist []shelfset.Shelf shelflist = s.GetShelfList(whereshelf) //获取冻存架列表 for si := 0; si < len(shelflist); si++ { //循环遍历冻存架,去样本库中查找样本 fieldStr := " UpdateTime= now() " updateStr := " UpdateTime= now() " //先将所有的状态置成-1//////////////////////////开始///////////////////////////////// var boxlist []shelfset.Box boxlist = s.GetBoxByShelfId(utils.ToStr(shelflist[si].Id)) boxlistcount := len(boxlist) for bi := 0; bi < boxlistcount; bi++ { updatename := Boxlinename(boxlist[bi].YStation) + utils.ToStr(boxlist[bi].XStation) updateStr = updateStr + "," updateStr = updateStr + updatename + "= -1" } if updateStr != "" { updatesql = " update CurrBoxCapacity set " + updateStr + " where ShelfId = " + utils.ToStr(shelflist[si].Id) fmt.Println(updatesql) _, err := s.DBE.Exec(updatesql) LogError(err) } //先将所有的状态置成-1//////////////////////////结束///////////////////////////////// //更改冻存盒的实际状态////////////////////开始////////////////////////////////////// boxwhere := " where IState=1 and DeletionStateCode = 0 and ShelfId =" + utils.ToStr(shelflist[si].Id) + "" svc := samplesinfo.GetSamplesInfoService(utils.DBE) boxdata = svc.QuerySampleBoxDataList(tablename, boxwhere) //一个冻存盒Id是一条数据 if len(boxdata) > 0 { for i := 0; i < len(boxdata); i++ { box := new(shelfset.Box) s.GetBoxById(utils.ToStr(boxdata[i].BoxId), box) if box.Id == 0 { //如果找不到冻存盒 continue } else { fieldStr = fieldStr + "," boxdata[i].AllCount = int(box.RowNum * box.ColumnNum) crate := float64(boxdata[i].CurrCount) / float64(boxdata[i].AllCount) * 100 c_str := strconv.FormatFloat(crate, 'f', 2, 64) c_f64, _ := strconv.ParseFloat(c_str, 64) boxdata[i].CurrRate = c_f64 fieldname = Boxlinename(box.YStation) + utils.ToStr(box.XStation) } fieldStr = fieldStr + fieldname + "=" + utils.ToStr(boxdata[i].CurrRate) } if fieldStr != "" { updatesql = " update CurrBoxCapacity set " + fieldStr + " where ShelfId = " + utils.ToStr(shelflist[si].Id) fmt.Println(updatesql) _, err := s.DBE.Exec(updatesql) LogError(err) } } //更改冻存盒的实际状态//////////////////////结束//////////////////////////////////// } } } // @Title 获取传感器列表 // @Description 获取传感器列表 // @Success 200 {object} business.device.DeviceChannels // @router /getsensorslist [get] func (this *EquipmentController) GetSensorsList() { svcauthcode := authcode.GetAuthCodeService(utils.DBE) var aclist []authcode.AuthCode whereac := " AccCode='" + this.User.AccCode + "'" aclist = svcauthcode.GetList(" Id desc limit 1 ", whereac) keystr := "" if len(aclist) > 0 { keystr = aclist[0].AuthCode } ////支持多个授权码 // for i := 0; i < len(aclist); i++ { // if ki == len(aclist)-1 { // keystr = keystr + "'" + aclist[i].AuthCode + "'" // } else { // keystr = keystr + "'" + aclist[i].AuthCode + "'" + "," // } // } //调接口获取sensors var ccsensor []equipment.CCSensor if keystr != "" { strUrlwaring := utils.Cfg.MustValue("server", "dataapiurl") + "/channels/channels" json.Unmarshal(ApiKeyRequest(strUrlwaring, "GET", keystr, nil), &ccsensor) } var datainfo DataInfo datainfo.Items = ccsensor this.Data["json"] = &datainfo this.ServeJSON() } // @Title 验证传感器编号是否有权限 // @Description 获取传感器列表 // @Success 200 {object} business.device.DeviceChannels // @router /validsensorsispermi [get] func (this *EquipmentController) ValidsensorsisPermi() { svcauthcode := authcode.GetAuthCodeService(utils.DBE) var aclist []authcode.AuthCode whereac := " AccCode='" + this.User.AccCode + "'" aclist = svcauthcode.GetList(" Id desc limit 1 ", whereac) var errinfo ErrorInfo if len(aclist) == 0 { errinfo.Message = "请先设置授权码" errinfo.Code = -2 this.Data["json"] = &errinfo this.ServeJSON() return } keystr := aclist[0].AuthCode sensors := this.GetString("sensors") sensorarr := strings.Split(sensors, ",") //调接口获取sensors var ccsensor []equipment.CCSensor if keystr != "" { strUrlwaring := utils.Cfg.MustValue("server", "dataapiurl") + "/channels/channels" json.Unmarshal(ApiKeyRequest(strUrlwaring, "GET", keystr, nil), &ccsensor) } isok := false //是否通过验证 nohavesensor := "" // 不存在的sensor for si := 0; si < len(sensorarr); si++ { isok = false for i := 0; i < len(ccsensor); i++ { if ccsensor[i].Serial == sensorarr[si] { isok = true break } } if !isok { nohavesensor = sensorarr[si] break } } if isok { errinfo.Message = "验证通过!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } else { errinfo.Message = "模块序列号" + nohavesensor + "不存在" errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() } } // @Title 根据容器id获取温度模块 // @Description 获取传感器列表 // @Success 200 {object} business.device.DeviceChannels // @router /getsensorsbyequid [get] func (this *EquipmentController) GetSensorsbyEquid() { equid := this.GetString("equid") svc := equipment.GetEquipmentService(utils.DBE) var esentitylist []equipment.EquipmentSensors wheresensor := "where AccCode='" + this.User.AccCode + "' and EquipmentId=" + equid esentitylist = svc.GetSensorsList(wheresensor) this.Data["json"] = esentitylist this.ServeJSON() } // @Title 报警器列表 // @Description 设备列表 // @Success 200 {object} business.device.DeviceChannels // @router /bindequipment/:id [get] func (this *EquipmentController) GetEquipmentByOrganId() { id := this.Ctx.Input.Param(":id") svc := equipment.GetEquipmentService(utils.DBE) var entity []equipment.Equipment where := "OrganizeId='" + id + "'" svc.GetEntities(&entity, where) var datainfo DataInfo datainfo.Items = entity this.Data["json"] = &datainfo this.ServeJSON() } // @Title 样本库获取容器列表 // @Description BioBank获取设备的名字与id、层数列表 // @Success 200 {object} business.device.DeviceChannels // @router /equipmentlist [get] //func (this *EquipmentController) NameIdRowList() { // svc := equipment.GetEquipmentService(utils.DBE) // svcrole := role.GetRoleService(utils.DBE) // roleids := svcrole.GetRoleidsByuid(utils.ToStr(this.User.Id)) // where := " a.AccCode = '" + this.User.AccCode + "'" // where = where + " and (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (select EquipmentId Id FROM Base_RoleEquipment where RoleId in (" + strings.Join(roleids, ",") + ")))" // var list []string // _, entities := svc.GetEquipmenViewtList(0, 0, "a.Id desc", where) // for i := 0; i < len(entities); i++ { // list = append(list, entities[i].Name) // list = append(list, utils.ToStr(entities[i].Id)) // list = append(list, utils.ToStr(entities[i].RowNum)) // list = append(list, utils.ToStr(entities[i].ColumnNum)) // list = append(list, utils.ToStr(entities[i].DItem)) // } // var datainfo DataInfo // datainfo.Items = list // this.Data["json"] = &datainfo // this.ServeJSON() //} // @Title 样本库获取Shelf列表 // @Description BioBank获取设备的名字与id、层数列表 // @Success 200 {object} business.device.DeviceChannels // @router /shelflist [get] //func (this *EquipmentController) Shelflist() { // EquipmentId := this.GetString("EquipmentId") // var list []shelfset.Shelf // svc := equipment.GetEquipmentService(utils.DBE) // where := " EquipmentId ='" + EquipmentId + "'" // svc.GetEntitysByWhere(ShelfName, where, &list) // var datainfo DataInfo // datainfo.Items = list // this.Data["json"] = &datainfo // this.ServeJSON() //} // @Title 样本库获取Box列表 // @Description BioBank获取设备的名字与id、层数列表 // @Success 200 {object} business.device.DeviceChannels // @router /boxlist [get] //func (this *EquipmentController) Boxlist() { // Shelf := this.GetString("Shelf") // var list []shelfset.Box // svc := equipment.GetEquipmentService(utils.DBE) // where := " ShelfId ='" + Shelf + "'" // svc.GetEntitysByWhere(BoxName, where, &list) // var datainfo DataInfo // datainfo.Items = list // this.Data["json"] = &datainfo // this.ServeJSON() //}