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

import com.uas.sso.entity.PartnershipRecordView;
import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.v2.service.PartnerShipService;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by hejq on 2017-09-29.
 */
@Service("v2.PartnerShipService")
public class PartnerShipServiceImpl implements PartnerShipService{

    @Autowired
    private VendorDao vendorDao;

    @Autowired
    private EnterpriseDao enterpriseDao;

    @Override
    public void addVendor(PartnershipRecordView record) {
        Enterprise myEn = enterpriseDao.findEnterpriseByEnBussinessCode(record.getCustUID());
        Enterprise vendEn = enterpriseDao.findEnterpriseByEnBussinessCode(record.getVendUID());
        if(null != myEn && null != vendEn) {
            List<Vendor> vendors = vendorDao.findByMyEnBusinessCodeAndVendBusinessCode(record.getCustUID(), record.getVendUID());
            Vendor vendor = new Vendor();
            if(!CollectionUtils.isEmpty(vendors)) {
                vendor = vendors.get(0);
                vendor.setErpstatus(Status.NOT_UPLOAD.value());
            } else {
                //客户
                vendor.setErpstatus(Status.NOT_UPLOAD.value());
                vendor.setMyUserUU(Long.valueOf(record.getCustUserCode()));
                vendor.setMyEnUU(myEn.getUu());

                //供应商
                vendor.setVendUserUU(vendEn.getEnAdminuu());
                vendor.setVendEnUU(vendEn.getUu());
                vendor.setSynchStatus(Constant.YES);
                vendor.setVendswitch(Constant.YES);
                vendor.setCustswitch(Constant.YES);
            }
            if(null != vendor) {
               vendorDao.save(vendor);
            }
        }
    }
}
