| package doumeemes.service.business.impl; | 
|   | 
| import doumeemes.core.exception.BusinessException; | 
| import doumeemes.core.model.ApiResponse; | 
| import doumeemes.core.model.LoginUserInfo; | 
| import doumeemes.core.model.PageData; | 
| import doumeemes.core.model.PageWrap; | 
| import doumeemes.core.utils.Constants; | 
| import doumeemes.core.utils.Utils; | 
| import doumeemes.dao.business.UserDeviceMapper; | 
| import doumeemes.dao.business.model.CompanyUser; | 
| import doumeemes.dao.business.model.UserDevice; | 
| import doumeemes.service.business.UserDeviceService; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.shiro.SecurityUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.util.CollectionUtils; | 
|   | 
| import java.util.Date; | 
| import java.util.List; | 
|   | 
| /** | 
|  * 设置类-人员绑定设备关联表Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2022/05/10 09:30 | 
|  */ | 
| @Service | 
| public class UserDeviceServiceImpl implements UserDeviceService { | 
|   | 
|     @Autowired | 
|     private UserDeviceMapper userDeviceMapper; | 
|     @Autowired | 
|     private CompanyUserServiceImpl companyUserService; | 
|   | 
|   | 
|     @Override | 
|     @Transactional(rollbackFor = {BusinessException.class,Exception.class}) | 
|     public ApiResponse create(UserDevice userDevice) { | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|   | 
|         if(StringUtils.isNotEmpty(userDevice.getUserIds())){ | 
|             String [] ids=userDevice.getUserIds().split(","); | 
|             try { | 
|                 for (String id : ids) { | 
|                     CompanyUser companyUser = companyUserService.findById(Integer.valueOf(id)); | 
|                     //查询人员是否绑定设备 | 
|                     UserDevice queryparam = new UserDevice(); | 
|                     queryparam.setDeleted(Constants.ZERO); | 
|                     queryparam.setDeviceId(userDevice.getDeviceId()); | 
|                     queryparam.setCompanyUserId(companyUser.getId()); | 
|                     queryparam.setUserId(companyUser.getUserId()); | 
|                     List<UserDevice> findList = this.findList(queryparam); | 
|                     if (findList.size() > 0) { | 
|                        throw new RuntimeException(); | 
|                     } | 
|                     userDevice.setCompanyUserId(companyUser.getId()); | 
|                     userDevice.setUserId(companyUser.getUserId()); | 
|                     userDevice.setCreateTime(new Date()); | 
|                     userDevice.setCreateUser(user.getId()); | 
|                     userDevice.setDeleted(Constants.ZERO); | 
|                     userDevice.setDeviceDate(new Date()); | 
|                     userDeviceMapper.insert(userDevice); | 
|                 } | 
|             }catch (Exception e){ | 
|                 return ApiResponse.failed("人员设备已关联!"); | 
|             } | 
|   | 
|         } | 
|         return ApiResponse.success(null); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteById(Integer id) { | 
|         UserDevice find=this.findById(id); | 
|         find.setDeleted(Constants.ONE); | 
|         userDeviceMapper.updateById(find); | 
|     } | 
|   | 
|     @Override | 
|     public void delete(UserDevice userDevice) { | 
|         UpdateWrapper<UserDevice> deleteWrapper = new UpdateWrapper<>(userDevice); | 
|         userDeviceMapper.delete(deleteWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteByIdInBatch(List<Integer> ids) { | 
|         if (CollectionUtils.isEmpty(ids)) { | 
|             return; | 
|         } | 
|         for(Integer id:ids){ | 
|             UserDevice find=this.findById(id); | 
|             find.setDeleted(Constants.ONE); | 
|             userDeviceMapper.updateById(find); | 
|         } | 
|        // userDeviceMapper.deleteBatchIds(ids); | 
|     } | 
|   | 
|     @Override | 
|     public ApiResponse updateById(UserDevice userDevice) { | 
|         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         CompanyUser companyUser = companyUserService.findById(userDevice.getCompanyUserId()); | 
|         //查询人员是否绑定设备 | 
|         UserDevice queryparam=new UserDevice(); | 
|         queryparam.setDeleted(Constants.ZERO); | 
|         queryparam.setDeviceId(userDevice.getDeviceId()); | 
|         queryparam.setCompanyUserId(userDevice.getCompanyUserId()); | 
|         queryparam.setUserId(companyUser.getUserId()); | 
|         List<UserDevice> findList=this.findList(queryparam); | 
|         if(findList.size()>0){ | 
|             if(findList.get(0).getId()!=userDevice.getId()){ | 
|                 return ApiResponse.failed("人员设备已关联!"); | 
|             } | 
|         } | 
|   | 
|         UserDevice find=this.findById(userDevice.getId()); | 
|         find.setUpdateUser(user.getId()); | 
|         find.setUpdateTime(new Date()); | 
|         find.setDeviceId(userDevice.getDeviceId()); | 
|         find.setDeviceDate(new Date()); | 
|         find.setCompanyUserId(userDevice.getCompanyUserId()); | 
|         find.setUserId(userDevice.getUserId()); | 
|         return ApiResponse.success(userDeviceMapper.updateById(find)); | 
|     } | 
|   | 
|     @Override | 
|     public void updateByIdInBatch(List<UserDevice> userDevices) { | 
|         if (CollectionUtils.isEmpty(userDevices)) { | 
|             return; | 
|         } | 
|         for (UserDevice userDevice: userDevices) { | 
|             this.updateById(userDevice); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public UserDevice findById(Integer id) { | 
|         return userDeviceMapper.selectById(id); | 
|     } | 
|   | 
|     @Override | 
|     public UserDevice findOne(UserDevice userDevice) { | 
|         QueryWrapper<UserDevice> wrapper = new QueryWrapper<>(userDevice); | 
|         return userDeviceMapper.selectOne(wrapper); | 
|     } | 
|   | 
|     @Override | 
|     public List<UserDevice> findList(UserDevice userDevice) { | 
|         QueryWrapper<UserDevice> wrapper = new QueryWrapper<>(userDevice); | 
|         return userDeviceMapper.selectList(wrapper); | 
|     } | 
|    | 
|     @Override | 
|     public PageData<UserDevice> findPage(PageWrap<UserDevice> pageWrap) { | 
|         IPage<UserDevice> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         QueryWrapper<UserDevice> queryWrapper = new QueryWrapper<>(); | 
|         Utils.MP.blankToNull(pageWrap.getModel()); | 
|         queryWrapper.lambda() | 
|                 .eq(pageWrap.getModel().getId() != null, UserDevice::getId, pageWrap.getModel().getId()) | 
|                 .eq(pageWrap.getModel().getDeleted() != null, UserDevice::getDeleted, pageWrap.getModel().getDeleted()) | 
|                 .eq(pageWrap.getModel().getCreateUser() != null, UserDevice::getCreateUser, pageWrap.getModel().getCreateUser()) | 
|                 .ge(pageWrap.getModel().getCreateTime() != null, UserDevice::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime())) | 
|                 .le(pageWrap.getModel().getCreateTime() != null, UserDevice::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime())) | 
|                 .eq(pageWrap.getModel().getUpdateUser() != null, UserDevice::getUpdateUser, pageWrap.getModel().getUpdateUser()) | 
|                 .ge(pageWrap.getModel().getUpdateTime() != null, UserDevice::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime())) | 
|                 .le(pageWrap.getModel().getUpdateTime() != null, UserDevice::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime())) | 
|                 .eq(pageWrap.getModel().getRemark() != null, UserDevice::getRemark, pageWrap.getModel().getRemark()) | 
|                 .eq(pageWrap.getModel().getCompanyUserId() != null, UserDevice::getCompanyUserId, pageWrap.getModel().getCompanyUserId()) | 
|                 .eq(pageWrap.getModel().getDeviceId() != null, UserDevice::getDeviceId, pageWrap.getModel().getDeviceId()) | 
|                 .ge(pageWrap.getModel().getDeviceDate() != null, UserDevice::getDeviceDate, Utils.Date.getStart(pageWrap.getModel().getDeviceDate())) | 
|                 .le(pageWrap.getModel().getDeviceDate() != null, UserDevice::getDeviceDate, Utils.Date.getEnd(pageWrap.getModel().getDeviceDate())) | 
|                 .eq(pageWrap.getModel().getUserId() != null, UserDevice::getUserId, pageWrap.getModel().getUserId()) | 
|         ; | 
|         for(PageWrap.SortData sortData: pageWrap.getSorts()) { | 
|             if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { | 
|                 queryWrapper.orderByDesc(sortData.getProperty()); | 
|             } else { | 
|                 queryWrapper.orderByAsc(sortData.getProperty()); | 
|             } | 
|         } | 
|         return PageData.from(userDeviceMapper.selectPage(page, queryWrapper)); | 
|     } | 
|   | 
|     @Override | 
|     public long count(UserDevice userDevice) { | 
|         QueryWrapper<UserDevice> wrapper = new QueryWrapper<>(userDevice); | 
|         return userDeviceMapper.selectCount(wrapper); | 
|     } | 
| } |