package com.huigou.topsun.sap.purchaseOrder.application.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.cache.DictUtil;
import com.huigou.cache.SystemCache;
import com.huigou.context.Operator;
import com.huigou.context.ThreadLocalUtil;
import com.huigou.data.domain.model.CommonDomainConstants;
import com.huigou.data.query.model.QueryDescriptor;
import com.huigou.data.query.model.QueryModel;
import com.huigou.topsun.base.bsnMessage.appliction.MannualStartWorkApplication;
import com.huigou.topsun.common.CommonUtils;
import com.huigou.topsun.ep.epApplication.EpApplication;
import com.huigou.topsun.ep.epApplication.EpVo.EpProductInfoVo;
import com.huigou.topsun.sap.common.DefaultHttpClient;
import com.huigou.topsun.sap.permit.application.SapPermitApplication;
import com.huigou.topsun.sap.permit.vo.SapPermitItemVo;
import com.huigou.topsun.sap.permit.vo.SapPermitVo;
import com.huigou.topsun.sap.processOutsource.domain.ProcessOutsourceMaterial;
import com.huigou.topsun.sap.processOutsource.domain.ProcessOutsourceOrderItem;
import com.huigou.topsun.sap.processOutsource.repository.ProcessOutsourceMaterialRepository;
import com.huigou.topsun.sap.processOutsource.repository.ProcessOutsourceOrderItemRepository;
import com.huigou.topsun.sap.purchaseApproval.application.impl.SapPurchaseApprovalApplicationImpl;
import com.huigou.topsun.sap.purchaseOrder.application.SapPurchaseOrderApplication;
import com.huigou.topsun.sap.purchaseOrder.application.SapPurchaseOrderItemApplication;
import com.huigou.topsun.sap.purchaseOrder.domain.SapPurchaseOrder;
import com.huigou.topsun.sap.purchaseOrder.domain.SapPurchaseOrderItem;
import com.huigou.topsun.sap.purchaseOrder.domain.query.SapPurchaseOrderQueryRequest;
import com.huigou.topsun.sap.purchaseOrder.domain.vo.*;
import com.huigou.topsun.sap.purchaseOrder.repository.SapPurchaseOrderRepository;
import com.huigou.topsun.sap.suppliers.domain.SuppliersInfo;
import com.huigou.topsun.sap.suppliers.domain.SuppliersPay;
import com.huigou.topsun.sap.suppliers.repository.SuppliersInfoRepository;
import com.huigou.topsun.sap.suppliers.repository.SuppliersPayRepository;
import com.huigou.uasp.bmp.common.BizBillStatus;
import com.huigou.uasp.bmp.operator.OperatorApplication;
import com.huigou.uasp.bmp.opm.application.OrgApplication;
import com.huigou.uasp.bmp.opm.domain.model.org.Org;
import com.huigou.uasp.bpm.FlowBroker;
import com.huigou.util.*;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: xin.lu
 * @Date: 2024/01/09/18:59
 * @Description:
 */
@Service("sapPurchaseOrderApplication")
public class SapPurchaseOrderApplicationImpl extends FlowBroker implements SapPurchaseOrderApplication {
    @Resource
    private SapPurchaseOrderRepository sapPurchaseOrderRepository;
    @Resource
    private SapPurchaseOrderItemApplication sapPurchaseOrderItemApplication;
    @Resource
    private OperatorApplication operatorApplication;
    @Autowired
    DefaultHttpClient defaultHttpClient;
    @Autowired
    SapPurchaseApprovalApplicationImpl sapPurchaseApprovalApplication;

    @Autowired
    private MannualStartWorkApplication mannualStartWorkApplication;
    @Autowired
    private OrgApplication orgApplication;
    @Autowired
    private SapPermitApplication sapPermitApplication;
    @Autowired
    private TaskExecutor asyncWriteExecutor;
    @Autowired
    private SuppliersInfoRepository suppliersInfoRepository;
    @Autowired
    private SuppliersPayRepository suppliersPayRepository;
    @Autowired
    private EpApplication epApplication;
    @Autowired
    private ProcessOutsourceOrderItemRepository processOutsourceOrderItemRepository;
    @Autowired
    private ProcessOutsourceMaterialRepository processOutsourceMaterialRepository;

