package com.jwipc.order.logic.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.aggregation.ModelAggregationService;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.api.dto.OaFileParamDTO;
import com.jwipc.exception.DaoException;
import com.jwipc.order.constant.PcnMange;
import com.jwipc.order.constant.PcnOaClient;
import com.jwipc.order.convert.PcnManagerConvert;
import com.jwipc.order.dto.*;
import com.jwipc.order.entity.*;
import com.jwipc.order.logic.PcnManagerLogic;
import com.jwipc.order.model.query.PcnChangeApproveCondQuery;
import com.jwipc.order.model.query.PcnManagerQuery;
import com.jwipc.order.service.*;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.clent.dto.SupplierInvitaionDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.FileClient;
import com.jwipc.sys.client.SequenceClient;
import com.jwipc.sys.client.UserClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.sys.client.dto.FileResposePreviewCDTO;
import com.jwipc.sys.client.dto.UserDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/19 0019 14:36
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PcnManagerLogicImpl implements PcnManagerLogic {

    @Autowired
    IPcnChangeApplyListService pcnChangeApplyListService;

    @Autowired
    IPcnChangeSubjectService pcnChangeSubjectService;

    @Autowired
    IPcnChangeApproveService pcnChangeApproveService;

    @Autowired
    IPcnChangeMaterialService pcnChangeMaterialService;

    @Autowired
    IPcnChangeFileService pcnChangeFileService;

    @Autowired
    PcnManagerConvert pcnManagerConvert;

    @Autowired
    SequenceClient sequenceClient;

    @Resource
    PcnOaClient pcnOaClient;

    @Autowired
    DictClient dictClient;

    @Autowired
    UserClient userClient;

    @Autowired
    ModelAggregationService modelAggregationService;

    private final FileClient fileClient;
    private final SupplierClient supplierClient;;


    @Override
    public PcnChangeApplyListDTO selectPcnMangeById(String id) {
        PcnChangeApplyList pcnChangeApplyList = pcnChangeApplyListService.getById(id);
        return pcnManagerConvert.entityToDTO(pcnChangeApplyList);
    }

    @Override
    public List<PcnChangeSubjectDTO> selectRecordByApplayId(String id) {
        List<PcnChangeSubject> pcnChangeSubjectList = pcnChangeSubjectService.list(new LambdaQueryWrapper<PcnChangeSubject>()
                .eq(PcnChangeSubject::getApplyListId,id));
        return pcnManagerConvert.selectRecordListToDTOs(pcnChangeSubjectList);
    }

    @Override
    public PageView queryPcnByPage(PcnManagerQuery query) {
        final Page<PcnChangeApplyList> page = PageUtil.toPage(query);
        final LambdaQueryWrapper<PcnChangeApplyList> queryWrapper = new LambdaQueryWrapper<>();
        if (LoginUtil.isSupplier()) {
            queryWrapper.like(PcnChangeApplyList::getSupplierNo, LoginUtil.getSupplierCode());
            queryWrapper.orderByDesc(PcnChangeApplyList::getCreateTime);
        }else{
            queryWrapper.eq(StrUtil.isNotEmpty(query.getSupplierNo()), PcnChangeApplyList::getSupplierNo, query.getSupplierNo());
            queryWrapper.like(StrUtil.isNotEmpty(query.getSupplierName()), PcnChangeApplyList::getSupplierName,query.getSupplierName());
            queryWrapper.orderByDesc(PcnChangeApplyList::getCreateTime);
        }
        queryWrapper.eq(!Objects.isNull(query.getStatus()), PcnChangeApplyList::getStatus,query.getStatus());
        final List<PcnChangeApplyList> list = pcnChangeApplyListService.list(page, queryWrapper);
        return PageUtil.getPV(pcnManagerConvert.pcnChangeApplyListToDTOs(list), page.getTotal());
    }

    @Override
    public void deleteRecord(String id) {
        pcnChangeSubjectService.remove(new LambdaQueryWrapper<PcnChangeSubject>().eq(PcnChangeSubject::getApplyListId,id));
        pcnChangeApproveService.remove(new LambdaQueryWrapper<PcnChangeApprove>().eq(PcnChangeApprove::getApplyListId,id));
        pcnChangeMaterialService.remove(new LambdaQueryWrapper<PcnChangeMaterial>().eq(PcnChangeMaterial::getApplyListId,id));
        pcnChangeFileService.remove(new LambdaQueryWrapper<PcnChangeFile>().eq(PcnChangeFile::getApplyListId,id));
        pcnChangeApplyListService.removeById(id);
    }

    @Override
    @Transactional
    public void saveFrom(PcnChangeApplyListDTO applyListDTO) {
        if(StringUtils.isEmpty(applyListDTO.getChangeStageEnd())){
            applyListDTO.setChangeStageEnd(null);
        }
        PcnChangeApplyList applyList = pcnManagerConvert.dtoToEntity(applyListDTO);
        if(StringUtils.isEmpty(applyList.getId())){
            applyList.setCreateTime(LocalDateTime.now());
            applyList.setCreateUser(LoginUtil.getUserId());
            applyList.setModifyTime(LocalDateTime.now());
            applyList.setModifyUser(LoginUtil.getUserId());
            applyList.setBeforeChangeAttachment(applyListDTO.getBeforeChangeAttachment());
            applyList.setAfterChangeAttachment(applyListDTO.getAfterChangeAttachment());
        }else{
            applyList.setModifyTime(LocalDateTime.now());
            applyList.setModifyUser(LoginUtil.getUserId());
        }
        applyList.setChangeContent("1");
        pcnChangeApplyListService.saveOrUpdate(applyList);
        //删除
        pcnChangeMaterialService.remove(new LambdaQueryWrapper<PcnChangeMaterial>().eq(PcnChangeMaterial::getApplyListId,applyList.getId()));
        pcnChangeFileService.remove(new LambdaQueryWrapper<PcnChangeFile>().eq(PcnChangeFile::getApplyListId,applyList.getId()));
        pcnChangeSubjectService.remove(new LambdaQueryWrapper<PcnChangeSubject>().eq(PcnChangeSubject::getApplyListId,applyList.getId()));

        String msters = applyListDTO.getMaterialRows();
        String files = applyListDTO.getFileColRows();
        if(!StringUtils.isEmpty(msters)){
            List<PcnChangeMaterial> ms = JSONArray.parseArray(msters, PcnChangeMaterial.class);
            ms.stream().forEach(a->a.setApplyListId(applyList.getId()));
            pcnChangeMaterialService.saveOrUpdateBatch(ms);
        }
        if(!StringUtils.isEmpty(files)){
            List<PcnChangeFile> ms = JSONArray.parseArray(files, PcnChangeFile.class);
            ms.stream().forEach(a->a.setApplyListId(applyList.getId()));
            // 通过stream检查列表中是否至少有一条数据的fileType字段值为"1"
            boolean hasFileType1 = ms.stream().anyMatch(a -> "1".equals(a.getFileType()));
            boolean hasFileType2 = ms.stream().anyMatch(a -> "2".equals(a.getFileType()));

            // 当type==1且列表中至少有一条数据的fileType字段值为"1"时执行保存或更新操作
            if (applyListDTO.getHaveEccnCode() == 1 && !hasFileType1) {
                throw new RuntimeException("请上传承认书!");
            }else if(applyListDTO.getHaveBreakMaterial() == 1 && !hasFileType2){
                throw new RuntimeException("请上传测试报告!");
            }else{
                pcnChangeFileService.saveOrUpdateBatch(ms);
            }
//            pcnChangeFileService.saveOrUpdateBatch(ms);
        }
        if(!StringUtils.isEmpty(applyListDTO.getSubjectValue())){
            PcnChangeSubject subject = new PcnChangeSubject();
            subject.setSubjectValue(String.valueOf(applyListDTO.getSubjectValue()));
            subject.setApplyListId(applyList.getId());
            pcnChangeSubjectService.saveOrUpdate(subject);
        }
        //调用 oa
        if(applyListDTO.getStatus() == PcnMange.STATUE_EXAMINE){//调用OA
            ApiResult result = pcnManageToSap(applyListDTO,applyList.getId());
            if(!result.getCode().equals(RestCode.SAP_SUCCESS.getCode())){
//                applyList.setStatus(PcnMange.STATUE_REFUSE);
                throw new DaoException("错误：" + result.getMessage());
            }
            applyList.setOaCode(result.getCode());
            applyList.setOaMsg(result.getMessage());
            if(applyListDTO.getFlag()!= null && applyListDTO.getFlag() == 1){
                applyList.setOaProcessId(String.valueOf(result.getData()));
            }else{
                applyList.setOaProcessId(applyListDTO.getOaProcessId());
            }

            applyList.setChangeNoticeRemarks("");
            pcnChangeApplyListService.updateById(applyList);
        }
    }

    @Override
    public List<PcnChangeMaterialDTO> queryMaterial(Long appListId) {
        List<PcnChangeMaterial> materials = pcnChangeMaterialService.list(new LambdaQueryWrapper<PcnChangeMaterial>().eq(PcnChangeMaterial::getApplyListId,appListId));
        return pcnManagerConvert.selectMaterialListToDTOs(materials);
    }

    @Override
    public List<PcnChangeFileDTO> getFileList(Long appListId) {
        List<PcnChangeFile> files = pcnChangeFileService.list(new LambdaQueryWrapper<PcnChangeFile>().eq(PcnChangeFile::getApplyListId,appListId));
        List<PcnChangeFileDTO> fileDTOS = pcnManagerConvert.selectFilesListToDTOs(files);
        modelAggregationService.processRelationAggregation(fileDTOS);
        return fileDTOS;
    }

    @Override
    public List<PcnChangeSubjectDTO> getSubjectList(Long appListId) {
        List<PcnChangeSubject> subjects = pcnChangeSubjectService.list(new LambdaQueryWrapper<PcnChangeSubject>().eq(PcnChangeSubject::getApplyListId,appListId));
        return pcnManagerConvert.selectSubjectListToDTOs(subjects);
    }

    @Override
    public PcnChangeSubjectDTO getSubjectOne(Long appListId) {
        PcnChangeSubject subject = pcnChangeSubjectService.getOne(new LambdaQueryWrapper<PcnChangeSubject>().eq(PcnChangeSubject::getApplyListId,appListId));
        return pcnManagerConvert.selectSubjectOneToDTO(subject);
    }

    @Override
    public void addAgreeRecord(PcnChangeApproveDTO approveDTO) {
        PcnChangeApprove approve = pcnManagerConvert.selectApproveListToDTOs(approveDTO);
        approve.setApproveTime(LocalDateTime.now());
        approve.setApprove(LoginUtil.getUserId());
        pcnChangeApproveService.save(approve);
    }

    @Override
    public PageView queryPcnApprovalPage(PcnChangeApproveCondQuery query) {
        final Page<PcnChangeApprove> page = PageUtil.toPage(query);
        final LambdaQueryWrapper<PcnChangeApprove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( PcnChangeApprove::getApplyListId, query.getApplyListId());
        final List<PcnChangeApprove> list = pcnChangeApproveService.list(page, queryWrapper);
        return PageUtil.getPV(pcnManagerConvert.selectApprovalListToDTOs(list), page.getTotal());
    }

    @Override
    public String getSupPgCode(String supplierCode) {
//        Supplier sup = pcnChangeSubjectService.getOne(supplierCode);
        SupplierDTO sup = supplierClient.getSupplierByCode(supplierCode);
        return sup.getPurchaseGroupCode();
    }

    @Override
    public String getRecommander(Long supplierId) {
        SupplierInvitaionDTO supplierInvitaionDTO = supplierClient.getRecommander(supplierId);
//        SupplierInvitaion supplierInvitaion =
        return supplierInvitaionDTO.getRecommender();
    }


    public ApiResult pcnManageToSap(PcnChangeApplyListDTO applyListDTO,Long applyListId){

        List<DictDTO> subs = dictClient.getDicByType("pcn_subject");
        List<DictDTO> deals = dictClient.getDicByType("stock_deal");
        List<DictDTO> nodes = dictClient.getDicByType("change_time_node");
        List<DictDTO> files = dictClient.getDicByType("pcn_fileType");

        Map<String, String> subMap = subs.stream().collect(Collectors.toMap(DictDTO::getCode,DictDTO::getName,(key1,key2)->key2));
        Map<String, String> dealsMap = deals.stream().collect(Collectors.toMap(DictDTO::getCode,DictDTO::getName,(key1,key2)->key2));
        Map<String, String> nodesMap = nodes.stream().collect(Collectors.toMap(DictDTO::getCode,DictDTO::getName,(key1,key2)->key2));
        Map<String, String> filesMap = files.stream().collect(Collectors.toMap(DictDTO::getCode,DictDTO::getName,(key1,key2)->key2));


        PcnManageToSap pcnManageToSap = new PcnManageToSap();
        final PcnManageToSap.Head head = new PcnManageToSap.Head();
        head.RecordId = String.valueOf(applyListId);
        head.SRMToS = PcnMange.BUSINESS_TYPE;
        head.Supplier = applyListDTO.getSupplierName();
        head.SupplierCode = applyListDTO.getSupplierNo();

        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = dateTime.format(formatter);
        head.ApplyDate = formattedDateTime;


        String code = "";
        if(!StringUtils.isEmpty(applyListDTO.getSubjectValue())){
            code = subMap.get(applyListDTO.getSubjectValue());
        }
        if(Objects.isNull(applyListDTO.getOaProcessId())){
            head.ProcessId = "";
        }else{
            head.ProcessId = applyListDTO.getOaProcessId();
        }
//        final PcnManageToSap.Head head = new PcnManageToSap.Head();
        head.ChangeType = code;
//        head.setApplicant(applyListDTO.getSqeUserId());
        //通过SQE角色名称查工号
        String sqeName = applyListDTO.getSqeUserName();
        UserDTO userDTO = userClient.getByName(sqeName);
        if(applyListDTO.getSqeUserId() != null && !applyListDTO.getSqeUserId().isEmpty()){
            //大小写转换
            String exApplicantS = applyListDTO.getSqeUserId().toUpperCase();
            head.Applicant = exApplicantS;
        }else{
            String exApplicantA = userDTO.getAccountName().toUpperCase();
            head.Applicant = exApplicantA;
        }

//        head.Others = applyListDTO.getChangeContent();
        head.ChangeReason = applyListDTO.getChangeReason();
        head.InventoryQuantity = String.valueOf(applyListDTO.getStockQuantity().doubleValue());
        head.InventoryDisposal = dealsMap.get(String.valueOf(applyListDTO.getStockDeal()));
        head.ChangeTimeNode = nodesMap.get(String.valueOf(applyListDTO.getChangeTimeNode()));
        head.StageChangeInit = applyListDTO.getChangeStageTime();
        //head.Applicant = applyListDTO.getSqeUserId();
        head.StageChangeEnd = applyListDTO.getChangeStageEnd();
        head.PermanentChange = applyListDTO.getChangeStageTime();
        head.ProvideSamples = applyListDTO.getHaveSendSamples() == 0 ? PcnMange.NO : PcnMange.YES;
        head.ProvideSampleQuantity = applyListDTO.getFreeSamplesNumber();
        head.LetterCommitment = applyListDTO.getHaveEccnCode() == 0 ? PcnMange.NO : PcnMange.YES;
        head.TestReport = applyListDTO.getHaveBreakMaterial() == 0 ? PcnMange.NO : PcnMange.YES;
        if(applyListDTO.getSqeUserId() != null && !applyListDTO.getSqeUserId().isEmpty()){
            //大小写转换
            String exSQES = applyListDTO.getSqeUserId().toUpperCase();
            head.SQE = exSQES;
        }else{
            String exSQEA = userDTO.getAccountName().toUpperCase();
            head.SQE = exSQEA;
        }

        if(applyListDTO.getPurchaseUserId()!= null && !applyListDTO.getPurchaseUserId().isEmpty()){
            //大小写转换
            String cg = applyListDTO.getPurchaseUserId().toUpperCase();
            head.CaiGou = cg;
        }

        if (org.apache.commons.lang3.StringUtils.isNotEmpty(applyListDTO.getBeforeChangeAttachment())){
            List<FileResposePreviewCDTO> fileResposePreviewDTOS = fileClient.getPreviewUrl(Arrays.asList(org.apache.commons.lang3.StringUtils.split(applyListDTO.getBeforeChangeAttachment(), ",")));
//            List<String> strings = fileResposePreviewDTOS.stream().map(FileResposePreviewCDTO::getPreviewUrl).toList();
            List<OaFileParamDTO> list1 = new ArrayList<>();
            for (FileResposePreviewCDTO fileResposePreviewDTO : fileResposePreviewDTOS) {
                OaFileParamDTO build = OaFileParamDTO.builder().fileName(fileResposePreviewDTO.getFileName()).url(fileResposePreviewDTO.getPreviewUrl())
                        .downloadUrl(fileResposePreviewDTO.getDownloadUrl()).build();
                list1.add(build);
            }

            //相关附件未盖章文件
            head.BeforeChangeAnnex = JSON.toJSONString(list1);
        }else {
            //相关附件未盖章文件
            head.BeforeChangeAnnex = "";
        }
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(applyListDTO.getAfterChangeAttachment())){
            List<FileResposePreviewCDTO> fileResposePreviewDTOS = fileClient.getPreviewUrl(Arrays.asList(org.apache.commons.lang3.StringUtils.split(applyListDTO.getAfterChangeAttachment(), ",")));
//            List<String> strings = fileResposePreviewDTOS.stream().map(FileResposePreviewCDTO::getPreviewUrl).toList();
            List<OaFileParamDTO> list1 = new ArrayList<>();
            for (FileResposePreviewCDTO fileResposePreviewDTO : fileResposePreviewDTOS) {
                OaFileParamDTO build = OaFileParamDTO.builder().fileName(fileResposePreviewDTO.getFileName()).url(fileResposePreviewDTO.getPreviewUrl())
                        .downloadUrl(fileResposePreviewDTO.getDownloadUrl()).build();
                list1.add(build);
            }
            //相关附件未盖章文件
            head.AfterChangeAnnex = JSON.toJSONString(list1);
        }else {
            //相关附件未盖章文件
            head.AfterChangeAnnex = "";
        }
//        head.setOthers(applyListDTO.getChangeContent());
//        head.setChangeReason(applyListDTO.getChangeReason());
//        head.setInventoryQuantity(String.valueOf(applyListDTO.getStockQuantity().doubleValue()));
//        head.setInventoryDisposal(dealsMap.get(String.valueOf(applyListDTO.getStockDeal())));
//        head.setChangeTimeNode(nodesMap.get(applyListDTO.getChangeTimeNode()));
//        head.setStageChangeInit(applyListDTO.getChangeStageTime());
//        head.setApplicant(applyListDTO.getSqeUserId());
//        head.setStageChangeEnd(applyListDTO.getChangeStageEnd());
//        head.setPermanentChange(applyListDTO.getChangeStageTime());
//        head.setProvideSamples(applyListDTO.getHaveSendSamples()==0?PcnMange.NO :PcnMange.YES);
//        head.setProvideSampleQuantity(applyListDTO.getFreeSamplesNumber());
//        head.setLetterCommitment(applyListDTO.getHaveEccnCode()==0?PcnMange.NO :PcnMange.YES);
//        head.setTestReport(applyListDTO.getHaveBreakMaterial()==0?PcnMange.NO :PcnMange.YES);
//        head.setSQE(applyListDTO.getSqeUserId());
        pcnManageToSap.setHead(head);
//        List<PcnChangeFile> ms = JSONArray.parseArray(files.toString(), PcnChangeFile.class);
//        ms.stream().forEach(a->a.setApplyListId(applyListId));

        List<PcnChangeFile> ms1 = pcnChangeFileService.getByType("1",applyListId);
        if (CollUtil.isNotEmpty(ms1)){
            List<Long> fileIdList = ms1.stream()
                    .map(PcnChangeFile::getFileId) // 提取 fileId 属性
                    .collect(Collectors.toList());
            List<FileResposePreviewCDTO> fileResposePreviewDTOS = fileClient.getPreviewUrl(fileIdList);
            List<OaFileParamDTO> list1 = new ArrayList<>();
            for (FileResposePreviewCDTO fileResposePreviewDTO : fileResposePreviewDTOS) {
                OaFileParamDTO build = OaFileParamDTO.builder().fileName(fileResposePreviewDTO.getFileName()).url(fileResposePreviewDTO.getPreviewUrl()).downloadUrl(fileResposePreviewDTO.getDownloadUrl()).build();
                list1.add(build);
            }
            head.PromiseAnnex = JSON.toJSONString(list1);
        }else {
            //相关附件未盖章文件
            head.PromiseAnnex = "";
        }

        List<PcnChangeFile> ms2 = pcnChangeFileService.getByType("2",applyListId);
        if(CollUtil.isNotEmpty(ms2)){
            List<Long> fileIdList = ms2.stream()
                    .map(PcnChangeFile::getFileId) // 提取 fileId 属性
                    .collect(Collectors.toList());
            List<FileResposePreviewCDTO> fileResposePreviewDTOS = fileClient.getPreviewUrl(fileIdList);
            List<OaFileParamDTO> list2 = new ArrayList<>();
            for (FileResposePreviewCDTO fileResposePreviewDTO : fileResposePreviewDTOS) {
                OaFileParamDTO build = OaFileParamDTO.builder().fileName(fileResposePreviewDTO.getFileName()).url(fileResposePreviewDTO.getPreviewUrl()).downloadUrl(fileResposePreviewDTO.getDownloadUrl()).build();
                list2.add(build);
            }
            head.TestReportAnnex = JSON.toJSONString(list2);
        }else {
            //相关附件未盖章文件
            head.TestReportAnnex = "";
        }

        List<PcnChangeFile> ms3 = pcnChangeFileService.getByType("11",applyListId);
        if(CollUtil.isNotEmpty(ms3)){
            List<Long> fileIdList = ms3.stream()
                    .map(PcnChangeFile::getFileId) // 提取 fileId 属性
                    .collect(Collectors.toList());
            List<FileResposePreviewCDTO> fileResposePreviewDTOS = fileClient.getPreviewUrl(fileIdList);
            List<OaFileParamDTO> list3 = new ArrayList<>();
            for (FileResposePreviewCDTO fileResposePreviewDTO : fileResposePreviewDTOS) {
                OaFileParamDTO build = OaFileParamDTO.builder().fileName(fileResposePreviewDTO.getFileName()).url(fileResposePreviewDTO.getPreviewUrl()).downloadUrl(fileResposePreviewDTO.getDownloadUrl()).build();
                list3.add(build);
            }
            head.OtherAnnex = JSON.toJSONString(list3);
        }else {
            //相关附件未盖章文件
            head.OtherAnnex = "";
        }

        List<PcnManageToSap.Item> items = new ArrayList<>();
        if(!StringUtils.isEmpty(applyListDTO.getMaterialRows())){
            List<PcnChangeMaterial> ms = JSONArray.parseArray(applyListDTO.getMaterialRows(), PcnChangeMaterial.class);
            ms.stream().forEach(a->{
                PcnManageToSap.Item item = new PcnManageToSap.Item();
                item.setChangeNumber(a.getMaterialCode());
                item.setChangeMaterialDesc(a.getMaterialName());
                items.add(item);
            });
        }
        Map map = new HashMap();
        map.put("item",items);
        pcnManageToSap.setItem(map);
//        System.out.printf(JSON.toJSONString(pcnManageToSap));
        ApiResult result = pcnOaClient.savePcnTOOA(pcnManageToSap);
        System.out.printf(JSON.toJSONString("result----"+result));
        return result;
    }

}
