cl
13 小时以前 a6b253099ba7f1609e30911821d19c7b06a716ec
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
package com.vincent.rsf.server.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.framework.common.BaseRes;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.server.api.controller.open.param.QueryTaskParam;
import com.vincent.rsf.server.api.service.OpenAsrsService;
import com.vincent.rsf.server.manager.controller.dto.LocStockDto;
import com.vincent.rsf.server.manager.entity.Task;
import com.vincent.rsf.server.manager.entity.TaskItem;
import com.vincent.rsf.server.manager.enums.LocStsType;
import com.vincent.rsf.server.manager.mapper.OpenAsrsMapper;
import com.vincent.rsf.server.manager.service.LocService;
import com.vincent.rsf.server.manager.service.TaskItemService;
import com.vincent.rsf.server.manager.service.TaskService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class OpenAsrsServiceImpl implements OpenAsrsService {
 
    @Resource
    private OpenAsrsMapper openAsrsMapper;
    @Resource
    private LocService locService;
    @Resource
    private TaskService taskService;
    @Resource
    private TaskItemService taskItemService;
 
    @Override
    public R queryLoc() {
        List<Map<String, Object>> rows = openAsrsMapper.countLocGroupByUseStatus();
        int f = countStatus(rows, LocStsType.LOC_STS_TYPE_F.type);
        int o = countStatus(rows, LocStsType.LOC_STS_TYPE_O.type);
        int d = countStatus(rows, LocStsType.LOC_STS_TYPE_D.type);
        int r = countStatus(rows, LocStsType.LOC_STS_TYPE_R.type);
        int s = countStatus(rows, LocStsType.LOC_STS_TYPE_S.type);
        int x = countStatus(rows, LocStsType.LOC_STS_TYPE_X.type);
 
        int oqty = o + d;
        int uqty = r + s;
 
        List<Map<String, Object>> pie = new ArrayList<>();
        pie.add(pieSlice("在库", f));
        pie.add(pieSlice("空", oqty));
        pie.add(pieSlice("使用", uqty));
        pie.add(pieSlice("禁用", x));
 
        int total = f + oqty + uqty + x;
        int used = f + uqty;
        double usedPr = 0D;
        if (total > 0) {
            usedPr = BigDecimal.valueOf(used)
                    .multiply(BigDecimal.valueOf(100))
                    .divide(BigDecimal.valueOf(total), 1, RoundingMode.HALF_UP)
                    .doubleValue();
        }
 
        return R.ok(
                Cools.add("pie", pie)
                        .add("stockCount", f)
                        .add("emptyCount", oqty)
                        .add("disableCount", x)
                        .add("total", total)
                        .add("used", used)
                        .add("usedPr", usedPr)
        );
    }
 
    private static int countStatus(List<Map<String, Object>> rows, String status) {
        if (rows == null) {
            return 0;
        }
        for (Map<String, Object> row : rows) {
            Object st = row.get("st");
            if (st != null && status.equals(String.valueOf(st))) {
                Object cnt = row.get("cnt");
                if (cnt instanceof Number) {
                    return ((Number) cnt).intValue();
                }
            }
        }
        return 0;
    }
 
    private static Map<String, Object> pieSlice(String name, int value) {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("name", name);
        m.put("value", value);
        return m;
    }
 
    @Override
    public R locIoLineCharts() {
        List<Map<String, Object>> dbRows = openAsrsMapper.aggregateInOutByDay();
        Map<String, int[]> byDay = new HashMap<>();
        if (dbRows != null) {
            for (Map<String, Object> row : dbRows) {
                String ymd = row.get("ymd") == null ? null : String.valueOf(row.get("ymd"));
                if (ymd == null || ymd.isEmpty()) {
                    continue;
                }
                int inq = toInt(row.get("inqty"));
                int outq = toInt(row.get("outqty"));
                byDay.put(ymd, new int[]{inq, outq});
            }
        }
 
        List<Map<String, Object>> list = new ArrayList<>();
        List<Integer> data1 = new ArrayList<>();
        List<Integer> data2 = new ArrayList<>();
 
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.DATE, 1);
            String str = sf.format(calendar.getTime());
            int[] pair = byDay.get(str);
            if (pair != null) {
                data1.add(pair[0]);
                data2.add(pair[1]);
            } else {
                data1.add(0);
                data2.add(0);
            }
        }
 
        Map<String, Object> inqty = new LinkedHashMap<>();
        inqty.put("name", "入库数量");
        inqty.put("data", data1.toArray(new Integer[0]));
        list.add(inqty);
        Map<String, Object> outqty = new LinkedHashMap<>();
        outqty.put("name", "出库数量");
        outqty.put("data", data2.toArray(new Integer[0]));
        list.add(outqty);
 
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("rows", list);
        return R.ok(map);
    }
 
    private static int toInt(Object o) {
        if (o == null) {
            return 0;
        }
        if (o instanceof Number) {
            return ((Number) o).intValue();
        }
        try {
            return new BigDecimal(o.toString()).intValue();
        } catch (Exception e) {
            return 0;
        }
    }
 
    @Override
    public R locDetlStatistics() {
        Page<Object> page = new Page<>(1, 100);
        return R.ok(locService.getLocDetls(page));
    }
 
    @Override
    public synchronized R queryTask(QueryTaskParam param) {
        if (Cools.isEmpty(param)) {
            return R.parse(BaseRes.PARAM);
        }
        if (Cools.isEmpty(param.getTaskNo())) {
            return R.error("任务号[taskNo]不能为空");
        }
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskCode, param.getTaskNo()));
        if (task == null) {
            return R.error("任务不存在");
        }
        List<TaskItem> wrkDetls = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
 
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("taskNo", param.getTaskNo());
        map.put("ioType", task.getTaskType());
        map.put("wrkDetls", wrkDetls);
        return R.ok().add(map);
    }
}