    @Override
    protected String saveBizAndApprovalData() {
        super.saveBizAndApprovalData();
        SDO sdo = this.getBizAndApprovalData();
        SapPurchaseOrder sapPurchaseOrder = getBizEntity(SapPurchaseOrder.class);
        if (sapPurchaseOrder.isNew()) {
            sapPurchaseOrder.setStatusId(BizBillStatus.APPLYING.getId());
        } else {
            sapPurchaseOrder = (SapPurchaseOrder) commonDomainService.loadAndFillinProperties(sapPurchaseOrder);
        }
        sapPurchaseOrder = sapPurchaseOrderRepository.save(sapPurchaseOrder);
//        SapPurchaseOrderItem sapPurchaseOrderItem = getBizEntity(SapPurchaseOrderItem.class);
//        if(sapPurchaseOrderItem!=null){
//            sapPurchaseOrderItem.setPurchaseOrderId(sapPurchaseOrder.getId());
//            sapPurchaseOrderItemApplication.saveSapPurchaseOrderItem(sapPurchaseOrderItem);
//        }
        return sapPurchaseOrder.getId();
    }

    @Override
    protected void setTaskDescription(DelegateTask delegateTask) {
        String bizId = delegateTask.getExecution().getProcessBusinessKey();
        delegateTask.setDescription(this.getApprovalSubjectName(bizId));
    }

    /**
     * 获取任务标题
     *
     * @param bizId
     * @return
     */
    private String getApprovalSubjectName(String bizId) {
        SapPurchaseOrder sapPurchaseOrder = this.sapPurchaseOrderRepository.findOne(bizId);
        //查询 单据日期
        String fillinDateStr = DateUtil.getDateFormat("yyyy-MM-dd HH:mm:ss", sapPurchaseOrder.getFillinDate());
        //设置标题
        return String.format("%s-%s(%s)", sapPurchaseOrder.getBatxt(), sapPurchaseOrder.getEbeln(), fillinDateStr);
    }

    @Override
    protected Map<String, Object> getProcessBizParams(String bizId) {
        // 返回业务数据给流程实例
        return ClassHelper.toMap(sapPurchaseOrderRepository.getOne(bizId));
    }
    /**
     * 流程撤销事件
     **/
    @Override
    protected void onRecallProcessInstance(DelegateExecution delegateExecution) {
        String bizId = delegateExecution.getProcessBusinessKey();
        // 修改单据状态为申请
        updateStatus(bizId, BizBillStatus.APPLYING);
    }

    /**
     * 任务完成前执行
     */
    @Override
    protected void onBeforeComplete(DelegateTask delegateTask) {
        super.onBeforeComplete(delegateTask);
        String bizId = delegateTask.getExecution().getProcessBusinessKey();
        if (this.isApplyProcUnit(delegateTask) && this.getApprovalParameter().isAdvanceProcessAction()) {
            updateStatus(bizId, BizBillStatus.APPROVING);
        }
    }

    /**
     * 回收事件
     **/
    @Override
    protected void onWithdraw(DelegateTask delegateTask, String destActivityId) {
        super.onWithdraw(delegateTask, destActivityId);
        String bizId = delegateTask.getExecution().getProcessBusinessKey();
        if (destActivityId.equalsIgnoreCase("apply")) {
            updateStatus(bizId, BizBillStatus.APPLYING);
        }
    }

    @Transactional
    protected void updateStatus(String bizId, BizBillStatus status) {
        Assert.hasText(bizId, CommonDomainConstants.ID_NOT_BLANK);
        this.commonDomainService.updateStatus(SapPurchaseOrder.class, bizId, status.getId());
    }

