935090232@qq.com
2021-03-19 81a3369395a7cccceb7cd36f5238cc6fe2aa88e5
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
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.system.common.interceptor.HostInterceptor;
import com.matrix.system.shopXcx.api.service.WXShopOrderService;
import com.matrix.system.shopXcx.api.service.WxShopActivitiesGroupService;
import com.matrix.system.shopXcx.api.vo.WxActivitiesGroupBuyVO;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
import com.matrix.system.shopXcx.dto.CreateGroupBuyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 小程序端 拼团接口
 *
 * @Author wzy
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "wxapi/groupBuy")
public class WxShopActivitiesGroupBuyController {
 
    @Autowired
    private ShopActivitiesDao shopActivitiesDao;
 
    @Autowired
    private ShopActivitiesGroupInfoDao shopActivitiesGroupInfoDao;
 
    @Autowired
    private ShopActivitiesGroupJoinDao shopActivitiesGroupJoinDao;
 
    @Autowired
    private ShopActivitiesGroupPriceDao shopActivitiesGroupPriceDao;
 
    @Autowired
    private ShopActivitiesGroupJoinUserDao shopActivitiesGroupJoinUserDao;
 
    @Autowired
    private ShopProductAttrRefDao shopProductAttrRefDao;
 
    @Autowired
    private ShopSkuDao shopSkuDao;
 
    @Autowired
    private ShopProductImgDao shopProductImgDao;
 
    @Autowired
    private ShopProductParamRefDao shopProductParamRefDao;
 
    @Autowired
    private ShopProductDao shopProductDao;
 
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
 
    @Autowired
    private WxShopActivitiesGroupService wxShopActivitiesGroupService;
 
    @Autowired
    private WXShopOrderService wxShopOrderService;
 
    @Value("${activities.groupBuy.limit}")
    private String groupBuyLimit;
 
    @Value("${groupBuy.pay.timeLimit}")
    private String payTimeLimit;
 
