|
|
|
@@ -0,0 +1,144 @@ |
|
|
|
package com.iformall.service.impl; |
|
|
|
|
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.IdWorker; |
|
|
|
import com.iformall.common.Result; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.WxActivity; |
|
|
|
import com.iformall.enums.EnumActivityStatus; |
|
|
|
import com.iformall.enums.EnumActivityType; |
|
|
|
import com.iformall.mapper.WxActivityMapper; |
|
|
|
import com.iformall.mapper.WxCouponChannelMapper; |
|
|
|
import com.iformall.service.WxActivityService; |
|
|
|
import com.iformall.service.WxCouponChannelService; |
|
|
|
import com.iformall.service.WxCouponService; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.net.URLDecoder; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author gongbiao |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
public class WxActivityServiceImpl implements WxActivityService { |
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxActivityMapper wxActivityMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxCouponChannelMapper wxCouponChannelMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxCouponService wxCouponService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxCouponChannelService wxCouponChannelService; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageInfo<WxActivity> listAsPage(WxActivity record, Integer pageIndex, Integer pageSize) { |
|
|
|
return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxActivityMapper.findList(record)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxActivity getById(Long id) { |
|
|
|
WxActivity wxActivity = wxActivityMapper.selectByPrimaryKey(id); |
|
|
|
String html = wxActivity.getHtml(); |
|
|
|
if (!StringUtils.isEmpty(html)) { |
|
|
|
try { |
|
|
|
html = URLDecoder.decode(html, "utf-8"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
logger.info("图文内容解码失败"); |
|
|
|
} |
|
|
|
wxActivity.setHtml(html); |
|
|
|
} |
|
|
|
return wxActivity; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) |
|
|
|
@Override |
|
|
|
public ResultData saveOrUpdate(WxActivity wxActivity) { |
|
|
|
if (wxActivity.getType().equals(EnumActivityType.STABLE.getCode())) { |
|
|
|
if (StringUtils.isEmpty(wxActivity.getCoverImg())) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "封面图不能为空"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(wxActivity.getTitle())) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "标题不能为空"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(wxActivity.getDetail())) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "活动说明不能为空"); |
|
|
|
} |
|
|
|
} else if (wxActivity.getType().equals(EnumActivityType.FREE.getCode())) { |
|
|
|
if (StringUtils.isEmpty(wxActivity.getHtml())) { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "图文不能为空"); |
|
|
|
} |
|
|
|
int length = wxActivity.getHtml().getBytes().length; |
|
|
|
if (length >= 0xFFFFFF) { //Mysql MiddleText length limited |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "图文内容过大无法保存"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR); |
|
|
|
} |
|
|
|
wxActivity.setStatus(EnumActivityStatus.STATUS_THROW_IN.getCode()); |
|
|
|
if (wxActivity.getId() == null) { |
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
wxActivity.setId(idWorker.nextId()); |
|
|
|
wxActivityMapper.insertSelective(wxActivity); |
|
|
|
} else { |
|
|
|
wxActivityMapper.updateByPrimaryKeySelective(wxActivity); |
|
|
|
} |
|
|
|
return new ResultData(Result.SUCCESS, "操作成功"); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) |
|
|
|
@Override |
|
|
|
public ResultData updateStatus(WxActivity wxActivity) { |
|
|
|
wxActivityMapper.updateByPrimaryKeySelective(wxActivity); |
|
|
|
return new ResultData(Result.SUCCESS, "操作成功"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void deleteById(Long id) { |
|
|
|
wxActivityMapper.deleteByPrimaryKey(id); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void printHtmlById(Long id, HttpServletResponse response) { |
|
|
|
try { |
|
|
|
response.setHeader("Content-type", "text/html;charset=utf-8"); |
|
|
|
WxActivity wxActivity = wxActivityMapper.selectByPrimaryKey(id); |
|
|
|
String html = StringUtils.isEmpty(wxActivity.getHtml()) ? "" : wxActivity.getHtml(); |
|
|
|
try { |
|
|
|
html = URLDecoder.decode(html, "utf-8"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
logger.info("图文内容解码失败"); |
|
|
|
} |
|
|
|
// 循环取出流中的数据 |
|
|
|
byte[] b = new byte[1024]; |
|
|
|
int len; |
|
|
|
InputStream inStream = new ByteArrayInputStream(html.getBytes()); |
|
|
|
while ((len = inStream.read(b)) > 0) { |
|
|
|
response.getOutputStream().write(b, 0, len); |
|
|
|
} |
|
|
|
inStream.close(); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.info("输出富文本内容" + e.getMessage()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |