| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | | import org.springblade.core.mp.support.Condition; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.basesetting.machine.entity.MachineSpec; |
| | | import org.springblade.mdm.basesetting.machine.mapper.MachineMapper; |
| | | import org.springblade.mdm.basesetting.machine.vo.MachineQueryVO; |
| | | import org.springblade.mdm.basesetting.machine.vo.MachineSaveVO; |
| | | import org.springblade.mdm.basesetting.machine.vo.MachineVO; |
| | | import org.springblade.system.feign.ISysClient; |
| | | import org.springblade.system.pojo.entity.Dept; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.InvalidPathException; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | |
| | | @Service |
| | | public class MachineService extends BizServiceImpl<MachineMapper, Machine> { |
| | | |
| | | @Autowired |
| | | private ISysClient sysClient; |
| | | @Transactional |
| | | public void saveMachine(MachineSaveVO vo){ |
| | | //TODO |
| | | checkMachine(vo); |
| | | if(existsByCode(vo.getCode(),null)){ |
| | | throw new ServiceException("机床编码已存在:"+vo.getCode()); |
| | | } |
| | | |
| | | Machine machine = new Machine(); |
| | | BeanUtil.copyProperties(vo, machine); |
| | | machine.setId(null); |
| | | this.save(machine); |
| | | } |
| | | |
| | | /** |
| | | * 检查机床录入数据 |
| | | * @param vo 录入的表单数据VO |
| | | */ |
| | | void checkMachine(MachineSaveVO vo){ |
| | | if(Func.isEmpty(vo.getCode())){ |
| | | throw new ServiceException("机床编码不能为空"); |
| | | } |
| | | |
| | | if(Func.isEmpty(vo.getName())){ |
| | | throw new ServiceException("机床类型不能为空"); |
| | | } |
| | | |
| | | if(Func.isEmpty(vo.getMachineGroupCode())){ |
| | | throw new ServiceException("所属组织不能为空"); |
| | | } |
| | | |
| | | if(Func.isEmpty(vo.getMachineSpec())){ |
| | | throw new ServiceException("机床类型不能为空"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据机床编码判断机床是否存在 |
| | | * @param code 机床编码 |
| | | * @param excludeId 排除id |
| | | * @return 是否存在 |
| | | */ |
| | | boolean existsByCode(String code,Long excludeId){ |
| | | return this.lambdaQuery().eq(Machine::getCode, code).ne(excludeId!=null,Machine::getId, excludeId).count()>0; |
| | | } |
| | | |
| | | public boolean updateMachine(MachineSaveVO vo) { |
| | | |
| | | checkMachine(vo); |
| | | if(existsByCode(vo.getCode(),vo.getId())){ |
| | | throw new ServiceException("机床编码已存在:"+vo.getCode()); |
| | | } |
| | | |
| | | Machine machine = this.getById(vo.getId()); |
| | | |
| | | machine.setMachineSpec(vo.getMachineSpec()); |
| | | machine.setCode(vo.getCode()); |
| | | machine.setName(vo.getName()); |
| | | machine.setMachineGroupId(vo.getMachineGroupId()); |
| | | machine.setMachineGroupCode(vo.getMachineGroupCode()); |
| | | machine.setManufacturer(vo.getManufacturer()); |
| | | machine.setOperator(vo.getOperator()); |
| | | machine.setControlSystem(vo.getControlSystem()); |
| | | machine.setOwnerDept(vo.getOwnerDept()); |
| | | machine.setPollingHours(vo.getPollingHours()); |
| | | machine.setProgSendDir(vo.getProgSendDir()); |
| | | machine.setProgReceiveDir(vo.getProgReceiveDir()); |
| | | machine.setProgTempDir(vo.getProgTempDir()); |
| | | machine.setStatus(vo.getStatus()); |
| | | machine.setRemark(vo.getRemark()); |
| | | |
| | | return this.updateById(machine); |
| | | } |
| | | |
| | |
| | | */ |
| | | public IPage<MachineVO> pageQuery(MachineQueryVO query) { |
| | | |
| | | LambdaQueryWrapper<Machine> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(query.getMachineGroupId()!=null, Machine::getMachineGroupId, query.getMachineGroupId()); |
| | | IPage<MachineVO> page = this.getBaseMapper().pageQuery(Condition.getPage(query),queryWrapper); |
| | | |
| | | //MachineSpec.valueOf() |
| | | for (MachineVO record : page.getRecords()) { |
| | | if(record.getMachineSpec() != null){ |
| | | MachineSpec spec = MachineSpec.valueOf(record.getMachineSpec()); |
| | | record.setMachineSpecName(spec.getText()); |
| | | if (query.getDeptId() != null) { |
| | | R<Dept> rs = sysClient.getDept(query.getDeptId()); |
| | | if(rs.isSuccess()){ |
| | | query.setDeptAncestors(rs.getData().getAncestors()+","+rs.getData().getId()); |
| | | } |
| | | |
| | | } |
| | | IPage<MachineVO> page = this.getBaseMapper().pageQuery(Condition.getPage(query), query); |
| | | |
| | | return page; |
| | | } |
| | | |
| | | /** |
| | | * 查询详情 |
| | | * @param id 机床id |
| | | * @return |
| | | */ |
| | | public MachineVO detail(long id) { |
| | | Machine machine = this.getById(id); |
| | | |
| | | MachineVO vo = new MachineVO(); |
| | | BeanUtil.copyProperties(machine, vo); |
| | | |
| | | return vo; |
| | | } |
| | | |
| | | /** |
| | | * 产生机床回传结构数据 |
| | | * @param id |
| | | */ |
| | | public void genMachineFileBackDirs(Long id) throws IOException { |
| | | Machine machine = this.getById(id); |
| | | if(Func.isNotBlank(machine.getProgReceiveDir())){ |
| | | if(isLegalDirectoryPath(machine.getProgReceiveDir())) { |
| | | Path path = Paths.get(machine.getProgReceiveDir()); |
| | | Files.createDirectories(path); |
| | | }else{ |
| | | throw new RuntimeException("非法的目录格式"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断是否是合法的目录格式 |
| | | * @param path |
| | | * @return |
| | | */ |
| | | static boolean isLegalDirectoryPath(String path) { |
| | | // 基础检查 |
| | | if (path == null || path.trim().isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | // 使用NIO检查路径格式 |
| | | Path nioPath; |
| | | try { |
| | | nioPath = Paths.get(path); |
| | | if (!nioPath.normalize().toString().equals(path)) { |
| | | return false; // 路径不规范 |
| | | } |
| | | } catch (InvalidPathException ex) { |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据机床编码获取机床 |
| | | * @param machineCode 机床编码 |
| | | * @return |
| | | */ |
| | | public Machine getByCode(String machineCode) { |
| | | return this.lambdaQuery().eq(Machine::getCode, machineCode).one(); |
| | | } |
| | | |
| | | @Transactional |
| | | public String importMachines(MultipartFile file) { |
| | | return "";//导入成功,给空串,部分不成功给提示 |
| | | } |
| | | } |