郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.gk.firework.Service.ServiceImpl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.DO.ProductDO;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.ProductCodeInfo;
import com.gk.firework.Domain.ProductInfo;
import com.gk.firework.Domain.StockInfo;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.BeanUtils;
import com.gk.firework.Domain.Utils.PageInfo;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.DirectionDetail;
import com.gk.firework.Domain.Vo.FireworkDeal;
import com.gk.firework.Domain.Vo.Product2JsonVo;
import com.gk.firework.Domain.Vo.ProductVo;
import com.gk.firework.Mapper.ProductInfoMapper;
import com.gk.firework.Service.ExcelExportService;
import com.gk.firework.Service.ProductCodeService;
import com.gk.firework.Service.ProductService;
import com.gk.firework.Service.StockService;
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.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @author : jingjy
 * @date : 2021/3/16 10:21
 */
@Service("ProductService")
public class ProductServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductService{
 
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private ExcelExportService excelExportService;
    @Autowired
    private StockService stockService;
    @Autowired
    private ProductCodeService productCodeService;
 
    @Override
    public List<ProductInfo> selectProductInfos(Map<String, Object>condition) {
        List<ProductInfo> productInfos = productInfoMapper.selectProductInfos(condition);
        return productInfos;
    }
 
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<ProductInfo> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
 
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        }else {
            orderItem.setAsc(false);
            orderItem.setColumn("createddate");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        if (StringUtils.isBlank(pageInfo.getSort())){
            pageInfo.setSort("createddate");
        }
        if (StringUtils.isBlank(pageInfo.getOrder())){
            pageInfo.setOrder("desc");
        }
        List<ProductInfo> productInfos = productInfoMapper.selectProductDataGrid(pageInfo.getCondition(),page);
        pageInfo.setResult(productInfos);
        pageInfo.setTotalCount(page.getTotal());
    }
 
    @Override
    public List<ProductInfo> selectByProduct(ProductInfo productInfo) {
        if (productInfo == null || StringUtils.isBlank(productInfo.getDirectionCode())) {
            return null;
        }
        return productInfoMapper.selectProductsByDirectionCode(productInfo.getDirectionCode(),productInfo.getCompanyNumber());
    }
 
    @Override
    public boolean hasProductByDire(String directionCode) {
        ProductInfo productInfo = productInfoMapper.selectProductByDirectionCode(directionCode);
        return productInfo != null;
    }
 
    @Override
    public List<String> hasNoProductByCodes(List<String> directionCodes) {
        List<String>list = new ArrayList<>();
        for (String dire : directionCodes){
            if (!hasProductByDire(dire)){
                list.add(dire);
            }
        }
        return list;
    }
 
    @Override
    public ProductInfo selectByDirection(String dire) {
        if (StringUtils.isBlank(dire)|| dire.length() < 10){
            return null;
        }
        dire = dire.substring(0,10);
        return productInfoMapper.selectProductByDirectionCode(dire);
    }
 
    @Override
    public ProductVo selectVoByDirection(String dire) {
        DirectionDetail directionDetail = FireworkDeal.dealDirectionCode(dire);
        ProductInfo productInfo = productInfoMapper.selectProductByDirectionCode(directionDetail.getItemCode());
        if (productInfo == null) return null;
        ProductVo productVo = BeanUtils.copy(productInfo, ProductVo.class);
        productVo.setDirectionCode(dire);
        productVo.setItemCode(directionDetail.getItemCode());
        productVo.setDateCode(directionDetail.getDateCode());
        productVo.setSerialNo(directionDetail.getSerialNo());
        return productVo;
    }
 
    @Override
    public String getSlice(String directionCode){
        if (StringUtils.isBlank(directionCode) || directionCode.length() < 10){
            return "";
        }
 
        Integer slice = productInfoMapper.getSliceByDirectionCode(directionCode.substring(0,10));
        if (slice == null){
            return null;
        }
        return "_slice"+slice;
    }
 
    @Override
    public String getSlice(Long productId){
        if (productId == null){
            return "";
        }
        ProductInfo productInfo = productInfoMapper.selectById(productId);
        if (productInfo == null){
            return null;
        }
        Integer slice = productInfoMapper.getSliceByDirectionCode(productInfo.getDirectionCode());
        if (slice == null){
            return null;
        }
        return "_slice"+slice;
    }
 
    @Override
    public void deleteByEnterpriseName(String enterpriseName,String name) {
        productInfoMapper.deleteByEnterpriseName(enterpriseName,name);
    }
 
    @Override
    public List<Product2JsonVo> transform2Json(String enterprisenumber, MultipartFile file, UserInfo userInfo) {
        if (StringUtils.isBlank(enterprisenumber)) {
            throw new BusinessException("参数传递错误");
        }
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
 
        String originalFilename = file.getOriginalFilename();
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析
            List<Product2JsonVo> product2JsonVos = excelExportService.parsingProduct(inputStream, userInfo, isExcel2007, enterprisenumber);
            //再做一遍筛查
            assert product2JsonVos.size() > 0;
            List<String> stringList = product2JsonVos.stream().map(Product2JsonVo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = stringList.stream().distinct().count();
            if (stringList.size() == count) {
                return product2JsonVos;
            } else {
                throw new BusinessException("文件中有重复产品在不同行");
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
 
    }
 
 
    /**
    * @Description: 根据产品编号和生产企业编号 查询个数
    * @date 2021/5/24 15:45
    */
    @Override
    public int countByEnterpriseNumberAndDirectionCode(String enterprisenumber, String directionCode) {
        if (StringUtils.isBlank(enterprisenumber)) {
            throw new BusinessException("企业编号不能为空");
        }
        if (StringUtils.isBlank(directionCode)) {
            throw new BusinessException("产品编号不能为空");
        }
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInfo::getIsDel, (byte) 0).
                eq(ProductInfo::getCompanyNumber, enterprisenumber)
                .eq(ProductInfo::getDirectionCode, directionCode);
        return productInfoMapper.selectCount(queryWrapper);
    }
 
    @Override
    public List<Product2JsonVo> transform2JsonSimple(MultipartFile file, UserInfo userInfo) {
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
 
        String originalFilename = file.getOriginalFilename();
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析
            List<Product2JsonVo> product2JsonVos = excelExportService.parsingProduct(inputStream, userInfo, isExcel2007);
            //再做一遍筛查
            assert product2JsonVos.size() > 0;
            List<String> stringList = product2JsonVos.stream().map(Product2JsonVo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = stringList.stream().distinct().count();
            if (stringList.size() == count) {
                return product2JsonVos;
            } else {
                throw new BusinessException("文件中有重复产品在不同行");
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
 
    @Override
    public List<ProductInfo> selectProductInfo(Map<String, Object> condition) {
        return productInfoMapper.selectProductInfo(condition);
    }
 
    @Override
    public List<String> getAllProductCodes() {
        return productInfoMapper.getAllProductCodes();
    }
 
    @Override
    @Transactional
    public void importDataByExcel(MultipartFile file, UserInfo user) {
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
 
        String originalFilename = file.getOriginalFilename();
 
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析导入
            List<ProductInfo> dataList= excelExportService.parseProductFromOldSystem(inputStream, user, isExcel2007);
            assert dataList.size() > 0;
            List<String> directionCodes = dataList.stream().map(ProductInfo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = directionCodes.stream().distinct().count();
            if (directionCodes.size() != count)
                throw new BusinessException("文件中有重复产品流向码");
 
            for (ProductInfo productInfo : dataList) {
                productInfo.setIsOld((byte)1);
                this.save(productInfo);
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
 
    @Override
    public boolean isProductUsed(Long id) {
        ProductInfo productInfo = productInfoMapper.selectById(id);
        if (productInfo == null){
            return false;
        }
        List<StockInfo> stockInfos = stockService.selectStockByProductId(id);
        if (stockInfos.size() > 0 ){
            return true;
        }
        List<ProductCodeInfo> productCodeInfos =  productCodeService.selectByItemCode(productInfo.getDirectionCode());
        return productCodeInfos.size() > 0;
    }
 
    @Override
    public List<String> selectTypes() {
 
        return productInfoMapper.selectTypes();
    }
 
    @Override
    public List<ProductDO> selectDoByDirections(List<String> direction10Codes) {
        if (direction10Codes == null || direction10Codes.size() == 0) {
            throw new BusinessException("系统入参为空");
        }
        return productInfoMapper.selectDoByDirections(direction10Codes);
    }
}