package com.matrix.system.shopXcx.api.action; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.pojo.PaginationVO; import com.matrix.core.tools.StringUtils; import com.matrix.biz.bean.BizUser; import com.matrix.system.common.bean.CustomerDataDictionary; import com.matrix.system.common.dao.CustomerDataDictionaryDao; import com.matrix.component.redis.RedisUserLoginUtils; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.common.interceptor.HostInterceptor; import com.matrix.system.shopXcx.bean.*; import com.matrix.system.shopXcx.dao.*; import com.matrix.system.shopXcx.api.service.WxShopCouponService; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author pengliang * @description 商品查询 * @date 2019-06-05 19:00 */ @Controller @RequestMapping(value = "/wxapi/ShopProduct") @CrossOrigin(origins = "*", maxAge = 3600) public class WxShopProduct { @Autowired private ShopProductDao shopProductDao; @Autowired private ShopCouponDao shopCouponDao; @Autowired private ShopProductAttrRefDao shopProductAttrRefDao; @Autowired private ShopSkuDao shopSkuDao; @Autowired private ShopProductImgDao shopProductImgDao; @Autowired private ShopProductParamRefDao shopProductParamRefDao; @Autowired private CustomerDataDictionaryDao dataDictionaryDao; @Autowired private ShopSearchRecordDao shopSearchRecordDao; @Autowired private ShopProductAttributeDao shopProductAttributeDao; @Autowired private ShopArticleDao shopArticleDao; @Autowired private RedisUserLoginUtils redisUserLoginUtils; @Autowired WxShopCouponService shopCouponService; @Autowired private ShopCollectionDao collectionDao; @Autowired private ShopProductCommentDao productCommentDao; /** * 根据id查询商品信息 * * @param id * @return */ @RequestMapping(value = "/getProductById/{shopId}/{id}") @ResponseBody public AjaxResult getProductById(@PathVariable Long shopId, @PathVariable("id") Integer id) { ShopProduct shopProduct = shopProductDao.selectById(id); if (shopProduct == null) { return new AjaxResult(AjaxResult.STATUS_FAIL, "没有查询到商品"); } List shopProductAttrRefs = shopProductAttrRefDao.selectByPid(id); List shopProductImgs = shopProductImgDao.selectByPid(id); List shopSkus = shopSkuDao.selectByPid(id); // List shopSkus = shopSkuDao.selectByPidAndShopid(id, shopId); List shopProductParamRefs = shopProductParamRefDao.selectByPid(id); shopProduct.setAttrRefs(shopProductAttrRefs); shopProduct.setProductImgs(shopProductImgs); shopProduct.setSkus(shopSkus); shopProduct.setParams(shopProductParamRefs); AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(shopProduct)); //查询产品适配的活动 List shopCoupons = shopCouponService.getHdListByProductId(id); result.putInMap("activity", shopCoupons); if (redisUserLoginUtils.isUserLogin()) { //查询产品适配的优惠券 result.putInMap("couponList", shopCouponService.getCouponListByProductId(id)); //查询产品是否被用户收藏 result.putInMap("checkCollect", CollectionUtils.isNotEmpty(collectionDao.selectByUserIdAndProid(id, redisUserLoginUtils.getLoginUser(BizUser.class).getOpenId()))); } else { result.putInMap("checkCollect", false); } //查询产品的评论 ShopProductComment productComment = new ShopProductComment(); productComment.setProductId(id); productComment.setLimit(2); productComment.setOffset(0); List list = productCommentDao.selectByProductId(productComment); result.putInMap("evaluateList", list); //统计评论总数 productComment.setOffset(-1); int total = productCommentDao.selectByProductTotal(productComment); result.putInMap("evaluateNum", total); return result; } /** * 查询商品 * * @param shopProduct * @return */ @RequestMapping(value = "/getShopProductShowList") @ResponseBody public AjaxResult getShopProductShowList(@RequestBody ShopProduct shopProduct) { List shopProducts = null; //查询是否匹配优惠条件 ShopCoupon shopCoupon = shopCouponDao.getCouponByTitle(shopProduct.getTitle(),HostInterceptor.getCompanyId()); if (shopCoupon != null && shopProduct.getOffset() < 1) { shopProducts = getCouponProduct(shopCoupon); } //如果根据优惠券没查询到产品则在根据标题查询 if (CollectionUtils.isEmpty(shopProducts)) { shopProduct.setDelFlag(AppConstance.DATA_USEABLE); shopProduct.setStatus(AppConstance.IS_PUTAWAY); shopProduct.setCompanyId(HostInterceptor.getCompanyId()); shopProducts = shopProductDao.selectByModelWx(shopProduct); } //保存搜索记录 //saveSearchHistroy(shopProduct); AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, shopProducts); return result; } /** * 根据优惠券查询合适的产品 * * @param shopCoupon * @return */ private List getCouponProduct(ShopCoupon shopCoupon) { //查询优惠券适配的所有产品 List shopProductList = new ArrayList<>(); if (StringUtils.isNotBlank(shopCoupon.getProductIds())) { //根据产品id查询,合适的产品 shopProductList.addAll(shopProductDao.selectProductByIds(shopCoupon.getProductIds(), shopCoupon.getShopId())); } if (StringUtils.isNotBlank(shopCoupon.getAttrIds())) { List pIds = new ArrayList<>(); //根据属性id查询合适的属性 List attrIds = StringUtils.strToColl(shopCoupon.getAttrIds(), ","); for (String attrId : attrIds) { List tempList = shopProductDao.selectProductByAttrid(attrId, shopCoupon.getShopId()); for (ShopProduct product : tempList) { if (!pIds.contains(product.getId())) { shopProductList.add(product); } } } } return shopProductList; } /** * 保存历史记录 * * @param shopProduct */ private void saveSearchHistroy(@RequestBody ShopProduct shopProduct) { BizUser bigUser = redisUserLoginUtils.getLoginUser(BizUser.class); ShopSearchRecord searchBean = new ShopSearchRecord(); if (StringUtils.isNotBlank(shopProduct.getTitle())) { searchBean.setSearchType(AppConstance.SEARCH_TYPE); searchBean.setSearchContent(shopProduct.getTitle()); } searchBean.setUserId(bigUser.getOpenId()); searchBean.setSearchPriceStart(shopProduct.getStartPrice()); searchBean.setSearchPriceEnd(shopProduct.getEndPrice()); searchBean.setSearchDel(AppConstance.DATA_USEABLE); if (StringUtils.isNotBlank(shopProduct.getAttrs())) { ShopProductAttribute shopProductAttribute = shopProductAttributeDao.selectById(Integer.valueOf(shopProduct.getAttrs())); searchBean.setSearchAttrs(shopProductAttribute.getAttrName()); } searchBean.setCreateBy(bigUser.getOpenId()); searchBean.setUpdateBy(bigUser.getOpenId()); shopSearchRecordDao.insert(searchBean); } }