Fixiaobai
2023-11-16 d8d129a2e41f7099968cb4f4dc1b028ab985135f
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
 
package com.chinaztt.mes.basic.controller;
 
import cn.hutool.core.text.csv.CsvReadConfig;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.excel.PartData;
import com.chinaztt.mes.basic.excel.PartUploadListener;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.basic.service.PartService;
import com.chinaztt.mes.basic.util.DictUtils;
import com.chinaztt.mes.basic.util.PartUtils;
import com.chinaztt.mes.basic.vo.PartVo;
import com.chinaztt.mes.common.util.JsonUtil;
import com.chinaztt.mes.common.wrapper.QueryWrapperUtil;
import com.chinaztt.ztt.admin.api.entity.SysDictItem;
import com.chinaztt.ztt.admin.api.feign.RemoteDictService;
import com.chinaztt.ztt.common.core.util.R;
import com.chinaztt.ztt.common.log.annotation.SysLog;
import com.chinaztt.ztt.common.security.annotation.Inner;
import com.chinaztt.ztt.common.security.util.SecurityUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.List;
 
 
/**
 * 零件
 *
 * @author fenglang
 * @date 2020-08-17 14:37:35
 */
@RestController
@AllArgsConstructor
@RequestMapping("/part")
@Api(value = "part", tags = "零件管理")
public class PartController {
 
    private final PartService partService;
    private final RemoteDictService remoteDictService;
    private final RedisTemplate redisTemplate;;
 
    private DictUtils dictUtils;
    private PartUtils partUtils;
 
    private PartMapper partMapper;
 
    private final static Logger logger = LoggerFactory.getLogger(PartController.class);
 
 
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
 
    @ApiOperation(value = "触发零件", notes = "触发零件")
    @SysLog("触发零件")
    @PostMapping("/triggerAdd")
    @Inner(value = false)
    public R triggerAdd(@RequestParam String params) {
        PartVo partVo = JSONObject.parseObject(params, PartVo.class);
        R<Object> objectR = new R<>();
        if(partService.triggerAdd(partVo).equals("1")){
            objectR.setCode(1);
            objectR.setMsg("操作成功");
        }else {
            objectR.setCode(0);
            objectR.setMsg("操作失败!联系人员查看日志!");
        }
        return objectR;
    }
 
 
 
    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param part  零件
     * @param bomId bomId
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getPartPage(Page page, Part part, Long bomId) {
        //System.out.println(bomId);
        //System.out.println(part);
        System.out.println(JsonUtil.jsonToString(page));
        return R.ok(partService.getPartPage(page, QueryWrapperUtil.gen(part), bomId));
    }
    /**
     * 对接志邦国际鹰联 erp 同步基礎零件
     * @return
     */
    @ApiOperation(value = "同步销售订单", notes = "同步销售订单")
    @PostMapping("/syncPart")
    @XxlJob("syncPart")
    public R syncPart() {
        String key = "syncPart_lock";
        if (redisTemplate.hasKey(key)) {
            throw new RuntimeException("有同步任务正在处理");
        }
        partService.syncPart();
        return R.ok();
    }
 
    //@InitBinder
    //protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    //    //System.out.println(request.getParameter("date"));
    //    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    //    CustomDateEditor editor = new CustomDateEditor(df, true);//true表示允许为空,false反之
    //    binder.registerCustomEditor(Date.class, editor);
    //}
 
    @ApiOperation(value = "同步Ifs", notes = "同步Ifs")
    @PostMapping("/syncNewIfs")
    //@XxlJob("syncPart")
    public R syncNewIfs(String date) {
        String key = "syncPart_lock"+ SecurityUtils.getUser().getId();
        if (redisTemplate.hasKey(key)) {
            throw new RuntimeException("有同步任务正在处理");
        }
        return R.ok(partService.syncNewIfs(date));
    }
    /**
     * 分页查询已关联检测模板的零件
     *
     * @param page 分页对象
     * @param part 零件
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getQualityPartList/page")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getQualityPartPage(Page page, Part part) {
        return R.ok(partService.getQualityPartPage(page, QueryWrapperUtil.gen(part).orderByDesc("create_time")));
    }
 
    /**
     * 根据零件号和零件描述查询库存信息接口
     *
     * @param  零件
     * @return
     */
    @ApiOperation(value = "根据零件号和零件描述查询库存信息接口", notes = "根据零件号和零件描述查询库存信息接口")
    @GetMapping("/getPart/page")
    public R getQualityPartPage(Part inAttr) {
        return R.ok(partService.getPart(inAttr));
    }
 
