123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.status.DirectionStatus;
- import com.qxgmat.data.dao.AdMapper;
- import com.qxgmat.data.dao.entity.Ad;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.Collection;
- import java.util.Date;
- import java.util.List;
- @Service
- public class AdService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(AdService.class);
- @Resource
- private AdMapper adMapper;
- public Page<Ad> listAdmin(int page, int size, String channel, String position, String order, DirectionStatus direction){
- Example example = new Example(Ad.class);
- if (channel != null)
- example.and(
- example.createCriteria()
- .andEqualTo("channel", channel)
- );
- if (position != null)
- example.and(
- example.createCriteria()
- .andEqualTo("position", position)
- );
- if(order == null || order.isEmpty()) order = "id";
- switch(direction){
- case ASC:
- example.orderBy(order).asc();
- break;
- case DESC:
- default:
- example.orderBy(order).desc();
- }
- return select(adMapper, example, page, size);
- }
- public List<Ad> all(String channel){
- Example example = new Example(Ad.class);
- Date day = new Date();
- example.and(
- example.createCriteria()
- .orGreaterThanOrEqualTo("startTime", day)
- .orIsNull("startTime")
- );
- example.and(
- example.createCriteria()
- .orLessThan("endTime", day)
- .orIsNull("endTime")
- );
- example.and(
- example.createCriteria()
- .andEqualTo("channel", channel)
- );
- example.orderBy("position").asc();
- return select(adMapper, example);
- }
- public Ad getAd(String channel, String place){
- Example example = new Example(Ad.class);
- Date day = new Date();
- example.and(
- example.createCriteria()
- .andEqualTo("channel", channel)
- .andEqualTo("place", place)
- );
- return one(adMapper, example);
- }
- @Transactional
- public Ad addAd(Ad entity){
- Ad in = getAd(entity.getChannel(), entity.getPlace());
- if (in != null){
- throw new ParameterException("广告位已设置");
- }
- return add(entity);
- }
- @Transactional
- public Ad editAd(Ad entity){
- Ad in = getAd(entity.getChannel(), entity.getPlace());
- if (in != null && !in.getId().equals(entity.getId())){
- throw new ParameterException("广告位已设置");
- }
- return edit(entity);
- }
- public Ad add(Ad ad){
- int result = insert(adMapper, ad);
- ad = one(adMapper, ad.getId());
- if(ad == null){
- throw new SystemException("广告添加失败");
- }
- return ad;
- }
- public Ad edit(Ad ad){
- Ad in = one(adMapper, ad.getId());
- if(in == null){
- throw new ParameterException("广告不存在");
- }
- int result = update(adMapper, ad);
- return ad;
- }
- public boolean delete(Number id){
- Ad in = one(adMapper, id);
- if(in == null){
- throw new ParameterException("广告不存在");
- }
- int result = delete(adMapper, id);
- return result > 0;
- }
- public Ad get(Number id){
- Ad in = one(adMapper, id);
- if(in == null){
- throw new ParameterException("广告不存在");
- }
- return in;
- }
- public Page<Ad> select(int page, int pageSize){
- return select(adMapper, page, pageSize);
- }
- public Page<Ad> select(Integer[] ids){
- return page(()->select(adMapper, ids), 1, ids.length);
- }
- public List<Ad> select(Collection ids){
- return select(adMapper, ids);
- }
- }
|