    /**
     * 获取拼团列表
     *
     * @param buyVO
     * @return
     */
    @PostMapping(value = "/showGroupByList")
    public AjaxResult showGroupByList(@RequestBody WxActivitiesGroupBuyVO buyVO) {
        buyVO.setCompanyId(HostInterceptor.getCompanyId());
        List<WxActivitiesGroupBuyVO> dataList = shopActivitiesGroupInfoDao.selectGroupBuyList(buyVO);
        int count = shopActivitiesGroupInfoDao.selectGroupBuyListCnt(buyVO);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList, count);
    }
 
    /**
     * 获取拼团信息及商品信息
     *
     * @param actId
     * @return
     */
    @GetMapping(value = "/findGroupBuyInfo/{actId}")
    public AjaxResult findGroupBuyInfo(@PathVariable("actId") Long actId) {
        WxActivitiesGroupBuyVO groupBuyVO = shopActivitiesGroupInfoDao.selectGroupInfoWithPriceByActId(actId);
        ShopProduct goods = shopProductDao.selectById(groupBuyVO.getGoodsId().intValue());
 
        //补充产品 扩展信息
        List<ShopProductAttrRef> shopProductAttrRefs = shopProductAttrRefDao.selectByPid(goods.getId());
        List<ShopProductImg> shopProductImgs = shopProductImgDao.selectByPid(goods.getId());
        List<ShopSku> shopSkus = shopSkuDao.selectByPid(goods.getId());
        List<ShopProductParamRef> shopProductParamRefs = shopProductParamRefDao.selectByPid(goods.getId());
        goods.setAttrRefs(shopProductAttrRefs);
        goods.setProductImgs(shopProductImgs);
        goods.setSkus(shopSkus);
        goods.setParams(shopProductParamRefs);
 
 
        AjaxResult ajaxResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, null);
        ajaxResult.putInMap("groupBuyInfo", groupBuyVO);
        ajaxResult.putInMap("goodsInfo", goods);
        return ajaxResult;
    }
 
    /**
     * 获取用户当前的拼团信息
     *
     * @return
     */
    @GetMapping(value = "/findOwnerGroupBuyInfo/{actId}")
    public AjaxResult findOwnerGroupBuyInfo(@PathVariable("actId") Long actId) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
 
        // 1、判断该用户在该活动中有没有正在拼团 若有,则返回正在拼团的用户列表,若没有则返回gruoping=1且返回正在拼团的拼团列表, 若加入了团未完成付款,则需要再做设置
        ShopActivitiesGroupJoin isExsit = shopActivitiesGroupJoinDao.selectIsExistGroupInfoByUserId(actId, bizUser.getOpenId());
        AjaxResult ajaxResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, null);
        if (isExsit == null) {
            ajaxResult.putInMap("grouping", 2);
            List<ShopActivitiesGroupJoin> list = shopActivitiesGroupJoinDao.selectGroupingListByActId(actId);
            ajaxResult.putInMap("groupList", list);
        } else {
            ajaxResult.putInMap("grouping", 1);
            ShopActivitiesGroupJoin groupJoin = shopActivitiesGroupJoinDao.selectOwnerGroupJoinInfoWithNoPay(isExsit.getId());
            ShopActivitiesGroupJoinUser groupJoinUser = shopActivitiesGroupJoinUserDao.selectGroupJoinUserByUserIdAndGjId(bizUser.getOpenId(), groupJoin.getId());
            ShopActivitiesGroupPrice price = shopActivitiesGroupPriceDao.selectById(groupJoin.getGpId());
            int theLast = price.getGpCount() - groupJoin.getJoinUserList().size();
            groupJoin.setTheLast(theLast);
            ajaxResult.putInMap("ownerGroupInfo", groupJoin);
            ajaxResult.putInMap("myself", groupJoinUser);
            ajaxResult.putInMap("price", price);
            ajaxResult.putInMap("theLast", theLast);
        }
        return ajaxResult;
    }
 
    /**
     * 获取当前拼团信息及用户列表
     *
     * @param gjId
     * @return
     */
    @GetMapping(value = "/findGroupJoinInfo/{gjId}")
    public AjaxResult findGroupJoinInfo(@PathVariable Long gjId) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        ShopActivitiesGroupJoin groupJoin = shopActivitiesGroupJoinDao.selectOwnerGroupJoinInfo(gjId);
        int isExist = 2;
 
        for (ShopActivitiesGroupJoinUser joinUser : groupJoin.getJoinUserList()) {
            if (bizUser.getOpenId().equals(joinUser.getUserId())) {
                isExist = 1;
            }
        }
        ShopActivitiesGroupPrice price = shopActivitiesGroupPriceDao.selectById(groupJoin.getGpId());
        int theLast = price.getGpCount() - groupJoin.getJoinUserList().size();
        AjaxResult ajaxResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, null);
        groupJoin.setTheLast(theLast);
        ajaxResult.putInMap("groupJoin", groupJoin);
        ajaxResult.putInMap("isExist", isExist);
        ajaxResult.putInMap("price", price);
        return ajaxResult;
    }
 
    /**
     * 拼团开团接口
     *
     * @param createGroupBuyDTO
     * @return
     */
    @PostMapping(value = "/createGroupBuy")
    @Transactional
    public AjaxResult createGroupBuy(@RequestBody CreateGroupBuyDTO createGroupBuyDTO) throws Exception {
        return wxShopActivitiesGroupService.createGroupBuy(createGroupBuyDTO);
    }
 
    /**
     * 用户加入拼团
     *
     * @param createGroupBuyDTO
     * @return
     */
    @PostMapping(value = "/joinGroupBuy")
    public AjaxResult joinGroupBuy(@RequestBody CreateGroupBuyDTO createGroupBuyDTO) throws Exception {
        return wxShopActivitiesGroupService.joinGroupBuy(createGroupBuyDTO);
    }
 
    @PostMapping(value = "/calPrice")
    public AjaxResult calPrice(@RequestBody CreateGroupBuyDTO createGroupBuyDTO) {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        ShopActivitiesGroupPrice groupPrice = shopActivitiesGroupPriceDao.selectById(createGroupBuyDTO.getGpId());
        // 判断参数中gjId是否为空,若为空则为团长开团。再判断团长价是否为空,如果不为空则拥有团长价
        if (createGroupBuyDTO.getGjId() == null) {
            if (groupPrice.getGpHeadPrice() != null) {
                createGroupBuyDTO.setPrice(groupPrice.getGpHeadPrice());
            } else {
                createGroupBuyDTO.setPrice(groupPrice.getGpPrice());
            }
        } else {
            createGroupBuyDTO.setPrice(groupPrice.getGpPrice());
        }
        BigDecimal postage = wxShopOrderService.calculationPostage(createGroupBuyDTO.getPrice(),bizUser.getCompanyId());
        BigDecimal payPrice = createGroupBuyDTO.getPrice().multiply(BigDecimal.valueOf(createGroupBuyDTO.getCount())).add(postage);
        AjaxResult ajaxResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, null);
        ajaxResult.putInMap("billPrice", createGroupBuyDTO.getPrice().multiply(BigDecimal.valueOf(createGroupBuyDTO.getCount())));
        ajaxResult.putInMap("totalPrice", payPrice);
        ajaxResult.putInMap("postage", postage);
        return ajaxResult;
    }
}