    /**
     * 分页查询未关联检测模板的零件
     *
     * @param page 分页对象
     * @param part 零件
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getNotInQualityPartList/page")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getNotInQualityPartPage(Page page, Part part) {
        return R.ok(partService.getNotInQualityPartPage(page, QueryWrapperUtil.gen(part).orderByDesc("create_time")));
    }
 
    /**
     * 分页查询添加来源信息的零件
     *
     * @param page 分页对象
     * @param part 零件
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getReportPartsPage/page")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getReportPartsPage(Page page, Part part) {
        return R.ok(partService.getReportPartsPage(page, QueryWrapperUtil.gen(part).orderByDesc("create_time")));
    }
 
    /**
     * 通过id查询零件
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(partService.getById(id));
    }
 
    /**
     * excel上传
     *
     * @return
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), PartData.class, new PartUploadListener(partService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }
 
    /**
     * 新增零件
     *
     * @param part 零件
     * @return R
     */
    @ApiOperation(value = "新增零件", notes = "新增零件")
    @SysLog("新增零件")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_part_add')")
    public R save(@RequestBody Part part) {
        return R.ok(partService.doSave(part));
    }
 
    /**
     * 修改零件
     *
     * @param part 零件
     * @return R
     */
    @ApiOperation(value = "修改零件", notes = "修改零件")
    @SysLog("修改零件")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_part_edit')")
    public R updateById(@RequestBody Part part) {
        return R.ok(partService.doUpdate(part));
    }
 
    /**
     * 通过id删除零件
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除零件", notes = "通过id删除零件")
    @SysLog("通过id删除零件")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_part_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(partService.removeById(id));
    }
 
 
    /**
     * 关键字查询
     *
     * @param keyword
     * @return
     */
    @ApiOperation(value = "关键字查询", notes = "关键字查询")
    @GetMapping("/query/{keyword}")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R query(@PathVariable("keyword") String keyword) {
        return R.ok(partService.query(keyword));
    }
 
    /**
     * 提供给pda的接口,根据零件号查询 cxf 201021
     *
     * @param partNo
     * @return
     */
    @ApiOperation(value = "根据零件号查询", notes = "根据零件号查询")
    @GetMapping("/pda/findByNo")
    @Inner(value = false)
    public R findByNo(String partNo) {
        return R.ok(partService.getOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, partNo)));
    }
 
    /**
     * @param index
     * @return
     */
    @GetMapping("/dict/{index}")
    public R<List<SysDictItem>> dir(@PathVariable("index") String index) {
        return remoteDictService.getDictByType(index);
    }
 
    /**
     * 同步ifs 数据
     *
     * @param ids
     * @return
     */
    @PostMapping("/ifsSync")
    public R ifsSync(@RequestBody List<Long> ids) {
        return partService.ifsSync(ids);
    }
 
    /**
     * 接收主数据平台推送零件数据
     */
    @PostMapping(value = "/mainSync")
    @Inner(false)
    public R parySync(String params) {
        return partService.parySync(JSONObject.parseObject(params));
    }
 
    /**
     * 获取工艺路线零件
     * @param page  分页对象
     * @param part  零件
     * @param bomId bomId
     * @return
     */
    @GetMapping("/getRoutingPartPage")
    @PreAuthorize("@pms.hasPermission('basic_part_view')")
    public R getRoutingPartPage(Page page, Part part, Long bomId) {
        return R.ok(partService.getRoutingPartPage(page, QueryWrapperUtil.gen(part), bomId));
    }
 
    @PostMapping("/uploadExcel")
    public R uploadExcel(MultipartFile file) {
        String key = "syncPart_lock" + SecurityUtils.getUser().getId();
        if (redisTemplate.hasKey(key)) {
            throw new RuntimeException("有导入任务正在处理");
        }
        partService.asyncUploadExcel(file);
        return R.ok();
    }
 
    /**
     * 设置Csv读文件配置
     * @return CsvWriteConfig
     */
    public static CsvReadConfig setCsvReadConfig(){
        CsvReadConfig csvReadConfig = new CsvReadConfig();
        // 设置 文本分隔符,文本包装符,默认双引号'"'
        //csvReadConfig.setTextDelimiter('\t');
        // 字段分割符号,默认为逗号
        csvReadConfig.setFieldSeparator('|');
        // 设置注释符号
        // csvReadConfig.setCommentCharacter('#');
        // CSV文件是否包含表头(因为表头不是数据内容)
        csvReadConfig.setContainsHeader(true);
        // 或者使用如下配置设置表头开始行号,-1L代表无表头
        // csvReadConfig.setHeaderLineNo(1L);
        //设置开始的行(包括),默认0,此处为原始文件行号
        // csvReadConfig.setBeginLineNo(0);
        // 是否跳过空白行,默认为true
        // csvReadConfig.setSkipEmptyRows(true);
        // 设置每行字段个数不同时是否抛出异常,默认false
        // csvReadConfig.setErrorOnDifferentFieldCount(false);
        return csvReadConfig;
    }
 
}