    @Override
    protected void onEnd(DelegateExecution delegateExecution) {
        super.onEnd(delegateExecution);
        String bizId = delegateExecution.getProcessBusinessKey();
        BizBillStatus status = approvePassed() ? BizBillStatus.COMPLETED : BizBillStatus.ABORTED;
        SapPurchaseOrder sapPurchaseOrder = sapPurchaseOrderRepository.findOne(bizId);
        sapPurchaseOrder.setStatusId(status.getId());
        SapPurchaseOrder save = sapPurchaseOrderRepository.save(sapPurchaseOrder);
        this.sendSapDate(save, "Y");
        asyncWriteExecutor.execute(()->
                //创建放行条
                this.startSapPermit(sapPurchaseOrder)
        );
    }

    public void startSapPermit(SapPurchaseOrder purchaseOrder){
        SapPermitVo sapPermitVo = new SapPermitVo();
        sapPermitVo.setCompany(purchaseOrder.getNamel());//供应商
        sapPermitVo.setApplyPerson(purchaseOrder.getPersonMemberId());
        sapPermitVo.setApplyPersonName(purchaseOrder.getPersonMemberName());
        sapPermitVo.setDutyDepart(purchaseOrder.getDeptId());
        sapPermitVo.setDutyDepartName(purchaseOrder.getDeptName());
        sapPermitVo.setPermitDate(new Date());
        List<SapPermitItemVo> sapPermitItemVos = new ArrayList<>();
        List<SapPurchaseOrderItem> purchaseOrderItems = sapPurchaseOrderItemApplication.getPurchaseOrderItems(purchaseOrder.getId());
        for (SapPurchaseOrderItem purchaseOrderItem : purchaseOrderItems) {
            //退货项目创建放行条
            if ("X".equals(purchaseOrderItem.getRetpo())){
                SapPermitItemVo sapPermitItemVo = new SapPermitItemVo();
                sapPermitItemVo.setBelongings(purchaseOrderItem.getMatnr() + "/" + purchaseOrderItem.getTxz01());
                sapPermitItemVo.setUnit(purchaseOrderItem.getMeins());

                String remark="采购订单项:"+purchaseOrder.getEbeln()+"："+purchaseOrderItem.getEbelp();
                sapPermitItemVo.setRemark(remark);
                sapPermitItemVo.setQuantity(new BigDecimal(purchaseOrderItem.getMenge()));
                sapPermitItemVo.setUnitName(purchaseOrderItem.getMsehl());
                sapPermitItemVos.add(sapPermitItemVo);
            }
        }
        if (!CollectionUtils.isEmpty(sapPermitItemVos)){
            sapPermitVo.setItemVos(sapPermitItemVos);
            //创建放行条
            sapPermitApplication.saveSapPermitVo(sapPermitVo);
        }

    }


    @Override
    protected void onAbortProcessInstance(DelegateExecution delegateExecution) {
        super.onAbortProcessInstance(delegateExecution);
        String bizId = delegateExecution.getProcessBusinessKey();
        SapPurchaseOrder sapPurchaseOrder = sapPurchaseOrderRepository.findOne(bizId);
        sapPurchaseOrder.setStatusId(BizBillStatus.ABORTED.getId());
        sapPurchaseOrderRepository.save(sapPurchaseOrder);
    }

    /**
     * @param delegateTask
     * @param destActivityId 回退到的目标节点id
     */
    @Override
    protected void onBack(DelegateTask delegateTask, String destActivityId) {
        super.onBack(delegateTask, destActivityId);
        if ("Apply".equalsIgnoreCase(destActivityId)) {
            String bizId = delegateTask.getExecution().getProcessBusinessKey();
            SapPurchaseOrder sapPurchaseOrder = sapPurchaseOrderRepository.findOne(bizId);
            sapPurchaseOrder.setStatusId(BizBillStatus.APPLYING.getId());
            SapPurchaseOrder save = sapPurchaseOrderRepository.save(sapPurchaseOrder);
            //this.sendSapDate(save, "N");
            // 调用 onEnd 方法
//            DelegateExecution execution = delegateTask.getExecution();
//            onEnd(execution);
        }
    }

