MothMovableServiceImpl.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.iamberry.wechat.service.mothMovable;
  2. import com.iamberry.wechat.core.entity.coupon.CouponItem;
  3. import com.iamberry.wechat.core.entity.coupon.CouponType;
  4. import com.iamberry.wechat.core.entity.mothMovable.MothMovable;
  5. import com.iamberry.wechat.core.entity.mothMovable.MothPopularity;
  6. import com.iamberry.wechat.core.entity.wx.QRCJson;
  7. import com.iamberry.wechat.face.admin.SystemService;
  8. import com.iamberry.wechat.face.mothMovable.MothMovableService;
  9. import com.iamberry.wechat.file.FileUtils;
  10. import com.iamberry.wechat.service.mapper.CouponItemMapper;
  11. import com.iamberry.wechat.service.mapper.CouponTypeMapper;
  12. import com.iamberry.wechat.service.mapper.MothMovableMapper;
  13. import com.iamberry.wechat.service.mapper.MothPopularityMapper;
  14. import com.iamberry.wechat.tools.*;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.awt.*;
  19. import java.io.File;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.UUID;
  25. /**
  26. * 活动生成图片的内容接口
  27. * @author xm
  28. * @Date 2019-04-25
  29. */
  30. @Service
  31. public class MothMovableServiceImpl implements MothMovableService {
  32. @Autowired
  33. private MothMovableMapper mothMovableMapper;
  34. @Autowired
  35. private MothPopularityMapper mothPopularityMapper;
  36. @Autowired
  37. private CouponTypeMapper couponTypeMapper;
  38. @Autowired
  39. private CouponItemMapper couponItemMapper;
  40. @Autowired
  41. private SystemService systemService;
  42. /**
  43. * 获取集合
  44. * @param mothMovable
  45. * @return List
  46. */
  47. @Override
  48. public List<MothMovable> getMothMovableList(MothMovable mothMovable){
  49. return mothMovableMapper.getMothMovableList(mothMovable);
  50. }
  51. /**
  52. * 查询单条数据
  53. * @param id
  54. * @return mothMovable
  55. */
  56. @Override
  57. public MothMovable getMothMovableById(Integer id){
  58. return mothMovableMapper.getMothMovableById(id);
  59. }
  60. /**
  61. * 增加数据
  62. * @param mothMovable
  63. * @return Integer
  64. */
  65. @Override
  66. @Transactional
  67. public Integer save(MothMovable mothMovable,String rootPath){
  68. //生成优惠券 -- 每位参加活动的人,发送wateroT1-T2滤芯优惠券 ,优惠券限7天内使用,否则实效
  69. // 成功参与征集的用户,赠送wateroT1/T2滤芯优惠券,共50个,先到先得
  70. Integer flag = 0 ;
  71. String uuidStr = "";
  72. String begin = systemService.selectOneShopRuleByIdDynamic(238).getRuleDesc();
  73. Integer num = Integer.valueOf(begin);
  74. MothMovable mm1 = new MothMovable();
  75. Integer allNum = mothMovableMapper.getNum(mm1);
  76. if(allNum < num){
  77. //创建优惠券
  78. Integer couponId = 10000;
  79. CouponType couponType = couponTypeMapper.getCouponTypeById(couponId);
  80. Calendar calendar = Calendar.getInstance();
  81. calendar.add(Calendar.DATE, 7); //有效期7天
  82. calendar.set(Calendar.HOUR_OF_DAY,23);
  83. calendar.set(Calendar.MINUTE,59);
  84. calendar.set(Calendar.SECOND,59);
  85. CouponItem couponItem = new CouponItem();
  86. uuidStr = StrUtils.getUUID();
  87. couponItem.setCouponItemId(uuidStr);
  88. couponItem.setCouponItemUseropenid(mothMovable.getMothMovaOpenId());
  89. couponItem.setCouponId(couponId);
  90. couponItem.setCouponReceiveDate(new Date());
  91. couponItem.setCouponUseEndDate(calendar.getTime());
  92. couponItem.setCouponUseStatus(1);
  93. couponItem.setCouponItemRemark(couponType.getCouponRemark());
  94. flag = couponItemMapper.insertCouponItem(couponItem);
  95. if(flag < 1){
  96. throw new RuntimeException("创建优惠券失败");
  97. }
  98. }
  99. boolean fl = false;
  100. //生成图片
  101. try {
  102. fl = geneImg(mothMovable,rootPath);
  103. }catch (Exception e){
  104. e.printStackTrace();
  105. }
  106. if(fl){
  107. //添加数据
  108. mothMovable.setCouponItemId(uuidStr);
  109. flag = mothMovableMapper.save(mothMovable);
  110. if(flag < 1){
  111. throw new RuntimeException("添加活动数据失败");
  112. }
  113. }else{
  114. throw new RuntimeException("生成图片失败");
  115. }
  116. return flag;
  117. }
  118. /**
  119. * 生成图片
  120. * @param mothMovable
  121. * @param rootPath 根目录
  122. * @return
  123. */
  124. private boolean geneImg(MothMovable mothMovable,String rootPath)throws Exception{
  125. Integer type = 0;
  126. if(mothMovable.getMothMovaBackType() == 5){
  127. type = 1; //头像放在中间
  128. }
  129. PosterNumUtils pnu = new PosterNumUtils(type);
  130. // ****/common/images/movable/
  131. String uuid = UUID.randomUUID().toString().substring(0,8);
  132. String codeName = uuid + "_code.jpg";
  133. String headName = uuid + "_head.jpg";
  134. String imgName = uuid + "_img.jpg";
  135. String path = rootPath + pnu.IMG_PATH;
  136. File targetFile = new File(path);
  137. if (!targetFile.exists()) {
  138. targetFile.mkdirs();
  139. }
  140. //生成机器二维码
  141. String json = String.format(NameUtils.QR_SCENE_STR, 2592000, ResultInfo.barCodeMothMovable + mothMovable.getMothMovaOpenId());
  142. QRCJson qrc = WeixinUtil.createQrcode(json);
  143. /*下载二维码*/
  144. DownloadWXCodeUtil.downloadByNIO2(qrc.getUrl(),codeName,path,pnu.CODE_WIDTH,pnu.CODE_HEIGHT,false);
  145. if(mothMovable.getMothMovaHead() == null || "".equals(mothMovable.getMothMovaHead())){
  146. String headImg = path+headName;
  147. String oldheadImg = path+PosterNumUtils.getBackPath(0);
  148. FileUtils.copyFile(oldheadImg,headImg);
  149. }else{
  150. DownloadWXCodeUtil.downloadByNIO2(mothMovable.getMothMovaHead(),headName,path,pnu.HEAD_WIDTH,pnu.HEAD_HEIGHT,pnu.HEAD_IS_CIRCLE);
  151. }
  152. /*海报位置*/
  153. String newImg = path+imgName;
  154. /*二维码位置*/
  155. String codeImg = path + codeName;
  156. /*头像位置*/
  157. String headImg = path + headName;
  158. /*复制海报*/ //common/images/activity/ditu.jpg
  159. String oldImgPath = path + PosterNumUtils.getBackPath(mothMovable.getMothMovaBackType());
  160. FileUtils.copyFile(oldImgPath,newImg);
  161. mothMovable.setMothMovaImgUrl(pnu.IMG_PATH + imgName);
  162. mothMovable.setMothMovaShareUrl(qrc.getUrl());
  163. /*添加文字*/
  164. ImageUtils.pressPost(newImg,
  165. headImg,pnu.HEAD_X,pnu.HEAD_Y,
  166. codeImg,pnu.CODE_X,pnu.CODE_Y,
  167. mothMovable.getMothMovaName(),pnu.TEXT_X,pnu.TEXT_Y,new Color(0,0,0),pnu.TEXT_FONT_SIZE,
  168. mothMovable.getMothMovaText(),pnu.TEXT_CONTENT_X,pnu.TEXT_CONTENT_Y,new Color(0,0,0),pnu.TEXT_CONTENT_FONT_SIZE,
  169. pnu.FONT_LINE_SPACING,pnu.FONT_NUMBER
  170. );
  171. return true;
  172. }
  173. /**
  174. * 修改数据
  175. * @param mothMovable
  176. * @return Integer
  177. */
  178. @Override
  179. public Integer update(MothMovable mothMovable){
  180. return mothMovableMapper.update(mothMovable);
  181. }
  182. /**
  183. * 删除数据
  184. * @param id
  185. * @return Integer
  186. */
  187. @Override
  188. public Integer delete(Integer id){
  189. return mothMovableMapper.delete(id);
  190. }
  191. @Override
  192. public Integer scanInfo(String mothMovaOpenId, String openId) {
  193. MothMovable mothMovable = new MothMovable();
  194. mothMovable.setMothMovaOpenId(mothMovaOpenId);
  195. List<MothMovable> mothMovableList = mothMovableMapper.getMothMovableList(mothMovable);
  196. if(mothMovableList == null || mothMovableList.size() < 1){
  197. return 0;
  198. }
  199. mothMovable = mothMovableList.get(0);
  200. MothPopularity mothPopularity = new MothPopularity();
  201. mothPopularity.setMothMovaId(mothMovable.getMothMovaId());
  202. mothPopularity.setMothPopuOpenId(openId);
  203. return mothPopularityMapper.save(mothPopularity);
  204. }
  205. }