package com.yeshi.fanli.service.impl.goods;
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.hibernate.HibernateException;
|
import org.hibernate.Query;
|
import org.hibernate.Session;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dao.goods.RecommendSectionDao;
|
import com.yeshi.fanli.dao.mybatis.share.ShareMapper;
|
import com.yeshi.fanli.entity.admin.RecommendSectionAdmin;
|
import com.yeshi.fanli.entity.bus.recommend.RecommendSection;
|
import com.yeshi.fanli.entity.bus.recommend.RecommendSectionDetail;
|
import com.yeshi.fanli.entity.bus.su.recommend.SuperRecommendSection;
|
import com.yeshi.fanli.entity.system.BusinessSystem;
|
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
|
import com.yeshi.fanli.service.inter.goods.RecommendSectionDetailService;
|
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
|
import com.yeshi.fanli.service.inter.goods.RecommendSectionService;
|
import com.yeshi.fanli.service.inter.goods.SuperRecommendSectionService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.Utils;
|
|
@Service
|
public class RecommendSectionServiceImpl implements RecommendSectionService {
|
|
@Resource
|
private ShareMapper shareMapper;
|
|
@Resource
|
private RecommendSectionDao recommendSectionDao;
|
|
@Resource
|
private SuperRecommendSectionService superRecommendSectionService;
|
|
@Resource
|
private RecommendSectionGoodsService recommendSectionGoodsService;
|
|
@Resource
|
private BusinessSystemService businessSystemService;
|
|
@Resource
|
private RecommendSectionDetailService recommendSectionDetailService;
|
|
@Transactional(rollbackFor=Exception.class)
|
public boolean addRecommendSection(RecommendSection section,RecommendSectionDetail detail) {
|
section.setCreatetime(java.lang.System.currentTimeMillis());
|
long rsid = (Long) recommendSectionDao.save(section);
|
if(rsid <= 0){
|
return false;
|
}
|
if(detail==null){
|
detail=new RecommendSectionDetail();
|
}
|
detail.setRecommendSection(section);
|
recommendSectionDetailService.saveSectionDetail(detail);
|
return true;
|
}
|
|
public long getCount() {
|
return recommendSectionDao
|
.getCount("select count(*) from RecommendSection");
|
}
|
|
@SuppressWarnings({ "unchecked" })
|
public List<RecommendSectionAdmin> getRecommendSections(int index,
|
String platform,String packages, String key) {
|
final int strat = index * Constant.PAGE_SIZE;
|
platform = Utils.getMap().get(platform);
|
com.yeshi.fanli.entity.system.BusinessSystem system = businessSystemService.getBusinessSystem(platform, packages);
|
List<RecommendSection> recommendSectionList=null;
|
List<SuperRecommendSection> superRecommendSectionList=null;
|
Map<Long,RecommendSectionAdmin> recommendSectionAdminMap=new HashMap<Long, RecommendSectionAdmin>();
|
List<RecommendSectionAdmin> recommendSectionAdmin=new ArrayList<RecommendSectionAdmin>();
|
if(system == null){
|
if("".equals(key.trim())){
|
recommendSectionList = (List<RecommendSection>) recommendSectionDao.excute(new HibernateCallback<List<RecommendSection>>(){
|
public List<RecommendSection> doInHibernate(Session session)
|
throws HibernateException {
|
Query query = session.createQuery(" from RecommendSection rs order by rs.orderby");
|
query.setFirstResult(strat);
|
query.setMaxResults(Constant.PAGE_SIZE);
|
return query.list();
|
}
|
});
|
}else{
|
final String likekey = "%"+key+"%";
|
recommendSectionList= (List<RecommendSection>) recommendSectionDao.excute(new HibernateCallback<List<RecommendSection>>(){
|
public List<RecommendSection> doInHibernate(Session session)
|
throws HibernateException {
|
Query query = session.createQuery(" from RecommendSection rs where rs.name like ? order by rs.orderby");
|
query.setFirstResult(strat);
|
query.setMaxResults(Constant.PAGE_SIZE);
|
query.setParameter(0, likekey);
|
return query.list();
|
}
|
});
|
}
|
List<Long> rsIdList = new ArrayList<Long>();
|
for (RecommendSection rs : recommendSectionList) {
|
rsIdList.add(rs.getId());
|
}
|
superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
|
Map<Long,List<BusinessSystem>> superMap = new HashMap<Long, List<BusinessSystem>>();
|
Long rsId=null;
|
List<BusinessSystem> systemList=null;
|
for (SuperRecommendSection srs : superRecommendSectionList) {
|
rsId = srs.getRecommendSection().getId();
|
systemList = superMap.get(rsId);
|
if(systemList==null){
|
systemList=new ArrayList<BusinessSystem>();
|
}
|
systemList.add(srs.getSystem());
|
superMap.put(rsId,systemList);
|
}
|
RecommendSectionAdmin rsa=null;
|
for (RecommendSection recommendSection : recommendSectionList) {
|
long id = recommendSection.getId();
|
rsa = recommendSectionAdminMap.get(id);
|
if(rsa==null){
|
rsa=new RecommendSectionAdmin();
|
}
|
List<BusinessSystem> list = superMap.get(id);
|
if(list != null){
|
rsa.getSystemList().addAll(superMap.get(id));
|
}
|
rsa.setRecommendSection(recommendSection);
|
recommendSectionAdminMap.put(id, rsa);
|
}
|
}else if(system != null){
|
if("".equals(key.trim())){
|
superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionBySystemId(system.getId(),strat, Constant.PAGE_SIZE);
|
List<Long> rsIdList =new ArrayList<Long>();
|
for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
|
RecommendSection recommendSection = superRecommendSection.getRecommendSection();
|
rsIdList.add(recommendSection.getId());
|
}
|
superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
|
}else{
|
String likekey = "%"+key+"%";
|
superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionBySystemId(system.getId(),strat, Constant.PAGE_SIZE,likekey);
|
List<Long> rsIdList =new ArrayList<Long>();
|
for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
|
RecommendSection recommendSection = superRecommendSection.getRecommendSection();
|
rsIdList.add(recommendSection.getId());
|
}
|
superRecommendSectionList = superRecommendSectionService.getSuperRecommendSectionsBySections(rsIdList);
|
}
|
RecommendSectionAdmin rsa=null;
|
for (SuperRecommendSection superRecommendSection : superRecommendSectionList) {
|
RecommendSection recommendSection = superRecommendSection.getRecommendSection();
|
rsa = recommendSectionAdminMap.get(recommendSection.getId());
|
if(rsa==null){
|
rsa=new RecommendSectionAdmin(recommendSection);
|
}
|
rsa.getSystemList().add(superRecommendSection.getSystem());
|
recommendSectionAdminMap.put(recommendSection.getId(), rsa);
|
}
|
}
|
Map map = Utils.orderBy(recommendSectionAdminMap);
|
Collection<RecommendSectionAdmin> values = map.values();
|
recommendSectionAdmin.addAll(values);
|
return recommendSectionAdmin;
|
|
}
|
|
public RecommendSection getRecommendSection(long id) {
|
return recommendSectionDao.find(RecommendSection.class, id);
|
}
|
|
@Transactional
|
public void deleteSections(final long[] rsids) {
|
|
superRecommendSectionService.deleteSuperRecommendSections(rsids);
|
recommendSectionGoodsService.deleteSectionGoodsBySections(rsids);
|
recommendSectionDetailService.deleteSectionDetailBySections(rsids);
|
recommendSectionDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session)
|
throws HibernateException {
|
|
StringBuffer sb = new StringBuffer(
|
"delete from RecommendSection s ");
|
for (int i = 0; i < rsids.length; i++) {
|
if (i == 0) {
|
sb.append(" where s.id=? ");
|
} else {
|
sb.append(" or s.id=?");
|
}
|
}
|
Query query = session.createQuery(sb.toString());
|
for (int i = 0; i < rsids.length; i++) {
|
query.setLong(i, rsids[i]);
|
}
|
query.executeUpdate();
|
return null;
|
}
|
});
|
|
}
|
|
public void updateSection(RecommendSection section) {
|
section.setCreatetime(java.lang.System.currentTimeMillis());
|
recommendSectionDao.update(section);
|
}
|
|
public int getCount(String platform, String packages, final String key) {
|
|
platform = Utils.getMap().get(platform);
|
final BusinessSystem system = businessSystemService.getBusinessSystem(platform, packages);
|
return (Integer) recommendSectionDao.excute(new HibernateCallback<Integer>() {
|
|
public Integer doInHibernate(Session session)
|
throws HibernateException {
|
String k = "%"+key+"%";
|
Query query;
|
if(system==null){
|
// Query query = session.createSQLQuery("select count(a.name) from (select name from yeshi_ec_recommend_section where `name` like ?) a");
|
query = session.createQuery("select count(rs.id) from RecommendSection rs where rs.name like ?");
|
query.setParameter(0, k);
|
}else{
|
// query =session.createSQLQuery("select count(a.name) from (select hs.name from yeshi_ec_super_recommendsection shs left join yeshi_ec_recommend_section hs on shs.recommendsection_id=hs.id where shs.system_id=? and hs.name like ?) a");
|
query=session.createQuery("select count(srs.id) from SuperRecommendSection srs where srs.system.id=? and srs.name like ?");
|
query.setParameter(0, system.getId());
|
query.setParameter(1, k);
|
}
|
Long result = (Long) query.uniqueResult();
|
int intValue = result.intValue();
|
return intValue;
|
}
|
});
|
|
}
|
|
public List<RecommendSection> getRecommendSectionAll() {
|
return recommendSectionDao.list("from RecommendSection rs order by rs.orderby");
|
}
|
|
@Override
|
public int getCounts(String key) {
|
return shareMapper.getCounts(key);
|
}
|
|
}
|