#
zwl
2026-04-03 99d39b9533897c8bda6409d4637b245d86967da2
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
package com.zy.asrs.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.dto.BasCrnpMonitorDto;
import com.zy.asrs.entity.dto.WcsCrnDto;
import com.zy.asrs.entity.dto.WcsCrnSyncResult;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.WcsCrnSyncService;
import com.zy.asrs.utils.CrnUtils;
import com.zy.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
public class BasCrnpController extends BaseController {
 
    @Autowired
    private BasCrnpService basCrnpService;
 
    @Autowired
    private WcsCrnSyncService wcsCrnSyncService;
 
    @Autowired
    private CrnUtils crnUtils;
 
    @RequestMapping(value = "/basCrnp/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") Integer id) {
        return R.ok(basCrnpService.getById(id));
    }
 
    @RequestMapping(value = "/basCrnp/list/auth")
    @ManagerAuth
    public R list(@RequestParam(defaultValue = "1") Integer curr,
                  @RequestParam(defaultValue = "10") Integer limit,
                  @RequestParam(required = false) Integer crnNo,
                  @RequestParam(required = false) String condition) {
        QueryWrapper<BasCrnp> wrapper = new QueryWrapper<>();
        if (crnNo != null) {
            wrapper.eq("crn_no", crnNo);
        }
        if (!Cools.isEmpty(condition)) {
            wrapper.and(inner -> inner.like("crn_no", condition).or().like("memo", condition));
        }
        wrapper.orderBy(true, true, "crn_no");
 
        Page<BasCrnp> page = basCrnpService.page(new Page<>(curr, limit), wrapper);
        List<BasCrnpMonitorDto> records = new ArrayList<>();
        for (BasCrnp basCrnp : page.getRecords()) {
            records.add(toMonitorDto(basCrnp));
        }
 
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("records", records);
        data.put("lastSyncTime", crnUtils.getLastSyncTime());
        data.put("syncError", crnUtils.getLastSyncError());
        return R.ok(data);
    }
 
    @RequestMapping(value = "/basCrnp/add/auth", method = RequestMethod.POST)
    @ManagerAuth
    public R add(BasCrnp basCrnp) {
        if (basCrnp == null || basCrnp.getCrnNo() == null) {
            return R.error("堆垛机编号不能为空");
        }
        if (basCrnpService.count(new QueryWrapper<BasCrnp>().eq("crn_no", basCrnp.getCrnNo())) > 0) {
            return R.parse(BaseRes.REPEAT).add("堆垛机编号");
        }
        Date now = new Date();
        basCrnp.setStatus(basCrnp.getStatus() == null ? 1 : basCrnp.getStatus());
        basCrnp.setCreateBy(getUserId());
        basCrnp.setCreateTime(now);
        basCrnp.setUpdateBy(getUserId());
        basCrnp.setUpdateTime(now);
        basCrnpService.save(basCrnp);
        return R.ok();
    }
 
    @RequestMapping(value = "/basCrnp/update/auth", method = RequestMethod.POST)
    @ManagerAuth
    public R update(BasCrnp basCrnp) {
        if (basCrnp == null || basCrnp.getCrnNo() == null) {
            return R.error("堆垛机编号不能为空");
        }
        BasCrnp old = basCrnpService.getById(basCrnp.getCrnNo());
        if (old == null) {
            return R.error("堆垛机配置不存在");
        }
        old.setStatus(basCrnp.getStatus() == null ? old.getStatus() : basCrnp.getStatus());
        old.setMemo(basCrnp.getMemo());
        old.setUpdateBy(getUserId());
        old.setUpdateTime(new Date());
        basCrnpService.updateById(old);
        return R.ok();
    }
 
    @RequestMapping(value = "/basCrnp/delete/auth", method = RequestMethod.POST)
    @ManagerAuth
    public R delete(@RequestParam(value = "ids[]") Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return R.error("请选择要删除的数据");
        }
        basCrnpService.removeByIds(Arrays.asList(ids));
        for (Integer id : ids) {
            crnUtils.crnMap.remove(id);
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/basCrnp/check/column/auth", method = RequestMethod.POST)
    @ManagerAuth
    public R checkColumn(@RequestBody JSONObject param) {
        String key = param == null ? null : param.getString("key");
        Object val = param == null ? null : param.get("val");
        if (!"crnNo".equals(key) || Cools.isEmpty(val)) {
            return R.ok();
        }
        BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper<BasCrnp>().eq("crn_no", val));
        if (basCrnp != null) {
            return R.parse(BaseRes.REPEAT).add(getComment(BasCrnp.class, key));
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/basCrnp/refresh/auth", method = RequestMethod.POST)
    @ManagerAuth
    public R refresh() {
        WcsCrnSyncResult result = wcsCrnSyncService.sync();
        if (!result.isSuccess()) {
            return R.error(result.getMessage());
        }
        return R.ok(result);
    }
 
    private BasCrnpMonitorDto toMonitorDto(BasCrnp basCrnp) {
        BasCrnpMonitorDto dto = new BasCrnpMonitorDto();
        dto.setCrnNo(basCrnp.getCrnNo());
        dto.setLocalStatus(basCrnp.getStatus());
        dto.setLocalStatusDesc(basCrnp.getStatus$());
        dto.setMemo(basCrnp.getMemo());
 
        WcsCrnDto runtime = crnUtils.crnMap.get(basCrnp.getCrnNo());
        if (runtime == null) {
            dto.setOnline(0);
            dto.setOnlineDesc("离线");
            dto.setMode(null);
            dto.setModeDesc("离线");
            dto.setDeviceStatus(null);
            dto.setDeviceStatusDesc("离线");
            dto.setTaskNo(null);
            dto.setAlarm(null);
            dto.setLastSyncTime(crnUtils.getLastSyncTime());
            dto.setSyncError(crnUtils.getLastSyncError());
            return dto;
        }
 
        dto.setOnline(runtime.getOnline());
        dto.setOnlineDesc(runtime.getOnline() != null && runtime.getOnline() == 1 ? "在线" : "离线");
        dto.setMode(runtime.getMode());
        dto.setModeDesc(runtime.getModeDesc());
        dto.setDeviceStatus(runtime.getStatus());
        dto.setDeviceStatusDesc(runtime.getStatusDesc());
        dto.setTaskNo(runtime.getTaskNo());
        dto.setAlarm(runtime.getAlarm());
        dto.setLastSyncTime(runtime.getLastSyncTime());
        dto.setSyncError(runtime.getSyncError());
        return dto;
    }
}