package com.yeshi.fanli.controller.web; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.Date; import java.util.List; 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.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.yeshi.fanli.entity.accept.AcceptData; import com.yeshi.fanli.entity.taobao.ShareHotGoods; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBriefExtra; import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException; import com.yeshi.fanli.service.inter.order.config.HongBaoManageService; import com.yeshi.fanli.service.inter.taobao.ShareHotGoodsService; import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailV2Service; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.TimeUtil; import com.yeshi.fanli.util.taobao.TaoBaoUtil; import com.yeshi.fanli.util.taobao.TaoKeApiUtil; import net.sf.json.JSONArray; @Controller @RequestMapping("api/web/v1/shareHotGoods") public class ShareHotGoodsController { @Resource private DaTaoKeGoodsDetailV2Service daTaoKeGoodsDetailV2Service; @Resource private ShareHotGoodsService shareHotGoodsService; @Resource private HongBaoManageService hongBaoManageService; /** * 拉取备选商品 * * @param callback * @param acceptData * @param kw * @param out */ @RequestMapping(value = "listPreGoods", method = RequestMethod.GET) public void listPreGoods(String callback, AcceptData acceptData, String kw, Integer page, PrintWriter out) { // 大淘客数据,更新 List list = shareHotGoodsService.listPreGoods(page); JSONArray array = new JSONArray(); Gson gson = new Gson(); for (TaoBaoGoodsBrief goods : list) { array.add(gson.toJson(TaoBaoUtil.getTaoBaoGoodsBriefExtra(goods, "100", null))); } out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(array))); } /** * 设置分享商品 * * @param callback * @param acceptData * @param ids * @param out */ @RequestMapping(value = "setShareGoods", method = RequestMethod.GET) public void setShareGoods(String callback, AcceptData acceptData, String ids, String day, PrintWriter out) { // 大淘客数据,更新 JSONArray array = JSONArray.fromObject(ids); if (StringUtil.isNullOrEmpty(day)) { day = TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24L, "yyyy-MM-dd"); } int count = 0; for (int i = 0; i < array.size(); i++) { // 获取商品详情 TaoBaoGoodsBrief goods = null; try { goods = TaoKeApiUtil.searchGoodsDetail(array.optLong(i)); } catch (TaobaoGoodsDownException e) { e.printStackTrace(); } if (goods != null && goods.getCouponAmount() != null && goods.getCouponAmount().compareTo(new BigDecimal(0)) > 0) { if (TaoBaoUtil.canAddToTLJBuyGoods(goods)) { count++; goods = daTaoKeGoodsDetailV2Service.filterTaoBaoGoods(goods).getGoods(); shareHotGoodsService.addShareHotGoods(new ShareHotGoods(null, day, new Date(), goods)); } } } out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("设置成功:" + count))); } /** * 获取已经选择的商品 * * @param callback * @param acceptData * @param day * @param out */ @RequestMapping(value = "listSelectedGoods", method = RequestMethod.GET) public void listSelectedGoods(String callback, AcceptData acceptData, String day, PrintWriter out) { if (StringUtil.isNullOrEmpty(day)) { day = TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24L, "yyyy-MM-dd"); } // 最多30个商品 List listGoods = shareHotGoodsService.listByDay(day, 30); JSONArray array = new JSONArray(); for (ShareHotGoods sh : listGoods) { TaoBaoGoodsBriefExtra item = TaoBaoUtil.getTaoBaoGoodsBriefExtra(sh.getGoods(), 100 + "", null); if (!StringUtil.isNullOrEmpty(item.getPictUrlWhite())) item.setPictUrl(item.getPictUrlWhite()); item.setAuctionUrl(sh.getId()); array.add(item); } out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(array))); } /** * 删除选择的商品 * * @param callback * @param acceptData * @param ids * @param out */ @RequestMapping(value = "deleteSelectedGoods", method = RequestMethod.GET) public void deleteSelectedGoods(String callback, AcceptData acceptData, String ids, PrintWriter out) { JSONArray array = JSONArray.fromObject(ids); for (int i = 0; i < array.size(); i++) { String id = array.optString(i); shareHotGoodsService.deleteById(id); } out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(""))); } }