liujie
8 天以前 850042f2e7921d4034363da4bf0276ae1d6aef05
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TErpGoodsAddDto;
import com.ruoyi.system.dto.TErpGoodsInfoSaveDto;
import com.ruoyi.system.dto.TErpGoodsUpdateStatusDto;
import com.ruoyi.system.importExcel.TErpGoodsExcel;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TErpGoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * erp商品 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@RestController
@RequestMapping("/t-erp-goods")
@Api(tags = "供应商商品管理")
public class TErpGoodsController {
 
 
    private final TErpGoodsService erpGoodsService;
    private final TokenService tokenService;
    private final TErpGoodsTypeService erpGoodsTypeService;
    private final TErpGoodsUnitService erpGoodsUnitService;
    private final TErpSupplierWarehousingBatchService erpSupplierWarehousingBatchService;
    private final TErpSupplierWarehousingService erpSupplierWarehousingService;
    private final TErpSupplierOutboundGoodsService erpSupplierOutboundGoodsService;
    private final TErpMaintenanceReminderService erpMaintenanceReminderService;
 
    @Autowired
    public TErpGoodsController(TErpGoodsService erpGoodsService, TokenService tokenService, TErpGoodsTypeService erpGoodsTypeService, TErpGoodsUnitService erpGoodsUnitService,
                               TErpSupplierWarehousingBatchService erpSupplierWarehousingBatchService, TErpSupplierWarehousingService erpSupplierWarehousingService,
                               TErpSupplierOutboundGoodsService erpSupplierOutboundGoodsService, TErpMaintenanceReminderService erpMaintenanceReminderService) {
        this.erpGoodsService = erpGoodsService;
        this.tokenService = tokenService;
        this.erpGoodsTypeService = erpGoodsTypeService;
        this.erpGoodsUnitService = erpGoodsUnitService;
        this.erpSupplierWarehousingBatchService = erpSupplierWarehousingBatchService;
        this.erpSupplierWarehousingService = erpSupplierWarehousingService;
        this.erpSupplierOutboundGoodsService = erpSupplierOutboundGoodsService;
        this.erpMaintenanceReminderService = erpMaintenanceReminderService;
    }
 
 
 
    /**
     * 获取erp问题上报管理列表
     */
    @ApiOperation(value = "供应商获取商品分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TErpGoodsVO>> pageList(@RequestBody TErpGoodsQuery query) {
        SysUser user = tokenService.getLoginUser().getUser();
        return R.ok(erpGoodsService.pageList(query,user));
    }
 
 
 