    @Override
    public Map<String, Object> slicedSapPurchaseOrderList(SapPurchaseOrderQueryRequest queryRequest) {
        QueryDescriptor queryDescriptor = this.sqlExecutorDao.getQuery(QUERY_XML_FILE_PATH, "sapPurchaseOrderList");
        QueryModel queryModel = this.sqlExecutorDao.getQueryModel(queryDescriptor, queryRequest);
        queryModel.putDictionary("status", BizBillStatus.getMap());
        if (queryRequest.getBillCode() != null) {
            queryModel.addCriteria(" and t.bill_code like:billCode");
            queryModel.putLikeParam("billCode", queryRequest.getBillCode());
        }
        if (queryRequest.getEbeln() != null) {
            queryModel.addCriteria(" and t.ebeln like:ebeln");
            queryModel.putLikeParam("ebeln", queryRequest.getEbeln());
        }
        queryModel.addCriteria(" order by fillin_date desc ");
        return this.sqlExecutorDao.executeSlicedQuery(queryModel);
    }

    @Override
    public SapPurchaseOrder loadSapPurchaseOrder(String id) {
        return sapPurchaseOrderRepository.findOne(id);
    }

    @Override
    @Transactional
    public void saveSapPurchaseOrderDto(SapPurchaseOrderDto sapPurchaseOrderDto) {
        SapPurchaseOrderVo sapPurchaseOrderVo = sapPurchaseOrderDto.getLsHeader();
        List<SapPurchaseOrder> purchaseOrders = sapPurchaseOrderRepository.findAllByEbeln(sapPurchaseOrderVo.getEbeln());
        String purchaseOrderId = "";
        //数据第一次接收，创建流程任务
            SapPurchaseOrder sapPurchaseOrder = new SapPurchaseOrder();
            BeanUtil.copyProperties(sapPurchaseOrderVo, sapPurchaseOrder);
        String ernam = sapPurchaseOrderVo.getErnam();
        Org org = orgApplication.loadMainOrgByLoginName(ernam);
        String acceptPersonCode = SystemCache.getParameter("acceptPersonCode", String.class);
        if (ObjectUtil.isNull(org)){
            org = orgApplication.loadMainOrgByLoginName(acceptPersonCode);
        }
        if (ObjectUtil.isNotNull(org)){
            Operator operator = operatorApplication.createOperatorByPersonMemberId(org.getId());
            ThreadLocalUtil.putOperator(operator);
        }else {
            //throw new RuntimeException("未找到采购订单创建人：" + ernam);
        }

        List<SapPurchaseOrderItemVo> ltItems = sapPurchaseOrderDto.getLtItems();
        List<SapPurchaseOrderItem> purchaseOrderItems = new ArrayList<>();
        String retpo = "";
        for (SapPurchaseOrderItemVo approvalItemVo : ltItems) {
            SapPurchaseOrderItem sapPurchaseApprovalItem = new SapPurchaseOrderItem();
            BeanUtil.copyProperties(approvalItemVo, sapPurchaseApprovalItem);
            purchaseOrderItems.add(sapPurchaseApprovalItem);
            if ("X".equals(approvalItemVo.getRetpo())){
                retpo = "X";
            }else {
                //审批要素不能为空，赋值E
                retpo = "E";
            }
        }
        sapPurchaseOrder.setRetpo(retpo);
            //初始化用户信息
          /*  String createdById = "5EEB2DC861694A65933CB4CE9A8D870A@C18E23AAAA6940E09DF7C122332C51E0";
            Operator operator = operatorApplication.createOperatorByPersonMemberId(createdById);
            sapPurchaseOrder.setDefaultValues(new OrgUnit(operator.getFullId(), operator.getFullName()));

            Map<String, Object> formData = BeanUtil.beanToMap(sapPurchaseOrder);
            String bizId = sapPurchaseApprovalApplication.startProcessInstance(SapPurchaseOrderApplication.PROCESS_DEFINITION_KEY, formData);
            sapPurchaseOrder.setId(bizId);
            //映射双方字段
            sapPurchaseOrder.setFillinDate(sapPurchaseOrderVo.getAedat());
            */
            //发起流程
           // String nextCode = codeGenerator.getNextCode("sapPurchaseOrder");
           // sapPurchaseOrder.setBillCode(nextCode);
           // sapPurchaseOrder =mannualStartWorkflow(sapPurchaseOrder);
          //  sapPurchaseOrder = this.sapPurchaseOrderRepository.save(sapPurchaseOrder);
          //  purchaseOrderId = sapPurchaseOrder.getId();
        if (CollectionUtils.isEmpty(purchaseOrders)) {
            //存在也新建 04.28注释的    05.11 把存在的作废掉
            purchaseOrderId=mannualStartWorkApplication.startWorkflow(sapPurchaseOrder,PROCESS_DEFINITION_KEY,"sapPurchaseOrder");
        } else {
            //将存在的作废掉   05.11 lxh
            for (SapPurchaseOrder purchaseOrder : purchaseOrders) {
                //终止状态不管
                //审批完成的，SAP撤审触发，终止审批完成的，并发起新的流程
                if (purchaseOrder.getStatusId() == BizBillStatus.COMPLETED.getId()){
                    purchaseOrder.setStatusId(BizBillStatus.ABORTED.getId());
                    purchaseOrderId=mannualStartWorkApplication.startWorkflow(sapPurchaseOrder,PROCESS_DEFINITION_KEY,"sapPurchaseOrder");
                }
                //在审批中，更新明细数据的（先删除，再新增）
                if (purchaseOrder.getStatusId() == BizBillStatus.APPROVING.getId() || purchaseOrder.getStatusId() == BizBillStatus.APPLYING.getId()){
                    List<SapPurchaseOrderItem> itemList = sapPurchaseOrderItemApplication.getPurchaseOrderItems(purchaseOrder.getId());
                    sapPurchaseOrderItemApplication.deletePurchaseOrderItems(itemList);
                    purchaseOrderId = purchaseOrder.getId();
                }
                this.sapPurchaseOrderRepository.save(purchaseOrder);
            }

            //数据第n次接收覆盖上一次数据
          /*    BeanUtil.copyProperties(sapPurchaseOrderVo, purchaseOrder);
            this.sapPurchaseOrderRepository.save(purchaseOrder);

            List<SapPurchaseOrderItem> purchaseOrderItems = sapPurchaseOrderItemApplication.getPurchaseOrderItems(purchaseOrder.getId());
            sapPurchaseOrderItemApplication.deletePurchaseOrderItems(purchaseOrderItems);
            purchaseOrderId = purchaseOrder.getId();*/
        }  //  lxh 04.28

        sapPurchaseOrderItemApplication.saveSapPurchaseOrderItems(purchaseOrderId, purchaseOrderItems);

    }


