#
Junjie
2024-08-16 fc8767d2b01d295aaa9e1a8dd2da55e4c0484130
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
package com.zy.asrs.wms.asrs.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.R;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.MatField;
import com.zy.asrs.wms.asrs.entity.OrderType;
import com.zy.asrs.wms.asrs.entity.enums.OrderSettleType;
import com.zy.asrs.wms.asrs.entity.param.CreateOrderParam;
import com.zy.asrs.wms.asrs.entity.param.UpdateOrderParam;
import com.zy.asrs.wms.asrs.entity.template.OrderTemplate;
import com.zy.asrs.wms.asrs.service.MatFieldService;
import com.zy.asrs.wms.asrs.service.OrderTypeService;
import com.zy.asrs.wms.common.annotation.OperationLog;
import com.zy.asrs.wms.common.domain.BaseParam;
import com.zy.asrs.wms.common.domain.KeyValVo;
import com.zy.asrs.wms.common.domain.PageParam;
import com.zy.asrs.wms.asrs.entity.Order;
import com.zy.asrs.wms.asrs.service.OrderService;
import com.zy.asrs.wms.system.controller.BaseController;
import com.zy.asrs.wms.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
@RequestMapping("/api")
public class OrderController extends BaseController {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderTypeService orderTypeService;
    @Autowired
    private MatFieldService matFieldService;
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Order, BaseParam> pageParam = new PageParam<>(baseParam, Order.class);
        return R.ok().add(orderService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/in/page")
    public R pageIn(@RequestBody Map<String, Object> map) {
        String condition = map.getOrDefault("condition", "").toString();
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Order, BaseParam> pageParam = new PageParam<>(baseParam, Order.class);
//        QueryWrapper<Order> wrapper = pageParam.buildWrapper(true);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
 
        ArrayList<Long> types = new ArrayList<>();
        for (OrderType orderType : orderTypeService.list(new LambdaQueryWrapper<OrderType>().eq(OrderType::getType, 1))) {
            types.add(orderType.getId());
        }
 
        wrapper.in(Order::getOrderType, types);
 
        if (!Cools.isEmpty(condition)) {
            wrapper.and(wrapper1 -> {
                wrapper1.or().like(Order::getOrderNo, condition);
                wrapper1.or().like(Order::getMemo, condition);
            });
        }
        return R.ok().add(orderService.page(pageParam, wrapper));
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/out/page")
    public R pageOut(@RequestBody Map<String, Object> map) {
        String condition = map.getOrDefault("condition", "").toString();
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Order, BaseParam> pageParam = new PageParam<>(baseParam, Order.class);
//        QueryWrapper<Order> wrapper = pageParam.buildWrapper(true);
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
 
        ArrayList<Long> types = new ArrayList<>();
        for (OrderType orderType : orderTypeService.list(new LambdaQueryWrapper<OrderType>().eq(OrderType::getType, 2))) {
            types.add(orderType.getId());
        }
 
        wrapper.in(Order::getOrderType, types);
 
        if (!Cools.isEmpty(condition)) {
            wrapper.and(wrapper1 -> {
                wrapper1.or().like(Order::getOrderNo, condition);
                wrapper1.or().like(Order::getMemo, condition);
            });
        }
        return R.ok().add(orderService.page(pageParam, wrapper));
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(orderService.list());
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @GetMapping("/order/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(orderService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('asrs:order:save')")
    @OperationLog("添加订单")
    @PostMapping("/order/save")
    @Transactional
    public R save(@RequestBody CreateOrderParam param) {
        orderService.createOrder(param);
        return R.ok("添加成功");
    }
 
    @PreAuthorize("hasAuthority('asrs:order:update')")
    @OperationLog("修改订单")
    @PostMapping("/order/update")
    @Transactional
    public R update(@RequestBody UpdateOrderParam param) {
        orderService.updateOrder(param);
        return R.ok("修改成功");
    }
 
    @PreAuthorize("hasAuthority('asrs:order:remove')")
    @OperationLog("删除订单")
    @PostMapping("/order/remove/{ids}")
    @Transactional
    public R remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            boolean result = orderService.deleteOrder(id);
            if (!result) {
                throw new CoolException("删除失败");
            }
        }
        return R.ok("删除成功");
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Order::getOrderNo, condition);
        }
        orderService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getOrderNo()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        String ioModel = map.getOrDefault("ioModel", "").toString();
        List<Order> list = orderService.list();
        if (!Cools.isEmpty(ioModel)) {
            LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
            ArrayList<Long> types = new ArrayList<>();
            for (OrderType orderType : orderTypeService.list(new LambdaQueryWrapper<OrderType>().eq(OrderType::getType, ioModel.equals("in") ? 1 : 2))) {
                types.add(orderType.getId());
            }
            wrapper.in(Order::getOrderType, types);
            list = orderService.list(wrapper);
        }
        ExcelUtil.build(ExcelUtil.create(list, Order.class), response);
    }
 
    @PreAuthorize("hasAuthority('asrs:order:list')")
    @PostMapping("/order/exportTemplate")
    public void exportTemplate(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ArrayList<OrderTemplate> list = new ArrayList<>();
        List<MatField> locFields = matFieldService.getLocFields();
        ExcelUtil.build(ExcelUtil.create(list, OrderTemplate.class, locFields), response);
    }
 
    @PostMapping("/order/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        List<OrderTemplate> list = ExcelUtil.parseExcelFile(file, OrderTemplate.class);
 
        HashMap<String, Object> map = new HashMap<>();
        for (OrderTemplate orderTemplate : list) {
            if (!map.containsKey(orderTemplate.getOrderNo())) {
                OrderType orderType = orderTypeService.getOne(new LambdaQueryWrapper<OrderType>().eq(OrderType::getName, orderTemplate.getOrderType()));
                if (orderType == null) {
                    throw new CoolException("订单类型不存在");
                }
 
                HashMap<String, Object> paramMap = new HashMap<>();
                ArrayList<HashMap<String, Object>> paramList = new ArrayList<>();
                paramList.add(paramMap);
 
                paramMap.put("matnr", orderTemplate.getMatnr());
                paramMap.put("batch", orderTemplate.getBatch());
                paramMap.put("anfme", orderTemplate.getAnfme());
                paramMap.put("memo", orderTemplate.getMemo());
                paramMap.putAll(orderTemplate.getDynamicFields());
 
                CreateOrderParam orderParam = new CreateOrderParam();
                orderParam.setOrderNo(orderTemplate.getOrderNo());
                orderParam.setOrderType(orderType.getId());
                orderParam.setOrderSettle(OrderSettleType.INIT.val());
                orderParam.setList(paramList);
 
                map.put(orderTemplate.getOrderNo(), orderParam);
            }else {
                CreateOrderParam orderParam = (CreateOrderParam) map.get(orderTemplate.getOrderNo());
 
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("matnr", orderTemplate.getMatnr());
                paramMap.put("batch", orderTemplate.getBatch());
                paramMap.put("anfme", orderTemplate.getAnfme());
                paramMap.put("memo", orderTemplate.getMemo());
                paramMap.putAll(orderTemplate.getDynamicFields());
 
                List<HashMap<String, Object>> paramList = orderParam.getList();
                paramList.add(paramMap);
 
                orderParam.setList(paramList);
                map.put(orderTemplate.getOrderNo(), orderParam);
            }
        }
 
        ArrayList<CreateOrderParam> orderParams = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            CreateOrderParam orderParam = (CreateOrderParam) entry.getValue();
            orderParams.add(orderParam);
        }
        orderService.createOrder(orderParams);
        return R.ok();
    }
 
}