package com.yeshi.fanli.service.impl.push;
|
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
import javax.transaction.Transactional;
|
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.push.PushCouponMapper;
|
import com.yeshi.fanli.entity.push.PushCoupon;
|
import com.yeshi.fanli.exception.PushException;
|
import com.yeshi.fanli.exception.push.PushCouponException;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.push.PushCouponRecordService;
|
import com.yeshi.fanli.service.inter.push.PushCouponService;
|
import com.yeshi.fanli.service.inter.push.PushService;
|
|
@Service
|
public class PushCouponServiceImpl implements PushCouponService {
|
|
@Resource
|
private PushService pushService;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private PushCouponMapper pushCouponMapper;
|
|
@Resource
|
private PushCouponRecordService pushCouponRecordService;
|
|
@Override
|
public int deleteByPrimaryKey(Long id) {
|
return pushCouponMapper.deleteByPrimaryKey(id);
|
}
|
|
@Override
|
public int insert(PushCoupon record) {
|
return pushCouponMapper.insert(record);
|
}
|
|
@Override
|
public int insertSelective(PushCoupon record) {
|
return pushCouponMapper.insertSelective(record);
|
}
|
|
@Override
|
public PushCoupon selectByPrimaryKey(Long id) {
|
return pushCouponMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public int updateByPrimaryKeySelective(PushCoupon record) {
|
return pushCouponMapper.updateByPrimaryKeySelective(record);
|
}
|
|
@Override
|
public int updateByPrimaryKey(PushCoupon record) {
|
return pushCouponMapper.updateByPrimaryKey(record);
|
}
|
|
|
@Override
|
public List<PushCoupon> listQuery(long start, int count, String key, Integer state) {
|
return pushCouponMapper.listQuery(start, count, key, state);
|
}
|
|
@Override
|
public long countQuery(String key, Integer state) {
|
return pushCouponMapper.countQuery(key, state);
|
}
|
|
@Override
|
@Transactional
|
public void save(PushCoupon record) throws PushCouponException {
|
if (record == null) {
|
throw new PushCouponException(1, "参数不正确");
|
}
|
|
String title = record.getTitle();
|
String content = record.getContent();
|
if (title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0) {
|
throw new PushCouponException(1, "标题或内容不能为空");
|
}
|
|
Date startTime = record.getStartTime();
|
Date endTime = record.getEndTime();
|
if (startTime == null || endTime == null) {
|
throw new PushCouponException(1, "有效期时间不能为空");
|
}
|
|
Integer amount = record.getAmount();
|
if (amount == null || amount < 1) {
|
throw new PushCouponException(1, "券数量不能小于1");
|
}
|
|
Long id = record.getId();
|
if (id == null) {
|
// 新增
|
record.setPushed(false);
|
record.setCreateTime(new Date());
|
record.setUpdateTime(new Date());
|
|
// 插入数据
|
insert(record);
|
} else {
|
// 修改
|
PushCoupon current = selectByPrimaryKey(id);
|
if (current == null) {
|
throw new PushCouponException(1, "该记录已不存在");
|
}
|
|
current.setTitle(title);
|
current.setContent(content);
|
current.setAmount(amount);
|
current.setStartTime(startTime);
|
current.setEndTime(endTime);
|
current.setUids(record.getUids());
|
current.setRemark(record.getRemark());
|
|
// 更新数据
|
updateByPrimaryKey(current);
|
}
|
|
}
|
|
@Override
|
@Transactional
|
public void executePush(Long id) throws Exception, PushCouponException, PushException {
|
|
PushCoupon pushCoupon = selectByPrimaryKey(id);
|
if (pushCoupon == null) {
|
throw new PushCouponException(1, "推送信息已不存在");
|
}
|
|
String title = pushCoupon.getTitle();
|
String content = pushCoupon.getContent();
|
if (title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0) {
|
throw new PushCouponException(1, "标题或内容不能为空");
|
}
|
|
|
// TODO 推送跳转
|
|
|
String uids = pushCoupon.getUids();
|
if (uids == null || uids.trim().length() == 0) {
|
// 全推
|
//pushService.pushZNX(null, title, content);
|
|
} else {
|
// 指定用户推送
|
String[] uidArray = uids.split(",");
|
|
if (uidArray != null) {
|
for (int i = 0; i < uidArray.length; i++) {
|
String str_uid = uidArray[i];
|
if (str_uid != null && str_uid.trim().length() > 0) {
|
//pushService.pushZNX(Long.parseLong(str_uid), title, content);
|
}
|
}
|
}
|
|
}
|
|
// 已推送
|
pushCoupon.setPushed(true);
|
// 推送时间
|
pushCoupon.setPushTime(new Date());
|
// 更新时间
|
pushCoupon.setUpdateTime(new Date());
|
|
updateByPrimaryKeySelective(pushCoupon);
|
}
|
|
|
@Override
|
@Transactional
|
public void deleteBatchByPrimaryKey(List<Long> list) {
|
if (list == null || list.size() == 0) {
|
return;
|
}
|
|
for (Long id: list) {
|
long receivedCount = pushCouponRecordService.countByPushId(id);
|
if (receivedCount > 0) {
|
continue;
|
}
|
|
pushCouponMapper.deleteByPrimaryKey(id);
|
}
|
}
|
|
@Override
|
public List<PushCoupon> listQueryEffective() {
|
return pushCouponMapper.listQueryEffective();
|
}
|
|
}
|