     /**
     * 远程调用sap接口，传递数据
     */
    public void sendSapDate(SapPurchaseOrder sapPurchaseOrder, String flag) {
        Map<String, Object> paramItem = new HashMap<>();
        paramItem.put("ebeln", sapPurchaseOrder.getEbeln());
        paramItem.put("BEDNR", sapPurchaseOrder.getBillCode());
        paramItem.put("frgkz", "10");
        paramItem.put("flag", flag);
        List<Map<String, Object>> paramList = new ArrayList<>();
        paramList.add(paramItem);
        Map<String, Object> param = new HashMap<>();
        param.put("MODE", "PO");
        param.put("DATA", paramList);

        ObjectMapper objectMapper = new ObjectMapper();
        String url = "pass_approval/approval";
        try {
            String result = defaultHttpClient.execute(param, url);
            List<Map<String, Object>> resultList = objectMapper.readValue(result, new TypeReference<List<Map<String, Object>>>() {
            });
            Map<String, Object> resultMap = resultList.get(0);
            if ("S".equals(resultMap.get("TYPE"))) {
                sapPurchaseOrder.setMsgId((String) resultMap.get("ID"));
                sapPurchaseOrder.setNumber((String) resultMap.get("NUMBER"));
                sapPurchaseOrder.setMsgType((String) resultMap.get("TYPE"));
                sapPurchaseOrder.setLogNo((String) resultMap.get("LOG_NO"));
                sapPurchaseOrder.setLogMsgNo((String) resultMap.get("LOG_MSG_NO"));
                sapPurchaseOrder.setMessage((String) resultMap.get("MESSAGE"));
                sapPurchaseOrder.setMessageV1((String) resultMap.get("MESSAGE_V1"));
                sapPurchaseOrder.setMessageV2((String) resultMap.get("MESSAGE_V1"));
                sapPurchaseOrder.setMessageV3((String) resultMap.get("MESSAGE_V1"));
                sapPurchaseOrder.setMessageV4((String) resultMap.get("MESSAGE_V1"));
                this.sapPurchaseOrderRepository.save(sapPurchaseOrder);
            } else {
                throw new RuntimeException("数据传输失败，请稍后手动重试！" + resultMap.get("MESSAGE"));
            }
        } catch (Exception e) {
            throw new RuntimeException("数据传输失败请检查数据后重试！" + e.getMessage());
        }
    }

    @Override
    public  String revokeApproval(String id){
        SapPurchaseOrder sapPurchaseOrder = this.sapPurchaseOrderRepository.findOne(id);
        if (sapPurchaseOrder.getStatusId()!=BizBillStatus.COMPLETED.getId()){
            throw new RuntimeException("审批完成的单据才能撤销！");
        }
        sapPurchaseOrder.setStatusId(BizBillStatus.ABORTED.getId());
        sapPurchaseOrderRepository.save(sapPurchaseOrder);
        List<SapPurchaseOrderItem> sapPurchaseOrderItemList=this.sapPurchaseOrderItemApplication.getPurchaseOrderItems(id);
        //发起流程
        SapPurchaseOrder newSapPurchaseOrder=new SapPurchaseOrder();
        ClassHelper.copyProperties(sapPurchaseOrder,newSapPurchaseOrder);
        newSapPurchaseOrder.setId("");

        String pId=mannualStartWorkApplication.startWorkflow(newSapPurchaseOrder,PROCESS_DEFINITION_KEY,"sapPurchaseOrder");
        newSapPurchaseOrder=this.sapPurchaseOrderRepository.findOne(pId);


        List<SapPurchaseOrderItem> newSapPurchaseOrderItemList=new ArrayList<>();
        for (SapPurchaseOrderItem sapPurchaseOrderItem:sapPurchaseOrderItemList){
            SapPurchaseOrderItem newSapPurchaseOrderItem=new SapPurchaseOrderItem();
            ClassHelper.copyProperties(sapPurchaseOrderItem,newSapPurchaseOrderItem);
            newSapPurchaseOrderItem.setId("");
            newSapPurchaseOrderItemList.add(newSapPurchaseOrderItem);
        }
        sapPurchaseOrderItemApplication.saveSapPurchaseOrderItems(newSapPurchaseOrder.getId(),newSapPurchaseOrderItemList);

        return newSapPurchaseOrder.getId();
    }

    @Override
    public Map<String, Object> print(String id) {
        SapPurchaseOrder sapPurchaseOrder = this.sapPurchaseOrderRepository.findOne(id);
        Map<String, Object> map = ClassHelper.beanToMap(sapPurchaseOrder);
        //获取加工商信息
        String lifnr = sapPurchaseOrder.getLifnr();
        map = this.getSupplierInfo(map,lifnr);

        //获取销售订单信息
        map = this.getSaleOrderInfo(map,sapPurchaseOrder.getId());

        //获取产品信息
        map = this.getProductInfo(map);

        //订单类型是ZNB3时对应工序外发（临时工序外协）
        if ("ZNB3".equals(sapPurchaseOrder.getBsart())){
            map = this.getProcessOutInfo(map);
        }
        return map;
    }

