ERPServiceImpl.java 11.4 KB
package com.huaheng.api.erp.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.huaheng.api.erp.domain.jind.ERPLoginResult;
import com.huaheng.api.erp.domain.jind.ErpLogin;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.system.config.domain.Config;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @author tongzonghao
 *
 */
@Service
public class ERPServiceImpl implements ERPService {

    @Resource
    private AddressService addressService;
    @Resource
    private ConfigService configService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;

    @Override
    public AjaxResult innerLoginERP() {
        ErpLogin erpLogin = new ErpLogin();
        erpLogin.setAcctID(QuantityConstant.ERP_AcctID);
        erpLogin.setAppID(QuantityConstant.ERP_AppID);
        erpLogin.setAppSec(QuantityConstant.ERP_AppSec);
        erpLogin.setUserName(QuantityConstant.ERP_UserName);
        erpLogin.setPassword(QuantityConstant.ERP_PASSWORD);
        erpLogin.setLCID(QuantityConstant.ERP_LCID);
        String url = addressService.selectAddress(QuantityConstant.ERP_K3CLOUD_LOGIN,
                QuantityConstant.DEFAULT_WAREHOUSE, "");
        String jsonParam = JSON.toJSONString(erpLogin);
        String result = OkHttpUtils.bodypost(url, jsonParam);
        ERPLoginResult erpLoginResult = JSON.parseObject(result, ERPLoginResult.class);
        String sessionId = erpLoginResult.getKDSVCSessionId();
        Config config1 = new Config();
        config1.setConfigKey(QuantityConstant.ERP_SESSION_ID);
        Config config = configService.selectConfigList(config1).get(0);
        config.setConfigValue(sessionId);
        int ret = configService.updateConfig(config);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult receiptMethod(int receiptHeaderId) {
        ReceiptHeader header = receiptHeaderService.getById(receiptHeaderId);
        return receiptMethod(header);
    }


    /**
     * 回传入库单数据
     * @param receiptHeader 任务头
     */
    @Transactional
    public AjaxResult receiptMethod(ReceiptHeader receiptHeader){
//
//        innerLoginERP();
//        ApiLog log = null;
//        String jsonParam = null;
//        SaveResult spInStock = null;
//        HttpHeaders headers = new HttpHeaders();
//        Map<String, Object> receipMap = new HashMap<String, Object>();
//        receipMap.put("Numbers", "CS0001");
//        jsonParam=JSON.toJSONString(receipMap);
//
//
//        log = ApiLogAspect.initApiLog("post", QuantityConstant.ERP_ServerUrl+QuantityConstant.STK_MISCELLANEOUS,
//                jsonParam, headers, QuantityConstant.DEFAULT_WAREHOUSE);
//        String okResult = OkHttpUtils.bodypost("http://117.158.214.140:5580/k3cloud/", jsonParam, "63f9ba5be6e724");
//        spInStock = JSON.parseObject(okResult, SaveResult.class);
//
//
////        if(receiptHeader==null){
////            return AjaxResult.error("入库单为空");
////        }
////        String msg = "WMS请求发送成功,接收返回内容是空的";
////        if(receiptHeader.getFirstStatus() == 800 &&
////                receiptHeader.getLastStatus() == 800){
////            //入库数据
////            Object receiptData;
////            //回传标识
////            String formId = "";
////            //获取回传入库信息
////            receiptData = param(receiptHeader,receiptHeader.getReceiptType());
////            ExtSaveParam saveParam = new ExtSaveParam<>(receiptData);
////
////            //返回结果
////            SaveResult spInStock = null;
////            Gson gson = new Gson();
////            ApiLog log = null;
////            String body = null;
////            HttpHeaders headers = new HttpHeaders();
////
////            try {
////                    String url = addressService.selectAddress(QuantityConstant.ERP_K3CLOUD_SAVE,
////                            QuantityConstant.DEFAULT_WAREHOUSE, "");
////                    String sessionId = configService.getKey(QuantityConstant.ERP_SESSION_ID);
////                    if(StringUtils.isEmpty(sessionId)){
////                        return AjaxResult.error("sessionId不能为空");
////                    }
////
////                    formId = QuantityConstant.STK_MISCELLANEOUS;
////                    Object[] objects = new Object[]{formId, saveParam.toJson()};
////                    ErpSaveObject erpSaveObject = new ErpSaveObject();
////                    erpSaveObject.setParameters(objects);
////                    body = gson.toJson(erpSaveObject);
////
////
////                    log = ApiLogAspect.initApiLog("post", QuantityConstant.ERP_ServerUrl+QuantityConstant.STK_MISCELLANEOUS,
////                            body, headers, QuantityConstant.DEFAULT_WAREHOUSE);
////                    String okResult = OkHttpUtils.bodypost(url, body, sessionId);
////                    spInStock = JSON.parseObject(okResult, SaveResult.class);
////
////
////                if (spInStock.getResult().getResponseStatus().isIsSuccess()) {
////                    SuccessEntity success = spInStock.getResult().
////                            getResponseStatus().getSuccessEntitys().get(0);
////                    Integer referId = Integer.valueOf(success.getId());
////                    String referCode = success.getNumber();
////                    //更新 入库单明细状态
////                    LambdaQueryWrapper<ReceiptDetail> queryWrapper = Wrappers.lambdaQuery();
////                    queryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
////                    List<ReceiptDetail> receiptDetails = receiptDetailService.list(queryWrapper);
////                    for (ReceiptDetail receiptDetail : receiptDetails) {
////                        receiptDetail.setReferId(referId);
////                        receiptDetail.setReferCode(referCode);
////                        receiptDetail.setQty(receiptDetail.getTaskQty());
////                        receiptDetailService.updateById(receiptDetail);
////                    }
////                    receiptHeader.setReferId(referId);
////                    receiptHeader.setReferCode(referCode);
////                    receiptHeaderService.updateById(receiptHeader);
////                    //修改回传后状态
////                    editReceiveStatus(receiptHeader.getId());
////                    receiptHeader = receiptHeaderService.getById(receiptHeader.getId());
////                    receiptDetailService.updateReceiptHeader(receiptHeader);
////                    //更新 入库单明细状态
////                    msg = "回传成功";
////                    return AjaxResult.success("回传成功");
////                } else {
////                    ArrayList<RepoError> errors = spInStock.getResult().getResponseStatus().getErrors();
////                    String errorStr = "";
////                    for (RepoError error : errors) {
////                        errorStr = error.getFieldName() + "==" + error.getMessage() + "==";
////                    }
////                    msg = errorStr;
////                    return AjaxResult.error(errorStr);
////                }
////            }catch (Exception e) {
////                e.printStackTrace();
////                ApiLogAspect.setApiLogException(log, e);
////            } finally {
////                ApiLogAspect.finishApiLog(log, headers, msg);
////            }
////        }
        return AjaxResult.success("回传成功");
    }

    /**
     * 判断入库单类型返回 回传入库信息
     * @param receiptHeader 入库头
     * @param billType 单据类型
     * @return 回传入库信息
     */
    private Object param(ReceiptHeader receiptHeader,String billType){
//        Object returnReceipt;
//        switch (billType) {
//            //简单生产入库单
//            case QuantityConstant.RECEIPT_BILL_TYPE_SC:
//                returnReceipt = buildSingleReceiptHead(receiptHeader);
//                break;
//            //其他生产入库单
//            case QuantityConstant.RECEIPT_BILL_TYPE_OR:
//                returnReceipt = buildOtherReceiptHead(receiptHeader);
//                break;
//            default:
//                throw new ServiceException("不支持的任务类型");
//        }
//        //获取入库单数据
//        LambdaQueryWrapper<ReceiptDetail> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(ReceiptDetail::getReceiptId,receiptHeader.getId());
//        //入库明细集合
//        List<ReceiptDetail> details = receiptDetailService.list(queryWrapper);
//
//        //回传入库明细集
//        List<ReceiptBillEntity> receiptInfos = getReceiptEntities(details);
//
//        //设置回传入库明细集
//        Class<?> receiptCls = returnReceipt.getClass();
//
//        if(OtherReceipt.class.isAssignableFrom(receiptCls)){
//            OtherReceipt otherReceipt = (OtherReceipt)returnReceipt;
//            otherReceipt.setEntity(receiptInfos);
//            //重新赋值给 returnReceipt
//            returnReceipt = otherReceipt;
//        }else if(SingleReceipt.class.isAssignableFrom(receiptCls)){
//            SingleReceipt singleReceipt = (SingleReceipt)returnReceipt;
//            singleReceipt.setEntity(receiptInfos);
//            //重新赋值给 returnReceipt
//            returnReceipt = singleReceipt;
//        }
//        return returnReceipt;

        return null;
    }


    private AjaxResult editReceiveStatus(Integer id) {
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setId(id);
        receiptHeader.setFirstStatus(900);
        receiptHeader.setLastStatus(900);
        if (!receiptHeaderService.updateById(receiptHeader)) {
            throw new ServiceException("更新头表状态失败");
        } else {
            LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
            List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambdaQueryWrapper);
            for (ReceiptDetail receiptDetail : receiptDetails) {
                receiptDetail.setProcessStamp("900");
            }
            if (!receiptDetailService.updateBatchById(receiptDetails)) {
                throw new ServiceException("更新明细失败");
            }
        }
        return AjaxResult.success("");
    }
}