package com.uas.platform.b2b.erp.service.impl;

import com.uas.platform.b2b.dao.VendorPerformanceAssessDao;
import com.uas.platform.b2b.erp.model.VendorPerformanceAssessErp;
import com.uas.platform.b2b.erp.service.VendorPerformanceAssessErpService;
import com.uas.platform.b2b.model.VendorPerformanceAssess;
import com.uas.platform.b2b.support.SystemSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 黄诚天 on 2017-10-12.
 */
@Service
public class VendorPerformanceAssessErpServiceImpl implements VendorPerformanceAssessErpService {

    @Autowired
    private VendorPerformanceAssessDao vpaDao;

    @Override
    public List<VendorPerformanceAssess> convertVPA(List<VendorPerformanceAssessErp> vpaErps) {
        List<VendorPerformanceAssess> vaps = new ArrayList<VendorPerformanceAssess>();
        long enUU = SystemSession.getUser().getEnterprise().getUu();
        for (VendorPerformanceAssessErp vpaErp : vpaErps) {
            VendorPerformanceAssess vpaResult = null;
            List<VendorPerformanceAssess> list = vpaDao.findByEnUUAndVapId(enUU,vpaErp.getVpa_id());
            if (!CollectionUtils.isEmpty(list)) {
                vpaResult = list.get(0);
            }

            if (vpaResult == null) {// 平台上不存在的新上传上来的物料，新增
                vaps.add(vpaErp.convert(enUU));
            } else {// 平台上已经存在的物料，修改
                //TODO 不需要？
                VendorPerformanceAssess vpa = vpaErp.convert(enUU);
                vpa.setVpaId(vpaResult.getVpaId());
                vaps.add(vpa);
            }
        }
        return vaps;
    }
}
