com.belle.yitiansystem.merchant.service.impl.MerchantServiceNewImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.belle.yitiansystem.merchant.service.impl.MerchantServiceNewImpl.java

Source

/**
 * 
 */
package com.belle.yitiansystem.merchant.service.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.belle.finance.biz.dubbo.ICostSetOfBooksDubboService;
import com.belle.finance.costsettlement.costsetofbooks.model.vo.CostSetofBooks;
import com.belle.infrastructure.util.DateUtil;
import com.belle.infrastructure.util.GetSessionUtil;
import com.belle.infrastructure.util.Md5Encrypt;
import com.belle.infrastructure.util.UUIDGenerator;
import com.belle.other.model.pojo.SupplierSp;
import com.belle.yitiansystem.merchant.constant.MerchantConstant;
import com.belle.yitiansystem.merchant.dao.impl.MerchantRejectedAddressDaoImpl;
import com.belle.yitiansystem.merchant.dao.mapper.MerchantMapper;
import com.belle.yitiansystem.merchant.dao.mapper.MerchantSupplierExpandMapper;
import com.belle.yitiansystem.merchant.dao.mapper.SupplierVoMapper;
import com.belle.yitiansystem.merchant.exception.MerchantSystemException;
import com.belle.yitiansystem.merchant.model.pojo.AttachmentFormVo;
import com.belle.yitiansystem.merchant.model.pojo.SupplierContract;
import com.belle.yitiansystem.merchant.service.IMerchantOperationLogService;
import com.belle.yitiansystem.merchant.service.IMerchantServiceNew;
import com.belle.yitiansystem.merchant.service.ISupplierContractService;
import com.belle.yitiansystem.systemmgmt.model.pojo.SystemmgtUser;
import com.belle.yitiansystem.taobao.exception.BusinessException;
import com.yougou.component.email.api.IEmailApi;
import com.yougou.component.email.model.MailSenderInfo;
import com.yougou.component.email.model.SubjectIdType;
import com.yougou.kaidian.common.util.DateUtil2;
import com.yougou.merchant.api.pic.service.IPictureService;
import com.yougou.merchant.api.pic.service.vo.MerchantPictureCatalog;
import com.yougou.merchant.api.supplier.service.IBrandCatApi;
import com.yougou.merchant.api.supplier.service.IMerchantsApi;
import com.yougou.merchant.api.supplier.service.ISupplierService;
import com.yougou.merchant.api.supplier.vo.BrandCatRelation;
import com.yougou.merchant.api.supplier.vo.BrandVo;
import com.yougou.merchant.api.supplier.vo.CatVo;
import com.yougou.merchant.api.supplier.vo.ContactsVo;
import com.yougou.merchant.api.supplier.vo.MerchantOperationLog;
import com.yougou.merchant.api.supplier.vo.MerchantRejectedAddressVo;
import com.yougou.merchant.api.supplier.vo.MerchantSupplierExpand;
import com.yougou.merchant.api.supplier.vo.MerchantUser;
import com.yougou.merchant.api.supplier.vo.SupplierVo;
import com.yougou.pc.api.ICommodityBaseApiService;
import com.yougou.pc.model.category.Category;
import com.yougou.purchase.api.IPurchaseApiService;
import com.yougou.purchase.model.Supplier;
import com.yougou.purchase.model.SupplierContact;

/**
 * @author huang.tao
 *
 */
@Service
public class MerchantServiceNewImpl implements IMerchantServiceNew {

    private final static Logger logger = LoggerFactory.getLogger(MerchantServiceNewImpl.class);

    @Value("${email.activate.src}")
    private String emailActivateSrc = "http://kaidian.yougou.com/merchants/login/activatemail.sc";

    @Resource
    private ICostSetOfBooksDubboService costSetofBookApi;
    @Resource
    private ISupplierService supplierService;
    @Resource
    private ICommodityBaseApiService commodityBaseApiService;
    @Resource
    private IEmailApi emailApi;
    @Resource
    private IMerchantsApi merchantsApi;
    @Resource
    private IBrandCatApi brandcatApi;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private IPurchaseApiService purchaseApiService;
    @Resource
    private MerchantRejectedAddressDaoImpl merchantRejectedAddressDaoImpl;
    @Resource
    private IMerchantOperationLogService merchantOperationLogService;
    @Resource
    private ISupplierContractService supplierContractService;
    @Resource
    private IPictureService pictureService;
    @Resource
    private MerchantSupplierExpandMapper merchantSupplierExpandMapper;
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private SupplierVoMapper supplierVoMapper;

    // ZT20140417305780 ?ZT20140903837400 
    private final String BOOKCODE_SJXS = "ZT20140417305780,ZT20140903837400";
    // (?)
    private final String BOOKCODE_SJFH = "ZT20120706694264";
    // ?
    private final String BOOKCODE_KJDS_SEZF = "ZT20151203198386";

