#
luxiaotao1123
2021-08-08 16df414db25bf21058077e37ad3bc28bba1cbb10
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
package zy.cloud.wms.common.service.erp;
 
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zy.cloud.wms.common.model.BillDto;
import zy.cloud.wms.common.service.erp.entity.UploadBill;
import zy.cloud.wms.common.service.erp.entity.UploadBillDetail;
import zy.cloud.wms.manager.entity.DocLog;
import zy.cloud.wms.manager.entity.DocType;
import zy.cloud.wms.manager.entity.Mat;
import zy.cloud.wms.manager.entity.RequestLog;
import zy.cloud.wms.manager.service.DocLogService;
import zy.cloud.wms.manager.service.DocTypeService;
import zy.cloud.wms.manager.service.MatService;
import zy.cloud.wms.manager.service.RequestLogService;
import zy.cloud.wms.manager.utils.HttpHandler;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * Created by vincent on 2021/3/23
 */
@Slf4j
@Service("erpService")
public class ErpService {
 
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private MatService matService;
    @Autowired
    private RequestLogService requestLogService;
    @Autowired
    private DocLogService docLogService;
 
    /**
     * 单据上报
     */
    public ErpR uploadBill(List<BillDto> dtos, Integer docId, String docNumber){
        try {
            Date now = new Date();
            if (Cools.isEmpty(dtos)) {
                return new ErpR(false, null);
            }
            DocType docType = docTypeService.selectById(docId);
            if (Cools.isEmpty(docType)) {
                return new ErpR(false, null);
            }
 
            // 日志
            List<DocLog> docLogs = new ArrayList<>();
 
            UploadBill uploadBill = new UploadBill();
            uploadBill.setNumber(docNumber);
            uploadBill.setBillDate(DateUtils.convert(new Date(), DateUtils.yyyyMMdd_F));
            uploadBill.setBTypeID("SHHT");
            uploadBill.setKTypeID("宏挺仓库");
            uploadBill.setVchType(docId);
            if (docId == 9) {
                uploadBill.setDifAtype(22);
            } else if (docId == 14) {
                uploadBill.setDifAtype(23);
            }
            uploadBill.setSummary(docType.getDocName() + " - " + DateUtils.convert(new Date(), "yyyy-MM-dd HH:mm"));
            List<UploadBillDetail> detail = new ArrayList<>();
            uploadBill.setDetail(detail);
            for (BillDto dto : dtos) {
                Mat mat = matService.selectByMatnr(dto.getMatnr());
                UploadBillDetail detl = new UploadBillDetail();
                detl.setUserCode(dto.getMatnr());
                detl.setQty(dto.getQty());
                detl.setPrice(0);
                detl.setUnit(mat==null?"暂无":mat.getUnit());
                detail.add(detl);
 
                // 日志
                docLogs.add(new DocLog(
                        docNumber,    // 单据编号
                        docId.longValue(),    // 单据类型
                        null,    // 单据名称
                        dto.getMatnr(),    // 商品编号
                        dto.getQty(),    // 上报数量
                        1,    // 结果
                        now,    // 上报时间
                        null    // 备注
                ));
            }
            log.warn(JSON.toJSONString(uploadBill));
            String response = new HttpHandler.Builder()
                    .setUri(ErpScheduler.URI)
                    .setPath(ErpScheduler.UPLOAD_BILL)
                    .setJson(JSON.toJSONString(uploadBill))
                    .build()
                    .doPost();
 
            try {
                docLogService.insertBatch(docLogs);
 
                // 日志记录
                RequestLog logInfo = new RequestLog();
                logInfo.setName("单据上传,单据类型" + docId);
                logInfo.setRequest(JSON.toJSONString(uploadBill)); // 入参
                logInfo.setResponse(response); // 出参
                logInfo.setCreateTime(new Date());
                requestLogService.insert(logInfo);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
 
 
            if (!Cools.isEmpty(response)) {
                log.warn(response);
                Result result = JSON.parseObject(response, Result.class);
                if (result.getCode() != 1) {
                    return new ErpR(false, result.getMsg());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErpR(false, e.getMessage());
        }
        return new ErpR(true, null);
    }
 
 
}