package cn.besteam.modules.medical.service.impl; import cn.besteam.common.constant.CacheKey; import cn.besteam.common.constant.CommonConstant; import cn.besteam.common.constant.ErrorCode; import cn.besteam.common.exception.BusinessException; import cn.besteam.common.util.RedisUtil; import cn.besteam.modules.medical.entity.MwDeviceAgentDevice; import cn.besteam.modules.medical.mapper.MwDeviceAgentDeviceMapper; import cn.besteam.modules.medical.service.IMwDeviceAgentDeviceService; import cn.besteam.modules.medical.vo.MwDeviceAgentDeviceAddBatchVO; import cn.besteam.modules.medical.vo.MwDeviceAgentDeviceExcelVO; import cn.besteam.modules.medical.vo.MwDeviceAgentDeviceVO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; /** * @Description: 设备代理商-设备 * @author: besteam * @date: 2020-04-25 * @version: V1.0 */ @Service public class MwDeviceAgentDeviceServiceImpl extends ServiceImpl implements IMwDeviceAgentDeviceService { @Autowired private RedisUtil redisUtil; @Override public IPage getDeviceList(String name, Integer pageNo, Integer pageSize) { Page page = new Page<>(pageNo, pageSize); List deviceList = baseMapper.getDeviceList(page, name); page.setRecords(deviceList); return page; } @Override public boolean move(Integer id, Integer deviceAgentId) { MwDeviceAgentDevice mwDeviceAgentDevice = getById(id); if (mwDeviceAgentDevice == null) { throw new BusinessException(ErrorCode.MW_DEVICE_NOT_FOUND); } mwDeviceAgentDevice.setDeviceAgentId(deviceAgentId); return updateById(mwDeviceAgentDevice); } @Transactional(rollbackFor = Exception.class) @Override public boolean moveBatch(String ids, Integer deviceAgentId) { String[] idArray = ids.split(","); for (String id : idArray) { MwDeviceAgentDevice mwDeviceAgentDevice = getById(Integer.parseInt(id)); if (mwDeviceAgentDevice == null) { continue; } mwDeviceAgentDevice.setDeviceAgentId(deviceAgentId); updateById(mwDeviceAgentDevice); } return true; } @Override public boolean updateStatus(Integer id, Integer status) { if (status == null || status < 1 || status > 2) { throw new BusinessException(ErrorCode.MW_DEVICE_STATUS_INVALID); } MwDeviceAgentDevice mwDeviceAgentDevice = getById(id); if (mwDeviceAgentDevice == null) { throw new BusinessException(ErrorCode.MW_DEVICE_NOT_FOUND); } mwDeviceAgentDevice.setStatus(status); return updateById(mwDeviceAgentDevice); } @Transactional(rollbackFor = Exception.class) @Override public boolean updateStatusBatch(String ids, Integer status) { if (status == null || status < 1 || status > 2) { throw new BusinessException(ErrorCode.MW_DEVICE_STATUS_INVALID); } String[] idArray = ids.split(","); for (String id : idArray) { MwDeviceAgentDevice mwDeviceAgentDevice = getById(Integer.parseInt(id)); if (mwDeviceAgentDevice == null) { continue; } mwDeviceAgentDevice.setStatus(status); updateById(mwDeviceAgentDevice); } return true; } @Transactional(rollbackFor = Exception.class) @Override public MwDeviceAgentDeviceAddBatchVO addBatch(Integer deviceAgentId, String sns) { MwDeviceAgentDeviceAddBatchVO vo = new MwDeviceAgentDeviceAddBatchVO(); List saveList = new ArrayList<>(); List replicateList = new ArrayList<>(); String[] snArray = sns.split("\\n"); for (String sn : snArray) { MwDeviceAgentDevice device = getOne(new QueryWrapper().lambda().eq(MwDeviceAgentDevice::getSn, sn)); if (device != null) { replicateList.add(sn); continue; } if (saveList.contains(sn)) { replicateList.add(sn); continue; } device = new MwDeviceAgentDevice(); device.setStatus(CommonConstant.STATUS_NORMAL); device.setDeviceAgentId(deviceAgentId); device.setSn(sn); save(device); // saveList.add(sn); } vo.setSaveList(saveList); vo.setReplicateList(replicateList); return vo; } @Override public List getDeviceExcelList(Integer deviceAgentId) { return baseMapper.getDeviceExcelList(deviceAgentId); } @Override public Integer getDeviceValidateStatus() { Integer status = 1; if (!redisUtil.hasKey(CacheKey.getGlobalDeviceValidateStatusKey())) { redisUtil.set(CacheKey.getGlobalDeviceValidateStatusKey(), status, -1); } else { status = redisUtil.get(CacheKey.getGlobalDeviceValidateStatusKey()); } return status; } @Override public boolean setDeviceValidateStatus(Integer status) { return redisUtil.set(CacheKey.getGlobalDeviceValidateStatusKey(), status, -1); } }