| package com.doumee.service.business.impl; | 
|   | 
| import com.doumee.core.constants.ResponseStatus; | 
| import com.doumee.core.exception.BusinessException; | 
| import com.doumee.core.utils.Constants; | 
| import com.doumee.dao.business.CarsMapper; | 
| import com.doumee.dao.business.model.Cars; | 
| import com.doumee.dao.business.model.JkCabinet; | 
| import com.doumee.dao.business.model.JkCabinetGrid; | 
| import com.doumee.service.business.third.model.LoginUserInfo; | 
| import com.doumee.service.business.third.model.PageData; | 
| import com.doumee.service.business.third.model.PageWrap; | 
| import com.doumee.core.utils.Utils; | 
| import com.doumee.dao.business.JkKeysMapper; | 
| import com.doumee.dao.business.model.JkKeys; | 
| import com.doumee.service.business.JkKeysService; | 
| 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 com.github.yulichang.interfaces.MPJBaseJoin; | 
| import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.util.CollectionUtils; | 
|   | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * 钥匙基本信息表Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2025/09/28 09:01 | 
|  */ | 
| @Service | 
| public class JkKeysServiceImpl implements JkKeysService { | 
|   | 
|     @Autowired | 
|     private JkKeysMapper jkKeysMapper; | 
|   | 
|     @Autowired | 
|     private CarsMapper carsMapper; | 
|   | 
|     @Override | 
|     public Integer create(JkKeys jkKeys) { | 
|         if(Objects.isNull(jkKeys) | 
|         || Objects.isNull(jkKeys.getCarId()) | 
|         || Objects.isNull(jkKeys.getRoleType()) | 
|         || StringUtils.isBlank(jkKeys.getCode()) | 
|         || StringUtils.isBlank(jkKeys.getRfidLable()) | 
|         ){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
|         } | 
|         //查询编号是否重复 | 
|         if(jkKeysMapper.selectCount(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getCode,jkKeys.getCode()).eq(JkKeys::getIsdeleted,Constants.ZERO))>Constants.ZERO){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙编号已存在!"); | 
|         } | 
|         Cars cars = carsMapper.selectById(jkKeys.getCarId()); | 
|         if(Objects.isNull(cars)||Constants.equalsInteger(cars.getIsdeleted(),Constants.ONE)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"车辆信息未查询到!"); | 
|         } | 
|         JkKeys carsKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getCarId,jkKeys.getCarId()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1")); | 
|         if(Objects.nonNull(carsKeys)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前车辆已被钥匙["+carsKeys.getCode()+"]绑定使用!"); | 
|         } | 
|         JkKeys rfidKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getRfidLable,jkKeys.getRfidLable()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1")); | 
|         if(Objects.nonNull(rfidKeys)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前RFID标签已被钥匙["+rfidKeys.getCode()+"]使用!"); | 
|         } | 
|         LoginUserInfo loginUserInfo = jkKeys.getLoginUserInfo(); | 
|         jkKeys.setCreateDate(new Date()); | 
|         jkKeys.setEditDate(jkKeys.getCreateDate()); | 
|         jkKeys.setCreator(loginUserInfo.getId()); | 
|         jkKeys.setEditor(loginUserInfo.getId()); | 
|         jkKeys.setIsdeleted(Constants.ZERO); | 
|         jkKeys.setStatus(Constants.ZERO); | 
|         jkKeys.setCarCode(cars.getCode()); | 
|         jkKeysMapper.insert(jkKeys); | 
|         return jkKeys.getId(); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteById(Integer id) { | 
|         JkKeys jkKeys = jkKeysMapper.selectById(id); | 
|         if(Objects.isNull(jkKeys)||Constants.equalsInteger(jkKeys.getIsdeleted(),Constants.ONE)){ | 
|             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
|         } | 
|         if(!Constants.equalsInteger(jkKeys.getStatus(),Constants.ZERO)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙已绑定,无法进行删除"); | 
|         } | 
|         jkKeysMapper.deleteById(id); | 
|     } | 
|   | 
|     @Override | 
|     public void delete(JkKeys jkKeys) { | 
|         UpdateWrapper<JkKeys> deleteWrapper = new UpdateWrapper<>(jkKeys); | 
|         jkKeysMapper.delete(deleteWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteByIdInBatch(List<Integer> ids) { | 
|         if (CollectionUtils.isEmpty(ids)) { | 
|             return; | 
|         } | 
|         jkKeysMapper.deleteBatchIds(ids); | 
|     } | 
|   | 
|     @Override | 
|     public void updateById(JkKeys jkKeys) { | 
|         if(Objects.isNull(jkKeys) | 
|                 || Objects.isNull(jkKeys.getId()) | 
|                 || Objects.isNull(jkKeys.getCarId()) | 
|                 || Objects.isNull(jkKeys.getRoleType()) | 
|                 || StringUtils.isBlank(jkKeys.getCode()) | 
|                 || StringUtils.isBlank(jkKeys.getRfidLable()) | 
|         ){ | 
|             throw new BusinessException(ResponseStatus.BAD_REQUEST); | 
|         } | 
|         //查询编号是否重复 | 
|         if(jkKeysMapper.selectCount(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getCode,jkKeys.getCode()).eq(JkKeys::getIsdeleted,Constants.ZERO))>Constants.ZERO){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙编号已存在!"); | 
|         } | 
|         Cars cars = carsMapper.selectById(jkKeys.getCarId()); | 
|         if(Objects.isNull(cars)||Constants.equalsInteger(cars.getIsdeleted(),Constants.ONE)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"车辆信息未查询到!"); | 
|         } | 
|         JkKeys carsKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getCarId,jkKeys.getCarId()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1")); | 
|         if(Objects.nonNull(carsKeys)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前车辆已被钥匙["+carsKeys.getCode()+"]绑定使用!"); | 
|         } | 
|         JkKeys rfidKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getRfidLable,jkKeys.getRfidLable()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1")); | 
|         if(Objects.nonNull(rfidKeys)){ | 
|             throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前RFID标签已被钥匙["+rfidKeys.getCode()+"]使用!"); | 
|         } | 
|         LoginUserInfo loginUserInfo = jkKeys.getLoginUserInfo(); | 
|         jkKeys.setEditDate(new Date()); | 
|         jkKeys.setEditor(loginUserInfo.getId()); | 
|         jkKeys.setCarCode(cars.getCode()); | 
|         jkKeysMapper.updateById(jkKeys); | 
|     } | 
|   | 
|     @Override | 
|     public void updateByIdInBatch(List<JkKeys> jkKeyss) { | 
|         if (CollectionUtils.isEmpty(jkKeyss)) { | 
|             return; | 
|         } | 
|         for (JkKeys jkKeys: jkKeyss) { | 
|             this.updateById(jkKeys); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public JkKeys findById(Integer id) { | 
|         return jkKeysMapper.selectById(id); | 
|     } | 
|   | 
|     @Override | 
|     public JkKeys findOne(JkKeys jkKeys) { | 
|         QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys); | 
|         return jkKeysMapper.selectOne(wrapper); | 
|     } | 
|   | 
|     @Override | 
|     public List<JkKeys> findList(JkKeys jkKeys) { | 
|   | 
|   | 
|   | 
|   | 
|         jkKeys.setIsdeleted(Constants.ZERO); | 
|         QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys); | 
|         return jkKeysMapper.selectList(new QueryWrapper<JkKeys>() | 
|                 .lambda().eq(JkKeys::getIsdeleted,Constants.ZERO) | 
|   | 
|         ); | 
|     } | 
|    | 
|     @Override | 
|     public PageData<JkKeys> findPage(PageWrap<JkKeys> pageWrap) { | 
|         IPage<JkKeys> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         QueryWrapper<JkKeys> queryWrapper = new QueryWrapper<>(); | 
|         Utils.MP.blankToNull(pageWrap.getModel()); | 
|         JkKeys model = pageWrap.getModel(); | 
|         MPJLambdaWrapper<JkKeys> wrapper = new MPJLambdaWrapper<JkKeys>() | 
|                 .selectAll(JkKeys.class) | 
|                 .select("j.code",JkKeys::getGridCode) | 
|                 .select("j1.name",JkKeys::getCabinetName) | 
|                 .leftJoin(Cars.class,Cars::getId,JkKeys::getCarId) | 
|                 .leftJoin(" jk_cabinet_grid j on j.key_id = t.id and j.isdeleted = 0  ") | 
|                 .leftJoin(" jk_cabinet j1 on j.CABINET_ID = j1.id ") | 
|                 .eq(JkKeys::getIsdeleted,Constants.ZERO) | 
| //                .eq(JkCabinetGrid::getIsdeleted,Constants.ZERO) | 
|                 .like(StringUtils.isNotBlank(model.getCode()),JkKeys::getCode,model.getCode()) | 
|                 .like(StringUtils.isNotBlank(model.getCarCode()),JkKeys::getCarCode,model.getCode()); | 
|         IPage<JkKeys> iPage = jkKeysMapper.selectJoinPage(page,JkKeys.class,wrapper); | 
|         for (JkKeys jkKeys:iPage.getRecords()) { | 
|             jkKeys.setIsBinding(StringUtils.isBlank(jkKeys.getGridCode())?Constants.ZERO:Constants.ONE); | 
|         } | 
|         return PageData.from(iPage); | 
|     } | 
|   | 
|   | 
|   | 
|     @Override | 
|     public long count(JkKeys jkKeys) { | 
|         QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys); | 
|         return jkKeysMapper.selectCount(wrapper); | 
|     } | 
| } |