Java tutorial
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.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.belle.infrastructure.orm.basedao.PageFinder; import com.belle.infrastructure.orm.basedao.Query; import com.belle.infrastructure.util.GetSessionUtil; import com.belle.other.model.pojo.SupplierContactSp; import com.belle.other.model.pojo.SupplierContract; import com.belle.other.model.pojo.SupplierSp; import com.belle.other.model.pojo.SupplierSp.CooperationModel; import com.belle.yitiansystem.merchant.dao.IMerchantOperationLogDao; import com.belle.yitiansystem.merchant.dao.mapper.MerchantOperationLogMapper; import com.belle.yitiansystem.merchant.model.pojo.MerchantOperationLog; import com.belle.yitiansystem.merchant.model.pojo.MerchantOperationLog.OperationType; import com.belle.yitiansystem.merchant.model.pojo.MerchantRejectedAddress; import com.belle.yitiansystem.merchant.model.pojo.MerchantUser; import com.belle.yitiansystem.merchant.service.IMerchantOperationLogService; import com.yougou.fss.api.vo.ShopVO; import com.yougou.merchant.api.supplier.vo.SupplierVo; @Service public class MerchantOperationLogServiceImpl implements IMerchantOperationLogService { private static final Map<String, String> BASIC_DATA_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final Map<String, String> ACCOUNT_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final Map<String, String> CONTACT_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final Map<String, String> CONTRACT_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final Map<String, String> AFTER_SERVICE_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final Map<String, String> SHOP_TRANSLATABLE_FIELDS = new HashMap<String, String>(); private static final String LINE_SEPARATOR = System.getProperty("line.separator"); static { // BASIC_DATA_TRANSLATABLE_FIELDS.put("supplier", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("supplierType", ""); BASIC_DATA_TRANSLATABLE_FIELDS.put("isInputYougouWarehouse", ""); BASIC_DATA_TRANSLATABLE_FIELDS.put("setOfBooksName", "???"); BASIC_DATA_TRANSLATABLE_FIELDS.put("taxRate", ""); BASIC_DATA_TRANSLATABLE_FIELDS.put("shipmentType", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("couponsAllocationProportion", ""); BASIC_DATA_TRANSLATABLE_FIELDS.put("contact", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("businessLocal", "?"); BASIC_DATA_TRANSLATABLE_FIELDS.put("account", "???"); BASIC_DATA_TRANSLATABLE_FIELDS.put("businessValidity", "?"); BASIC_DATA_TRANSLATABLE_FIELDS.put("subBank", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("tallageNo", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("bankLocal", ""); BASIC_DATA_TRANSLATABLE_FIELDS.put("institutional", "?"); BASIC_DATA_TRANSLATABLE_FIELDS.put("businessLicense", "??"); BASIC_DATA_TRANSLATABLE_FIELDS.put("taxpayer", "?"); // ? ACCOUNT_TRANSLATABLE_FIELDS.put("userName", "???"); ACCOUNT_TRANSLATABLE_FIELDS.put("loginName", "??"); ACCOUNT_TRANSLATABLE_FIELDS.put("password", "?"); ACCOUNT_TRANSLATABLE_FIELDS.put("status", "??"); ACCOUNT_TRANSLATABLE_FIELDS.put("isAdministrator", "??"); ACCOUNT_TRANSLATABLE_FIELDS.put("deleteFlag", "?"); ACCOUNT_TRANSLATABLE_FIELDS.put("isYougouAdmin", "??"); // ? CONTACT_TRANSLATABLE_FIELDS.put("contact", "??"); CONTACT_TRANSLATABLE_FIELDS.put("type", ""); CONTACT_TRANSLATABLE_FIELDS.put("telePhone", "???"); CONTACT_TRANSLATABLE_FIELDS.put("mobilePhone", "??"); CONTACT_TRANSLATABLE_FIELDS.put("fax", "??"); CONTACT_TRANSLATABLE_FIELDS.put("email", "?"); CONTACT_TRANSLATABLE_FIELDS.put("address", "?"); // ?? CONTRACT_TRANSLATABLE_FIELDS.put("contractNo", "???"); CONTRACT_TRANSLATABLE_FIELDS.put("effectiveDate", "()"); CONTRACT_TRANSLATABLE_FIELDS.put("failureDate", "()"); CONTRACT_TRANSLATABLE_FIELDS.put("clearingForm", "?"); // ? AFTER_SERVICE_TRANSLATABLE_FIELDS.put("supplierName", "??"); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("consigneeName", "??"); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("consigneePhone", ""); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("consigneeTell", "?"); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("warehousePostcode", ""); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("warehouseArea", ""); AFTER_SERVICE_TRANSLATABLE_FIELDS.put("warehouseAdress", "?"); // SHOP_TRANSLATABLE_FIELDS.put("shopName", "??"); SHOP_TRANSLATABLE_FIELDS.put("shopURL", "URL"); SHOP_TRANSLATABLE_FIELDS.put("access", "Acess?"); SHOP_TRANSLATABLE_FIELDS.put("auditStatus", "?"); SHOP_TRANSLATABLE_FIELDS.put("auditFlag", ""); } @Resource private IMerchantOperationLogDao merchantOperationLogDao; @Resource private MerchantOperationLogMapper logMapper; @Override @Transactional public void saveMerchantOperationLog(MerchantOperationLog operationLog) throws Exception { merchantOperationLogDao.save(operationLog); } @Override public PageFinder<MerchantOperationLog> queryMerchantOperationLog(String merchantCode, Query query) throws Exception { Session session = null; try { session = merchantOperationLogDao.getHibernateSession(); Criteria criteria = session.createCriteria(MerchantOperationLog.class); criteria.add(Restrictions.eq("merchantCode", merchantCode)); criteria.addOrder(Order.desc("operated")); return merchantOperationLogDao.pagedByCriteria(criteria, query.getPage(), query.getPageSize()); } finally { merchantOperationLogDao.releaseHibernateSession(session); } } @Override public PageFinder<MerchantOperationLog> queryMerchantOperationLogByOperationType(String merchantCode, com.yougou.merchant.api.supplier.vo.MerchantOperationLog.OperationType operationType, Query query) throws Exception { Session session = null; try { session = merchantOperationLogDao.getHibernateSession(); Criteria criteria = session.createCriteria(MerchantOperationLog.class); if (StringUtils.isNotEmpty(merchantCode)) { criteria.add(Restrictions.eq("merchantCode", merchantCode)); } criteria.add(Restrictions.eq("operationType", operationType)); criteria.addOrder(Order.desc("operated")); return merchantOperationLogDao.pagedByCriteria(criteria, query.getPage(), query.getPageSize()); } finally { merchantOperationLogDao.releaseHibernateSession(session); } } @Override public String buildMerchantBasicDataOperationNotes(SupplierSp source, SupplierSp target) throws Exception { if (target == null) { throw new NullPointerException("target"); } if (source == null) { return ""; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : BASIC_DATA_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("isInputYougouWarehouse", entry.getKey())) { CooperationModel[] cooperationModels = SupplierSp.CooperationModel.values(); o1 = cooperationModels[(Integer) o1].getDescription(); o2 = cooperationModels[(Integer) o2].getDescription(); } else if (StringUtils.equals("shipmentType", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : ""; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : ""; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); } @Override public String buildMerchantAccountOperationNotes(MerchantUser source, MerchantUser target) throws Exception { if (target == null) { throw new NullPointerException("target"); } if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : ACCOUNT_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("status", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "?" : "?"; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "?" : "?"; } else if (StringUtils.equals("isAdministrator", entry.getKey()) || StringUtils.equals("deleteFlag", entry.getKey()) || StringUtils.equals("isYougouAdmin", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : "?"; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : "?"; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); } @Override public String buildMerchantContactOperationNotes(SupplierContactSp source, SupplierContactSp target) throws Exception { if (target == null) { throw new NullPointerException("target"); } if (source == null) { return "?"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : CONTACT_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("type", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : ObjectUtils.equals(2, o1) ? "?" : ObjectUtils.equals(3, o1) ? "" : ObjectUtils.equals(4, o1) ? "" : ObjectUtils.equals(5, o1) ? "" : ""; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : ObjectUtils.equals(2, o2) ? "?" : ObjectUtils.equals(3, o2) ? "" : ObjectUtils.equals(4, o2) ? "" : ObjectUtils.equals(5, o2) ? "" : ""; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); } @Override public String buildMerchantContractOperationNotes(SupplierContract source, SupplierContract target) throws Exception { if (target == null) { throw new NullPointerException("target"); } if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : CONTRACT_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("clearingForm", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : ObjectUtils.equals(2, o1) ? "" : ObjectUtils.equals(3, o1) ? "?" : ObjectUtils.equals(4, o1) ? "" : ""; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : ObjectUtils.equals(2, o2) ? "" : ObjectUtils.equals(3, o2) ? "?" : ObjectUtils.equals(4, o2) ? "" : ""; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); } @Override public String buildMerchantAfterServiceAddrOperationNotes(MerchantRejectedAddress source, MerchantRejectedAddress target) throws Exception { if (target == null) { throw new NullPointerException("target"); } if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : AFTER_SERVICE_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); } /** * ? * @see com.belle.yitiansystem.merchant.service.IMerchantOperationLogService#buildMerchantShopOperationNotes(java.lang.Object, com.yougou.fss.api.vo.ShopVO) */ @Override public String buildMerchantShopOperationNotes(ShopVO source, ShopVO target) throws Exception { if (target == null) { //throw new NullPointerException("target"); return "?" + source.getShopName() + ""; } if (source == null) { return "?" + target.getShopName() + ""; } //String str1 = null; //String str2 = null; StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : SHOP_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if ("auditStatus".equals(entry.getKey())) { if ((Integer) o1 == 0) { o1 = "??"; } else if ((Integer) o1 == 1) { o1 = ""; } else if ((Integer) o1 == 3) { o1 = ""; } else { o1 = ""; } if ((Integer) o2 == 0) { o2 = "??"; } else if ((Integer) o2 == 1) { o2 = ""; } else if ((Integer) o2 == 3) { o2 = ""; } else { o2 = ""; } } else if ("access".equals(entry.getKey())) { if ("Y".equals((String) o1)) { o1 = "?"; } else { o1 = ""; } if ("Y".equals((String) o2)) { o2 = "?"; } else { o2 = ""; } } else if ("auditFlag".equals(entry.getKey())) { if ("0".equals((String) o1)) { o1 = "?"; } else { o1 = "??"; } if ("0".equals((String) o2)) { o2 = "?"; } else { o2 = "??"; } } if (!ObjectUtils.equals(o1, o2)) { sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } if (sb.toString().length() <= 0) { sb.append("?"); } return sb.toString(); } /** * ? * @see com.belle.yitiansystem.merchant.service.IMerchantOperationLogService#buildMerchantShopOperationNotes(java.lang.Object, com.yougou.fss.api.vo.ShopVO) */ @Override public String buildMerchantShopRuleOperationNotes(String source, String target) throws Exception { StringBuilder sb = new StringBuilder(); if (!ObjectUtils.equals(source, target)) { sb.append(MessageFormat.format("{0}??{1}?{2}{3}", "???", source, target, LINE_SEPARATOR)); } if (sb.toString().length() <= 0) { sb.append("?"); } return sb.toString(); } /** * TODO OperationType * @see com.belle.yitiansystem.merchant.service.IMerchantOperationLogService#queryMerchantOperationLogByOperationType(java.lang.String, com.belle.yitiansystem.merchant.model.pojo.MerchantOperationLog.OperationType, com.belle.infrastructure.orm.basedao.Query) */ @Override public PageFinder<MerchantOperationLog> queryMerchantOperationLogByOperationType(String merchantCode, OperationType operationType, Query query) throws Exception { Session session = null; try { session = merchantOperationLogDao.getHibernateSession(); Criteria criteria = session.createCriteria(MerchantOperationLog.class); if (StringUtils.isNotEmpty(merchantCode)) { criteria.add(Restrictions.eq("merchantCode", merchantCode)); } criteria.add(Restrictions.eq("operationType", operationType)); criteria.addOrder(Order.desc("operated")); return merchantOperationLogDao.pagedByCriteria(criteria, query.getPage(), query.getPageSize()); } finally { merchantOperationLogDao.releaseHibernateSession(session); } } @Override public PageFinder<MerchantOperationLog> queryMerchantOperationLog(SupplierVo supplierVo, Query query) { //Amend by LQ on 20150420 int count = 0; count = logMapper.selectMerchantOperationLogCount(supplierVo.getSupplierCode(), supplierVo.getId()); if (count < 1) { return new PageFinder<MerchantOperationLog>(query.getPage(), query.getPageSize(), 0, null); } else { List<Map> logList = logMapper.selectMerchantOperationLog(supplierVo.getSupplierCode(), supplierVo.getId(), query); List<MerchantOperationLog> resultList = new ArrayList<MerchantOperationLog>(); for (Map map : logList) { MerchantOperationLog log = new MerchantOperationLog(); log.setMerchantCode((String) map.get("merchant_code")); log.setId((String) map.get("id")); log.setOperator((String) map.get("operator")); log.setOperated((Date) map.get("operated")); log.setOperationNotes((String) map.get("operation_notes")); String type = (String) map.get("operation_type"); log.setOperationType(Enum.valueOf(OperationType.class, type)); resultList.add(log); } return new PageFinder<MerchantOperationLog>(query.getPage(), query.getPageSize(), count, resultList); } } @Override @Transactional public void addMerchantOperationLog(String merchantCode, OperationType type, String msg, HttpServletRequest request) { MerchantOperationLog log = new MerchantOperationLog(); log.setMerchantCode(merchantCode); log.setOperationType(type); log.setOperator(GetSessionUtil.getSystemUser(request).getLoginName()); log.setOperated(new Date()); log.setOperationNotes(msg); try { merchantOperationLogDao.save(log); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override @Transactional public void addMerchantOperationLog(String merchantCode, OperationType type, String msg, String userName) { MerchantOperationLog log = new MerchantOperationLog(); log.setMerchantCode(merchantCode); log.setOperationType(type); log.setOperator(userName); log.setOperated(new Date()); log.setOperationNotes(msg); try { merchantOperationLogDao.save(log); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public PageFinder<MerchantOperationLog> queryMerchantOperationLogByUser(String userId, com.yougou.merchant.api.supplier.vo.MerchantOperationLog.OperationType operationType, Query query) { Session session = null; try { session = merchantOperationLogDao.getHibernateSession(); Criteria criteria = session.createCriteria(MerchantOperationLog.class); if (StringUtils.isNotEmpty(userId)) { criteria.add(Restrictions.eq("userId", userId)); } criteria.add(Restrictions.eq("operationType", operationType)); criteria.addOrder(Order.desc("operated")); return merchantOperationLogDao.pagedByCriteria(criteria, query.getPage(), query.getPageSize()); } finally { merchantOperationLogDao.releaseHibernateSession(session); } } }