package com.yeshi.fanli.controller.admin; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.tencentcloud.COSManager; import com.yeshi.fanli.entity.bus.recommend.DynamicRecommend; import com.yeshi.fanli.entity.bus.recommend.MaskKey; import com.yeshi.fanli.entity.bus.recommend.RecommendReply; import com.yeshi.fanli.entity.bus.user.UserInfo; import com.yeshi.fanli.entity.taobao.SearchFilter; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult; import com.yeshi.fanli.service.MaskKeyService; import com.yeshi.fanli.service.inter.goods.RecommendDetailsService; import com.yeshi.fanli.service.inter.goods.RecommendReplyService; import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService; import com.yeshi.fanli.service.inter.user.DynamicRecommendService; import com.yeshi.fanli.service.inter.user.UserInfoService; import com.yeshi.fanli.tag.PageEntity; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.GsonUtil; import com.yeshi.fanli.util.MoneyBigDecimalUtil; import com.yeshi.fanli.util.taobao.TaoBaoUtil; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/v1/dynamic") public class DynamicRecommendAdminController { @Resource private DynamicRecommendService dynamicRecommendService; @Resource private RecommendReplyService recommendReplyService; @Resource private RecommendDetailsService recommendDetailsService; @Resource private MaskKeyService maskKeyService; @Resource private HongBaoManageService hongBaoManageService; @Resource private UserInfoService userInfoService; /** * * @param key * 1.用户ID 2.用户名称 3.发布内容 * @param page * @param out */ @RequestMapping(value = "getDynamicRecommends", method = RequestMethod.POST) public void getDynamicRecommends(String key, int page, int type, PrintWriter out) { List dynamicRecommends = dynamicRecommendService.getDynamicRecommendList(key, type, page); int count = dynamicRecommendService.getCount(key, type); PageEntity pe = new PageEntity(); pe.setPageIndex(page); pe.setPageSize(Constant.PAGE_SIZE); pe.setTotalCount(count); JSONObject data = new JSONObject(); data.put("count", count); data.put("pe", pe); data.put("dynamicRecommends", GsonUtil.toJsonByUserAp(dynamicRecommends)); out.append(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "deleteDynamicRecommends", method = RequestMethod.POST) public void deleteDynamicRecommends(long[] drids, PrintWriter out) { for (long id : drids) { dynamicRecommendService.delete(id); } out.print(JsonUtil.loadTrueResult("")); } @RequestMapping(value = "getRecommendReplys", method = RequestMethod.POST) public void getRecommendReplys(long drid, int page, PrintWriter out) { List list = recommendReplyService.getRecommendReplys(drid, page); int count = recommendReplyService.getCount(drid); PageEntity pe = new PageEntity(); pe.setPageIndex(page); pe.setPageSize(Constant.PAGE_SIZE); pe.setTotalCount(count); JSONObject data = new JSONObject(); data.put("count", count); data.put("pe", pe); data.put("list", GsonUtil.toJsonByUserAp(list)); out.append(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "deleteRecommendReplys", method = RequestMethod.POST) public void deleteRecommendReplys(long[] rrids, PrintWriter out) { for (long id : rrids) { recommendReplyService.delete(id); } out.print(JsonUtil.loadTrueResult("")); } @RequestMapping(value = "getMaskKeys", method = RequestMethod.POST) public void getMaskKeys(String key, int page, PrintWriter out) { List list = maskKeyService.getMaskKeys(key, page); int count = maskKeyService.getCount(key); PageEntity pe = new PageEntity(); pe.setPageIndex(page); pe.setPageSize(Constant.PAGE_SIZE); pe.setTotalCount(count); JSONObject data = new JSONObject(); data.put("count", count); data.put("pe", pe); data.put("list", GsonUtil.toJson(list)); out.append(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "addMaskKey", method = RequestMethod.POST) public void addMaskKey(String key, PrintWriter out) { long id = maskKeyService.addMaskKey(key); if (id > 0) { out.append(JsonUtil.loadTrueResult("")); return; } else { out.append(JsonUtil.loadFalseResult("添加失败")); } } @RequestMapping(value = "deleteMaskKeys", method = RequestMethod.POST) public void deleteMaskKeys(long[] ids, PrintWriter out) { for (long id : ids) { maskKeyService.delete(id); } out.append(JsonUtil.loadTrueResult("")); } @RequestMapping(value = "getGoodsInfo", method = RequestMethod.POST) public void getGoodsInfo(String url, PrintWriter out) { TaoBaoGoodsBrief tb = null; String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?"; Pattern p = Pattern.compile(URL_REGEX); Matcher matcher = p.matcher(url); if (!matcher.find()) { out.print(JsonUtil.loadFalseResult("暂不支持该类型!")); return; } url = matcher.group(); if (url.contains("ju.taobao.com") || url.contains(".juhuasuan.com")) {// 聚划�? int index = url.indexOf("item_id"); if (index < 0) { out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!")); return; } url = url.substring(index); int last = url.indexOf("&"); String id = ""; if (last > 0) id = url.substring(url.indexOf("=") + 1, url.indexOf("&")); else { id = url.substring(url.indexOf("=" + 1)); } tb = TaoBaoUtil.isAlimama(id); if (tb == null) { tb = TaoBaoUtil.parsePhoneTmAndTb(id); } } else if (url.contains("http://zmnxbc.com")) { // 手机端天猫APP分享 tb = TaoBaoUtil.parsePhoneShareUrlByTM(url); } else if (url.contains("c.b1yt.com")) { // 手机端淘宝和�?��APP分享 tb = TaoBaoUtil.parsePhoneShareUrlByTB(url); } else if (url.contains("h5.m.taobao") || url.contains("detail.m.tmall") || url.contains("item.taobao") || url.contains("detail.tmall")) { // 手机页面和电脑页�? int index = url.indexOf("id"); if (index < 0) { out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!")); return; } url = url.substring(index); int last = url.indexOf("&"); String id = ""; if (last > 0) id = url.substring(url.indexOf("=") + 1, url.indexOf("&")); else { id = url.substring(url.indexOf("=") + 1); } tb = TaoBaoUtil.isAlimama(id); if (tb == null) { tb = TaoBaoUtil.parsePhoneTmAndTb(id); } } if (tb == null) { out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!")); return; } JSONObject data = new JSONObject(); JSONObject taoBaoGoodsJSON = new JSONObject(); taoBaoGoodsJSON.put("title", tb.getTitle()); taoBaoGoodsJSON.put("zkPrice", tb.getZkPrice()); taoBaoGoodsJSON.put("auctionId", tb.getAuctionId()); data.put("taoBaoGoodsBrief", taoBaoGoodsJSON); data.put("tbImgs", tb.getImgList()); out.print(JsonUtil.loadTrueResult(data)); } @RequestMapping(value = "makePublic", method = RequestMethod.POST) public void makePublic(DynamicRecommend dynamicRecommend, @RequestParam(value = "files", required = false) CommonsMultipartFile[] files, PrintWriter out) { if (dynamicRecommend == null) { out.print(JsonUtil.loadFalseResult("dynamicRecommend不能为空")); return; } if (dynamicRecommend.getReason() == null || "".equals(dynamicRecommend.getReason().trim())) { out.print(JsonUtil.loadFalseResult("推荐理由不能为空")); return; } if (dynamicRecommend.getAuctionId() == null) { out.print(JsonUtil.loadFalseResult("商品ID不能为空")); return; } if (dynamicRecommend.getUrl() == null || "".equals(dynamicRecommend.getUrl().trim())) { out.print(JsonUtil.loadFalseResult("url不能为空")); return; } if (dynamicRecommend.getuPicUrl() == null && files == null) { out.print(JsonUtil.loadFalseResult("请选择一张图片")); return; } String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?"; Pattern p = Pattern.compile(URL_REGEX); Matcher matcher = p.matcher(dynamicRecommend.getUrl()); if (!matcher.find()) { out.print(JsonUtil.loadFalseResult("暂不支持该类型!")); return; } String url = matcher.group(); dynamicRecommend.setUrl(url); if (files != null) { COSManager cosManager = COSManager.getInstance(); String paths = ""; for (CommonsMultipartFile file : files) { try { String filename = file.getOriginalFilename(); int index = filename.lastIndexOf("."); String type = filename.substring(index); String filePath = cosManager .uploadFile(file.getInputStream(), UUID.randomUUID().toString().replace("-", "") + type) .getUrl(); paths += "," + filePath; } catch (IOException e) { e.printStackTrace(); out.println(JsonUtil.loadFalseResult("上传图片失败")); return; } } paths = paths.replaceFirst(",", ""); String getuPicUrl = dynamicRecommend.getuPicUrl(); if (getuPicUrl == null) { dynamicRecommend.setuPicUrl(paths); } else { dynamicRecommend.setuPicUrl(getuPicUrl + "," + paths); } } TaoBaoGoodsBrief taoBaoGoodsBrief = null; taoBaoGoodsBrief = TaoBaoUtil.isAlimama(dynamicRecommend.getAuctionId()); if (taoBaoGoodsBrief == null) { taoBaoGoodsBrief = TaoBaoUtil.parsePhoneTmAndTb(dynamicRecommend.getAuctionId()); dynamicRecommend.setHongbao(new BigDecimal(0)); } else { SearchFilter filter = new SearchFilter(); filter.setKey(String.format(TaoBaoUtil.TB_URL, dynamicRecommend.getAuctionId())); TaoBaoSearchResult search = TaoBaoUtil.search(filter); List taoBaoGoodsBriefs = search.getTaoBaoGoodsBriefs(); if (taoBaoGoodsBriefs.size() == 0) { out.println(JsonUtil.loadFalseResult("发布失败,商品已下架")); return; } taoBaoGoodsBrief = taoBaoGoodsBriefs.get(0); BigDecimal proportion = hongBaoManageService.getFanLiRate(); BigDecimal tkRate = taoBaoGoodsBrief.getTkRate(); BigDecimal zkPrice = taoBaoGoodsBrief.getZkPrice(); BigDecimal tkp = proportion.divide(BigDecimal.valueOf(100)) .multiply(tkRate.divide(BigDecimal.valueOf(100))); BigDecimal hongbao = MoneyBigDecimalUtil.mul(zkPrice, tkp); dynamicRecommend.setHongbao(hongbao); } String title = taoBaoGoodsBrief.getTitle(); int type = taoBaoGoodsBrief.getUserType(); dynamicRecommend.setGname(title); dynamicRecommend.setZkPrice(taoBaoGoodsBrief.getZkPrice()); dynamicRecommend.setgPicUrl(taoBaoGoodsBrief.getPictUrl()); dynamicRecommend.setCreatetime(System.currentTimeMillis()); if (type == 0) { // tb dynamicRecommend.setGoodsType(1); // tb } else { dynamicRecommend.setGoodsType(2); // 天猫 } String reason = dynamicRecommend.getReason(); reason = maskKeyService.maskAction(reason); dynamicRecommend.setReason(reason); long u = userInfoService.getRandomShamUser(); if (u == 0) { out.print(JsonUtil.loadFalseResult("暂无虚拟用户")); return; } dynamicRecommend.setUserInfo(new UserInfo(u)); dynamicRecommend.setType(1); long makePublic = dynamicRecommendService.makePublic(dynamicRecommend); if (makePublic > 0) { JSONObject data = new JSONObject(); data.put("auctionId", makePublic); out.println(JsonUtil.loadTrueResult(data)); } else { out.println(JsonUtil.loadFalseResult("发布失败")); } } }