yangys
2024-05-07 9b677ea5c6978788d135fc15da3d78c5a93789c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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<AndonTypeMapper, AndonType> 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<AndonRecord> 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<Long> deviceTypeIds = vo.getDeviceTypeIds();
        if (Func.isNotEmpty(list)) {
          List<DeviceAndonType> typeList = this.deviceAndonTypeService.list(Lambda.eq(DeviceAndonType::getAndonTypeId, typeId));
          List<Long> collect = (List<Long>)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<AndonTypeVO> andonTypeList(String keyword) {
        return this.baseMapper.andonTypeList(keyword);
    }
 
    @Override
    public AndonTypeDetailVO detail(Long typeId) {
        return this.baseMapper.detail(typeId);
    }
 
    @Override
    public List<BindingDeviceTypeVO> allDeviceTypes(String typeId) {
        return this.baseMapper.allDeviceTypes(typeId);
    }
 
    @Override
    public List<AndonTypeVO> workBanding(Long workstationId) {
        return this.baseMapper.workBanding(workstationId);
    }
 
    private boolean sameList(List<Long> l1, List<Long> 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<AndonRecord> 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<AndonRecord> 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<Long> deviceTypeIds) {
        List<DeviceAndonType> 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<AndonType> list = list(Lambda.eq(AndonType::getCode, code));
        
        if (Func.isNotEmpty(list)) {
            throw new ServiceException(MessageUtils.message("andon.type.code.dub", new Object[0]));
        }
    }
}