com.gettec.fsnip.fsn.service.market.impl.ResourceServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.gettec.fsnip.fsn.service.market.impl.ResourceServiceImpl.java

Source

package com.gettec.fsnip.fsn.service.market.impl;

import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_BRAND_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_CERT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_DESTROY_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_DISHSNO_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_DIS_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_LICENSE_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_LIQUOR_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_LOGO_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_ORG_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_PRODEP_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_PRODUCT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_QS_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_REPORT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WASTE_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_BRAND_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_CERT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_DESTROY_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_DISHSNO_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_DIS_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_LICENSE_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_LIQUOR_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_LOGO_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_ORG_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_PRODEP_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_PRODUCT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_QS_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_REPORT_PATH;
import static com.gettec.fsnip.fsn.util.SystemDefaultInterface.FSN_FTP_UPLOAD_WEB_WASTE_PATH;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.axis.encoding.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.gettec.fsnip.fsn.dao.market.MkTestResourceDAO;
import com.gettec.fsnip.fsn.exception.DaoException;
import com.gettec.fsnip.fsn.exception.JPAException;
import com.gettec.fsnip.fsn.exception.ServiceException;
import com.gettec.fsnip.fsn.model.business.BusinessBrand;
import com.gettec.fsnip.fsn.model.business.BusinessUnit;
import com.gettec.fsnip.fsn.model.business.EnterpriseRegiste;
import com.gettec.fsnip.fsn.model.business.LiutongFieldValue;
import com.gettec.fsnip.fsn.model.business.ProducingDepartment;
import com.gettec.fsnip.fsn.model.business.ProductionLicenseInfo;
import com.gettec.fsnip.fsn.model.dishs.DishsNo;
import com.gettec.fsnip.fsn.model.market.Resource;
import com.gettec.fsnip.fsn.model.market.ResourceType;
import com.gettec.fsnip.fsn.model.product.BusinessCertification;
import com.gettec.fsnip.fsn.model.product.ImportedProduct;
import com.gettec.fsnip.fsn.model.product.Product;
import com.gettec.fsnip.fsn.model.product.ProductDestroyRecord;
import com.gettec.fsnip.fsn.model.test.ImportedProductTestResult;
import com.gettec.fsnip.fsn.model.test.TestProperty;
import com.gettec.fsnip.fsn.model.test.TestResult;
import com.gettec.fsnip.fsn.model.waste.WasteDisposa;
import com.gettec.fsnip.fsn.service.business.BusinessBrandService;
import com.gettec.fsnip.fsn.service.business.EnterpriseRegisteService;
import com.gettec.fsnip.fsn.service.business.LiutongFieldValueService;
import com.gettec.fsnip.fsn.service.business.ProducingDepartmentService;
import com.gettec.fsnip.fsn.service.business.ProductionLicenseService;
import com.gettec.fsnip.fsn.service.common.impl.BaseServiceImpl;
import com.gettec.fsnip.fsn.service.dishs.DishsNoService;
import com.gettec.fsnip.fsn.service.market.ResourceService;
import com.gettec.fsnip.fsn.service.market.ResourceTypeService;
import com.gettec.fsnip.fsn.service.product.ImportedProductService;
import com.gettec.fsnip.fsn.service.product.ProductDestroyRecordService;
import com.gettec.fsnip.fsn.service.product.ProductService;
import com.gettec.fsnip.fsn.service.test.ImportedProductTestResultService;
import com.gettec.fsnip.fsn.service.waste.WasteDisposaService;
import com.gettec.fsnip.fsn.util.FileUtils;
import com.gettec.fsnip.fsn.util.MKReportNOUtils;
import com.gettec.fsnip.fsn.util.PropertiesUtil;
import com.gettec.fsnip.fsn.util.UploadUtil;
import com.lhfs.fsn.service.testReport.TestReportService;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

@Service(value = "testResourceService")
public class ResourceServiceImpl extends BaseServiceImpl<Resource, MkTestResourceDAO> implements ResourceService {
    @Autowired
    private MkTestResourceDAO testResourceDAO;
    @Autowired
    private ResourceTypeService resourceTypeService;
    @Autowired
    private ProductService productService;
    @Autowired
    private TestReportService testReportService;
    @Autowired
    private EnterpriseRegisteService enterpriseService;
    @Autowired
    private BusinessBrandService businessBrandService;
    @Autowired
    private ProducingDepartmentService producingDepartmentService;
    @Autowired
    private ProductionLicenseService productionLicenseService;
    @Autowired
    private LiutongFieldValueService liutongFieldValueService;
    @Autowired
    private ImportedProductService importedProductService;
    @Autowired
    private ImportedProductTestResultService importedProductTestResultService;
    @Autowired
    private WasteDisposaService wasteDisposaService;
    @Autowired
    private DishsNoService dishsNoService;
    @Autowired
    private ProductDestroyRecordService productDestroyRecordService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * ?idurl
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateResourceUrl(Long resId, String pdfUrl) throws ServiceException {
        try {
            Resource resource = findById(resId);
            resource.setUrl(pdfUrl);
            update(resource);
        } catch (ServiceException sex) {
            throw new ServiceException("MkTestResourceServiceImpl" + sex.getMessage(), sex.getException());
        }
    }

