package com.hb.proj.base.service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.hb.proj.model.DevicePO; import com.hb.proj.model.DeviceParamPO; import com.hb.proj.model.DeviceVO; import com.hb.xframework.dao.core.SpringJdbcDAO; import com.hb.xframework.dao.util.PageModel; import com.hb.xframework.dao.util.UUIDHexGenerator; @Service public class DeviceService { @Autowired private SpringJdbcDAO dao; public List loadByType(String devType){ String tps="'"+devType.replace(",", "','")+"'"; String sql=""" select device_name,device_code,device_type,well_name from tzl_gather_device d left join tzl_well w on d.well_id=w.well_id and w.del_if=false where d.del_if=false """ + " and device_type in ("+tps+")"; return dao.queryForList(sql, DeviceVO.class); } public PageModel query(Map args,int pageNo,int pageSize){ List sqlParams=new ArrayList(); StringBuilder sql=new StringBuilder(100); sql.append("select w.well_name,d.* "); sql.append(" from tzl_gather_device d "); sql.append(" left join tzl_well w on d.well_id=w.well_id and w.del_if=false"); sql.append(" where d.del_if=false "); if(args!=null&&StringUtils.isNotBlank((String)args.get("deviceKey"))) { sql.append(" and (device_name like ? or device_code like ?)"); sqlParams.add("%"+args.get("deviceKey")+"%"); sqlParams.add("%"+args.get("deviceKey")+"%"); } sql.append(" order by device_code"); Object[] sqlArgs=sqlParams.size()>0?sqlParams.toArray():null; return dao.queryForPagedData(sql.toString(),pageNo, pageSize,DeviceVO.class ,sqlArgs); } public String add(DevicePO device) { UUIDHexGenerator uuid=UUIDHexGenerator.getInstance(); device.setDeviceId(uuid.generate()); device.setModifyTime(new Date()); device.setDelIf(false); dao.insert(device,"tzl_gather_device"); return device.getDeviceId(); } public boolean delete(String deviceId) { String sql="update tzl_gather_device set del_if=true,modify_time=now() where device_id=?"; dao.exeUpdate(sql,deviceId); return true; } public DeviceVO get(String deviceId) { String sql=""" select w.well_name,d.* from tzl_gather_device d left join tzl_well w on d.well_id=w.well_id and w.del_if=false where d.device_id=? """; return dao.queryForPojo(sql,DeviceVO.class,deviceId); } public boolean update(DevicePO device) { device.setModifyTime(new Date()); device.setDelIf(false); return dao.update(device, "tzl_gather_device", "device_id")>0; } public boolean existDevice(String deviceCode,String deviceId) { if(StringUtils.isBlank(deviceId)) { String sql="select count(1) from tzl_gather_device where del_if=false and device_code=?"; return dao.queryForObject(sql, Integer.class,deviceCode)>0; } else { String sql="select count(1) from tzl_gather_device where del_if=false and device_code=? and device_id!=?"; return dao.queryForObject(sql, Integer.class, deviceCode,deviceId)>0; } } /** * 更新设备的相关设置(标定、采集单位) * @param param * @return */ public boolean updateDeviceParamConfig(DeviceParamPO param) { if(param==null) { return false; } dao.exeUpdate("delete from tzl_gather_device_param where device_code=? and param_code=?", param.getDeviceCode(),param.getParamCode()); UUIDHexGenerator uuid=UUIDHexGenerator.getInstance(); param.setDevParamId(uuid.generate()); param.setModifyTime(new Date()); dao.insert(param,"tzl_gather_device_param"); return true; } /** * 对设备进行井的绑定(同类设备只能绑定一个) * @param deviceCode * @param deviceType * @param wellId * @return */ public boolean updateBindWell(String deviceCode,String deviceType,String wellId) { dao.exeUpdate("update tzl_gather_device set well_id=null where well_id=? and device_type=?", wellId,deviceType); //先解绑原有的绑定 dao.exeUpdate("update tzl_gather_device set well_id=? where device_code=?", wellId,deviceCode); return true; } /** * 查询井的某类设备信息 * @param wellId * @param devType * @return */ public DeviceVO getByWell(String wellId,String devType) { String sql=""" select * from tzl_gather_device where del_if=false and well_id=? and device_type=? limit 1 """; return dao.queryForPojo(sql, DeviceVO.class, wellId,devType); } }