skyouc
2025-04-14 af455bd20edfd02cb3e8c7710e65aee798720dca
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
package com.vincent.rsf.server.manager.service.impl;
 
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.entity.dto.PoItemsDto;
import com.vincent.rsf.server.api.service.ReportMsgService;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.common.utils.CommonUtil;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.common.utils.FieldsUtils;
import com.vincent.rsf.server.manager.controller.params.BatchUpdateParam;
import com.vincent.rsf.server.manager.entity.AsnOrder;
import com.vincent.rsf.server.manager.entity.excel.AsnOrderTemplate;
import com.vincent.rsf.server.manager.mapper.AsnOrderItemMapper;
import com.vincent.rsf.server.manager.entity.AsnOrderItem;
import com.vincent.rsf.server.manager.mapper.AsnOrderMapper;
import com.vincent.rsf.server.manager.mapper.PurchaseMapper;
import com.vincent.rsf.server.manager.service.AsnOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.manager.service.PurchaseService;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.service.FieldsItemService;
import com.vincent.rsf.server.system.service.FieldsService;
import com.vincent.rsf.server.system.utils.ExtendFieldsUtils;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Service("asnOrderItemService")
public class AsnOrderItemServiceImpl extends ServiceImpl<AsnOrderItemMapper, AsnOrderItem> implements AsnOrderItemService {
 
    @Resource
    private AsnOrderMapper asnOrderMapper;
 
    @Autowired
    private FieldsService fieldsService;
 
    @Autowired
    private FieldsItemService fieldsItemService;
 
    @Override
    public R generateBarcode(List<Long> orders) {
        if (Objects.isNull(orders) || orders.isEmpty()) {
            throw new CoolException("参数不能为空!!");
        }
        List<AsnOrderItem> items = this.list(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getId, orders));
        if (items.isEmpty()) {
            throw new CoolException("单据明细不存在!!");
        }
        items.forEach(item -> {
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_LABEL_CODE, item);
            if (Objects.isNull(ruleCode) || StringUtils.isBlank(ruleCode)) {
                throw new CoolException("系统错误,请检查「SYS_LABEL_CODE」标签规则是否存在");
            }
            item.setTrackCode(ruleCode);
        });
        if (!this.saveOrUpdateBatch(items)) {
            throw new CoolException("生成条码失败!!");
        }
 
        return R.ok();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R excelImport(MultipartFile file, HashMap<String, Object> hashMap) throws Exception {
        ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), AsnOrderTemplate.class, ExcelUtil.getDefaultImportParams());
        if (result.getList().isEmpty()) {
            throw new CoolException("物料导入失败!!");
        }
        if (!Objects.isNull(hashMap.get("asnId"))) {
            throw new CoolException("主单ID为空,无法操作!!");
        }
        AsnOrder order = asnOrderMapper.selectOne(new LambdaQueryWrapper<AsnOrder>().eq(AsnOrder::getId, hashMap.get("asnId")));
        if (Objects.isNull(order)) {
            throw new CoolException("ASN单据不存在!!");
        }
        List<AsnOrderItem> itemList = new ArrayList<>();
        result.getList().forEach(template -> {
            AsnOrderItem orderItem = new AsnOrderItem();
            BeanUtils.copyProperties(template, orderItem);
            orderItem.setAsnId(order.getId())
                    .setAsnCode(order.getCode());
            itemList.add(orderItem);
        });
        if (!this.saveBatch(itemList)) {
            throw new CoolException("保存失败!!");
        }
        return R.ok("操作成功!!");
    }
 
    /**
     * @author Ryan
     * @description ASN明细单据保存,及扩展字段保存
     * @param
     * @return
     * @time 2025/4/7 09:59
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fieldsSave(Map<String, Object> params){
        AsnOrderItem asnOrderItem = JSONObject.parseObject(JSONObject.toJSONString(params), AsnOrderItem.class);
        if (StringUtils.isBlank(asnOrderItem.getTrackCode())) {
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_LABEL_CODE, asnOrderItem);
            asnOrderItem.setTrackCode(ruleCode).setBarcode(ruleCode);;
        }
        if (Objects.isNull(asnOrderItem.getAnfme()) || Double.compare(asnOrderItem.getAnfme(), 0.0) <= 0) {
            throw new CoolException("计划收货数不能为空!!");
        }
        //保存扩展字段
        try {
            String uuid16 = CommonUtil.randomUUID16();
            Boolean fields = FieldsUtils.saveFields(params, uuid16);
            if (fields) {
                asnOrderItem.setFieldsIndex(uuid16);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!this.saveOrUpdate(asnOrderItem)) {
            throw new CoolException("收货通知单明细保存失败!!");
        }
        return true;
    }
 
    @Override
    public IPage<Map<String, Object>> listByAsnId(PageParam<AsnOrderItem, BaseParam> pageParam, QueryWrapper<AsnOrderItem> buildWrapper) {
        IPage<Map<String, Object>> hsahMap = this.baseMapper.resultForMap(pageParam, buildWrapper);
        if (hsahMap.getRecords().isEmpty()) {
            return hsahMap.setRecords(new ArrayList<>());
        }
        hsahMap.setRecords(FieldsUtils.getExtendFields(hsahMap.getRecords()));
        return hsahMap;
 
    }
 
}