自动化立体仓库 - WMS系统
#
whycq
2025-01-08 d3cb0e841e6585aa84a45f18bb30965db8d1a6aa
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package com.zy.asrs.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
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 com.core.exception.CoolException;
import com.zy.asrs.entity.BomMat;
import com.zy.asrs.service.BomMatService;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.common.entity.MatExcel;
import com.zy.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
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.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
 
@Slf4j
@RestController
public class BomMatController extends BaseController {
 
    @Autowired
    private BomMatService bomMatService;
 
    @RequestMapping(value = "/bomMat/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") String id) {
        return R.ok(bomMatService.selectById(String.valueOf(id)));
    }
 
    @RequestMapping(value = "/bomMat/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<BomMat> wrapper = new EntityWrapper<>();
        excludeTrash(param);
        convert(param, wrapper);
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));}
        return R.ok(bomMatService.selectPage(new Page<>(curr, limit), wrapper));
    }
 
    private <T> void convert(Map<String, Object> map, EntityWrapper<T> 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 = "/bomMat/add/auth")
    @ManagerAuth
    public R add(BomMat bomMat) {
        Date now = new Date();
        bomMat.setZpalletAnfme(bomMat.getBomAnfme() * bomMat.getBomCount());
        bomMat.setModiUser(getUserId());
        bomMat.setModiTime(now);
        bomMat.setAppeUser(getUserId());
        bomMat.setAppeTime(now);
        bomMatService.insert(bomMat);
        return R.ok();
    }
 
    @RequestMapping(value = "/bomMat/update/auth")
    @ManagerAuth
    public R update(BomMat bomMat){
        if (Cools.isEmpty(bomMat) || null==bomMat.getId()){
            return R.error();
        }
        bomMat.setZpalletAnfme(bomMat.getBomAnfme() * bomMat.getBomCount());
        bomMat.setModiUser(getUserId());
        bomMat.setModiTime(new Date());
        bomMatService.updateById(bomMat);
        return R.ok();
    }
 
    @RequestMapping(value = "/bomMat/delete/auth")
    @ManagerAuth
    public R delete(@RequestParam(value="ids[]") Long[] ids){
         for (Long id : ids){
            bomMatService.deleteById(id);
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/bomMat/export/auth")
    @ManagerAuth
    public R export(@RequestBody JSONObject param){
        EntityWrapper<BomMat> wrapper = new EntityWrapper<>();
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
        Map<String, Object> map = excludeTrash(param.getJSONObject("bomMat"));
        convert(map, wrapper);
        List<BomMat> list = bomMatService.selectList(wrapper);
        return R.ok(exportSupport(list, fields));
    }
 
    @RequestMapping(value = "/bomMatQuery/auth")
    @ManagerAuth
    public R query(String condition) {
        EntityWrapper<BomMat> wrapper = new EntityWrapper<>();
        wrapper.like("id", condition);
        Page<BomMat> page = bomMatService.selectPage(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (BomMat bomMat : page.getRecords()){
            Map<String, Object> map = new HashMap<>();
            map.put("id", bomMat.getId());
            map.put("value", bomMat.getId());
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping(value = "/bomMat/check/column/auth")
    @ManagerAuth
    public R query(@RequestBody JSONObject param) {
        Wrapper<BomMat> wrapper = new EntityWrapper<BomMat>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
        if (null != bomMatService.selectOne(wrapper)){
            return R.parse(BaseRes.REPEAT).add(getComment(BomMat.class, String.valueOf(param.get("key"))));
        }
        return R.ok();
    }
 
    /**
     * excel导入
     */
    @PostMapping(value = "/bomMat/excel/import/auth")
    @ManagerAuth(memo = "组件档案excel导入")
    @Transactional
    public R cstmrExcelImport(MultipartFile file) throws IOException {
        InputStream inStream = file.getInputStream();
        String fileMime = file.getContentType();
        int excelVersion = 2007;
        if ("application/vnd.ms-excel".equals(fileMime)) {
            excelVersion = 2003;
        }
        Workbook book = null;
        try {
            if (excelVersion == 2003) {
                book = new HSSFWorkbook(inStream);
            }
            else {  // 当 excel 是 2007 时
                book = new XSSFWorkbook(inStream);
            }
        } catch (Exception e) {
            log.error("fail", e);
            return R.error("导入文件格式错误,请使用xls后缀的文件!");
        }
 
        Sheet sheet = book.getSheetAt(0);
        int totalRows = sheet.getLastRowNum() + 1;    // 总
        Long userId = getUserId();
        Date now = new Date();
        DataFormatter dataFormatter = new DataFormatter();
 
        for (int i = 1; i < totalRows; i++) {
            Row row = sheet.getRow(i);
            // 部件品号
            String unitNum =  dataFormatter.formatCellValue(row.getCell(0));
            // 部件品名
            String unitName = dataFormatter.formatCellValue(row.getCell(1));
            // 部件规格
            String unitSpace = dataFormatter.formatCellValue(row.getCell(2));
            // 组件品号
            String bomNum = dataFormatter.formatCellValue(row.getCell(3));
            // 组件品名
            String bomName = dataFormatter.formatCellValue(row.getCell(4));
            // 组件规格
            String bomSpace = dataFormatter.formatCellValue(row.getCell(5));
            // 元件品号
            String elementNum = dataFormatter.formatCellValue(row.getCell(6));
            // 元件品名
            String elementName = dataFormatter.formatCellValue(row.getCell(7));
            // 元件规格
            String elementSpace = dataFormatter.formatCellValue(row.getCell(8));
            // 备注
            String memo = dataFormatter.formatCellValue(row.getCell(9));
            // 套数
            Double bomCount = Double.parseDouble(dataFormatter.formatCellValue(row.getCell(10)));
            // 组成用量
            Double bomAnfme = Double.parseDouble(dataFormatter.formatCellValue(row.getCell(11)));
            BomMat bomMat = bomMatService.selectByThreeCode(unitNum, bomNum, elementNum);
            // 没有就新建,有就更新
            if (Cools.isEmpty(bomMat)) {
                createBomMat(unitNum, unitName, unitSpace, bomNum, bomName, bomSpace, elementNum, elementName, elementSpace, memo, bomCount, bomAnfme,getUserId());
            } else {
                updateBomMat(bomMat,unitName,unitSpace,bomName,bomSpace,elementName,elementSpace,memo,bomCount,bomAnfme,userId);
            }
 
        }
        return R.ok();
    }
 
    /**
     * excel导入模板下载
     */
    @RequestMapping(value = "/bomMat/excel/import/mould")
    public void matExcelImportMould(HttpServletResponse response) throws IOException {
        List<BomMat> excels = new ArrayList<>();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("组件档案Excel导入模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), BomMat.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("sheet1")
                .doWrite(excels);
    }
 
    private void createBomMat(String unitNum,String unitName,String unitSpace,
                              String bomNum,String bomName,String bomSpace,
                              String elementNum, String elementName,String elementSpace,
                              String memo,Double bomCount,Double bomAnfme,Long userId) {
        Date now = new Date();
        BomMat bomMat = new BomMat();
        bomMat.setUnitNum(unitNum);
        bomMat.setUnitName(unitName);
        bomMat.setUnitSpace(unitSpace);
 
        bomMat.setBomNum(bomNum);
        bomMat.setBomName(bomName);
        bomMat.setBomSpace(bomSpace);
 
        bomMat.setElementNum(elementNum);
        bomMat.setElementName(elementName);
        bomMat.setElementSpace(elementSpace);
 
        bomMat.setMemo(memo);
        bomMat.setBomCount(bomCount);
        bomMat.setBomAnfme(bomAnfme);
        bomMat.setZpalletAnfme(bomAnfme * bomCount);
 
        bomMat.setModiUser(userId);
        bomMat.setModiTime(now);
        bomMat.setAppeUser(userId);
        bomMat.setAppeTime(now);
 
        bomMat.setIsDeleted((short)0);
        if (!bomMatService.insert(bomMat)) {
            throw new CoolException("新建组件档案失败!");
        }
    }
    private void updateBomMat(BomMat bomMat,String unitName,String unitSpace,String bomName,String bomSpace,String elementName,String elementSpace,String memo,Double bomCount,Double bomAnfme,Long userId) {
        Date now = new Date();
        bomMat.setUnitName(unitName);
        bomMat.setUnitSpace(unitSpace);
        bomMat.setBomName(bomName);
        bomMat.setBomSpace(bomSpace);
        bomMat.setElementName(elementName);
        bomMat.setElementSpace(elementSpace);
        bomMat.setMemo(memo);
        bomMat.setBomCount(bomCount);
        bomMat.setBomAnfme(bomAnfme);
        bomMat.setZpalletAnfme(bomCount*bomAnfme);
        bomMat.setModiUser(userId);
        bomMat.setModiTime(now);
        if (!bomMatService.update(bomMat,new EntityWrapper<BomMat>().eq("unit_num",bomMat.getUnitNum()).eq("bom_num",bomMat.getBomNum()).eq("element_num",bomMat.getElementNum()))) {
            throw new CoolException("更新组件档案失败!");
        }
 
    }
}