PickUpPointManagerImpl.java 9.33 KB
package com.diligrp.website.manager.impl;

import com.alibaba.rocketmq.common.message.Message;
import com.diligrp.util.rocketMQUtil.DiliMQProducer;
import com.diligrp.website.dao.PickUpCityDao;
import com.diligrp.website.dao.PickUpPersonDao;
import com.diligrp.website.dao.PickUpPointDao;
import com.diligrp.website.domain.PickUpCity;
import com.diligrp.website.domain.PickUpPerson;
import com.diligrp.website.domain.PickUpPointDomain;
import com.diligrp.website.manager.PickUpPointManager;
import com.diligrp.website.util.dao.BaseQuery;
import com.diligrp.website.util.redis.RedisUtil;
import com.google.common.base.Objects;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <B>Description</B> TODO <br />
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * 
 * @createTime 2014年8月13日 下午4:59:13
 * @author liujie
 */
@Component
public class PickUpPointManagerImpl implements PickUpPointManager {

	@Resource
	private PickUpPointDao pickUpPointDao;
	@Resource
	private PickUpCityDao pickUpCityDao;
	@Resource
	private PickUpPersonDao pickUpPersonDao;
	private final static String pickup_key = "dili_redis_service_";
	private final static Integer cityRedisKeyTime = 86400;
	@Resource(name = "redisUtils")
	private RedisUtil redisUtil;
	@Resource
	private DiliMQProducer diliMQProducer;
	@Value("${mq.manweb.pickupInfoChange.pnr.tag}")
	private String PICKUP_CHANGE_TAG;
	@Value("${mq.manweb.pickupInfoChange.topic}")
	private String PICKUP_CHANGE_TOPIC;
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getPickInfo(com.diligrp
	 * .website.util.dao.BaseQuery)
	 */
	@Override
	public List<PickUpPointDomain> getPickInfo(BaseQuery bq) throws Exception {
		return pickUpPointDao.getPickInfo(bq);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getCountPickInfo(com.diligrp
	 * .website.util.dao.BaseQuery)
	 */
	@Override
	public Integer getCountPickInfo(BaseQuery bq) throws Exception {
		return pickUpPointDao.getCountPickInfo(bq);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#savePickUpInfo(com.diligrp
	 * .website.domain.PickUpPointDomain)
	 */
	@Override
	@Transactional
	public boolean savePickUpInfo(PickUpPointDomain pickUpPointDomain) {
		try {
			List<PickUpCity> pickUpCityList = pickUpPointDomain.getCityList();
			List<PickUpPerson> pickUpPersonList = pickUpPointDomain.getPersonList();
			Long pickId = pickUpPointDao.savePickUpInfo(pickUpPointDomain);
			if (pickUpCityList != null) {
				for (int i = 0; i < pickUpCityList.size(); i++) {
					PickUpCity city = pickUpCityList.get(i);
					city.setPickId(pickId);
					pickUpCityDao.savePickUpCity(city);
				}
			}
			for (int i = 0; i < pickUpPersonList.size(); i++) {
				PickUpPerson person = pickUpPersonList.get(i);
				person.setPickid(pickId);
				pickUpPersonDao.savePickUpPerson(person);
			}
			try {
				Message msg = new Message(PICKUP_CHANGE_TOPIC, PICKUP_CHANGE_TAG, Json.toJson(pickUpPointDomain).getBytes("utf-8"));
				diliMQProducer.sendMsg(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#modifyPickUpInfo(com.diligrp
	 * .website.domain.PickUpPointDomain)
	 */
	@Override
	@Transactional
	public boolean modifyPickUpInfo(PickUpPointDomain pickUpPointDomain)
			throws Exception {
		List<PickUpCity> pickUpCityList = pickUpPointDomain.getCityList();
		List<PickUpPerson> pickUpPersonList = pickUpPointDomain.getPersonList();

		if (Objects.equal(pickUpPointDomain.getIsMarket(), 1)) {
			pickUpCityDao.deletePickUpCityById(pickUpPointDomain.getId());
		}
		pickUpPersonDao.deletePickUpPerson(pickUpPointDomain.getId());
		if (pickUpCityList != null) {
			for (int i = 0; i < pickUpCityList.size(); i++) {
				PickUpCity city = pickUpCityList.get(i);
				city.setPickId(pickUpPointDomain.getId());
				pickUpCityDao.savePickUpCity(city);
			}
		}
		for (int i = 0; i < pickUpPersonList.size(); i++) {
			PickUpPerson person = pickUpPersonList.get(i);
			person.setPickid(pickUpPointDomain.getId());
			pickUpPersonDao.savePickUpPerson(person);
		}
		boolean flag  = pickUpPointDao.modifyPickUpInfo(pickUpPointDomain);
		if(flag){
			redisUtil.deleteByKey(pickup_key+pickUpPointDomain.getId());
			redisUtil.deleteByKey(pickup_key+pickUpPointDomain.getPickCode());
		}
		try {
			Message msg = new Message(PICKUP_CHANGE_TOPIC, PICKUP_CHANGE_TAG, Json.toJson(pickUpPointDomain).getBytes("utf-8"));
			diliMQProducer.sendMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#modifyPickUpInfoDeafult
	 * (com.diligrp.website.domain.PickUpPointDomain)
	 */
	@Override
	@Transactional
	public boolean modifyPickUpInfoDeafult(Long id) throws Exception {
		pickUpPointDao.modifyPickUpInfoDeafult();
		boolean flag = pickUpPointDao.modifyStateByPickUpId(id);
		return flag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getPickUpPointDomainInfo
	 * (java.lang.Long)
	 */
	@Override
	public PickUpPointDomain getPickUpPointDomainInfo(Long id) throws Exception {
//		PickUpPointDomain domain = redisUtil.getObject(pickup_key+id, PickUpPointDomain.class);
//		if(domain ==null){
//			domain = pickUpPointDao.getPickUpPointDomainInfo(id);
//			if(domain!=null){
//				redisUtil.setObjectByExpire(pickup_key+id, domain, cityRedisKeyTime);
//			}
//		}
//		return domain;
		return pickUpPointDao.getPickUpPointDomainInfo(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getRelationPickInfo(java
	 * .lang.Long)
	 */
	@Override
	public List<PickUpPointDomain> getRelationPickInfo(Long id)
			throws Exception {
		return pickUpPointDao.getRelationPickInfo(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getNotMarketPickInfo()
	 */
	@Override
	public List<PickUpPointDomain> getNotMarketPickInfo() throws Exception {
		return pickUpPointDao.getNotMarketPickInfo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#modifyRelationPoint(java
	 * .util.Map)
	 */
	@Override
	@Transactional
	public boolean modifyRelationPoint(List<Long> list, Long pickId) {
		try {
			pickUpPointDao.modifyRelationPointParent(pickId);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map<String, Long> map = new HashMap<String, Long>();
					map.put("pickId", pickId);
					map.put("id", list.get(i));
					pickUpPointDao.modifyRelationPoint(map);
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getIsMarketPickUpInfo()
	 */
	@Override
	public List<PickUpPointDomain> getIsMarketPickUpInfo() throws Exception {
		return pickUpPointDao.getIsMarketPickUpInfo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getIsNotMarketPickUpInfo()
	 */
	@Override
	public List<PickUpPointDomain> getIsNotMarketPickUpInfo() throws Exception {
		return pickUpPointDao.getIsNotMarketPickUpInfo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getAllMarketPickUpInfo()
	 */
	@Override
	public List<PickUpPointDomain> getAllMarketPickUpInfo() throws Exception {
		return pickUpPointDao.getAllMarketPickUpInfo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.diligrp.website.manager.PickUpPointManager#getCodeByPickUpInfo(java
	 * .lang.String)
	 */
	@Override
	public PickUpPointDomain getCodeByPickUpInfo(String code) throws Exception {
		PickUpPointDomain domain = redisUtil.getObject(pickup_key+code, PickUpPointDomain.class);
		if(domain ==null){
			domain = pickUpPointDao.getCodeByPickUpInfo(code);
			if(domain!=null){
				redisUtil.setObjectByExpire(pickup_key+code, domain, cityRedisKeyTime);
			}
		
		}
		return domain;
	}

	@Override
	public PickUpPointDomain getDefaultPickUpInfo() throws Exception {
		return pickUpPointDao.getDefaultPickUpInfo();
	}

	@Override
	public List<PickUpPointDomain> getPickUpInfoByName(String pickName) {
		return pickUpPointDao.getPickUpInfoByName(pickName);
	}

	@Override
	public List<PickUpPointDomain> getRecommendPickUp() {
		return pickUpPointDao.getRecommendPickUp();
	}

	@Override
	public List<PickUpPointDomain> getPickUpListByCityId(Long cityId) {
		return pickUpPointDao.getPickUpListByCityId(cityId);
	}

	@Override
	public boolean configRecommend(PickUpPointDomain pickUpPointDomain) throws Exception {
		return pickUpPointDao.modifyPickUpInfo(pickUpPointDomain);
	}

	@Override
	public List<PickUpPointDomain> getPickUpInfoByCityName(String cityName) {
		return pickUpPointDao.getPickUpInfoByCityName(cityName);
	}

	@Override
	public List<PickUpPointDomain> getAllPickUpListByCityId(Long cityId) {
		return pickUpPointDao.getAllPickUpListByCityId(cityId);
	}
}