    // FIXME ?code??
    @Override
    public List<CostSetofBooks> getCostSetofBooksList() throws Exception {
        List<CostSetofBooks> list = costSetofBookApi.queryAllCostSetOfBooks();
        if (CollectionUtils.isNotEmpty(list)) {
            List<CostSetofBooks> tmpList = new ArrayList<CostSetofBooks>();
            for (CostSetofBooks books : list) {
                if (BOOKCODE_SJXS.indexOf(books.getSetOfBooksCode()) != -1) {
                    tmpList.add(books);
                } else if (BOOKCODE_SJFH.equalsIgnoreCase(books.getSetOfBooksCode())) {
                    tmpList.add(books);
                } else if (BOOKCODE_KJDS_SEZF.equalsIgnoreCase(books.getSetOfBooksCode())) {
                    tmpList.add(books);
                }
            }
            list = tmpList;
        }
        return list;
    }

    @Override
    public List<CostSetofBooks> getAllCostSetofBooksList() throws Exception {
        return costSetofBookApi.queryAllCostSetOfBooks();
    }

    @Override
    public SupplierVo getSupplierVoById(String id) {
        return merchantMapper.selectByPrimaryKey(id);
    }

    @Override
    public SupplierVo getMerchantVoByCode(String merchantCode) {
        SupplierVo _vo = new SupplierVo();
        _vo.setSupplierCode(merchantCode);
        _vo.setIsValid(null);
        List<SupplierVo> vos = null;
        try {
            vos = supplierService.querySupplierByVo(_vo);
        } catch (Exception e) {
            logger.error("mct Api?.", e);
        }
        return CollectionUtils.isNotEmpty(vos) ? vos.get(0) : null;
    }

    // @Override
    // public boolean addMerchant(HttpServletRequest req, SupplierSp supplierSp,
    // String bankNoHidden, String catNameHidden) {
    // SupplierVo supplier = new SupplierVo();
    // try {
    // SystemmgtUser user = GetSessionUtil.getSystemUser(req);
    // supplier.setId(UUIDGenerator.getUUID());
    // supplier.setUpdateTimestamp(System.currentTimeMillis());// 
    // supplier.setSupplier(supplierSp.getSupplier());
    // supplier.setSupplierCode(CodeGenerate.getSupplierCode());// ?
    // supplier.setContact(supplierSp.getContact());// ??
    // supplier.setAccount(supplierSp.getAccount());// ?
    // supplier.setSubBank(supplierSp.getSubBank());// ?
    // supplier.setBankLocal(supplierSp.getBankLocal());// 
    // supplier.setBusinessLicense(supplierSp.getBusinessLicense());// ??
    // supplier.setBusinessLocal(supplierSp.getBusinessLocal());// ?
    // supplier.setBusinessValidity(supplierSp.getBusinessValidity());// ?
    // supplier.setTaxpayer(supplierSp.getTaxpayer());// ?
    // supplier.setInstitutional(supplierSp.getInstitutional());// ?
    // supplier.setTallageNo(supplierSp.getTallageNo());// ??
    // supplier.setTaxRate(supplierSp.getTaxRate());// 
    // supplier.setCouponsAllocationProportion(supplierSp.getCouponsAllocationProportion());//
    // 
    // supplier.setIsValid(2);// ?
    // supplier.setSupplierType(supplierSp.getSupplierType());// 
    // supplier.setIsInputYougouWarehouse(supplierSp.getIsInputYougouWarehouse());//
    // ?
    // supplier.setIsUseYougouWms(supplierSp.getIsUseYougouWms());// ?WMS
    // supplier.setSetOfBooksCode(supplierSp.getSetOfBooksCode());// ???
    // supplier.setSetOfBooksName(supplierSp.getSetOfBooksName());// ????
    // supplier.setCreator(user != null ? user.getUsername() : null);// 
    // supplier.setUpdateUser(user != null ? user.getUsername() : null);// 
    // supplier.setUpdateDate(new Date());//  
    // supplier.setDeleteFlag(1);// 
    // supplier.setShipmentType(supplierSp.getShipmentType());// ?
    // //supplier.setTradeCurrency(supplierSp.getTradeCurrency());
    //
    // //
    // com.yougou.merchant.api.supplier.vo.MerchantUser merchantUser = new
    // com.yougou.merchant.api.supplier.vo.MerchantUser();
    // merchantUser.setLoginName(supplierSp.getLoginAccount());// ??
    // // ?MD5
    // String password = Md5Encrypt.md5(supplierSp.getLoginPassword());
    // merchantUser.setId(UUIDGenerator.getUUID());
    // merchantUser.setPassword(password);// ?
    // merchantUser.setMerchantCode(supplier.getSupplierCode());// ?
    // merchantUser.setUserName("");// ?
    // merchantUser.setCreateTime(DateUtil.getDateTime(new Date()));
    // merchantUser.setStatus(1);// ? 1?
    // merchantUser.setIsAdministrator(1); // 1?
    // merchantUser.setDeleteFlag(1);// 
    // merchantUser.setIsYougouAdmin(0);
    // supplier.setUser(merchantUser);
    //
    // this.bulidSupplierBrandCat(supplier, bankNoHidden, catNameHidden);
    //
    // //?
    // supplierService.insertSupplierVoForMerchant(supplier);
    // } catch (Exception e) {
    // logger.error(".", e);
    // return false;
    // }
    // return true;
    // }
    //
    // @Override
    // public boolean updateMerchant(HttpServletRequest req, SupplierSp
    // supplierSp, String bankNoHidden, String catNameHidden) {
    // SupplierVo supplier = new SupplierVo();
    // try {
    // SystemmgtUser user = GetSessionUtil.getSystemUser(req);
    // supplier.setId(supplierSp.getId()); // Id
    // supplier.setUpdateTimestamp(System.currentTimeMillis());// 
    // supplier.setSupplier(supplierSp.getSupplier());// ??
    // supplier.setSupplierCode(supplierSp.getSupplierCode());// ?
    // supplier.setContact(supplierSp.getContact());// ??
    // supplier.setAccount(supplierSp.getAccount());// ?
    // supplier.setSubBank(supplierSp.getSubBank());// ?
    // supplier.setBankLocal(supplierSp.getBankLocal());// 
    // supplier.setBusinessLicense(supplierSp.getBusinessLicense());// ??
    // supplier.setBusinessLocal(supplierSp.getBusinessLocal());// ?
    // supplier.setBusinessValidity(supplierSp.getBusinessValidity());// ?
    // supplier.setTaxpayer(supplierSp.getTaxpayer());// ?
    // supplier.setInstitutional(supplierSp.getInstitutional());// ?
    // supplier.setTallageNo(supplierSp.getTallageNo());// ??
    // supplier.setTaxRate(supplierSp.getTaxRate());// 
    // supplier.setCouponsAllocationProportion(supplierSp.getCouponsAllocationProportion());//
    // 
    // supplier.setIsValid(supplierSp.getIsValid());// ?
    // supplier.setSupplierType(supplierSp.getSupplierType());// 
    // supplier.setIsInputYougouWarehouse(supplierSp.getIsInputYougouWarehouse());//
    // ?
    // supplier.setSetOfBooksCode(supplierSp.getSetOfBooksCode());// ???
    // supplier.setSetOfBooksName(supplierSp.getSetOfBooksName());// ????
    // //supplier.setCreator(user != null ? user.getUsername() : null);// 
    // supplier.setUpdateUser(user != null ? user.getUsername() : null);// 
    // supplier.setUpdateDate(new Date());//  
    // supplier.setDeleteFlag(1);// 
    // supplier.setShipmentType(supplierSp.getShipmentType());// ?
    // supplier.setInventoryCode(supplierSp.getInventoryCode());//?
    // /*if(supplierSp.getTradeCurrency() != null) {
    // supplier.setTradeCurrency(supplierSp.getTradeCurrency());
    // }*/
    // supplier.setIsUseYougouWms(supplierSp.getIsUseYougouWms());//?WMS
    //
    // this.bulidSupplierBrandCat(supplier, bankNoHidden, catNameHidden);
    //
    // //?
    // supplierService.updateSupplierVoForMerchant(supplier);
    // } catch (Exception e) {
    // logger.error(".", e);
    // return false;
    // }
    // return true;
    // }