    public Map<String, Object> getSupplierInfo(Map<String, Object> map, String lifnr){
        List<SuppliersInfo> suppliersInfoList = suppliersInfoRepository.findByPartner(lifnr);
        if (CollectionUtils.isEmpty(suppliersInfoList)){
            throw new RuntimeException("根据编码：" + lifnr + "未找到供应商信息");
        }
        SuppliersInfo suppliersInfo = suppliersInfoList
                .stream()
                .sorted(Comparator.comparing(SuppliersInfo::getFillinDate, Comparator.reverseOrder()))
                .collect(Collectors.toList()).get(0);
        StringBuffer addressBuf = new StringBuffer();
        addressBuf.append(suppliersInfo.getLand1Name())
                .append(suppliersInfo.getRegionName())
                .append(suppliersInfo.getCity1())
                .append(suppliersInfo.getStreet())
                .append(suppliersInfo.getStrSuppl1())
                .append(suppliersInfo.getStrSuppl2())
                .append(suppliersInfo.getStrSuppl3())
                .append(suppliersInfo.getStrSuppl4());
        String address = addressBuf.toString();
        map.put("address",address);
        List<SuppliersPay> payList = suppliersPayRepository.findAllBySId(suppliersInfo.getId());
        SuppliersPay suppliersPay = payList.get(0);
        String zterm = suppliersPay.getZterm();//付款条件（月结方式）
        String zwels = suppliersPay.getZwels();//付款方式清单（付款方式）
        map.put("zterm", DictUtil.getDictionaryDetailText("zterm",zterm));
        map.put("zwels",DictUtil.getDictionaryDetailText("zwels",zwels));
        return map;
    }

    public Map<String, Object> getSaleOrderInfo(Map<String, Object> map,String orderId){
        List<SapPurchaseOrderItem> purchaseOrderItems = this.sapPurchaseOrderItemApplication.getPurchaseOrderItems(orderId);
        SapPurchaseOrderItem purchaseOrderItem = purchaseOrderItems.get(0);
        BeanUtil.beanToMap(purchaseOrderItem,map,false,false);
//        map.put("eindt",purchaseOrderItem.getEindt());//交货日期
//        map.put("lgobe",purchaseOrderItem.getLgobe());//仓库
//        map.put("telf1",purchaseOrderItem.getTelf1());//联系人电话
//        map.put("matnr2",purchaseOrderItem.getMatnr2());//产品编码
//        map.put("maktx",purchaseOrderItem.getMaktx());//产品名称
//        map.put("strSuppl",purchaseOrderItem.getStrSuppl());//交货地点
//        map.put("msehl",purchaseOrderItem.getMsehl());//单位
        //销售单号多个的情况放明细列表中
        Set<SapSaleOrderVo> saleOrderVos = purchaseOrderItems
                .stream()
                .map(sapPurchaseOrderItem -> {
                    SapSaleOrderVo sapSaleOrderVo = new SapSaleOrderVo();
                    BeanUtil.copyProperties(sapPurchaseOrderItem, sapSaleOrderVo);
                    return sapSaleOrderVo;
                }).collect(Collectors.toSet());
        BigDecimal zero = BigDecimal.ZERO;//计算成品数量
        for (SapSaleOrderVo saleOrderVo : saleOrderVos) {
            zero = zero.add(saleOrderVo.getKwmeng());
        }
        map.put("number",zero);
        map.put("saleOrderVos",saleOrderVos);
        //计算总额 = （含税单价+运输费）* 成品数量
        BigDecimal netpr = purchaseOrderItem.getNetpr();//含税单价
        BigDecimal kbetr2 = purchaseOrderItem.getKbetr2();//运输费
        BigDecimal amount = netpr.add(kbetr2).multiply(zero);
        map.put("netpr",netpr);
        map.put("kbetr2",kbetr2);
        map.put("priceAmount",netpr.add(kbetr2));//合计单价
        map.put("amount",amount);

        map.put("loss","1%");//LOSS数，默认1%
        //实际送货数量 = 成品数量 + LOSS数  (向上取整)
        BigDecimal multiply = zero.multiply(new BigDecimal("0.01"));
        long loss = multiply.setScale( 0, RoundingMode.UP).longValue(); // 向上取整
        BigDecimal realNumber = zero.add(BigDecimal.valueOf(loss));
        map.put("realNumber",realNumber);
        return map;
    }

