package com.qianwen.smartman.modules.andon.service.impl; import java.util.List; import java.util.stream.Collectors; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.google.common.collect.Lists; import com.qianwen.core.log.exception.ServiceException; import com.qianwen.core.mp.service.impl.BladeServiceImpl; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.utils.Lambda; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.smartman.modules.andon.convert.AndonTypeConvert; import com.qianwen.smartman.modules.andon.entity.AndonRecord; import com.qianwen.smartman.modules.andon.entity.AndonType; import com.qianwen.smartman.modules.andon.entity.DeviceAndonType; import com.qianwen.smartman.modules.andon.enums.AndonStatusEnum; import com.qianwen.smartman.modules.andon.mapper.AndonTypeMapper; import com.qianwen.smartman.modules.andon.service.IAndonRecordService; import com.qianwen.smartman.modules.andon.service.IAndonTypeService; import com.qianwen.smartman.modules.andon.service.IDeviceAndonTypeService; import com.qianwen.smartman.modules.andon.vo.AndonTypeDetailVO; import com.qianwen.smartman.modules.andon.vo.AndonTypeSaveVO; import com.qianwen.smartman.modules.andon.vo.AndonTypeUpdateVO; import com.qianwen.smartman.modules.andon.vo.AndonTypeVO; import com.qianwen.smartman.modules.andon.vo.BindingDeviceTypeVO; import com.qianwen.smartman.modules.system.service.ICodeGeneratorService; import com.qianwen.smartman.modules.tpm.enums.MetaTypeEnum; @Service public class AndonTypeServiceImpl extends BladeServiceImpl implements IAndonTypeService { private final ICodeGeneratorService codeGeneratorService; private final IDeviceAndonTypeService deviceAndonTypeService; private final IAndonRecordService recordService; public AndonTypeServiceImpl(final ICodeGeneratorService codeGeneratorService, final IDeviceAndonTypeService deviceAndonTypeService, final IAndonRecordService recordService) { this.codeGeneratorService = codeGeneratorService; this.deviceAndonTypeService = deviceAndonTypeService; this.recordService = recordService; } @Override @Transactional(rollbackFor = {Exception.class}) public AndonTypeVO saveAndonType(AndonTypeSaveVO vo) { andonTypeCodeGenerator(vo); checkDuplicateCode(vo.getCode()); AndonType type = new AndonType().setCode(vo.getCode()).setName(vo.getName()).setRemark(vo.getRemark()); save(type); saveDeviceTypeBinding(type.getId(), vo.getDeviceTypeIds()); return AndonTypeConvert.INSTANCE.convert(type); } @Override @Transactional(rollbackFor = {Exception.class}) public Boolean updateAndonType(AndonTypeUpdateVO vo) { Long typeId = vo.getId(); notUpdate(typeId); boolean update = Lambda.updateWrapper(this.baseMapper).set(AndonType::getName, vo.getName()).set(AndonType::getRemark, vo.getRemark()).eq(AndonType::getId, typeId).update(); List list = this.recordService.list(Lambda.eq(AndonRecord::getTypeId, typeId) .in(AndonRecord::getCurStatus, Lists.newArrayList(new Integer[] { AndonStatusEnum.ALREADY_RECEIVED.getStatus(), AndonStatusEnum.HAVA_INITIATED.getStatus() }))); List deviceTypeIds = vo.getDeviceTypeIds(); if (Func.isNotEmpty(list)) { List typeList = this.deviceAndonTypeService.list(Lambda.eq(DeviceAndonType::getAndonTypeId, typeId)); List collect = (List)typeList.stream().map(DeviceAndonType::getDeviceTypeId).collect(Collectors.toList()); boolean sameList = sameList(collect, deviceTypeIds); if (sameList) throw new ServiceException(MessageUtils.message("andon.type.device.type.update", new Object[0])); } update = (this.deviceAndonTypeService.remove(Lambda.eq(DeviceAndonType::getAndonTypeId, typeId)) && update); saveDeviceTypeBinding(typeId, deviceTypeIds); return Boolean.valueOf(update); } @Override public Boolean removeAndonType(Long typeId) { notRemove(typeId); return Boolean.valueOf(removeById(typeId)); } @Override public List andonTypeList(String keyword) { return this.baseMapper.andonTypeList(keyword); } @Override public AndonTypeDetailVO detail(Long typeId) { return this.baseMapper.detail(typeId); } @Override public List allDeviceTypes(String typeId) { return this.baseMapper.allDeviceTypes(typeId); } @Override public List workBanding(Long workstationId) { return this.baseMapper.workBanding(workstationId); } private boolean sameList(List l1, List l2) { if (l1.size() != l2.size()) { return true; } String c1 = l1.stream().sorted((v0, v1) -> { return v0.compareTo(v1); }).map((v0) -> { return String.valueOf(v0); }).collect(Collectors.joining(",")); String c2 = l2.stream().sorted((v0, v1) -> { return v0.compareTo(v1); }).map((v0) -> { return String.valueOf(v0); }).collect(Collectors.joining(",")); return !c1.equals(c2); } private void notRemove(Long typeId) { List list = this.recordService.list(Lambda.eq(AndonRecord::getTypeId, typeId)); if (Func.isNotEmpty(list)) { throw new ServiceException(MessageUtils.message("andon.type.has.record", new Object[0])); } } private void notUpdate(Long typeId) { List list = this.recordService.list(Lambda.eq(AndonRecord::getTypeId, typeId).in(AndonRecord::getCurStatus, Lists.newArrayList(AndonStatusEnum.ALREADY_RECEIVED.getStatus(), AndonStatusEnum.HAVA_INITIATED.getStatus()))); if (Func.isNotEmpty(list)) throw new ServiceException(MessageUtils.message("andon.type.not.update", new Object[0])); } private void saveDeviceTypeBinding(Long andonTypeId, List deviceTypeIds) { List types = deviceTypeIds.stream().map(dt -> { return new DeviceAndonType().setAndonTypeId(andonTypeId).setDeviceTypeId(dt); }).collect(Collectors.toList()); this.deviceAndonTypeService.saveBatch(types); } private void andonTypeCodeGenerator(AndonTypeSaveVO vo) { if (Func.isBlank(vo.getCode())) { String code = this.codeGeneratorService.getGeneratorCode(vo, MetaTypeEnum.ANDON_TYPE.getCode()); if (Func.isBlank(code)) { throw new ServiceException(MessageUtils.message("cps.tpm.maintain.coding.rules", new Object[0])); } vo.setCode(code); } } private void checkDuplicateCode(String code) { List list = list(Lambda.eq(AndonType::getCode, code)); if (Func.isNotEmpty(list)) { throw new ServiceException(MessageUtils.message("andon.type.code.dub", new Object[0])); } } }