    /**
     * ?pdf
     * @param input
     * @param report
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Resource saveAutoPdfResource(InputStream input, TestResult report) throws ServiceException {
        try {
            if (input == null) {
                return null;
            }
            TestResult orig_report = testReportService.findById(report.getId());
            Resource resource = operationResourcesOfPdf(orig_report, input, null, report.getRepAttachments(),
                    orig_report.getTestType());
            return resource;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * ?
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean savePdfResource(TestResult report) {
        try {
            if (report == null) {
                return false;
            }
            if (report.isAutoReportFlag()) {
                return true;
            }
            Set<Resource> repAttachments = report.getRepAttachments();
            TestResult orig_report = testReportService.findById(report.getId());
            for (Resource resource : repAttachments) {
                if (!report.isNewFlag()) {
                    if (resource.getId() != null) {
                        /* ?pdfresourceid?pdfftp */
                        return true;
                    }
                }
                if (resource.getFile() != null) {
                    /* pdf */
                    ByteArrayInputStream in_fullPdf = new ByteArrayInputStream(resource.getFile());
                    ByteArrayInputStream in_interceptionPdf = new ByteArrayInputStream(resource.getFile());
                    resource = operationResourcesOfPdf(orig_report, in_fullPdf, in_interceptionPdf, repAttachments,
                            report.getTestType());
                } else if (resource.getUrl() != null && !resource.getUrl().equals("")) {
                    /* ??pdf */
                    create(resource);
                    Set<Resource> removes = getListOfRemoves(orig_report.getRepAttachments(), repAttachments);
                    if (!CollectionUtils.isEmpty(removes)) {
                        orig_report.removeResources(removes);
                        for (Resource res : removes) {
                            delete(res);
                        }
                    }
                    orig_report.setInterceptionPdfPath(resource.getInterceptionPdfPath());
                    orig_report.setFullPdfPath(resource.getUrl());
                }
                if (resource != null && resource.getId() != null) {
                    orig_report.getRepAttachments().clear();
                    orig_report.getRepAttachments().add(resource);
                    orig_report.setFullPdfPath(resource.getUrl());
                    orig_report.setSignFlag(false);
                    testReportService.update(orig_report);
                    report.setRepAttachments(orig_report.getRepAttachments());
                    break;
                } else {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 
     * @param orig_report
     * @param in_fullPdf
     * @param in_interceptionPdf
     * @param repAttachments
     * @param testType
     * @return
     */
    private Resource operationResourcesOfPdf(TestResult orig_report, InputStream in_full,
            ByteArrayInputStream in_cut, Set<Resource> repAttachments, String testType) {
        try {
            /* 1.????? */
            String reportNOEng = MKReportNOUtils.convertCharacter(orig_report.getServiceOrder(),
                    orig_report.getUploadPath());
            orig_report.setUploadPath(reportNOEng);
            /* 2.? */
            Set<Resource> adds = new HashSet<Resource>();
            Map<String, String> map = null;
            String randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
            String fileName = reportNOEng + "-" + randomStr + "-report.pdf";
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_REPORT_PATH) + "/" + reportNOEng;
            /* 3.pdfftp? */
            UploadUtil uploadUtil = new UploadUtil();
            map = uploadUtil.uploadReportPdf(in_full, in_cut, ftpPath, fileName, reportNOEng, testType);
            if (map != null) {
                Resource resource = new Resource();
                if (UploadUtil.IsOss()) {
                    resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                } else {
                    resource.setUrl(map.get("fullPdfPath"));
                }
                resource.setFileName(fileName);
                resource.setName(fileName);
                resource.setType(resourceTypeService.findById(3L));
                create(resource);
                adds.add(resource);
                /* 4.pdf? */
                orig_report.setFullPdfPath(map.get("fullPdfPath"));
                if (map != null) {
                    orig_report.setInterceptionPdfPath(map.get("interceptionPdfPath"));
                }
                Set<Resource> removes = getListOfRemoves(orig_report.getRepAttachments(), repAttachments);
                if (!CollectionUtils.isEmpty(removes)) {
                    orig_report.removeResources(removes);
                    for (Resource res : removes) {
                        delete(res);
                    }
                }
                if (!CollectionUtils.isEmpty(adds)) {
                    orig_report.addResources(adds);
                }
                testReportService.update(orig_report); // pdf?
                return resource;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * ?
     * @param businessBrand
     * @return void
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveBusinessBrandResources(BusinessBrand businessBrand) throws ServiceException {
        try {
            Set<Resource> nowLogAttachments = businessBrand.getLogAttachments();
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_BRAND_PATH);
            String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_BRAND_PATH);
            UploadUtil uploadUtil = new UploadUtil();
            for (Resource resource : nowLogAttachments) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                if (resource.getId() != null) {
                    resource = findById(resource.getId());
                    continue;
                }
                if (resource.getId() == null && resource.getFile() != null) {
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            resource.setUrl(webUrl + "/" + fileName);
                        }
                        resource.setFileName(fileName);
                        resource.setName(fileName);
                        create(resource);
                        adds.add(resource);
                    }
                }
            }

            /* ????? */
            if (businessBrand.getId() != null) {
                BusinessBrand origBrand = businessBrandService.findById(businessBrand.getId());
                Set<Resource> removes = getListOfRemoves(origBrand.getLogAttachments(), nowLogAttachments);
                if (!CollectionUtils.isEmpty(removes)) {
                    origBrand.removeResources(removes);
                    for (Resource resource : removes) {
                        this.delete(resource.getId());
                    }
                }
                if (!CollectionUtils.isEmpty(adds)) {
                    origBrand.addResources(adds);
                }
                businessBrandService.update(origBrand);
            }
        } catch (ServiceException ioe) {
            throw new ServiceException(
                    "MkTestResourceServiceImpl.saveBusinessBrandResources()-->" + ioe.getMessage(), ioe);
        }
    }

    /**
     * ???
     * @param business
     * @return void
     * @throws ServiceException
     * @author LongXianZhen
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCertificationResources(BusinessUnit business) throws ServiceException {
        try {
            List<BusinessCertification> listOfCertification = business.getListOfCertification();
            String randomStr = "";
            String fileName = "";
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_CERT_PATH);
            String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_CERT_PATH);
            UploadUtil uploadUtil = new UploadUtil();
            for (BusinessCertification certification : listOfCertification) {
                Resource certResource = certification.getCertResource(); //?
                Resource certIconRes = certification.getCert().getCertIconResource();//???
                /*?*/
                if (certResource.getId() == null && certResource.getFile() != null) {
                    randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                    fileName = business.getId() + "-" + randomStr + ".jpg";
                    boolean success_upload = uploadUtil.uploadFile(certResource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            certResource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            certResource.setUrl(webUrl + "/" + fileName);
                        }
                        certResource.setFileName(certResource.getName());
                        certResource.setName(certResource.getName());
                        create(certResource);
                    }
                }
                /*?*/
                if (certification.getCert().getStdStatus() != null && certification.getCert().getStdStatus() == 1) {
                    if (certIconRes != null && certIconRes.getUrl() == null && certIconRes.getFile() != null) {
                        randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                        fileName = business.getId() + "-" + randomStr + ".jpg";
                        boolean success_upload_Icon = uploadUtil.uploadFile(certIconRes.getFile(), ftpPath,
                                fileName);
                        if (success_upload_Icon) {
                            if (UploadUtil.IsOss()) {
                                certIconRes.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                            } else {
                                certIconRes.setUrl(webUrl + "/" + fileName);
                            }

                            certIconRes.setFileName(certIconRes.getName());
                            certIconRes.setName(certIconRes.getName());
                        }
                    }
                }

            }
        } catch (ServiceException sex) {
            throw new ServiceException("?service-error???",
                    sex.getException());
        }
    }

    /**
     * ??
     * @product
     * @return void
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveProResource(Product product) throws ServiceException {
        try {
            Set<Resource> proAttachments = product.getProAttachments();
            Product origProduct = productService.getDAO().findByBarcode(product.getBarcode());
            /* 1.?? */
            Set<Resource> removes = null;
            if (origProduct != null) {
                removes = getListOfRemoves(origProduct.getProAttachments(), proAttachments);
            }
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            String imgUrls = "";
            /* 2.???resourceid?null?? */
            for (Resource resource : proAttachments) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                fileName = product.getBarcode() + randomStr + "." + resource.getType().getRtDesc();
                if (resource.getId() != null && origProduct != null) {
                    /* 2.1 ??barcode */
                    resource = findById(resource.getId());
                    if (removes.size() > 0) {
                        for (Resource rs : removes) {
                            if (rs.getId() != resource.getId()) {
                                imgUrls += resource.getUrl() + "|";
                                break;
                            }
                        }
                    } else {
                        imgUrls += resource.getUrl() + "|";
                    }
                    continue;
                } else if (resource.getId() != null && origProduct == null) {
                    /* 2.2 ??barcode? */
                    resource.setId(null);
                    imgUrls += resource.getUrl() + "|";
                    continue;
                }
                if (resource.getFile() != null) {
                    /* 2.3 ?? */
                    String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_PRODUCT_PATH) + "/"
                            + product.getBarcode();
                    UploadUtil uploadUtil = new UploadUtil();
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_PRODUCT_PATH) + "/"
                                + product.getBarcode();
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            resource.setUrl(webUrl + "/" + fileName);
                        }
                        resource.setFileName(fileName);
                        resource.setName(fileName);
                        imgUrls = webUrl + "/" + fileName + "|" + imgUrls;
                        if (origProduct != null) {
                            create(resource);
                            adds.add(resource);
                        }
                    }
                }
            }
            /* 3.?? */
            if (origProduct != null) {
                /* 3.1 ?? */
                if (!CollectionUtils.isEmpty(removes)) {
                    origProduct.removeResources(removes);
                    for (Resource resource : removes) {
                        long count = getDAO().getRelationCountByResourceId(resource.getId());
                        if (count == 1) {
                            delete(resource);
                        }
                    }
                }
                /* 3.2 ?? */
                if (!CollectionUtils.isEmpty(adds)) {
                    origProduct.addResources(adds);
                }
                /* 3.3 ??? */
                if (imgUrls.equals("")) {
                    imgUrls = "http://qa.fsnrec.com/portal/img/product/temp/temp.jpg";
                }
                origProduct.setImgUrl(imgUrls);
                origProduct.setExpirationDate(product.getExpirationDate());
                productService.update(origProduct);
            } else {
                if (product.getProAttachments().size() == 0) {
                    imgUrls = "http://qa.fsnrec.com/portal/img/product/temp/temp.jpg";
                }
            }
            product.setImgUrl(imgUrls);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ???
     * 
     * @param orgAttachments
     * @param businessUnit
     * @return void
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveOrgnizationResource(Set<Resource> orgAttachments, BusinessUnit busUnit)
            throws ServiceException {
        try {
            if (orgAttachments == null || orgAttachments.size() < 1) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(busUnit.getName());
            /* ??? */
            LiutongFieldValue ltOrgan_orig = liutongFieldValueService.findByProducerIdAndValueAndDisplay(
                    busUnit.getId(), orig_enterprise.getOrganizationNo(), "?");
            List<Long> liutongOrgResId = null;
            if (ltOrgan_orig != null) {
                /* ??-????id */
                liutongOrgResId = liutongFieldValueService.getResourceIds(busUnit.getId(),
                        orig_enterprise.getOrganizationNo(), "?");
            }
            /* 1.?[??]? */
            Set<Resource> removes = getListOfRemoves(
                    orig_enterprise == null ? null : orig_enterprise.getOrgAttachments(), orgAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(orgAttachments, "org", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeOrgResources(removes);
                for (Resource resource : removes) {
                    BigInteger resId = BigInteger.valueOf(resource.getId());
                    /* ???-????  */
                    if (liutongOrgResId != null && liutongOrgResId.contains(resId)) {
                        ltOrgan_orig.removeResources(resource);
                    }
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addOrgResources(adds);
                if (ltOrgan_orig != null) {
                    ltOrgan_orig.addResources(adds);
                }
            }
            enterpriseService.update(orig_enterprise);
            if (ltOrgan_orig != null) {
                liutongFieldValueService.update(ltOrgan_orig);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ??
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveLicenseResource(Set<Resource> licAttachments, String name) throws ServiceException {
        try {
            if (licAttachments == null || licAttachments.size() < 1) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            if (orig_enterprise != null && orig_enterprise.getStatus().equals("")) {
                licAttachments = orig_enterprise.getLicAttachments();
                return;
            }
            /* 1.?[?]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getLicAttachments(), licAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(licAttachments, "license", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeLicenseResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addLicResources(adds);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("??", e);
        }
    }

    /**
     * ????
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveDisResource(Set<Resource> disAttachments, String name) throws ServiceException {
        try {
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            /* 1.?[???]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getDisAttachments(), disAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(disAttachments, "dis", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes) && adds.size() > 0) {
                orig_enterprise.removeDisResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds) && adds.size() > 0) {
                orig_enterprise.addDisResources(adds);
            } else {
                orig_enterprise.addDisResources(removes);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ????
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveQsResource(Set<Resource> qsAttachments, String name) throws ServiceException {
        try {
            if (qsAttachments == null || qsAttachments.size() < 1) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            /* 1.?[???]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getQsAttachments(), qsAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(qsAttachments, "qs", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeQsResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addQsResources(adds);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ??Logo
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveLogoResource(Set<Resource> logoAttachments, String name) throws ServiceException {
        try {
            if (logoAttachments == null || logoAttachments.size() < 1) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            /* 1.?[?Logo]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getLogoAttachments(), logoAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(logoAttachments, "logo", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeLogoResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addLogoResources(adds);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ????
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveTaxRegResource(Set<Resource> taxRegAttachments, String name) throws ServiceException {
        try {
            if (taxRegAttachments == null) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            /* 1.?[??]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getTaxRegAttachments(), taxRegAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(taxRegAttachments, "taxReg", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeTaxRegResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addTaxRegResources(adds);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ?????
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveLiquorResource(Set<Resource> liquorAttachments, String name) throws ServiceException {
        try {
            if (liquorAttachments == null) {
                return;
            }
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(name);
            /* 1.?[??]? */
            Set<Resource> removes = getListOfRemoves(orig_enterprise.getLiquorAttachments(), liquorAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(liquorAttachments, "liquor", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_enterprise.removeLiquorResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_enterprise.addLiquorResources(adds);
            }
            enterpriseService.update(orig_enterprise);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ??
     * 
     * @throws ServiceException
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveProDepResource(Set<Resource> proDepAttachments, Long proDepId) throws ServiceException {
        try {
            ProducingDepartment orig_proDep = producingDepartmentService.findById(proDepId);
            /* 1.?[??]? */
            Set<Resource> removes = getListOfRemoves(orig_proDep.getDepAttachments(), proDepAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(proDepAttachments, "proDep", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_proDep.removeDepResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_proDep.addDepResources(adds);
            }
            producingDepartmentService.update(orig_proDep);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ?????
     * @param enRegiste
     * @return void
     * @throws ServiceException
     * @author LongXianZhen
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveLicAndOrgResource(EnterpriseRegiste enRegiste) throws ServiceException {
        try {
            Set<Resource> licAttachments = enRegiste.getLicAttachments();
            Set<Resource> addLics = new HashSet<Resource>();
            String randomStr = "";
            String fileName = ""; // pdf??
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LICENSE_PATH);
            String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LICENSE_PATH);
            UploadUtil uploadUtil = new UploadUtil();
            EnterpriseRegiste enReg = enterpriseService.findById(enRegiste.getId());
            for (Resource resource : licAttachments) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                fileName = randomStr + "." + resource.getType().getRtDesc();
                if (resource.getFile() != null) {
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            resource.setUrl(webUrl + "/" + fileName);
                        }
                        resource.setFileName(fileName);
                        resource.setName(fileName);
                        create(resource);
                        addLics.add(resource);
                    }
                }
            }
            if (StringUtils.isNotBlank(enRegiste.getProductNo())) {
                Set<Resource> addService = new HashSet<Resource>();
                Set<Resource> qsAttachments = enRegiste.getQsAttachments();
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_QS_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_QS_PATH);
                for (Resource resource : qsAttachments) {
                    randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    if (resource.getFile() != null) {
                        boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                        if (success_upload) {
                            if (UploadUtil.IsOss()) {
                                resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                            } else {
                                resource.setUrl(webUrl + "/" + fileName);
                            }
                            resource.setFileName(fileName);
                            resource.setName(fileName);
                            create(resource);
                            addService.add(resource);
                        }
                    }
                }
                enReg.addQsResources(addService);
            }
            if (StringUtils.isNotBlank(enRegiste.getPassNo())) {
                Set<Resource> addPass = new HashSet<Resource>();
                Set<Resource> disAttachments = enRegiste.getDisAttachments();
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_DIS_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_DIS_PATH);
                for (Resource resource : disAttachments) {
                    randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    if (resource.getFile() != null) {
                        boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                        if (success_upload) {
                            if (UploadUtil.IsOss()) {
                                resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                            } else {
                                resource.setUrl(webUrl + "/" + fileName);
                            }
                            resource.setFileName(fileName);
                            resource.setName(fileName);
                            create(resource);
                            addPass.add(resource);
                        }
                    }
                }
                enReg.addDisResources(addPass);
            }
            if (StringUtils.isNotBlank(enRegiste.getServiceNo())) {
                Set<Resource> addPro = new HashSet<Resource>();
                Set<Resource> liquorAttachments = enRegiste.getLiquorAttachments();
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LIQUOR_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LIQUOR_PATH);
                for (Resource resource : liquorAttachments) {
                    randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    if (resource.getFile() != null) {
                        boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                        if (success_upload) {
                            if (UploadUtil.IsOss()) {
                                resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                            } else {
                                resource.setUrl(webUrl + "/" + fileName);
                            }
                            resource.setFileName(fileName);
                            resource.setName(fileName);
                            create(resource);
                            addPro.add(resource);
                        }
                    }
                    enReg.setLiquorAttachments(addPro);
                }
            }

            enReg.addLicResources(addLics);
            //         enReg.addOrgResources(addOrgs);
            enterpriseService.update(enReg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Long countBusPdfByBusId(Long id) throws ServiceException {
        try {
            return getDAO().countBySql("business_pdf_to_resource", " where business_id= ?1", new Object[] { id });
        } catch (JPAException jpae) {
            throw new ServiceException("MkTestResourceServiceImpl.countBusPdfByBusId() " + jpae.getMessage(), jpae);
        }
    }

    @Override
    public List<Resource> getListBusPdfWithPage(Long busId, int page, int pageSize) throws ServiceException {
        try {
            return getDAO().getListBusPdfByBusUnitIdWithPage(page, pageSize, busId);
        } catch (DaoException daoe) {
            throw new ServiceException("MkTestResourceServiceImpl.getListBusPdfWithPage() " + daoe.getMessage(),
                    daoe.getException());
        }
    }

    /**
     * ftp
     * 
     * @param attachments
     * @param type
     * @param barcode
     * @return Set<MkTestResource> ?
     * @throws ServiceException
     * @author ZhangHui
     */
    private Set<Resource> operationResources(Set<Resource> attachments, String type, String barcode)
            throws ServiceException {
        try {
            String ftpPath = "";
            String webUrl = "";
            if (type.equals("license")) { // ??
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LICENSE_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LICENSE_PATH);
            } else if (type.equals("org")) { // ??
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_ORG_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_ORG_PATH);
            } else if (type.equals("dis")) { // ???
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_DIS_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_DIS_PATH);
            } else if (type.equals("qs")) { // ??
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_QS_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_QS_PATH);
            } else if (type.equals("logo")) {// ?logo
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LOGO_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LOGO_PATH);
            } else if (type.equals("taxReg")) {// ?
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LICENSE_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LICENSE_PATH);
            } else if (type.equals("liquor")) { // ??
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LIQUOR_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LIQUOR_PATH);
            } else if (type.equals("proDep")) { // ?
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_PRODEP_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_PRODEP_PATH);
            } else if (type.equals("product")) { // ?
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_PRODUCT_PATH) + "/" + barcode;
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_PRODUCT_PATH) + "/" + barcode;
            } else if (type.equals("record")) { // ?
                ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_DESTROY_PATH);
                webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_DESTROY_PATH);
            }

            Set<Resource> adds = new HashSet<Resource>();
            for (Resource resource : attachments) {
                /* 1. */
                if (resource.getId() != null && resource.getUrl() != null) {
                    continue;
                }
                /*2.url????ftp*/
                if (resource.getUrl() != null && resource.getUrl().length() > 0) {
                    create(resource);
                    adds.add(resource);
                    continue;
                }
                /* 3.? */
                if (resource.getFile() != null) {
                    String randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                    String urlFileName = randomStr + "." + resource.getType().getRtDesc();
                    UploadUtil uploadUtil = new UploadUtil();
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, urlFileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + urlFileName));
                        } else {
                            resource.setUrl(webUrl + "/" + urlFileName);
                        }
                        resource.setFileName(resource.getFileName());
                        resource.setName(resource.getFileName());
                        if (resource.getId() == null) {
                            create(resource);
                            adds.add(resource);
                        } else {
                            // ???ftp
                            Resource org_res = findById(resource.getId());
                            org_res.setUrl(resource.getUrl());
                            org_res.setFileName(resource.getFileName());
                            org_res.setName(resource.getFileName());
                            update(org_res);
                        }
                    }
                }
            }
            return adds;
        } catch (ServiceException sex) {
            throw new ServiceException("id??", sex);
        }
    }

    /**
     * ???
     */
    @Override
    public List<Resource> addResourcesKendoUI(Collection<Resource> resources) throws ServiceException {
        List<Resource> rsss = new ArrayList<Resource>();
        if (resources != null) {
            for (Resource rs : resources) {
                //this.updateResource(rs);
                ResourceType type = resourceTypeService.findByName(FileUtils.getExtension(rs.getFileName()));
                if (type != null) {
                    rs.setType(type);
                }
                rs.setFileBase64(Base64.encode(rs.getFile()));
                /*if (rs.getType() == null) {
                   System.out.println((rs.getType() == null) + " is null");
                   continue;
                }*/
                rsss.add(rs);
                //System.out.println("add files");
            }
        }
        return rsss;
    }

    /**
     * ???
     * 
     * @param productionLicense
     * @param new_qsAttachments
     * @return void
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public ProductionLicenseInfo saveQsResource(ProductionLicenseInfo proLicense) throws ServiceException {
        try {
            if (proLicense == null || proLicense.getId() == null) {
                return null;
            }
            ProductionLicenseInfo orig_proLicense = productionLicenseService.findById(proLicense.getId());
            Set<Resource> qsAttachments = proLicense.getQsAttachments();
            /* 1.?[??]? */
            Set<Resource> removes = getListOfRemoves(orig_proLicense.getQsAttachments(), qsAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(qsAttachments, "qs", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_proLicense.removeQsResources(removes);
                for (Resource resource : removes) {
                    long count = productionLicenseService.getDAO().countRelationshipByResIdAndQsNo(resource.getId(),
                            proLicense.getId());
                    if (count == 0) {
                        delete(resource);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_proLicense.addQsResources(adds);
            }
            productionLicenseService.update(orig_proLicense);
            return orig_proLicense;
        } catch (DaoException dao) {
            throw new ServiceException(
                    "[DaoException]MkTestResourceServiceImpl.saveQsResource()-->" + dao.getMessage(),
                    dao.getException());
        } catch (ServiceException sex) {
            throw new ServiceException(
                    "[ServiceException]MkTestResourceServiceImpl.saveQsResource()-->" + sex.getMessage(),
                    sex.getException());
        }
    }

    /**
     * ?????
     */
    @Override
    public Set<Resource> getListOfRemoves(Collection<Resource> origAttachments,
            Collection<Resource> nowAttachments) {
        if (origAttachments == null) {
            return null;
        }
        Set<Resource> removes = new HashSet<Resource>();
        List<Long> currentId = new ArrayList<Long>();
        for (Resource resource : nowAttachments) {
            Long id = resource.getId();
            if (id != null) {
                currentId.add(id);
            }
        }
        for (Resource resource : origAttachments) {
            if (resource.getId() != null && !currentId.contains(resource.getId())) {
                removes.add(resource);
            }
        }
        return removes;
    }

    /**
     * ?resource?
     * @author ZhangHui 2015/6/3
     */
    @Override
    public Set<Resource> getListOfAdds(Collection<Resource> nowAttachments) {
        if (nowAttachments == null) {
            return null;
        }

        Set<Resource> adds = new HashSet<Resource>();
        for (Resource resource : nowAttachments) {
            if (resource.getFile() != null) {
                adds.add(resource);
            }
        }

        return adds;
    }

    /**
     * excel?
     */
    @Override
    public List<TestProperty> paseExcelByResource(Resource resource) throws ServiceException {
        if (resource == null) {
            return null;
        }
        List<TestProperty> items = new ArrayList<TestProperty>();
        try {
            if (resource.getFileName().toUpperCase().endsWith("XLS")) {
                WorkbookSettings ws = new WorkbookSettings();
                ws.setEncoding("iso-8859-1");
                Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(resource.getFile()), ws);
                Sheet sheet = wb.getSheet(0);
                if (sheet.getRows() < 1) {
                    return null;
                }
                for (int i = 1; i < sheet.getRows(); i++) {
                    if (sheet.getCell(0, i).getContents().trim().length() < 1
                            || sheet.getCell(3, i).getContents().trim().length() < 1) {
                        continue;
                    }
                    TestProperty itm = new TestProperty();
                    itm.setName(sheet.getCell(0, i).getContents().trim());
                    itm.setUnit(sheet.getCell(1, i).getContents().trim());
                    itm.setTechIndicator(sheet.getCell(2, i).getContents().trim());
                    itm.setResult(sheet.getCell(3, i).getContents().trim());
                    itm.setAssessment("?");
                    if (sheet.getCell(4, i).getContents().trim().contains("?")) {
                        itm.setAssessment("??");
                    }
                    itm.setStandard(sheet.getCell(5, i).getContents().trim());
                    items.add(itm);
                }
                if (wb != null) {
                    wb.close();
                }
            } else {
                XSSFRow row = null;
                XSSFWorkbook xwb = new XSSFWorkbook(new ByteArrayInputStream(resource.getFile()));
                XSSFSheet sheet = xwb.getSheetAt(0);
                for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) {
                    row = sheet.getRow(i);
                    if (row == null || row.getCell(0) == null || row.getCell(0).toString().trim().length() < 1
                            || row.getCell(3).toString().trim().length() < 1) {
                        continue;
                    }
                    TestProperty itm = new TestProperty();
                    itm.setName(row.getCell(0).toString().trim());
                    itm.setUnit(row.getCell(1).toString().trim());
                    itm.setTechIndicator(row.getCell(2).toString().trim());
                    itm.setResult(row.getCell(3).toString().trim());
                    itm.setAssessment("?");
                    if (row.getCell(4).toString().trim().contains("?")) {
                        itm.setAssessment("??");
                    }
                    itm.setStandard(row.getCell(5).toString().trim());
                    items.add(itm);
                }
            }
        } catch (Exception e) {
            throw new ServiceException("MkTestResourceServiceImpl.paseExcelByResource() " + e.getMessage(), e);
        }
        return items;
    }

    /**
     * ??
     * 
     * @param fieldValue
     * @return Set<MkTestResource>
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public LiutongFieldValue saveResouce(LiutongFieldValue fieldValue) throws ServiceException {
        try {
            if (fieldValue == null || fieldValue.getId() == null) {
                return null;
            }
            /* 1.?type */
            String type = "";
            if (fieldValue.getDisplay().equals("?")) {
                type = "org";
            } else if (fieldValue.getDisplay().equals("??")) {
                type = "license";
            } else {
                return null;
            }
            LiutongFieldValue orig_fieldValue = null;
            orig_fieldValue = liutongFieldValueService.findById(fieldValue.getId());
            /* 2.?? */
            Set<Resource> removes = getListOfRemoves(
                    orig_fieldValue != null ? orig_fieldValue.getAttachments() : null, fieldValue.getAttachments());
            /* 3.?ftp */
            Set<Resource> adds = operationResources(fieldValue.getAttachments(), type, null);
            /* 3.? */
            if (!CollectionUtils.isEmpty(removes)) {
                orig_fieldValue.removeResources(removes);
                for (Resource resource : removes) {
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                orig_fieldValue.addResources(adds);
            }
            setFieldValue(orig_fieldValue, fieldValue);
            liutongFieldValueService.update(orig_fieldValue);
            return orig_fieldValue;
        } catch (ServiceException sex) {
            throw new ServiceException("MkTestResourceServiceImpl.saveResouce()-->" + sex.getMessage(),
                    sex.getException());
        }
    }

    /**
     * ???
     * 
     * @param orig_fieldValue
     * @param fieldValue
     * @return void
     * @author ZhangHui
     */
    private void setFieldValue(LiutongFieldValue orig_fieldValue, LiutongFieldValue fileValue) {
        orig_fieldValue.setFullFlag(fileValue.isFullFlag());
        orig_fieldValue.setMsg(fileValue.getMsg());
        orig_fieldValue.setValue(fileValue.getValue());
        orig_fieldValue.setPassFlag(fileValue.getPassFlag());
    }

    /**
     * ??pdf  MkTestResource 
     * @param pictures
     * @param reportNo
     * @param testType
     * @return Resource
     * @throws ServiceException
     * @author TangXin
     */
    @Override
    public Resource getPdfByPcitures(List<Resource> pictures, String reportNo, String testType)
            throws ServiceException {
        try {
            if (pictures == null || pictures.size() < 1) {
                return null;
            }
            if (reportNo == null || reportNo.equals("")) {
                reportNo = "" + System.currentTimeMillis();
            }
            Resource result = new Resource();
            String reportNOEng = MKReportNOUtils.convertCharacter(reportNo, null);
            String randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
            String uploadFileName = reportNOEng + "-" + randomStr + "-report.pdf";
            UploadUtil uploadUtil = new UploadUtil();
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_REPORT_PATH) + "/" + reportNOEng;
            Map<String, String> map = uploadUtil.uploadReportPdf(pictures, ftpPath, uploadFileName, reportNOEng);
            result.setFileName(reportNo + ".pdf");
            result.setName(reportNo + ".pdf");
            result.setUrl(map.get("fullPdfPath"));
            result.setInterceptionPdfPath(map.get("interceptionPdfPath"));
            result.setType(resourceTypeService.findById(3L));
            return result;
        } catch (Exception e) {
            throw new ServiceException("MkTestResourceServiceImpl.getPdfByPcitures() " + e.getMessage(), e);
        }
    }

    @Override
    public MkTestResourceDAO getDAO() {
        return testResourceDAO;
    }

    /**
     *  ?????
     * @author zhawaneng
     *  2015-03-06
     * @param productImg
     * @param barcode
     * @return
     */
    @Override
    public Product setImgToProduct(String productImg, String barcode) {
        Product product = new Product();
        String[] split = productImg.split("\\|");
        Set<Resource> resources = new HashSet<Resource>();
        String randomStr = "";
        String fileName = "";
        String imgUrls = "http://qa.fsnrec.com/portal/img/product/temp/temp.jpg";
        String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_PRODUCT_PATH) + "/" + barcode;
        for (String imgurl : split) {

            if (imgurl == null || "".equals(imgurl) || "null".equals(imgurl)) {
                continue;
            }

            Resource resource = new Resource();
            randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
            /* ??  */
            String imgType = FileUtils.getExtension(imgurl);
            fileName = barcode + randomStr + "." + imgType;
            UploadUtil uploadUtil = new UploadUtil();
            /*   */
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_PRODUCT_PATH) + "/" + barcode;
            /* InputStream  */
            InputStream downloadFileStream = uploadUtil.downloadFileStream(imgurl);
            /* InputStream  */
            boolean success_upload = uploadUtil.uploadFile(downloadFileStream, ftpPath, fileName);
            /* ???? */
            if (success_upload) {
                try {
                    if (UploadUtil.IsOss()) {
                        resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                    } else {
                        resource.setUrl(webUrl + "/" + fileName);
                    }
                    resource.setFileName(fileName);
                    resource.setName(fileName);
                    ResourceType type = resourceTypeService.findByName(imgType);
                    resource.setType(type);
                    create(resource);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                resources.add(resource);
            }
            imgUrls += webUrl + "/" + fileName + "|";
        }
        if (!CollectionUtils.isEmpty(resources)) {
            product.addResources(resources);
        }
        if (StringUtils.isNotEmpty(imgUrls)) {
            product.setImgUrl(imgUrls);
        }
        return product;

    }

    /**
     * 
     * @author HY
     */
    @Override
    public void savePropagandaResource(Resource resource, String name) throws ServiceException {
        String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_LOGO_PATH);
        String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_LOGO_PATH);
        /* 1. */
        if (resource.getFile() == null && resource.getName() != null && resource.getUrl() != null) {
            return;
        } else if (resource.getFile() != null) { /* 3.? */
            String randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
            String urlFileName = randomStr + "." + resource.getType().getRtDesc();
            UploadUtil uploadUtil = new UploadUtil();
            boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, urlFileName);
            if (success_upload) {
                if (UploadUtil.IsOss()) {
                    resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + urlFileName));
                } else {
                    resource.setUrl(webUrl + "/" + urlFileName);
                }
                resource.setFileName(resource.getFileName());
                resource.setName(resource.getFileName());
            }
        }
    }

    /**
     * ????
     * @author longxianzhen 2015/05/27
     */
    @Override
    public void saveLabResource(ImportedProduct impProduct) throws ServiceException {
        try {
            Set<Resource> labAttachments = impProduct.getLabelAttachments();
            Product pro = productService.findById(impProduct.getProductId());
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            //String imgUrls = "";
            /* 2.??resourceid?null?? */
            for (Resource resource : labAttachments) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                fileName = pro.getBarcode() + randomStr + "." + resource.getType().getRtDesc();
                if (resource.getId() != null) {
                    continue;
                }
                /* 2.3 ? */
                String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_PRODUCT_PATH) + "/" + pro.getBarcode()
                        + "/lab";
                UploadUtil uploadUtil = new UploadUtil();
                boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                if (success_upload) {
                    String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_PRODUCT_PATH) + "/"
                            + pro.getBarcode() + "/lab";
                    if (UploadUtil.IsOss()) {
                        resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                    } else {
                        resource.setUrl(webUrl + "/" + fileName);
                    }
                    //resource.setFileName(fileName);
                    //resource.setName(fileName);
                    create(resource);
                    adds.add(resource);
                }
            }
            /* 3.??? */
            if (impProduct.getId() != null) {
                ImportedProduct origImpProduct = importedProductService.getDAO().findById(impProduct.getId());
                /* 1.? */
                Set<Resource> removes = null;
                if (origImpProduct != null) {
                    removes = getListOfRemoves(origImpProduct.getLabelAttachments(), labAttachments);
                }
                /* 3.1 ?? */
                if (!CollectionUtils.isEmpty(removes)) {
                    origImpProduct.removeResources(removes);
                    for (Resource resource : removes) {
                        long count = getDAO().getRelationCountByResourceId(resource.getId());
                        if (count == 1) {
                            delete(resource);
                        }
                    }
                }
                /* 3.2 ?? */
                if (!CollectionUtils.isEmpty(adds)) {
                    origImpProduct.addResources(adds);
                }

                importedProductService.update(origImpProduct);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ??????
     * @author longxianzhen 2015/05/27
     */
    @Override
    public void saveSanResource(ImportedProductTestResult impProductTestResult) throws ServiceException {
        try {
            Set<Resource> sanAttachments = impProductTestResult.getSanitaryAttachments();
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            //String imgUrls = "";
            /* 2.??????resourceid?null?? */
            for (Resource resource : sanAttachments) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                fileName = impProductTestResult.getSanitaryCertNo() + randomStr + "."
                        + resource.getType().getRtDesc();
                if (resource.getId() != null) {
                    continue;
                }
                /* 2.3 ????? */
                String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_REPORT_PATH) + "/"
                        + impProductTestResult.getSanitaryCertNo();
                UploadUtil uploadUtil = new UploadUtil();
                boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                if (success_upload) {
                    if (UploadUtil.IsOss()) {
                        resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                    } else {
                        String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_REPORT_PATH) + "/"
                                + impProductTestResult.getSanitaryCertNo();
                        resource.setUrl(webUrl + "/" + fileName);
                    }
                    //resource.setFileName(fileName);
                    //resource.setName(fileName);
                    create(resource);
                    adds.add(resource);
                }
            }
            /* 3.??? */
            if (impProductTestResult.getId() != null) {
                ImportedProductTestResult origImpProTset = importedProductTestResultService.getDAO()
                        .findById(impProductTestResult.getId());
                /* 1.????? */
                Set<Resource> removes = null;
                if (origImpProTset != null) {
                    removes = getListOfRemoves(origImpProTset.getSanitaryAttachments(), sanAttachments);
                }
                /* 3.1 ?????? */
                if (!CollectionUtils.isEmpty(removes)) {
                    origImpProTset.removeResources(removes);
                    for (Resource resource : removes) {
                        long count = getDAO().getRelationCountByResourceId(resource.getId());
                        if (count == 1) {
                            delete(resource);
                        }
                    }
                }
                /* 3.2 ?????? */
                if (!CollectionUtils.isEmpty(adds)) {
                    origImpProTset.addResources(adds);
                }

                importedProductTestResultService.update(origImpProTset);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ?????PDF?
     * @author longxianzhen 2015/05/27
     */
    @Override
    public void saveSanPdfResource(ImportedProductTestResult impProductTestResult) throws ServiceException {
        try {
            Set<Resource> sanAttachments = impProductTestResult.getSanitaryPdfAttachments();
            Set<Resource> adds = new HashSet<Resource>();
            /* 2.??????resourceid?null?? */
            for (Resource resource : sanAttachments) {
                if (resource.getId() != null) {
                    continue;
                } else {
                    adds.add(resource);
                }
            }
            /* 3.??? */
            if (impProductTestResult.getId() != null) {
                ImportedProductTestResult origImpProTset = importedProductTestResultService.getDAO()
                        .findById(impProductTestResult.getId());
                /* 1.????? */
                Set<Resource> removes = null;
                if (origImpProTset != null) {
                    removes = getListOfRemoves(origImpProTset.getSanitaryPdfAttachments(), sanAttachments);
                }
                /* 3.1 ?????? */
                if (!CollectionUtils.isEmpty(removes)) {
                    origImpProTset.removePdfResources(removes);
                    for (Resource resource : removes) {
                        long count = getDAO().getRelationCountByResourceId(resource.getId());
                        if (count == 1) {
                            delete(resource);
                        }
                    }
                }
                /* 3.2 ?????? */
                if (!CollectionUtils.isEmpty(adds)) {
                    origImpProTset.addPdfResources(adds);
                }

                importedProductTestResultService.update(origImpProTset);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

    /**
     * ??id???
     * @author longxianzhen 2015/06/10
     */
    @Override
    public List<Resource> getProductImgListByproId(Long proId) throws ServiceException {
        try {
            return testResourceDAO.getProductImgListByproId(proId);
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage() + "?", e.getException());
        }
    }

    public List<Resource> getRebackImgListByreportId(Long reportId) throws ServiceException {
        try {
            return testResourceDAO.getRebackImgListByreportId(reportId);
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage() + "?", e.getException());
        }
    }

    /**
     * ??id??????????
     * @author longxianzhen 2015/08/03
     */
    @Override
    public Map<String, String> getBusinessUnitCertById(Long buId) throws ServiceException {
        try {
            return testResourceDAO.getBusinessUnitCertById(buId);
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage() + "??id????",
                    e.getException());
        }
    }

    /**
     * ?qs?idqs?
     * @author longxianzhen 2015/08/06
     */
    @Override
    public List<Resource> getQsResourceByQsId(Long qsId) throws ServiceException {
        try {
            return testResourceDAO.getQsResourceByQsId(qsId);
        } catch (DaoException e) {
            throw new ServiceException(e.getMessage() + "", e.getException());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void savePiceFile(WasteDisposa wasteDisposa) throws ServiceException {
        try {
            Set<Resource> piceFiles = wasteDisposa.getPiceFile();
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WASTE_PATH);
            String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_WASTE_PATH);
            UploadUtil uploadUtil = new UploadUtil();
            for (Resource resource : piceFiles) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                if (resource.getId() != null) {
                    resource = findById(resource.getId());
                    continue;
                }
                if (resource.getId() == null && resource.getFile() != null) {
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            resource.setUrl(webUrl + "/" + fileName);
                        }
                        resource.setFileName(fileName);
                        resource.setName(fileName);
                        create(resource);
                        adds.add(resource);
                    }
                }
            }

            /* ????? */
            if (wasteDisposa.getId() != null) {
                WasteDisposa origwasteDisposa = wasteDisposaService.findById(wasteDisposa.getId());
                Set<Resource> removes = getListOfRemoves(origwasteDisposa.getPiceFile(), piceFiles);
                if (!CollectionUtils.isEmpty(removes)) {
                    origwasteDisposa.removeResources(removes);
                    for (Resource resource : removes) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds)) {
                    origwasteDisposa.addResources(adds);
                }
                wasteDisposaService.update(origwasteDisposa);
            }
        } catch (ServiceException ioe) {
            throw new ServiceException(
                    "MkTestResourceServiceImpl.saveBusinessBrandResources()-->" + ioe.getMessage(), ioe);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveDishsnoFile(DishsNo dishsNo) throws ServiceException {
        try {
            Set<Resource> dishsnoFile = dishsNo.getDishsnoFile();
            Set<Resource> adds = new HashSet<Resource>();
            String randomStr = "";
            String fileName = "";
            String ftpPath = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_DISHSNO_PATH);
            String webUrl = PropertiesUtil.getProperty(FSN_FTP_UPLOAD_WEB_DISHSNO_PATH);
            UploadUtil uploadUtil = new UploadUtil();
            for (Resource resource : dishsnoFile) {
                randomStr = sdf.format(new Date()) + (new Random().nextInt(1000) + 1);
                if (resource.getId() != null) {
                    resource = findById(resource.getId());
                    continue;
                }
                if (resource.getId() == null && resource.getFile() != null) {
                    fileName = randomStr + "." + resource.getType().getRtDesc();
                    boolean success_upload = uploadUtil.uploadFile(resource.getFile(), ftpPath, fileName);
                    if (success_upload) {
                        if (UploadUtil.IsOss()) {
                            resource.setUrl(uploadUtil.getOssSignUrl(ftpPath + "/" + fileName));
                        } else {
                            resource.setUrl(webUrl + "/" + fileName);
                        }
                        resource.setFileName(fileName);
                        resource.setName(fileName);
                        create(resource);
                        adds.add(resource);
                    }
                }
            }

            /* ????? */
            if (dishsNo.getId() != null) {
                DishsNo origndishsNo = dishsNoService.findById(dishsNo.getId());
                Set<Resource> removes = getListOfRemoves(origndishsNo.getDishsnoFile(), dishsnoFile);
                if (!CollectionUtils.isEmpty(removes)) {
                    origndishsNo.removeResources(removes);
                    for (Resource resource : removes) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds)) {
                    origndishsNo.addResources(adds);
                }
                dishsNoService.update(origndishsNo);
            }
        } catch (ServiceException ioe) {
            throw new ServiceException(
                    "MkTestResourceServiceImpl.saveBusinessBrandResources()-->" + ioe.getMessage(), ioe);
        }
    }

    /**
     * ???
     * 
     * @param orgAttachments
     * @param businessUnit
     * @return void
     * @throws ServiceException
     * @author ZhangHui
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Set<Resource> saverecResource(Set<Resource> recAttachments, ProductDestroyRecord productDestroyRecord)
            throws ServiceException {
        try {
            if (recAttachments == null || recAttachments.size() < 1) {
                return null;
            }
            Set<Resource> removes = getListOfRemoves(
                    productDestroyRecord == null ? null : productDestroyRecord.getRecAttachments(), recAttachments);
            /* 2.? */
            Set<Resource> adds = operationResources(recAttachments, "record", null);
            /* 3.?? */
            if (!CollectionUtils.isEmpty(removes)) {
                productDestroyRecord.removerecResources(removes);
                for (Resource resource : removes) {
                    //            BigInteger resId = BigInteger.valueOf(resource.getId());
                    delete(resource);
                }
            }
            if (!CollectionUtils.isEmpty(adds)) {
                //   productDestroyRecord.addrecResources(adds);
                recAttachments = adds;
            }
            //   productDestroyRecordService.update(productDestroyRecord);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
        return recAttachments;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteResourceByResultId(long resultId) {
        this.getDAO().deleteResourceByResultId(resultId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveBusinessCert(BusinessUnit businessUnit) throws ServiceException {
        try {
            EnterpriseRegiste orig_enterprise = enterpriseService.findbyEnterpriteName(businessUnit.getName());

            Set<Resource> orgAttachments = businessUnit.getOrgAttachments();

            if (!(orgAttachments == null || orgAttachments.size() < 1)) {
                /* 1.?[??]? */
                Set<Resource> removes = getListOfRemoves(
                        orig_enterprise == null ? null : orig_enterprise.getOrgAttachments(), orgAttachments);
                /* 2.? */
                Set<Resource> adds = operationResources(orgAttachments, "org", null);
                /* 3.?? */
                if (!CollectionUtils.isEmpty(removes)) {
                    orig_enterprise.removeOrgResources(removes);
                    for (Resource resource : removes) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds)) {
                    orig_enterprise.addOrgResources(adds);
                }

            } else {
                Set<Resource> oldremoves = orig_enterprise.getOrgAttachments();
                Set<Resource> removes = new HashSet<Resource>();
                removes.addAll(oldremoves);
                if (!CollectionUtils.isEmpty(removes)) {
                    orig_enterprise.removeOrgResources(removes);
                    for (Resource resource : removes) {
                        delete(resource);
                    }
                }
            }

            Set<Resource> licAttachments = businessUnit.getLicAttachments();

            if (!(licAttachments == null || licAttachments.size() < 1)) {
                /* 1.?[?]? */
                Set<Resource> removes1 = getListOfRemoves(orig_enterprise.getLicAttachments(), licAttachments);
                /* 2.? */
                Set<Resource> adds1 = operationResources(licAttachments, "license", null);
                /* 3.?? */
                if (!CollectionUtils.isEmpty(removes1)) {
                    orig_enterprise.removeLicenseResources(removes1);
                    for (Resource resource : removes1) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds1)) {
                    orig_enterprise.addLicResources(adds1);
                }

            }

            Set<Resource> taxRegAttachments = businessUnit.getTaxRegAttachments();

            if (!(taxRegAttachments == null || taxRegAttachments.size() < 1)) {
                /* 1.?[??]? */
                Set<Resource> removes2 = getListOfRemoves(orig_enterprise.getTaxRegAttachments(),
                        taxRegAttachments);
                /* 2.? */
                Set<Resource> adds2 = operationResources(taxRegAttachments, "taxReg", null);
                /* 3.?? */
                if (!CollectionUtils.isEmpty(removes2)) {
                    orig_enterprise.removeTaxRegResources(removes2);
                    for (Resource resource : removes2) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds2)) {
                    orig_enterprise.addTaxRegResources(adds2);
                }
            }

            Set<Resource> disAttachments = businessUnit.getDisAttachments();

            if (disAttachments != null && disAttachments.size() > 0) {

                /* 1.?[???]? */
                Set<Resource> removes = getListOfRemoves(orig_enterprise.getDisAttachments(), disAttachments);
                /* 2.? */
                Set<Resource> adds = operationResources(disAttachments, "dis", null);
                /* 3.?? */
                if (!CollectionUtils.isEmpty(removes) && removes.size() > 0) {
                    orig_enterprise.removeDisResources(removes);
                    for (Resource resource : removes) {
                        delete(resource);
                    }
                }
                if (!CollectionUtils.isEmpty(adds) && adds.size() > 0) {
                    orig_enterprise.addDisResources(adds);
                }
            }
            enterpriseService.update(orig_enterprise);

        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException("?", e);
        }
    }

}