#
Junjie
2023-12-29 7e68f7b8bf6c67be60faeac758511650c61c6f6d
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
package com.zy.asrs.wms.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.asrs.common.domain.dto.SafeStoDo;
import com.zy.asrs.common.domain.dto.SafeStoDto;
import com.zy.asrs.common.wms.entity.Mat;
import com.zy.asrs.common.wms.entity.Prior;
import com.zy.asrs.common.wms.service.MatService;
import com.zy.asrs.common.wms.service.PriorService;
import com.zy.asrs.framework.annotations.ManagerAuth;
import com.zy.asrs.framework.common.*;
import com.zy.asrs.framework.domain.KeyValueVo;
import com.zy.asrs.common.web.BaseController;
import com.zy.asrs.framework.exception.CoolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
@RestController
public class PriorController extends BaseController {
 
    @Autowired
    private PriorService priorService;
    @Autowired
    private MatService matService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
 
    @RequestMapping(value = "/prior/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") String id) {
        return R.ok(priorService.getById(String.valueOf(id)));
    }
 
    @RequestMapping(value = "/prior/page/auth")
    @ManagerAuth
    public R page(@RequestParam(defaultValue = "1") Integer curr,
                  @RequestParam(defaultValue = "10") Integer limit,
                  @RequestParam(required = false) String condition,
                  @RequestParam(required = false) String timeRange,
                  @RequestParam Map<String, Object> param) {
        LambdaQueryWrapper<Prior> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Prior::getId, condition);
        }
        if (!Cools.isEmpty(timeRange)) {
            String[] range = timeRange.split(RANGE_TIME_LINK);
            wrapper.ge(Prior::getCreateTime, DateUtils.convert(range[0]));
            wrapper.le(Prior::getCreateTime, DateUtils.convert(range[1]));
        }
        return R.ok(priorService.page(new Page<>(curr, limit), wrapper));
    }
 
 
    @RequestMapping(value = "/prior/add/auth")
    @ManagerAuth
    public R add(Prior prior) {
        priorService.save(prior);
        return R.ok();
    }
 
    @RequestMapping(value = "/prior/update/auth")
    @ManagerAuth
    public R update(Prior prior){
        if (Cools.isEmpty(prior) || null==prior.getId()){
            return R.error();
        }
        priorService.updateById(prior);
        return R.ok();
    }
 
    @RequestMapping(value = "/prior/delete/auth")
    @ManagerAuth
    public R delete(@RequestParam(value="ids[]") Long[] ids){
         for (Long id : ids){
            priorService.removeById(id);
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/priorQuery/auth")
    @ManagerAuth
    public R query(String condition) {
        LambdaQueryWrapper<Prior> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Prior::getId, condition);
        Page<Prior> page = priorService.page(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Prior prior : page.getRecords()){
            Map<String, Object> map = new HashMap<>();
            map.put("id", prior.getId());
            map.put("value", prior.getId());
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping("/prior/all/get/kv")
    @ManagerAuth
    public R getDataKV(@RequestParam(required = false) String condition) {
        List<KeyValueVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Prior> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Prior::getId, condition);
        }
        priorService.page(new Page<>(1, 30), wrapper).getRecords().forEach(item -> vos.add(new KeyValueVo(String.valueOf(item.getId()), item.getId())));
        return R.ok().add(vos);
    }
 
    /**
     * 安全库存警告分页
     */
    @RequestMapping(value = "/safeSto/list/auth")
    @ManagerAuth
    public R safeStoList(@RequestParam(defaultValue = "1")Integer curr,
                         @RequestParam(defaultValue = "10")Integer limit,
                         @RequestParam Map<String, Object> param){
        Long hostId = getHostId();
        if (hostId != null) {
            param.put("host_id", hostId);
        }
        IPage<SafeStoDo> safeQtyPage = priorService.getSafeQtyPage(curr, limit, param);
        return R.ok(safeQtyPage);
    }
 
    @RequestMapping(value = "/safeSto/add/auth")
    @ManagerAuth(memo = "添加安全库存")
    public R del(@RequestBody SafeStoDto dto){
        if (Cools.isEmpty(dto.getMatId(), dto.getSafeQua())) {
            return R.parse(BaseRes.PARAM);
        }
        if (dto.getSafeQua() <= 0.0D) {
            return R.error("安全库存量必须大于零");
        }
        Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getId, dto.getMatId()).eq(Mat::getHostId, getHostId()));
        if (mat != null) {
            Prior prior = new Prior();
            prior.setHostId(getHostId());
            prior.setUuid("SP" + snowflakeIdWorker.nextId());
            prior.setMatId(mat.getId());
            prior.setMatnr(mat.getMatnr());
            prior.setMaktx(mat.getMaktx());
            prior.setLocNo(dto.getLocNo());
            prior.setSafeQua(dto.getSafeQua());
            prior.setCreateBy(getUserId());
            prior.setCreateTime(new Date());
            prior.setUpdateBy(getUserId());
            prior.setUpdateTime(new Date());
            prior.setStatus(1);
            prior.setMemo(dto.getMemo());
            if (!priorService.save(prior)) {
                throw new CoolException("数据保存失败");
            }
        } else {
            throw new CoolException("服务器内部错误");
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/safeSto/delete/auth")
    @ManagerAuth(memo = "删除库存预警")
    @Transactional
    public R del(@RequestBody List<SafeStoDto> dtos){
        if (Cools.isEmpty(dtos)) {
            return R.parse(BaseRes.PARAM);
        }
        for (SafeStoDto dto : dtos) {
            if (!priorService.remove(new LambdaQueryWrapper<Prior>().eq(Prior::getId, dto.getId()).eq(Prior::getHostId, getHostId()))) {
                throw new CoolException("删除安全库存失败,请重新尝试");
            }
        }
        return R.ok();
    }
 
}