    @Override
    public boolean updateHistoryMerchants(HttpServletRequest req, SupplierSp supplierSp, String bankNoHidden,
            String catNameHidden) {
        SupplierVo supplier = new SupplierVo();

        try {
            supplier.setId(supplierSp.getId());
            supplier.setSupplierCode(supplierSp.getSupplierCode());
            SystemmgtUser user = GetSessionUtil.getSystemUser(req);
            supplier.setCreator(user != null ? user.getUsername() : null);

            // 
            com.yougou.merchant.api.supplier.vo.MerchantUser merchantUser = new com.yougou.merchant.api.supplier.vo.MerchantUser();
            merchantUser.setLoginName(supplierSp.getLoginAccount());// ??
            // ?MD5
            String password = Md5Encrypt.md5(supplierSp.getLoginPassword());
            merchantUser.setId(UUIDGenerator.getUUID());
            merchantUser.setPassword(password);// ?
            merchantUser.setMerchantCode(supplierSp.getSupplierCode());// ?
            merchantUser.setUserName("");// ?
            merchantUser.setCreateTime(DateUtil.getDateTime(new Date()));
            merchantUser.setStatus(1);// ? 1?
            merchantUser.setIsAdministrator(1); // 1?
            merchantUser.setDeleteFlag(1);// 
            merchantUser.setIsYougouAdmin(0);
            supplier.setUser(merchantUser);

            // ??
            this.bulidSupplierBrandCat(supplier, bankNoHidden, catNameHidden);

            // save
            brandcatApi.updateLimitBrandCatObj(supplier);
        } catch (Exception e) {
            logger.error("???.", e);
            return false;
        }

        return true;
    }

    @Override
    public boolean updateMerchantsBankAndCat(SupplierVo supplier, String bankNoHidden, String catNameHidden) {
        try {
            // ??
            this.bulidSupplierBrandCat(supplier, bankNoHidden, catNameHidden);
            // save
            brandcatApi.updateLimitBrandCatObj(supplier);
        } catch (Exception e) {
            logger.error("??.", e);
            return false;
        }

        return true;
    }

