sqlAdd := ``
var values []interface{}
if req.Name != "" {
sqlAdd += ` and bd.name like ? `
values = append(values, "%"+req.Name+"%")
}
if req.DeviceType != "" {
sqlAdd += ` and bd.device_type = ? `
values = append(values, req.DeviceType)
}
if req.Code != "" {
sqlAdd += ` and bd.code like ? `
values = append(values, "%"+req.Code+"%")
}
if req.DeviceModel != "" {
sqlAdd += ` and bd.device_model like ? `
values = append(values, "%"+req.DeviceModel+"%")
}
if req.Supplier != "" {
sqlAdd += ` and bd.supplier like ? `
values = append(values, "%"+req.Supplier+"%")
}
if req.EnableDate != nil && len(req.EnableDate) > 0 {
if req.EnableDate[0] != "" {
sqlAdd += ` and bd.enable_date > ? `
values = append(values, req.EnableDate[0])
}
if len(req.EnableDate) > 1 && req.EnableDate[1] != "" {
sqlAdd += ` and bd.enable_date < ? `
values = append(values, req.EnableDate[1])
}
}
if req.TheFloor != "" {
sqlAdd += ` and bd.the_floor like ? `
values = append(values, "%"+req.TheFloor+"%")
}
if req.DeviceLocation != "" {
sqlAdd += ` and bd.device_location like ? `
values = append(values, "%"+req.DeviceLocation+"%")
}
if req.AreaType != "" {
sqlAdd += ` and bd.area_type like ? `
values = append(values, "%"+req.TheFloor+"%")
}
if req.BuildingID != 0 {
sqlAdd += ` and bd.building_id = ? `
values = append(values, req.BuildingID)
}
type Count struct {
C int `orm:"C"`
}
count := new(Count)
//1.分页
err = g.DB().Raw(` select count(1) C from building_device as bd where bd.id is not null `+sqlAdd, values...).Scan(count)
if err != nil {
g.Log().Error(err)
return
}
total = count.C
if req.PageNum == 0 {
req.PageNum = 1
}
page = req.PageNum
//2. 查询数据
if req.PageSize == 0 {
req.PageSize = comModel.PageSize
}
err = g.DB().Raw(" select * from building_device as bd where bd.id is not null "+sqlAdd, values...).Limit((req.PageNum-1)*req.PageSize, req.PageSize).Scan(&list)
if err != nil {
g.Log().Error(err)
return
}
// 合同还没做,这里先默认所有建筑为停用状态
for _, v := range list {
v.ContractStatus = 2
}
return