    private Map<String, Object> getProductInfo(Map<String, Object> map) {
        String matnr2 = (String) map.get("matnr2");
        //从EP系统查询产品信息
        EpProductInfoVo epProductInfoVo = epApplication.queryEpProductInfo(matnr2);
        epProductInfoVo.setPrintColor(epProductInfoVo.getFrontColor()+";"+epProductInfoVo.getOppositeColor());
        BeanUtil.beanToMap(epProductInfoVo,map,false,false);
//        String imageUrl = "http://192.168.3.91:9000/mes/2024/10/15/20241015164918998370277.png";
//        map.put("frontImg",imageUrl);
//        map.put("backImg",imageUrl);
        try {
            File file = new File("product-"+map.get("ebeln")+".png");
            if(!file.exists()) {
                String IMG_TYPE_PNG = "image/png";
                map.put("barCode", CommonUtils.genBarCode128((String) map.get("ebeln"), IMG_TYPE_PNG, file,128,10, HumanReadablePlacement.HRP_BOTTOM));
            } else {
                map.put("barCode",file);
            }
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("PDF生成失败！");
        }
        return map;
    }

    private Map<String, Object> getProcessOutInfo(Map<String, Object> map) {
        String ebeln = (String) map.get("ebeln");//采购订单号
        //根据订单号查询外协单明细
        ProcessOutsourceOrderItem outsourceOrderItem = processOutsourceOrderItemRepository.findByEbeln(ebeln);
        map.put("outProcess01",outsourceOrderItem.getTxz01());
        map.put("outProcess02",outsourceOrderItem.getTxz02());
        //临时工序外发，计算印张数；实际送货数量，LOSS数
        BigDecimal number = (BigDecimal) map.getOrDefault("number",BigDecimal.ZERO);//成品数量
        BigDecimal model = (BigDecimal)map.get("model") != null ? (BigDecimal)map.get("model") : BigDecimal.ZERO;//排版数量
        BigDecimal printLoss = outsourceOrderItem.getPrintLoss() != null ? outsourceOrderItem.getPrintLoss() : BigDecimal.ZERO;//印张LOSS数
        map.put("printLoss",printLoss);
        //印张量 = 成品数量/排版数量
        BigDecimal printNumber = BigDecimal.ZERO;
        if (number.compareTo(BigDecimal.ZERO) > 0 && model.compareTo(BigDecimal.ZERO) > 0){
            printNumber = number.divide(model,2,RoundingMode.UP);
        }
        map.put("printNumber",printNumber);

        //LOSS数 = 印张LOSS * 排版数量
        BigDecimal loss = printLoss.multiply(model);
        map.put("loss",loss);

        //实际送货数量 = 成品数量 + LOSS数
        BigDecimal realNumber = number.add(loss);
        map.put("realNumber",realNumber);

        //查询工序外协单组件
        List<ProcessOutsourceMaterial> materialList = processOutsourceMaterialRepository.findByProcessOutsourceId(outsourceOrderItem.getProcessOutsourceId());
        for (ProcessOutsourceMaterial material : materialList) {
            material.setDeliveryMethodOut(DictUtil.getDictionaryDetailText("deliveryMethodOut",material.getDeliveryMethodOut()));
        }
        map.put("materialList",materialList);
        return map;
    }
}