    /**
     * ??
     * 
     * @param supplier
     * @param bankNoHidden
     * @param catNameHidden
     */
    private void bulidSupplierBrandCat(SupplierVo supplier, String bankNoHidden, String catNameHidden) {
        // ?|
        if (StringUtils.isNotBlank(bankNoHidden)) { // bankNoHidden:[Hfjt;POwu;CMs6]
            // ??
            List<BrandVo> _brand_list = new ArrayList<BrandVo>();
            // ?
            List<CatVo> _cat_list = new ArrayList<CatVo>();
            // ??
            List<BrandCatRelation> _brandcat_list = new ArrayList<BrandCatRelation>();

            String[] _arraybank = bankNoHidden.split(";");

            Map<String, String> brankNoIdMap = new HashMap<String, String>();
            Map<String, String> catStructNameIdMap = new HashMap<String, String>();
            if (ArrayUtils.isNotEmpty(_arraybank)) {
                BrandVo _temp_brand = null;
                for (String _brandNo : _arraybank) {
                    _temp_brand = new BrandVo();
                    _temp_brand.setId(UUIDGenerator.getUUID());
                    _temp_brand.setSupplyId(supplier.getId());
                    _temp_brand.setBrandNo(_brandNo);

                    _brand_list.add(_temp_brand);
                    // ??IdNo
                    brankNoIdMap.put(_brandNo, _temp_brand.getId());
                }
                supplier.setBrandVos(_brand_list);
            }

            // 
            // [brand_no;struct_name_brand_no;struct_name_brand_no;struct_name]
            if (StringUtils.isNotBlank(catNameHidden)) {
                Set<String> catSet = new HashSet<String>();
                String[] catNameStr = catNameHidden.split("_");
                for (String string : catNameStr) {
                    String[] catStr = string.split(";");
                    catSet.add(catStr[1]);
                }
                if (CollectionUtils.isNotEmpty(catSet)) {
                    CatVo _temp_cat = null;
                    for (String set : catSet) {
                        _temp_cat = new CatVo();
                        _temp_cat.setId(UUIDGenerator.getUUID());
                        _temp_cat.setSupplyId(supplier.getId());
                        _temp_cat.setStructName(set);
                        Category c = commodityBaseApiService.getCategoryByStructName(set);
                        _temp_cat.setCatNo(c.getCatNo());

                        _cat_list.add(_temp_cat);
                        // ?Idstructname
                        catStructNameIdMap.put(set, _temp_cat.getId());
                    }
                    supplier.setCatVos(_cat_list);
                }

                BrandCatRelation relation = null;
                for (String string : catNameStr) {
                    String[] catStr = string.split(";");
                    relation = new BrandCatRelation();
                    relation.setId(UUIDGenerator.getUUID());
                    relation.setBrandId(brankNoIdMap.get(catStr[0]));
                    relation.setCatId(catStructNameIdMap.get(catStr[1]));
                    _brandcat_list.add(relation);
                }
                if (CollectionUtils.isNotEmpty(_brandcat_list)) {
                    supplier.setBrandcatRelations(_brandcat_list);
                }
            }
        }
    }

    /**  */
    public static final String C_USER_ACTIVATEPASSWORD_ID_TIME = "com.yougou.kaidian.merchant.user.activatepassword.id";

    @Override
    public boolean updateEmail(String id, String email, String operatorName) throws Exception {
        // ??
        String activaturl = emailActivateSrc + "?code=" + id;
        // 
        StringBuffer strB = new StringBuffer();
        strB.append("<img src='http://s1.ygimg.cn/template/common/images/logo-yg.png'>");
        strB.append(
                "<div>--------------------------------------------------------------------------------------------------</div>");
        strB.append("<div><strong> </strong></div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ????!??</div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ????12?1</div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href='" + activaturl + "'>" + activaturl + "</a></div>");

        // ?
        com.yougou.merchant.api.supplier.vo.MerchantUser oldUser = merchantsApi.getMerchantUserById(id);
        if (StringUtils.isNotBlank(oldUser.getEmail()) && oldUser.getEmail().equals(email)
                && oldUser.getEmailstatus() == 1)
            return true;

        com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
        vo.setId(id);
        vo.setEmail(email);
        vo.setEmailstatus(NumberUtils.INTEGER_ZERO); // 
        vo.setDeleteFlag(NumberUtils.INTEGER_ONE);
        merchantsApi.updateMerchantUser(vo);
        this.sandMail(email, "--", strB.toString());
        this.redisTemplate.opsForHash().put(C_USER_ACTIVATEPASSWORD_ID_TIME, id, System.currentTimeMillis());

        // 
        MerchantOperationLog log = new MerchantOperationLog();
        log.setId(UUIDGenerator.getUUID());
        log.setMerchantCode(oldUser.getMerchantCode());
        log.setOperator(operatorName);
        log.setOperated(new Date());
        log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);
        String oldEmail = "";
        if (!StringUtils.isEmpty(oldUser.getEmail())) {
            oldEmail = oldUser.getEmail();
        }
        log.setOperationNotes(MessageFormat
                .format("?{0}?{1}", oldEmail, email));
        merchantsApi.saveMerchantOperationLog(log);