    @ApiOperation(value = "供应商添加商品")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TErpGoodsAddDto dto) {
        SysUser user = tokenService.getLoginUser().getUser();
        if (erpGoodsService.isExit(dto.getGoodsIdCode(), dto.getQuasiNumber())) {
            return R.fail("erp商品已存在");
        }
        TErpGoods goods = new TErpGoods();
        goods.setSupplierClinicId(user.getUserId().toString());
        goods.setCreateId(user.getUserId().toString());
        goods.setGoodsIdCode(dto.getGoodsIdCode());
        goods.setQuasiNumber(dto.getQuasiNumber());
        goods.setManufacturer(dto.getManufacturer());
        goods.setFormulationSpec(dto.getFormulationSpec());
        goods.setPackingSpec(dto.getPackingSpec());
        goods.setGoodsName(dto.getGoodsName());
        return R.ok(erpGoodsService.save(goods));
    }
 
    @ApiOperation(value = "供应商删除商品")
    @DeleteMapping(value = "/delete/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        return R.ok(erpGoodsService.removeById(id));
    }
 
 
    /**
     * 启用 停用
     */
    @ApiOperation(value = "供应商启用 停用商品")
    @PostMapping(value = "/updateStatus")
    public R<Boolean> updateStatus(@RequestBody TErpGoodsUpdateStatusDto dto) {
        TErpGoods goods = erpGoodsService.getById(dto.getId());
        if(dto.getState()==1 && goods.getTypeId()==null){
            return R.fail("操作失败,请先完善商品信息");
        }
        if(dto.getState()==1 && goods.getClinicPurchasePrice()==null){
            return R.fail("操作失败,请设置供应商分佣比例后再启用");
        }
        goods.setState(dto.getState());
        boolean b = erpGoodsService.updateById(goods);
        return R.ok(b);
    }
 
    @ApiOperation(value = "供应商完善商品")
    @PostMapping(value = "/saveDataInfo")
    public R<Boolean> saveDataInfo(@RequestBody @Valid TErpGoodsInfoSaveDto dto) {
        SysUser user = tokenService.getLoginUser().getUser();
        TErpGoods goods = erpGoodsService.getById(dto.getId());
        BeanUtils.copyProperties(dto,goods);
        if(goods.getMaintenanceInterval()!=null){
 
            List<TErpSupplierWarehousing> list = erpSupplierWarehousingService.list(new LambdaQueryWrapper<TErpSupplierWarehousing>().eq(TErpSupplierWarehousing::getGoodsId, dto.getId()));
            if(!list.isEmpty()){
                List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchService.list(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().in(TErpSupplierWarehousingBatch::getWarehousingId, list.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
                for (TErpSupplierWarehousingBatch tErpSupplierWarehousingBatch : tErpSupplierWarehousingBatches) {
                    List<TErpSupplierOutboundGoods> list1 = erpSupplierOutboundGoodsService.list(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().eq(TErpSupplierOutboundGoods::getWarehousingBatchId, tErpSupplierWarehousingBatch.getId()));
                    if(!list1.isEmpty()){
                        int sum = list1.stream().mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
                        if (tErpSupplierWarehousingBatch.getWarehousingNumber()-sum>0) {
                            // 生成保养单
                            TErpMaintenanceReminder tErpMaintenanceReminder = new TErpMaintenanceReminder();
                            tErpMaintenanceReminder.setClinicSupplierId(user.getUserId().toString());
                            tErpMaintenanceReminder.setMaintenanceType(1);
                            tErpMaintenanceReminder.setWarehousingBatchId(tErpSupplierWarehousingBatch.getId());
                            Integer day = Integer.valueOf(goods.getMaintenanceInterval());
                            // 当前时间+day天
                            LocalDateTime time1 = LocalDateTime.now().plusDays(day);
                            tErpMaintenanceReminder.setMaintenanceTime(time1);
                            erpMaintenanceReminderService.save(tErpMaintenanceReminder);
                        }
                    }
 
                }
 
            }
 
        }
 
 
        return R.ok(erpGoodsService.updateById(goods));
    }
 
    @ApiOperation(value = "供应商商品详情")
    @GetMapping(value = "/detail/{id}")
    public R<TErpGoods> detail( @PathVariable String  id) {
        TErpGoods goods = erpGoodsService.getById(id);
        return R.ok(goods);
    }
 
    @ApiOperation(value = "商品管理信息导出--导出")
    @PostMapping("/export")
    public void listExport(@RequestBody TErpGoodsQuery query)
    {
        SysUser user = tokenService.getLoginUser().getUser();
        List<TErpGoodsVO> pageList = erpGoodsService.listExport(query,user);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TErpGoodsVO.class, pageList);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("商品管理信息导出.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
 
    @ApiOperation(value = "商品管理信息导入")
    @PostMapping("/importFile")
    public AjaxResult importFile(@RequestParam(value = "file") MultipartFile file ) {
 
        SysUser user = tokenService.getLoginUser().getUser();
        JSONObject jsonObject = new JSONObject();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        ArrayList<String> names = new ArrayList<>();
        ArrayList<TErpGoods> tErpGoods = new ArrayList<>();
 
        try {
            inputStream = file.getInputStream();
            List<TErpGoodsExcel> carAnnuallyImportExcels = ExcelImportUtil.importExcel(inputStream, TErpGoodsExcel.class, params);
            Set<String> collect = carAnnuallyImportExcels.stream().map(TErpGoodsExcel::getGoodsIdCode).collect(Collectors.toSet());
            if (collect.size() != carAnnuallyImportExcels.size()) {
                return AjaxResult.error("导入数据中商品标识码重复");
            }
            for (TErpGoodsExcel erpGoodsExcel : carAnnuallyImportExcels) {
                TErpGoods goods = new TErpGoods();
                goods.setSupplierClinicId(user.getUserId().toString());
                goods.setGoodsSource(1);
 
                // 判空
                if(erpGoodsExcel.getGoodsName()==null || erpGoodsExcel.getGoodsName().isEmpty()){
                    names.add("商品名称不能为空");
                    continue;
                }
                if(erpGoodsExcel.getGoodsIdCode()==null || erpGoodsExcel.getGoodsIdCode().isEmpty()
                || erpGoodsExcel.getQuasiNumber() ==null || erpGoodsExcel.getQuasiNumber().isEmpty()
                || erpGoodsExcel.getManufacturer()==null || erpGoodsExcel.getManufacturer().isEmpty()
                || erpGoodsExcel.getFormulationSpec()==null || erpGoodsExcel.getFormulationSpec().isEmpty()
                || erpGoodsExcel.getPackingSpec()==null || erpGoodsExcel.getPackingSpec().isEmpty()
                || erpGoodsExcel.getClinicPurchasePrice()==null|| erpGoodsExcel.getClinicPurchasePrice().isEmpty()
                || erpGoodsExcel.getWarningInventory()==null|| erpGoodsExcel.getWarningInventory().isEmpty()
                || erpGoodsExcel.getTypeName()==null|| erpGoodsExcel.getTypeName().isEmpty()){
                    names.add(erpGoodsExcel.getGoodsName()+" :信息有误,请检查后重新提交。");
                    continue;
                }
                goods.setGoodsName(erpGoodsExcel.getGoodsName());
                // 判断数据是否正确
                // 判断标识码是否重复
                long count = erpGoodsService.count(new QueryWrapper<TErpGoods>().eq("goods_id_code", erpGoodsExcel.getGoodsIdCode()));
                if(count>0){
                    names.add(erpGoodsExcel.getGoodsName() + " :标识码重复");
                    continue;
                }
                goods.setGoodsIdCode(erpGoodsExcel.getGoodsIdCode());
                goods.setQuasiNumber(erpGoodsExcel.getQuasiNumber());
                goods.setManufacturer(erpGoodsExcel.getManufacturer());
                goods.setFormulationSpec(erpGoodsExcel.getFormulationSpec());
                goods.setPackingSpec(erpGoodsExcel.getPackingSpec());
 
                // 商品类型
                TErpGoodsType one = erpGoodsTypeService.getOne(new LambdaQueryWrapper<TErpGoodsType>().eq(TErpGoodsType::getTypeName, erpGoodsExcel.getTypeName()));
                if(one==null){
                    names.add(erpGoodsExcel.getGoodsName() + " :商品类型不存在");
                    continue;
                }
                goods.setTypeId(one.getId());
 
                // 采购价
                try {
                    goods.setClinicPurchasePrice(new BigDecimal(erpGoodsExcel.getClinicPurchasePrice()).setScale(2, RoundingMode.HALF_UP));
                }catch (Exception e){
                    names.add(erpGoodsExcel.getGoodsName() + " :采购价有误");
                    continue;
                }
                // 预警库存
                try {
                    goods.setWarningInventory(Integer.valueOf(erpGoodsExcel.getWarningInventory()));
                }catch (Exception e){
                    names.add(erpGoodsExcel.getGoodsName() + " :预警库存有误");
                    continue;
                }
                goods.setGoodsYards(erpGoodsExcel.getGoodsYards());
                try {
                    if(erpGoodsExcel.getMaintenanceInterval()!=null && !erpGoodsExcel.getMaintenanceInterval().isEmpty()){
                        Integer i = Integer.valueOf(erpGoodsExcel.getMaintenanceInterval());
                    }
                }catch (Exception e){
                    names.add(erpGoodsExcel.getGoodsName() + " :保养周期有误");
                    continue;
                }
                goods.setMaintenanceInterval(erpGoodsExcel.getMaintenanceInterval());
 
                try {
                    if(erpGoodsExcel.getLowPurchaseQuantity()!=null && !erpGoodsExcel.getLowPurchaseQuantity().isEmpty()){
                        goods.setLowPurchaseQuantity(Integer.valueOf(erpGoodsExcel.getLowPurchaseQuantity()));
                    }
                }catch (Exception e){
                    names.add(erpGoodsExcel.getGoodsName() + " :最低采购数量有误");
                    continue;
                }
 
                if(erpGoodsExcel.getLowUnitName()!=null && !erpGoodsExcel.getLowUnitName().isEmpty()){
                    TErpGoodsUnit one1 = erpGoodsUnitService.getOne(new LambdaQueryWrapper<TErpGoodsUnit>().eq(TErpGoodsUnit::getUnitName, erpGoodsExcel.getLowUnitName()));
                    if (one1 == null) {
                        names.add(erpGoodsExcel.getGoodsName() + " :最低采购单位有误");
                        continue;
                    }
                    goods.setLowUnitId(one1.getId());
                }
 
 
                if(erpGoodsExcel.getIsPrescriptionDrug() !=null && !erpGoodsExcel.getIsPrescriptionDrug().isEmpty()){
                    if(erpGoodsExcel.getIsPrescriptionDrug().equals("是")){
                        goods.setIsPrescriptionDrug(1);
                    }else if (erpGoodsExcel.getIsPrescriptionDrug().equals("否")){
                        goods.setIsPrescriptionDrug(2);
                    }else {
                        names.add(erpGoodsExcel.getGoodsName() + " :是否为处方药有误");
                        continue;
                    }
                }
 
                if(erpGoodsExcel.getPackingUnitName() !=null && !erpGoodsExcel.getPackingUnitName().isEmpty()){
                    TErpGoodsUnit unit = erpGoodsUnitService.getOne(new LambdaQueryWrapper<TErpGoodsUnit>().eq(TErpGoodsUnit::getUnitName, erpGoodsExcel.getPackingUnitName()));
                    if(unit != null){
                        goods.setPackingUnitId(unit.getId());
                    }else {
                        names.add(erpGoodsExcel.getGoodsName() + " :包装单位有误");
                        continue;
                    }
                }
 
                goods.setInstructionsUse(erpGoodsExcel.getInstructionsUse());
                goods.setSideEffect(erpGoodsExcel.getSideEffect());
                goods.setSimplifiedCode(erpGoodsExcel.getSimplifiedCode());
                goods.setDosageForm(erpGoodsExcel.getDosageForm());
                goods.setUsageClassification(erpGoodsExcel.getUsageClassification());
                goods.setProductBrand(erpGoodsExcel.getProductBrand());
                goods.setPlaceOfOrigin(erpGoodsExcel.getPlaceOfOrigin());
                goods.setIngredient(erpGoodsExcel.getIngredient());
                goods.setTrait(erpGoodsExcel.getTrait());
                goods.setIndication(erpGoodsExcel.getIndication());
                goods.setTaboo(erpGoodsExcel.getTaboo());
                goods.setPrecautions(erpGoodsExcel.getPrecautions());
                goods.setInteraction(erpGoodsExcel.getInteraction());
                goods.setStorage(erpGoodsExcel.getStorage());
                tErpGoods.add( goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("记录导入失败!");
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if(names.size()>0){
            return AjaxResult.error("导入失败记录如下:",names);
        }
        erpGoodsService.saveBatch(tErpGoods);
        return AjaxResult.success();
 
    }
 
 
}