jyy
2021-03-18 a5c67e27c22831e54e0a4b9eb22166368d446673
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.matrix.system.shopXcx.api.action;
 
import com.matrix.biz.bean.BizUser;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.CustomerDataDictionaryDao;
import com.matrix.system.common.interceptor.HostInterceptor;
import com.matrix.system.shopXcx.api.service.WxShopCouponService;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
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<ShopProductAttrRef> shopProductAttrRefs = shopProductAttrRefDao.selectByPid(id);
        List<ShopProductImg> shopProductImgs = shopProductImgDao.selectByPid(id);
        List<ShopSku> shopSkus = shopSkuDao.selectByPid(id);
        List<ShopProductParamRef> 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<ShopCoupon> 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<ShopProductComment> 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<ShopProduct> shopProducts = null;
        shopProduct.setMallType(ShopProduct.MALL_MICRO);
        //查询是否匹配优惠条件
        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<ShopProduct> getCouponProduct(ShopCoupon shopCoupon) {
 
        //查询优惠券适配的所有产品
        List<ShopProduct> shopProductList = new ArrayList<>();
        if (StringUtils.isNotBlank(shopCoupon.getProductIds())) {
            //根据产品id查询,合适的产品
            shopProductList.addAll(shopProductDao.selectProductByIds(shopCoupon.getProductIds(), shopCoupon.getShopId()));
        }
        if (StringUtils.isNotBlank(shopCoupon.getAttrIds())) {
            List<Integer> pIds = new ArrayList<>();
            //根据属性id查询合适的属性
            List<String> attrIds = StringUtils.strToColl(shopCoupon.getAttrIds(), ",");
            for (String attrId : attrIds) {
                List<ShopProduct> 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);
    }
 
 
}