#
mrzhssss
2022-03-24 4c1e8761e3fa0516d5e6d316e838e83c8e0f5edf
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
package zy.cloud.wms.manager.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.common.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import zy.cloud.wms.manager.entity.*;
import zy.cloud.wms.manager.entity.dto.PutShelfDTO;
import zy.cloud.wms.manager.service.*;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.R;
import zy.cloud.wms.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
@RestController
public class ReceiveDetlController extends BaseController {
 
    @Autowired
    private ReceiveDetlService receiveDetlService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private ReceiveLogService receiveLogService;
    @Autowired
    private ReceiveService receiveService;
 
    @RequestMapping(value = "/receiveDetl/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") String id) {
        return R.ok(receiveDetlService.selectById(String.valueOf(id)));
    }
 
    @RequestMapping(value = "/receiveDetl/list/auth")
    @ManagerAuth
    public R list(@RequestParam(defaultValue = "1")Integer curr,
                  @RequestParam(defaultValue = "10")Integer limit,
                  @RequestParam(required = false)String orderByField,
                  @RequestParam(required = false)String orderByType,
                  @RequestParam Map<String, Object> param){
        EntityWrapper<ReceiveDetl> wrapper = new EntityWrapper<>();
        excludeTrash(param);
        convert(param, wrapper);
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));}
        return R.ok(receiveDetlService.selectPage(new Page<>(curr, limit), wrapper));
    }
 
    private void convert(Map<String, Object> map, EntityWrapper wrapper){
        for (Map.Entry<String, Object> entry : map.entrySet()){
            String val = String.valueOf(entry.getValue());
            if (val.contains(RANGE_TIME_LINK)){
                String[] dates = val.split(RANGE_TIME_LINK);
                wrapper.ge(entry.getKey(), DateUtils.convert(dates[0]));
                wrapper.le(entry.getKey(), DateUtils.convert(dates[1]));
            } else {
                wrapper.like(entry.getKey(), val);
            }
        }
    }
 
    @RequestMapping(value = "/receiveDetl/add/auth")
    @ManagerAuth
    public R add(ReceiveDetl receiveDetl) {
        receiveDetlService.insert(receiveDetl);
        return R.ok();
    }
 
    @RequestMapping(value = "/receiveDetl/update/auth")
    @ManagerAuth
    public R update(ReceiveDetl receiveDetl){
        if (Cools.isEmpty(receiveDetl) || null==receiveDetl.getId()){
            return R.error();
        }
        receiveDetlService.updateById(receiveDetl);
        return R.ok();
    }
 
    @RequestMapping(value = "/receiveDetl/delete/auth")
    @ManagerAuth
    public R delete(@RequestParam(value="ids[]") Long[] ids){
         for (Long id : ids){
            receiveDetlService.deleteById(id);
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/receiveDetl/export/auth")
    @ManagerAuth
    public R export(@RequestBody JSONObject param){
        EntityWrapper<ReceiveDetl> wrapper = new EntityWrapper<>();
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
        Map<String, Object> map = excludeTrash(param.getJSONObject("receiveDetl"));
        convert(map, wrapper);
        List<ReceiveDetl> list = receiveDetlService.selectList(wrapper);
        return R.ok(exportSupport(list, fields));
    }
 
    @RequestMapping(value = "/receiveDetlQuery/auth")
    @ManagerAuth
    public R query(String condition) {
        EntityWrapper<ReceiveDetl> wrapper = new EntityWrapper<>();
        wrapper.like("id", condition);
        Page<ReceiveDetl> page = receiveDetlService.selectPage(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (ReceiveDetl receiveDetl : page.getRecords()){
            Map<String, Object> map = new HashMap<>();
            map.put("id", receiveDetl.getId());
            map.put("value", receiveDetl.getId());
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping(value = "/receiveDetl/check/column/auth")
    @ManagerAuth
    public R query(@RequestBody JSONObject param) {
        Wrapper<ReceiveDetl> wrapper = new EntityWrapper<ReceiveDetl>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
        if (null != receiveDetlService.selectOne(wrapper)){
            return R.parse(BaseRes.REPEAT).add(getComment(ReceiveDetl.class, String.valueOf(param.get("key"))));
        }
        return R.ok();
    }
 
    /**
     * 上架动作,插入库存明细,更新单据明细
     * @param putShelfDTO
     * @return
     */
    @RequestMapping("/receiveDetl/addIn")
    @ManagerAuth
    public R addIn(@RequestBody PutShelfDTO putShelfDTO){
        /**
         * 控管与数据初始化
         */
        if (Cools.isEmpty(putShelfDTO.getId(),putShelfDTO.getRemain(),putShelfDTO.getSelect())){
            return R.error("输入数据有误,请重新输入");
        }
        Date date = new Date();
        double remain = Double.parseDouble(putShelfDTO.getRemain());
        double anfme = Double.parseDouble(putShelfDTO.getAnfme());
        double inQty = Double.parseDouble(putShelfDTO.getInQty());
        if (anfme - (remain + inQty) < 0){
            return R.error("本次入库数量大于所需入库数量");
        }
        Node targetLoc = nodeService.selectOne(new EntityWrapper<Node>()
                .eq("id", putShelfDTO.getSelect()));
 
 
        /**
         * 更新库存
         */
        LocDetl checkLoc = locDetlService.selectOne(new EntityWrapper<LocDetl>()
                .eq("loc_no", targetLoc.getName())
                .eq("matnr", putShelfDTO.getMatnr())
                .eq("batch", putShelfDTO.getBatch()));
        if (Cools.isEmpty(checkLoc)) {
            LocDetl locDetl = new LocDetl();
            locDetl.setHostId(getHostId());
            locDetl.setLocNo(targetLoc.getName());
            locDetl.setNodeId(targetLoc.getId());
            locDetl.setAnfme(remain);
            locDetl.setMatnr(putShelfDTO.getMatnr());
            locDetl.setMaktx(putShelfDTO.getMaktx());
            locDetl.setCreateBy(getUserId());
            locDetl.setCreateTime(date);
            locDetl.setUpdateBy(getUserId());
            locDetl.setUpdateTime(date);
            locDetl.setBatch(putShelfDTO.getBatch());
            locDetlService.insert(locDetl);
        }else {
            checkLoc.setAnfme(checkLoc.getAnfme() + remain);
            locDetlService.update(checkLoc,new EntityWrapper<LocDetl>()
                    .eq("loc_no", targetLoc.getName())
                    .eq("matnr", putShelfDTO.getMatnr())
                    .eq("batch", putShelfDTO.getBatch()));
        }
 
        /**
         * 反写订单数量
         */
 
        ReceiveDetl targetRece = receiveDetlService.selectOne(new EntityWrapper<ReceiveDetl>()
                .eq("id", putShelfDTO.getId()));
        targetRece.setInQty((int) (targetRece.getInQty() + remain));
        receiveDetlService.update(targetRece,new EntityWrapper<ReceiveDetl>()
                .eq("id",targetRece.getId()));
 
        /**
         * 上架完成之后,存放数据至上架统计表 man_receive_log
         */
        ReceiveLog receiveLog = new ReceiveLog();
        receiveLog.setOrderNo(putShelfDTO.getOrderNo());
        receiveLog.setNodeId(targetLoc.getId());
        receiveLog.setNodeName(targetLoc.getName());
        receiveLog.setMatnr(putShelfDTO.getMatnr());
        receiveLog.setAnfme(remain);
        receiveLog.setBatch(putShelfDTO.getBatch());
        receiveLog.setCreateBy(getUserId());
        receiveLog.setUpdateBy(getUserId());
        receiveLog.setIoType(1);
        receiveLogService.insert(receiveLog);
        return R.ok("添加成功");
    }
 
    /**
     * 撤销上架操作
     */
    @RequestMapping("/receiveDetl/renew/auth")
    @ManagerAuth
    @Transactional
    public R renew(@RequestBody ReceiveDetl receiveDetl){
        /**
         * 控管
         */
        if (receiveDetl.getInQty() <= 0){
            return R.error("没有上架记录,无法撤回");
        }
        Receive receive = receiveService.selectOne(new EntityWrapper<Receive>()
                .eq("id", receiveDetl.getOrderId()));
        if (Cools.isEmpty(receive)) {
            return R.error("找不到该单据,请联系管理员");
        }
        if (receive.getSettle() == 4){
            return R.error("该单据已经完结");
        }
 
        /**
         * 修改库存数量,同时撤回上架完成后,将操作记录反写回上架统计表 man_receive_log
         */
        List<ReceiveLog> receiveLogs = receiveLogService.selectList(new EntityWrapper<ReceiveLog>()
                .eq("order_no", receiveDetl.getOrderNo())
                .eq("matnr", receiveDetl.getMatnr())
                .eq("batch", receiveDetl.getBatch())
                .eq("io_type", 1));
        if (Cools.isEmpty(receiveLogs)) {
            return R.error("找不到入库记录");
        }
        for (ReceiveLog receiveLog : receiveLogs) {
            locDetlService.delete(new EntityWrapper<LocDetl>()
                    .eq("loc_no",receiveLog.getNodeName())
                    .eq("matnr",receiveLog.getMatnr())
                    .eq("batch",receiveLog.getBatch()));
            receiveLog.setIoType(101);
            receiveLog.setCreateBy(getUserId());
            receiveLog.setUpdateBy(getUserId());
            receiveLogService.insert(receiveLog);
        }
 
        /**
         * 反写回入库档
         */
        receiveDetl.setInQty(0);
        receiveDetl.setUpdateTime(new Date());
        receiveDetl.setUpdateBy(getUserId());
        receiveDetlService.update(receiveDetl,new EntityWrapper<ReceiveDetl>()
                .eq("id",receiveDetl.getId()));
        return R.ok("撤回成功");
    }
 
}