Crunchy
2025-06-14 b2f31607cbe26d721cd7514b619162b3e355b1aa
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
package com.wms_admin.server.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms_admin.server.entity.Product;
import com.wms_admin.server.mapper.ProductMapper;
import com.wms_admin.server.service.ProductModelService;
import com.wms_admin.server.service.ProductNameService;
import com.wms_admin.server.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms_admin.utils.MyUtils;
import com.wms_admin.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2023-05-24
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
 
    @Resource
    private ProductMapper mapper;
 
    @Autowired
    private ProductModelService modelService;
 
    @Autowired
    private ProductNameService nameService;
    /**
     * 入库添加操作
     * @param product 实体类
     * @return 返回结果:1 成功;0:失败
     */
    @Override
    public Integer AddProductMessage(Product product) {
        Integer productModelId = modelService.SelectProductModelId(product.getProductModel(), product.getProductCode());
        product.setProductModelId(productModelId);
        Integer productNameId = nameService.SelectProductNameId(product.getProductName());
        product.setProductNameId(productNameId);
        return mapper.insert(product);
    }
 
    /**
     * 根据条件分页查询数据
     * @param startTime 开始时间,可以为空,但是一旦有开始时间那么一定有结束时间
     * @param endTime 结束时间
     * @param productModel 库存型号
     * @param page 分页条件
     * @return 返回数据
     */
    @Override
    public IPage<Product> ListProductMessage(String startTime, String endTime, String productModel, Page page) {
        return mapper.SelectHistory(startTime, endTime, productModel, page);
    }
 
    /**
     * 根据条件导出所有的数据
     * @param startTime 开始时间,可以为空,但是一旦有开始时间那么一定有结束时间
     * @param endTime 结束时间
     * @param productModel 库存型号
     * @return 返回查询到的数据
     */
    @Override
    public List<Product> ExcelDerive(String startTime, String endTime, String productModel) {
        return mapper.SelectHistory(startTime, endTime, productModel);
    }
 
    @Override
    public List<Map<String, Object>> SelectExceedThirtyDayData() {
        return mapper.ExceedThirtyDay();
    }
 
    @Override
    public Map<String, Object> SelectWeekNumData() {
        List<Map<String, Object>> maps = mapper.SelectWeekNumData();
        List<Object> xList = new ArrayList<>();
        List<Object> enterList = new ArrayList<>();
        List<Object> outList = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Object date = map.get("date");
            String time0 = MyUtils.getTime_0((Date) date);
            xList.add(time0);
            enterList.add(map.get("product_num"));
            outList.add(map.get("out_product_num"));
        }
        Collections.reverse(xList);
        Collections.reverse(enterList);
        Collections.reverse(outList);
        Map<String, Object> map = new HashMap<>();
        map.put("xList", xList);
        map.put("yEnterList", enterList);
        map.put("yOutList", outList);
        return map;
    }
 
    @Override
    public Integer TimerCountWeekDayData() {
        return mapper.TimerCountWeekDayData();
    }
 
    @Override
    public List<Map<String, Object>> SelectNameData(String productName) {
        return mapper.SelectNameData(productName);
    }
 
    @Override
    public List<Map<String, Object>> PieData() {
        String beginningMonthKey = "beginningMonth";
        String endOfMonthKey = "endOfMonth";
        // 将月初,月末数据拼接到该数据中,并且时间对应
        List<Map<String, Object>> maps = mapper.PieData();
        // 月初剩余库总数数据
        Map<String, Object> beginningMonth = JSONObject.parseObject(JSON.toJSONString(RedisUtil.get("beginningMonth")));
        // 月末剩余库总数数据
        Map<String, Object> endOfMonth = JSONObject.parseObject(JSON.toJSONString(RedisUtil.get("endOfMonth")));
        // 拼接月初剩余库总数数据
        List<Map<String, Object>> beginningMonthMaps = publicCode(maps, beginningMonth, beginningMonthKey);
        // 在拼接完月初的数据后以此为基础拼接
        return publicCode(beginningMonthMaps, endOfMonth, endOfMonthKey);
    }
 
    @Override
    public Product SelectIdProduct(String code) {
        return mapper.SelectIdProduct(code);
    }
 
    // 将Redis中的数据与Mysql中的数据拼接
    private List<Map<String, Object>> publicCode(List<Map<String, Object>> mainMaps, Map<String, Object> maps, String jsonKey){
        for (Map<String, Object> map : mainMaps){
            // mysql中的时间,以此为基础拼接
            String mainKey = (String)map.get("datetime");
            // 库存参数
            List<String> num = JSON.parseArray(maps.get("num").toString(), String.class);
            // Redis中存储的月份
            List<String> datetime = JSON.parseArray(maps.get("datetime").toString(), String.class);
            // 循环,如果时间相同则进行拼接
            for (int i = 0; i < datetime.size(); i++){
                if (mainKey.equals(datetime.get(i))){
                    map.put(jsonKey, num.get(i));
                }
            }
            // 判断是否存在key,如果不存在拼接为0
            if (!map.containsKey(jsonKey)){
                // 如果不存在赋值为0
                map.put(jsonKey, 0);
            }
        }
        return mainMaps;
    }
}