        return true;
    }

    @Override
    public boolean updateEmail(MerchantUser merchantUser, String operatorName) throws Exception {
        String id = merchantUser.getId();
        String email = merchantUser.getEmail().trim();
        // ??
        String activaturl = emailActivateSrc + "?code=" + id;
        // 
        StringBuffer strB = new StringBuffer();
        strB.append("<img src='http://s1.ygimg.cn/template/common/images/logo-yg.png'>");
        strB.append(
                "<div>--------------------------------------------------------------------------------------------------</div>");
        strB.append("<div><strong> </strong></div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ????!??</div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ????12?1</div>");
        strB.append(
                "<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href='" + activaturl + "'>" + activaturl + "</a></div>");

        // ?
        com.yougou.merchant.api.supplier.vo.MerchantUser oldUser = merchantsApi.getMerchantUserById(id);
        if (StringUtils.isNotBlank(oldUser.getEmail()) && oldUser.getEmail().equals(email)
                && oldUser.getEmailstatus() == 1)
            return true;

        com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
        vo.setId(id);
        vo.setEmail(email);
        vo.setEmailstatus(NumberUtils.INTEGER_ZERO); // 
        vo.setDeleteFlag(NumberUtils.INTEGER_ONE);
        merchantsApi.updateMerchantUser(vo);
        this.sandMail(email, "--", strB.toString());
        this.redisTemplate.opsForHash().put(C_USER_ACTIVATEPASSWORD_ID_TIME, id, System.currentTimeMillis());

        // 
        MerchantOperationLog log = new MerchantOperationLog();
        log.setId(UUIDGenerator.getUUID());
        log.setMerchantCode(oldUser.getId());// 
        log.setOperator(operatorName);
        log.setOperated(new Date());
        log.setUserId(id);//
        log.setRemark(merchantUser.getRemark());
        log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);//
        String oldEmail = "";
        if (!StringUtils.isEmpty(oldUser.getEmail())) {
            oldEmail = oldUser.getEmail();
        }
        log.setOperationNotes(MessageFormat
                .format("?{0}?{1}", oldEmail, email));
        // TODO 

        merchantsApi.saveMerchantOperationLog(log);

        return true;
    }

    /**
     * ??
     */
    public void sandMail(String addresss, String title, String content) throws Exception {
        // TODO Auto-generated method stub
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setToAddress(addresss);
        mailInfo.setTitle(title);
        mailInfo.setContent(content);
        mailInfo.setSubject(SubjectIdType.SUBJECT_ID_MERCHANT_FINDPWD);
        mailInfo.setModelType(com.yougou.component.email.model.ModelType.MODEL_TYPE_MERCHANT_FINDPWD);
        emailApi.sendNow(mailInfo);
    }

    @Override
    public boolean deleteMerchantUser(String id, String user) {
        if (StringUtils.isBlank(id))
            return false;

        com.yougou.merchant.api.supplier.vo.MerchantUser tempUser = merchantsApi.getMerchantUserById(id);
        try {
            com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
            vo.setId(id);
            vo.setDeleteFlag(NumberUtils.INTEGER_ZERO);
            merchantsApi.updateMerchantUser(vo);

            // 
            MerchantOperationLog log = new MerchantOperationLog();
            log.setId(UUIDGenerator.getUUID());
            log.setMerchantCode(tempUser.getMerchantCode());
            log.setOperator(user);
            log.setOperated(new Date());
            log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);
            log.setOperationNotes(MessageFormat.format("???{0}", tempUser.getLoginName()));
            merchantsApi.saveMerchantOperationLog(log);
        } catch (Exception e) {
            logger.error(MessageFormat.format("?[{0}]", tempUser.getLoginName()), e);
            return false;
        }
        return true;
    }

    @Override
    public boolean updateMerchantState(String id, Integer status, String user) {
        if (StringUtils.isBlank(id) || null == status)
            return false;

        com.yougou.merchant.api.supplier.vo.MerchantUser tempUser = merchantsApi.getMerchantUserById(id);
        String operationNotes = NumberUtils.INTEGER_ONE.equals(status) ? "????{0}"
                : "????{0}";
        try {
            com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
            vo.setId(id);
            vo.setStatus(status);
            vo.setDeleteFlag(NumberUtils.INTEGER_ONE);
            merchantsApi.updateMerchantUser(vo);

            // 
            MerchantOperationLog log = new MerchantOperationLog();
            log.setId(UUIDGenerator.getUUID());
            log.setMerchantCode(tempUser.getMerchantCode());
            log.setOperator(user);
            log.setOperated(new Date());
            log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);
            log.setOperationNotes(MessageFormat.format(operationNotes, tempUser.getLoginName()));
            merchantsApi.saveMerchantOperationLog(log);
        } catch (Exception e) {
            logger.error(MessageFormat.format(operationNotes, tempUser.getLoginName()) + " ", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean addLinkmanList(SupplierContact contact, String operator) throws Exception {
        Integer result = this.purchaseApiService.insertSupplierContact(contact);

        /** ? Modifier by yang.mq **/
        MerchantOperationLog log = new MerchantOperationLog();
        log.setId(UUIDGenerator.getUUID());
        String supplierCode = this.getSupplierVoById(contact.getSupplyId()).getSupplierCode();
        log.setMerchantCode(supplierCode);
        log.setOperator(operator);
        log.setOperated(new Date());
        log.setOperationType(MerchantOperationLog.OperationType.CONTACT);
        log.setOperationNotes(MessageFormat.format("({0})?({1})",
                new Object[] { supplierCode, contact.getContact() }));
        merchantsApi.saveMerchantOperationLog(log);

        return result > 0 ? true : false;
    }

    @Override
    public boolean updateLinkmanList(SupplierContact contact, String operator) throws Exception {
        Integer result = this.purchaseApiService.updateSupplierContact(contact);

        /** ? Modifier by yang.mq **/
        MerchantOperationLog log = new MerchantOperationLog();
        String supplierCode = this.getSupplierVoById(contact.getSupplyId()).getSupplierCode();
        log.setId(UUIDGenerator.getUUID());
        log.setMerchantCode(supplierCode);
        log.setOperator(operator);
        log.setOperated(new Date());
        log.setOperationType(MerchantOperationLog.OperationType.CONTACT);
        log.setOperationNotes(MessageFormat.format("({0})?({1})",
                new Object[] { supplierCode, contact.getContact() }));
        merchantsApi.saveMerchantOperationLog(log);

        return result > 0 ? true : false;
    }

    /*********  . Start from here . Add by LQ on 20150714 ************/

    @Override
    public int saveMerchantRejectedAddress(MerchantRejectedAddressVo merchantRejectedAddressVo) throws Exception {

        return merchantSupplierExpandMapper.saveRejectedAddress(merchantRejectedAddressVo);
    }

    @Override
    public int updateRejectedAddress(MerchantRejectedAddressVo merchantRejectedAddressVo) throws Exception {
        MerchantRejectedAddressVo vo = merchantSupplierExpandMapper
                .selectRejectedAddressById(merchantRejectedAddressVo.getId());
        if (null != vo) {
            return merchantSupplierExpandMapper.updateRejectedAddress(merchantRejectedAddressVo);
        } else {
            return merchantSupplierExpandMapper.saveRejectedAddress(merchantRejectedAddressVo);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = MerchantSystemException.class)
    public void updateSupplierMerchant(Map<String, Object> params) throws Exception {
        Supplier supplier = (Supplier) params.get("supplier");
        // String supplierId = (String)params.get("supplierId");
        // SupplierContract supplierContract =
        // (SupplierContract)params.get("supplierContract");
        MerchantUser merchantUser = (MerchantUser) params.get("merchantUser");
        MerchantRejectedAddressVo rejectedAddress = (MerchantRejectedAddressVo) params.get("rejectedAddress");
        List<SupplierContact> contactVoList = (List<SupplierContact>) params.get("contactVoList");
        MerchantSupplierExpand supplierExpand = (MerchantSupplierExpand) params.get("supplierExpand");
        MerchantPictureCatalog pictureCatalog = (MerchantPictureCatalog) params.get("pictureCatalog");

        Integer result = purchaseApiService.updateSupplier(supplier);
        if (result > 0) {// ????
            // ???
            // if( null!=contactVoList && 0<contactVoList.size() ){
            // purchaseApiService.insertSupplierContactList( contactVoList);
            // }
            if (null != contactVoList && 0 < contactVoList.size()) {
                for (int i = 0; i < contactVoList.size(); i++) {
                    // 6????
                    Integer updateNum = purchaseApiService.updateSupplierContact(contactVoList.get(i));
                    if (null == updateNum || updateNum < 1) {
                        purchaseApiService.insertSupplierContact(contactVoList.get(i));
                    }
                }
            }
            //
            updateRejectedAddress(rejectedAddress);
            //
            updateMerchantUser(merchantUser);
            // ???
            supplierContractService.updateContractForSupplier(params);

            // 
            SupplierContract supplierContract = (SupplierContract) params.get("supplierContract");
            supplierExpand.setContractRemainingDays(supplierContract.getContractRemainingDays());
            supplierExpand.setMarkRemainingDays(supplierContract.getMarkRemainingDays());
            // ??
            updateSupplierExpand(supplierExpand);

        } else {// ???
            throw new BusinessException("???.");
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = MerchantSystemException.class)
    public String saveSupplierMerchant(Map<String, Object> params) throws Exception {

        Supplier supplier = (Supplier) params.get("supplier");
        MerchantUser merchantUser = (MerchantUser) params.get("merchantUser");
        MerchantRejectedAddressVo rejectedAddress = (MerchantRejectedAddressVo) params.get("rejectedAddress");
        List<SupplierContact> contactVoList = (List<SupplierContact>) params.get("contactVoList");
        MerchantSupplierExpand supplierExpand = (MerchantSupplierExpand) params.get("supplierExpand");
        MerchantPictureCatalog pictureCatalog = (MerchantPictureCatalog) params.get("pictureCatalog");
        Integer result = purchaseApiService.insertSupplier(supplier);
        if (result > 0) {// ????
            // ???
            // if( null!=contactVoList && 0<contactVoList.size() ){
            // purchaseApiService.insertSupplierContactList( contactVoList);
            // }
            // try {
            for (int i = 0; i < contactVoList.size(); i++) {
                purchaseApiService.insertSupplierContact(contactVoList.get(i));
            }
            //
            saveMerchantRejectedAddress(rejectedAddress);

            //
            saveMerchantUser(merchantUser);
            // ???
            supplierContractService.saveContractForSupplier(params);

            // 
            SupplierContract supplierContract = (SupplierContract) params.get("supplierContract");
            supplierExpand.setContractRemainingDays(supplierContract.getContractRemainingDays());
            supplierExpand.setMarkRemainingDays(supplierContract.getMarkRemainingDays());
            // ??
            saveSupplierExpand(supplierExpand);
            // ?
            pictureService.insertPicCatalog(pictureCatalog);
            // } catch (Exception e) {
            // // 
            // supplier.setDeleteFlag( MerchantConstant.DELETED );
            // supplier.setSupplier("?");
            // purchaseApiService.updateSupplier(supplier);
            // //throw new BusinessException(e.getMessage());
            // throw new BusinessException("?.");
            // }

        } else {// ???
            throw new BusinessException("???.");
        }

        return "success";
    }

    /**
     * ???
     */
    @Override
    public MerchantUser getMerchantsBySuppliceCode(String supplierCode) {
        MerchantUser merchantUser = new MerchantUser();
        merchantUser.setMerchantCode(supplierCode);
        merchantUser.setDeleteFlag(MerchantConstant.NOT_DELETED);
        merchantUser.setIsAdministrator(MerchantConstant.YES);
        List<MerchantUser> merchantUserList = merchantSupplierExpandMapper.queryMerchantUserList(merchantUser);
        if (merchantUserList != null && merchantUserList.size() > 0) {
            merchantUser = merchantUserList.get(0);
        }
        return merchantUser;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = MerchantSystemException.class)
    public int saveMerchantUser(MerchantUser merchantUser) throws BusinessException {
        int result;
        try {
            result = merchantSupplierExpandMapper.insertMerchantUser(merchantUser);
        } catch (Exception e) {
            logger.error("???", e);
            throw new BusinessException("???");
        }

        return result;
    }

    // @Override
    // public int saveMerchantUser(MerchantUser merchantUser) throws Exception{
    // return merchantSupplierExpandMapper.insertMerchantUser(merchantUser);
    //
    // }

    // @Override
    // public int updateMerchantUser(MerchantUser merchantUser) throws
    // BusinessException {
    // int result;
    // try {
    // result = merchantSupplierExpandMapper.updateMerchantUser(merchantUser);
    // } catch (Exception e) {
    // logger.error("???",e);
    // throw new BusinessException("???");
    // }
    //
    // return result;
    // }
    @Override
    public int updateMerchantUser(MerchantUser merchantUser) {
        MerchantUser vo = merchantSupplierExpandMapper.selectMerchantUserById(merchantUser.getId());
        if (null != vo) {
            return merchantSupplierExpandMapper.updateMerchantUser(merchantUser);
        } else {
            return merchantSupplierExpandMapper.insertMerchantUser(merchantUser);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = MerchantSystemException.class)
    public int saveSupplierExpand(MerchantSupplierExpand supplierExpand) throws BusinessException {
        int result;
        try {
            result = merchantSupplierExpandMapper.insert(supplierExpand);
        } catch (Exception e) {
            logger.error("???", e);
            throw new BusinessException("???");
        }

        return result;
    }

    // @Override
    // public int saveSupplierExpand( MerchantSupplierExpand supplierExpand )
    // throws Exception {
    // // TODO Auto-generated method stub
    // return merchantSupplierExpandMapper.insert(supplierExpand);
    // }

    @Override
    public int updateSupplierExpand(MerchantSupplierExpand supplierExpand) throws BusinessException {
        int result;
        try {
            result = merchantSupplierExpandMapper.updateByPrimaryKeySelective(supplierExpand);
        } catch (Exception e) {
            logger.error("??", e);
            throw new BusinessException("??");
        }
        return result;
    }

    // @Override
    // public int updateSupplierExpand( MerchantSupplierExpand supplierExpand )
    // throws Exception {
    // // TODO Auto-generated method stub
    // return
    // merchantSupplierExpandMapper.updateByPrimaryKeySelective(supplierExpand);
    // }

    @Override
    public MerchantSupplierExpand getSupplierExpandVoById(String supplierId) {
        MerchantSupplierExpand supplierExpand = merchantSupplierExpandMapper.selectBySupplierId(supplierId);
        if (supplierExpand != null) {
            return supplierExpand;
        } else {
            return null;
        }
    }

    @Override
    public MerchantSupplierExpand getSupplierExpandVoByCode(String merchantCode) {
        MerchantSupplierExpand supplierExpand = merchantSupplierExpandMapper.selectByMerchantCode(merchantCode);
        if (supplierExpand != null) {
            return supplierExpand;
        } else {
            return null;
        }
    }

    @Override
    public List<ContactsVo> getContactsBySupplierId(String supplierId) {
        return merchantSupplierExpandMapper.getContactsBySupplierId(supplierId);
    }

    @Override
    public MerchantRejectedAddressVo getRejectedAddressBySupplierCode(String supplierCode) {
        List<MerchantRejectedAddressVo> list = merchantSupplierExpandMapper
                .getRejectedAddressBySupplierCode(supplierCode);
        if (null != list && 0 < list.size()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void insertMerchantLog(MerchantOperationLog log) {
        supplierVoMapper.insertMerchantLog(log);
    }

    /**
     * 
     * 
     * @param params
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = MerchantSystemException.class)
    public void updateNatural(MerchantSupplierExpand supplierExpand, AttachmentFormVo attachmentFormVo)
            throws Exception {

        supplierContractService.saveOrUpdateByAttachmentFormVo(attachmentFormVo);
        if (null != attachmentFormVo.getMarkRemainingDays()) {
            supplierExpand.setMarkRemainingDays(attachmentFormVo.getMarkRemainingDays());
        }
        supplierExpand.setUptateTime(DateUtil2.getCurrentDateTimeToStr2());
        merchantSupplierExpandMapper.updateByPrimaryKeySelective(supplierExpand);
        //???
        SupplierVo supplier = new SupplierVo();
        supplier.setId(attachmentFormVo.getSupplierId());
        updateMerchantsBankAndCat(supplier, attachmentFormVo.getBankNoHidden(),
                attachmentFormVo.getCatNameHidden());
    }

    @Override
    public boolean updateMobile(String id, String mobile, String loginName) {
        com.yougou.merchant.api.supplier.vo.MerchantUser oldUser = merchantsApi.getMerchantUserById(id);
        if (StringUtils.isNotBlank(oldUser.getMobileCode()) && oldUser.getMobileCode().equals(mobile))
            return true;

        com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
        vo.setId(id);
        vo.setMobileCode(mobile);
        vo.setDeleteFlag(NumberUtils.INTEGER_ONE);
        merchantsApi.updateMerchantUser(vo);
        // 
        MerchantOperationLog log = new MerchantOperationLog();
        log.setId(UUIDGenerator.getUUID());
        log.setMerchantCode(oldUser.getMerchantCode());
        log.setOperator(loginName);
        log.setOperated(new Date());
        log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);
        String oldEmail = "";
        if (!StringUtils.isEmpty(oldUser.getMobileCode())) {
            oldEmail = oldUser.getMobileCode();
        }
        log.setOperationNotes(
                MessageFormat.format("???{0}?{1}", oldEmail, mobile));
        merchantsApi.saveMerchantOperationLog(log);
        return true;
    }

    @Override
    public boolean updateMobile(MerchantUser merchantUser, String loginName) {
        String id = merchantUser.getId();
        String mobile = merchantUser.getMobileCode().trim();
        com.yougou.merchant.api.supplier.vo.MerchantUser oldUser = merchantsApi.getMerchantUserById(id);
        if (StringUtils.isNotBlank(oldUser.getMobileCode()) && oldUser.getMobileCode().equals(mobile))
            return true;

        com.yougou.merchant.api.supplier.vo.MerchantUser vo = new com.yougou.merchant.api.supplier.vo.MerchantUser();
        vo.setId(id);
        vo.setMobileCode(mobile);
        vo.setDeleteFlag(NumberUtils.INTEGER_ONE);
        merchantsApi.updateMerchantUser(vo);
        // 
        MerchantOperationLog log = new MerchantOperationLog();
        log.setId(UUIDGenerator.getUUID());
        log.setMerchantCode(oldUser.getMerchantCode());
        log.setUserId(id);//
        log.setOperator(loginName);
        log.setOperated(new Date());
        log.setRemark(merchantUser.getRemark());
        log.setOperationType(MerchantOperationLog.OperationType.ACCOUNT);//
        String oldEmail = "";
        if (!StringUtils.isEmpty(oldUser.getMobileCode())) {
            oldEmail = oldUser.getMobileCode();
        }
        log.setOperationNotes(
                MessageFormat.format("???{0}?{1}", oldEmail, mobile));
        // TODO 

        merchantsApi.saveMerchantOperationLog(log);
        return true;
    }

    @Override
    public boolean queryExistContactOfThisType(SupplierContact contact) throws Exception {
        // TODO Auto-generated method stub
        int count = merchantSupplierExpandMapper.queryExistContactOfThisType(contact.getSupplyId(),
                contact.getType());

        if (count > 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * ??
     * @param map
     * @return
     */
    public List<Map<String, Object>> getMerchantInfo(Map<String, List<String>> map) {
        return merchantMapper.getMerchantInfo(map);

    }
}