package com.j1.mai.action; import java.io.PrintWriter; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.alibaba.fastjson.JSON; import com.j1.base.dto.ServiceMessage; import com.j1.base.page.Page; import com.j1.base.type.MsgStatus; import com.j1.item.dto.ProductBrandSearchParam; import com.j1.item.model.BrandMarketGoods; import com.j1.item.model.BrandStreet; import com.j1.item.model.BrandStreetItem; import com.j1.item.model.ProductBrand; import com.j1.mai.dto.Response; import com.j1.mai.dto.base.CarefulBrandCategory; import com.j1.mai.dto.rsp.AllBrandStreetResponse; import com.j1.mai.dto.rsp.CarefulSelectBrandsResponse; import com.j1.mai.model.common.JsonResult; import com.j1.mai.model.common.SoaApiBaseAction; import com.j1.mai.util.ArrayUtil; import com.j1.mai.util.BeanTransUtil; import com.j1.mai.util.NumericUtil; import com.j1.mai.util.StringUtil; import com.j1.soa.common.ResultMsg; import com.j1.soa.resource.item.api.BrandMarketGoodsService; import com.j1.soa.resource.item.api.BrandStreetItemService; import com.j1.soa.resource.item.api.BrandStreetService; import com.j1.soa.resource.item.api.ProductBrandSearchService; import com.j1.soa.resource.item.api.ProductBrandService; /** * 品牌相关接口 * * @author jiucaizi * @since 3.5 */ @Controller @Scope("request") @RequestMapping("/brand") public class BrandThemesAction extends SoaApiBaseAction { static Logger LOG = Logger.getLogger(BrandThemesAction.class); public final static String BRANDS = "brandList"; public final static String ITEMS = "itemList"; public final static String ALLITEM = "allBrand"; @Autowired private BrandStreetItemService brandStreetItemService; @Autowired private BrandStreetService brandStreetService; @Autowired private ProductBrandService productBrandService; @Autowired private BrandMarketGoodsService brandMarketGoodsService; @Autowired private ProductBrandSearchService productBrandSearchService; /** * 精品街列表 (for h5 ,数据放入data下) * 1 查询品牌街列表 * 2品牌街列表查询单个品牌街信息 * 3组装结果返回 */ @RequestMapping("/listH5") public void list2(HttpServletRequest request, HttpServletResponse response, final @RequestParam(value = "id", required = false) String id) { BrandStreet brand = new BrandStreet(); /* 客户端(web/wap/app) */ brand.setClient("app"); ServiceMessage<List<BrandStreet>> brandList = null; CarefulSelectBrandsResponse res = new CarefulSelectBrandsResponse(); try { brandList = brandStreetService.getBrandStreetByMul(brand); } catch (Exception e) { LOG.error("brandStreetService.getBrandStreetByMul:" + e); } finally { if(ArrayUtil.resultIsEmp(brandList)) { this.setResultInfo(MsgStatus.NO_RESULT.getCode(), ResultMsg.Common.QUERY_FAILURE).write(request, response); /*writeResponse(request, response, res.setStatus(3).setMsg(ResultMsg.Common.QUERY_FAILURE));*/ return; } else { BeanTransUtil.mapping(brandList,res); /*this._result.put(BRANDS, brandList.getResult());*/ } } int selectID = NumericUtil.parseInt(id, 0); if(selectID != 0) { //逻辑修改为 0值不查询下一级 /*selectStreet = ArrayUtil.findFirstFromList(brandList.getResult()); selectID = selectStreet.getId();*/ //选取品牌街ID BrandStreetItem item = new BrandStreetItem(); item.setBrandStreetId(selectID); LOG.info("setBrandStreetId:" + selectID); /*item.setIsRecomment("Y");*///设置是否推荐(Y/N,为空表示查询全部) item.setIsMarketGoods("Y"); //是否查询品牌推荐商品(Y/N) ServiceMessage<List<BrandStreetItem>> itemList = null; try { itemList = brandStreetItemService.getBrandStreetItemByStreetId(item); } catch (Exception e) { LOG.error("brandStreetItemService.getBrandStreetItemByStreetId:" + e); } finally { if(ArrayUtil.resultIsEmp(itemList)) { LOG.info("itemList: emp"); /*this._result.put(ITEMS, new JSONArray());*/ } else { LOG.info("itemList: "+ itemList.getResult()); //选取 CarefulBrandCategory care = res.getById(selectID); if(care == null) { LOG.info("select CarefulBrandCategory:null"); } else { BeanTransUtil.mapping(itemList,care); } /*this._result.put(ITEMS, itemList.getResult());*/ } } } this._result.setObjData(JSON.toJSONString(res)); /*this._result.setObjData(res);*/ /*writeResponse(request, response, res.setStatus(0).setMsg(ResultMsg.Common.OK));*/ this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK) .write(request, response); } /** * * 1 查询品牌街列表 * 2品牌街列表查询单个品牌街信息 * 3组装结果返回 */ @RequestMapping("/list") public void list(HttpServletRequest request, HttpServletResponse response, final @RequestParam(value = "id", required = false) String id) { BrandStreet brand = new BrandStreet(); /* 客户端(web/wap/app) */ brand.setClient("app"); ServiceMessage<List<BrandStreet>> brandList = null; CarefulSelectBrandsResponse res = new CarefulSelectBrandsResponse(); try { brandList = brandStreetService.getBrandStreetByMul(brand); } catch (Exception e) { LOG.error("brandStreetService.getBrandStreetByMul:" + e); } finally { if(ArrayUtil.resultIsEmp(brandList)) { /*this.setResultInfo(MsgStatus.NO_RESULT.getCode(), ResultMsg.Common.QUERY_FAILURE).write(request, response);*/ writeResponse(request, response, res.setStatus(3).setMsg(ResultMsg.Common.QUERY_FAILURE)); return; } else { BeanTransUtil.mapping(brandList,res); /*this._result.put(BRANDS, brandList.getResult());*/ } } int selectID = NumericUtil.parseInt(id, 0); if(selectID != 0) { //逻辑修改为 0值不查询下一级 /*selectStreet = ArrayUtil.findFirstFromList(brandList.getResult()); selectID = selectStreet.getId();*/ //选取品牌街ID BrandStreetItem item = new BrandStreetItem(); item.setBrandStreetId(selectID); LOG.info("setBrandStreetId:" + selectID); /*item.setIsRecomment("Y");*///设置是否推荐(Y/N,为空表示查询全部) item.setIsMarketGoods("Y"); //是否查询品牌推荐商品(Y/N) ServiceMessage<List<BrandStreetItem>> itemList = null; try { itemList = brandStreetItemService.getBrandStreetItemByStreetId(item); } catch (Exception e) { LOG.error("brandStreetItemService.getBrandStreetItemByStreetId:" + e); } finally { if(ArrayUtil.resultIsEmp(itemList)) { LOG.info("itemList: emp"); /*this._result.put(ITEMS, new JSONArray());*/ } else { LOG.info("itemList: "+ itemList.getResult()); //选取 CarefulBrandCategory care = res.getById(selectID); if(care == null) { LOG.info("select CarefulBrandCategory:null"); } else { BeanTransUtil.mapping(itemList,care); } /*this._result.put(ITEMS, itemList.getResult());*/ } } } writeResponse(request, response, res.setStatus(0).setMsg(ResultMsg.Common.OK)); /*this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK) .write(request, response);*/ } /** * * @date 2016年6月15日 下午2:10:14 * @Title: listBrandStreet * @Description:app 3.7.3重新定义json返回 * 1 查询品牌街列表 * 2品牌街列表查询单个品牌街信息 * 3组装结果返回 * @param request * @param response * @param id * @throws */ @RequestMapping("/listBrandStreet") public void listBrandStreet(HttpServletRequest request, HttpServletResponse response, final @RequestParam(value = "id", required = false) String id) { BrandStreet brand = new BrandStreet(); /* 客户端(web/wap/app) */ brand.setClient("app"); try { /*获取品牌街主数据*/ ServiceMessage<List<BrandStreet>> brandListResult = null; CarefulSelectBrandsResponse res = new CarefulSelectBrandsResponse(); brandListResult = brandStreetService.getBrandStreetByMul(brand); if (brandListResult.getStatus() != MsgStatus.NORMAL) { logger.error("getBrandStreetByMul:" + brandListResult.getStatus().getCode() + ":" + brandListResult.getMessage()); this.setResultInfo(brandListResult.getStatus().getCode(), brandListResult.getMessage()); return; } _result.put("brandList", brandListResult.getResult()); int selectID = NumericUtil.parseInt(id, 0); if(selectID != 0) { //逻辑修改为 0值不查询下一级 /*selectStreet = ArrayUtil.findFirstFromList(brandList.getResult()); selectID = selectStreet.getId();*/ //选取品牌街ID BrandStreetItem item = new BrandStreetItem(); item.setBrandStreetId(selectID); LOG.info("setBrandStreetId:" + selectID); item.setIsRecomment("Y");///设置是否推荐(Y/N,为空表示查询全部) item.setIsMarketGoods("N"); //是否查询品牌推荐商品(Y/N) ServiceMessage<List<BrandStreetItem>> itemListResult = brandStreetItemService.getBrandStreetItemByStreetId(item); if (itemListResult.getStatus() != MsgStatus.NORMAL) { logger.error("getBrandStreetItemByStreetId:" + itemListResult.getStatus().getCode() + ":" + itemListResult.getMessage()); this.setResultInfo(itemListResult.getStatus().getCode(), itemListResult.getMessage()); return; } _result.put("brandStreetItem", itemListResult.getResult()); } } catch (Exception e) { LOG.error(e.getMessage(), e); setResultInfo(MsgStatus.EXCEPTION.getCode(),"品牌街查询数据失败"); } finally { write(request, response); } } /** * 查询品牌列表,含分页 * @Title: findAllByPage * @Description: 根据查询条件分页查询(条件:拼音码,品牌街名称,页数,每页数量) * @param @param request * @param @param response * @param @param pinYin * @param @param brandName * @param @param pnum * @param @param psize 设定文件 * @return void 返回类型 * @throws *//* @RequestMapping("/all2") public void listByPage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "pinYin", required = false) String pinYin, //品牌首字母拼音码 @RequestParam(value = "brandName", required = false) String brandName,//品牌名称(模糊查询) @RequestParam(value = "pnum", required = false) String pnum, @RequestParam(value = "psize", required = false) String psize) { ProductBrand productBrand = new ProductBrand(); if(!StringUtil.isEmpty(pinYin)) { productBrand.setPin(StringUtil.getTrimString(pinYin)); } if(!StringUtil.isEmpty(brandName)) { productBrand.setProductBrandName(StringUtil.getTrimString(brandName)); } ServiceMessage<Page<ProductBrand>> allBrand = null; try { allBrand = productBrandService.pageProductBrand( productBrand, NumericUtil.parseInt(pnum, 1), NumericUtil.parseInt(psize, 500)); } catch (Exception e) { LOG.error("approveGoodsService.findAllByPage:" + e); this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE) .write(request, response); return; } finally { LOG.debug("approveSortService.findAllByPage:" + allBrand.getStatus()); if(ArrayUtil.resultIsEmpWithPage(allBrand)) { this._result.put("allBrand", new JSONArray()); this._result.put("totalPage", 0); this._result.put("pinYin", StringUtil.getTrimString(pinYin)); this._result.put("brandName", StringUtil.getTrimString(brandName)); this._result.put("psize", NumericUtil.parseInt(psize, 50)); this._result.put("pnum", NumericUtil.parseInt(pnum, 1)); this.setResultInfo(MsgStatus.NO_RESULT.getCode(), ResultMsg.Common.QUERY_FAILURE) .write(request, response); } else { this._result.put("allBrand", allBrand.getResult().getData()); this._result.put("totalPage", allBrand.getResult().getTotalPage()); this._result.put("pinYin", StringUtil.getTrimString(pinYin)); this._result.put("brandName", StringUtil.getTrimString(brandName)); this._result.put("pnum", NumericUtil.parseInt(pnum, 1)); this._result.put("psize", NumericUtil.parseInt(psize, 50)); this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK) .write(request, response); } } } */ /** * 查询品牌列表 * @Title: all * @Description: 查询所有品牌列表(for h5 ,数据放入data下) * @param @param request * @param @param response * @return void 返回类型 品牌列表 */ @RequestMapping("/allH5") public void all2(HttpServletRequest request, HttpServletResponse response) { ProductBrand productBrand = new ProductBrand(); ServiceMessage<Page<ProductBrand>> allBrand = null; AllBrandStreetResponse res = new AllBrandStreetResponse(); try { allBrand = productBrandService.pageProductBrand( productBrand,1, 2000); } catch (Exception e) { LOG.error("approveGoodsService.pageProductBrand:" + e); this.setResultInfo(MsgStatus.EXCEPTION.getCode(), ResultMsg.Common.QUERY_FAILURE).write(request, response); /*writeResponse(request, response, res.setStatus(1).setMsg(ResultMsg.Common.QUERY_FAILURE));*/ return; } finally { if(ArrayUtil.resultIsEmpWithPage(allBrand)) { /*writeResponse(request, response, res.setStatus(3).setMsg(ResultMsg.Common.QUERY_FAILURE));*/ this.setResultInfo(MsgStatus.NO_RESULT.getCode(), ResultMsg.Common.QUERY_FAILURE).write(request, response); return; } else { List<ProductBrand> brandList = allBrand.getResult().getData(); for(ProductBrand p: brandList) { res.addProductBrand(p); } res.sort(); this._result.setObjData(res); this.setResultInfo(MsgStatus.NORMAL.getCode(), ResultMsg.Common.OK).write(request, response); /*writeResponse(request, response, res.setStatus(0).setMsg(ResultMsg.Common.OK));*/ } } } /** * 查询品牌列表 * @Title: all * @Description: 查询所有品牌列表 * @param @param request * @param @param response * @return void 返回类型 品牌列表 */ @RequestMapping("/all") public void all(HttpServletRequest request, HttpServletResponse response) { ProductBrand productBrand = new ProductBrand(); ServiceMessage<Page<ProductBrand>> allBrand = null; AllBrandStreetResponse res = new AllBrandStreetResponse(); try { allBrand = productBrandService.pageProductBrand( productBrand,1, 2000); } catch (Exception e) { LOG.error("approveGoodsService.pageProductBrand:" + e); writeResponse(request, response, res.setStatus(1).setMsg(ResultMsg.Common.QUERY_FAILURE)); return; } finally { if(ArrayUtil.resultIsEmpWithPage(allBrand)) { writeResponse(request, response, res.setStatus(3).setMsg(ResultMsg.Common.QUERY_FAILURE)); return; } else { List<ProductBrand> brandList = allBrand.getResult().getData(); for(ProductBrand p: brandList) { res.addProductBrand(p); } res.sort(); writeResponse(request, response, res.setStatus(0).setMsg(ResultMsg.Common.OK)); } } } /** * * @Title: findByProductBrandId * @Description: 根据品牌ID查询品牌信息,推荐商品列表 * @param @param request * @param @param response * @param @param id 品牌ID * @param @param pnum 页数 * @param @param psize 每页条数 * @param @param sort 商品排序(AllSort 0, SaleSort 1, low2High 2, high2Low 3, 4其它) * @return void 返回类型 * @throws */ @RequestMapping("/findProductBrand") public void findByProductBrandId(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = false) String id, @RequestParam(value = "pnum", required = false) String pnum, @RequestParam(value = "psize", required = false) String psize, @RequestParam(value = "sort", required = false) String sort) { _result.put("pnum", NumericUtil.parseInt(pnum, 1)); try { ServiceMessage<ProductBrand> message = productBrandService .getProductBrandByBrandId(NumericUtil.parseInt(id, 1)); _result.put( "productBrand", message.getResult() == null ? new JSONObject() : JSONObject .fromObject(message.getResult())); } catch (Exception e) { LOG.error("productBrandService.getProductBrandById获取失败,异常信息:" + e); _result.put("productBrand", new JSONObject()); } try { BrandMarketGoods item = new BrandMarketGoods(); item.setProductBrandId(NumericUtil.parseInt(id, 1)); // 设置查询品牌街ID ServiceMessage<List<BrandMarketGoods>> sm = brandMarketGoodsService .getBrandGoodsByBrandId(item); _result.put( "brandMarketGoodsList", ArrayUtil.resultIsEmp(sm) ? new JSONArray() : JSONArray .fromObject(sm.getResult())); } catch (Exception e) { LOG.error("brandMarketGoodsService.getBrandGoodsByBrandId获取失败,异常信息:" + e); _result.put("brandMarketGoodsList", new JSONArray()); } try { ProductBrandSearchParam param = new ProductBrandSearchParam(); /* 页数 */ param.setPageNum(NumericUtil.parseInt(pnum, 1)); /* 每页条数 */ param.setPageSize(NumericUtil.parseInt(psize, 20)); /* 品牌ID */ param.setProductBrandId(NumericUtil.parseInt(id, 1)); setSort(param, sort); ServiceMessage<Page<Map>> result = productBrandSearchService .pageBrandGoodsList(param); int totalPage = result.getResult().getTotalPage(); _result.put("totalPage", totalPage); _result.put("brandGoodsList", JSONArray.fromObject(result.getResult().getData())); } catch (Exception e) { LOG.error("productBrandSearchService.pageBrandGoodsList获取失败,异常信息:" + e); _result.put("totalPage", 1); _result.put("brandGoodsList", new JSONArray()); } setResultInfo(MsgStatus.NORMAL.getCode(), "查询成功").write(request, response); } /** * * @Title: setSort * @Description: 设置排序 * @param @param param * @param @param sort 设定文件 * @return void 返回类型 * @throws */ private void setSort(final ProductBrandSearchParam param, final String sort) { //商品排序(AllSort 0, SaleSort 1, low2High 2, high2Low 3, 4其它) int sortNum = NumericUtil.parseInt(sort, 0); int[] rights = {-1,//综合 0, //销售量降序 3, //价格降序 2, //价格升序 -1 //其他 }; int selectSort = -1; if(sortNum < rights.length && sortNum >=0) { selectSort = rights[sortNum]; } if(selectSort >=0) { param.setOrderBy("" + selectSort); } else { param.setOrderBy(null); } /*for(int i: rights) { if(sortNum == i) { param.setOrderBy("" + i); return; } }*/ // orderBy =='0' 销售量降序 // orderBy =='1' 销售量升序 // orderBy =='2' 价格降序 // orderBy =='3' 价格升序 // orderBy =='4' 评论降序 // orderBy =='5' 评论升序 // orderBy =='6' 时间降序 // orderBy =='7' 时间升序 } /** * * @Title: writeResponse * @Description: 通过fastJson方式把返回数据转换Json并写入resp * @param @param request * @param @param response * @param @param res 写入数据 * @return void 返回类型 */ public void writeResponse(HttpServletRequest request, HttpServletResponse response, Response res) { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); String callback = request.getParameter("callback"); String result = null; PrintWriter writer = null; try { writer = response.getWriter(); result = JSON.toJSONString(res); if (!StringUtil.isEmpty(callback)) { result = callback + "(" + result + ")"; } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (writer != null) { writer.write(result); writer.close(); } if (result != null) _result = new JsonResult(); } } }