zhanghl
2025-05-23 54ffd5235b2af6c7f5e1cbda8b083fc3da4cd993
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
package com.aps.core.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.aps.common.core.utils.DateUtils;
import com.aps.common.core.utils.poi.ExcelUtil;
import com.aps.common.security.utils.DictUtils;
import com.aps.common.security.utils.SecurityUtils;
import com.aps.core.domain.ApsMaterialProductGroupManagement;
import com.aps.core.mapper.ApsMaterialProductGroupManagementMapper;
import com.aps.core.service.IApsMaterialProductGroupManagementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 物料产品组数据管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class ApsMaterialProductGroupManagementServiceImpl implements IApsMaterialProductGroupManagementService {
    @Autowired
    private ApsMaterialProductGroupManagementMapper apsMaterialProductGroupManagementMapper;
 
    /**
     * 查询物料产品组数据管理
     *
     * @param id 物料产品组数据管理主键
     * @return 物料产品组数据管理
     */
    @Override
    public ApsMaterialProductGroupManagement selectApsMaterialProductGroupManagementById(Long id) {
        return apsMaterialProductGroupManagementMapper.selectApsMaterialProductGroupManagementById(id);
    }
 
    /**
     * 查询物料产品组数据管理列表
     *
     * @param apsMaterialProductGroupManagement 物料产品组数据管理
     * @return 物料产品组数据管理
     */
    @Override
    public List<ApsMaterialProductGroupManagement> selectApsMaterialProductGroupManagementList(ApsMaterialProductGroupManagement apsMaterialProductGroupManagement) {
        return apsMaterialProductGroupManagementMapper.selectApsMaterialProductGroupManagementList(apsMaterialProductGroupManagement);
    }
 
    /**
     * 新增物料产品组数据管理
     *
     * @param apsMaterialProductGroupManagement 物料产品组数据管理
     * @return 结果
     */
    @Override
    public int insertApsMaterialProductGroupManagement(ApsMaterialProductGroupManagement apsMaterialProductGroupManagement) {
        apsMaterialProductGroupManagement.setId(IdUtil.getSnowflakeNextId());
        apsMaterialProductGroupManagement.setCreateBy(SecurityUtils.getUsername());
        apsMaterialProductGroupManagement.setCreateTime(DateUtils.getNowDate());
        return apsMaterialProductGroupManagementMapper.insertApsMaterialProductGroupManagement(apsMaterialProductGroupManagement);
    }
 
    /**
     * 修改物料产品组数据管理
     *
     * @param apsMaterialProductGroupManagement 物料产品组数据管理
     * @return 结果
     */
    @Override
    public int updateApsMaterialProductGroupManagement(ApsMaterialProductGroupManagement apsMaterialProductGroupManagement) {
        apsMaterialProductGroupManagement.setUpdateTime(DateUtils.getNowDate());
        apsMaterialProductGroupManagement.setUpdateBy(SecurityUtils.getUsername());
        return apsMaterialProductGroupManagementMapper.updateApsMaterialProductGroupManagement(apsMaterialProductGroupManagement);
    }
 
    /**
     * 批量删除物料产品组数据管理
     *
     * @param ids 需要删除的物料产品组数据管理主键
     * @return 结果
     */
    @Override
    public int deleteApsMaterialProductGroupManagementByIds(Long[] ids) {
        return apsMaterialProductGroupManagementMapper.deleteApsMaterialProductGroupManagementByIds(ids);
    }
 
    /**
     * 删除物料产品组数据管理信息
     *
     * @param id 物料产品组数据管理主键
     * @return 结果
     */
    @Override
    public int deleteApsMaterialProductGroupManagementById(Long id) {
        return apsMaterialProductGroupManagementMapper.deleteApsMaterialProductGroupManagementById(id);
    }
 
    @SneakyThrows
    @Override
    public int batchInsertApsMaterialProductGroupManagement(MultipartFile file) {
        ExcelUtil<ApsMaterialProductGroupManagement> util = new ExcelUtil<>(ApsMaterialProductGroupManagement.class);
        List<ApsMaterialProductGroupManagement> list = util.importExcel(file.getInputStream());
        DictUtils.CacheValue cacheValue = DictUtils.getCacheValue("aps_factory");
        DictUtils.CacheValue cacheDomain = DictUtils.getCacheValue("aps_domain");
        Set<String> keys = new HashSet<>();
        list.forEach(item -> {
            item.setId(IdUtil.getSnowflakeNextId());
            item.setDomain(cacheDomain.get(item.getDomain()));
            item.setFactory(cacheValue.get(item.getFactory()));
            item.setCreateBy(SecurityUtils.getUsername());
            item.setCreateTime(DateUtils.getNowDate());
        });
        Collections.reverse(list);
        list = list.stream()
                .filter(item -> {
                    item.setKey(null);
                    if (!keys.contains(item.getKey())) {
                        keys.add(item.getKey());
                        return true;
                    }
                    return false;
                })
                .collect(Collectors.toList());
 
        if (!list.isEmpty()) {
            Set<String> facCodeKey = apsMaterialProductGroupManagementMapper.selectByFacOrMaterial(keys);
            if (!facCodeKey.isEmpty()) {
                Iterator<ApsMaterialProductGroupManagement> it = list.iterator();
                while (it.hasNext()) {
                    ApsMaterialProductGroupManagement item = it.next();
                    if (facCodeKey.contains(item.getKey())) {
                        LambdaUpdateWrapper<ApsMaterialProductGroupManagement> wrapper = new LambdaUpdateWrapper<>();
                        wrapper.eq(ApsMaterialProductGroupManagement::getFactory, item.getFactory());
                        wrapper.eq(ApsMaterialProductGroupManagement::getMaterialCode, item.getMaterialCode());
                        item.setCreateBy(null);
                        item.setCreateTime(null);
                        item.setId(null);
                        item.setUpdateBy(SecurityUtils.getUsername());
                        item.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                        apsMaterialProductGroupManagementMapper.update(item, wrapper);
                        it.remove();
                    }
                }
            }
 
            if (!list.isEmpty()) {
                apsMaterialProductGroupManagementMapper.insert(list);
            }
        }
        return list.size();
 
    }
}